UNPKG

5.86 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2019 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt The complete set of authors may be found
7 * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
8 * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
9 * Google as part of the polymer project is also subject to an additional IP
10 * rights grant found at http://polymer.github.io/PATENTS.txt
11 */
12var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
13 if (k2 === undefined) k2 = k;
14 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
15}) : (function(o, m, k, k2) {
16 if (k2 === undefined) k2 = k;
17 o[k2] = m[k];
18}));
19var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20 Object.defineProperty(o, "default", { enumerable: true, value: v });
21}) : function(o, v) {
22 o["default"] = v;
23});
24var __importStar = (this && this.__importStar) || function (mod) {
25 if (mod && mod.__esModule) return mod;
26 var result = {};
27 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28 __setModuleDefault(result, mod);
29 return result;
30};
31Object.defineProperty(exports, "__esModule", { value: true });
32exports.main = void 0;
33require('source-map-support').install();
34const path = __importStar(require("path"));
35const ansi = require("ansi-escape-sequences");
36const semver = __importStar(require("semver"));
37const commandLineUsage = require("command-line-usage");
38const flags_1 = require("./flags");
39const config_1 = require("./config");
40const server_1 = require("./server");
41const versions_1 = require("./versions");
42const manual_1 = require("./manual");
43const runner_1 = require("./runner");
44const util_1 = require("./util");
45const installedVersion = () => require(path.join('..', 'package.json')).version;
46async function main(argv) {
47 // Don't block anything on a network query to NPM.
48 const latestVersionPromise = latestVersionFromNpm();
49 let results;
50 try {
51 results = await realMain(argv);
52 }
53 catch (e) {
54 console.error(e);
55 process.exitCode = 1;
56 }
57 try {
58 notifyIfOutdated(await latestVersionPromise);
59 }
60 catch (e) {
61 // Don't set a non-zero exit code just because the NPM query failed. Maybe
62 // we're behind a firewall and can't contact NPM.
63 console.error(`\nFailed to check NPM for latest version:\n${e}`);
64 }
65 return results;
66}
67exports.main = main;
68async function latestVersionFromNpm() {
69 const stdout = await util_1.runNpm(['info', 'tachometer@latest', 'version']);
70 return stdout.toString('utf8').trim();
71}
72function notifyIfOutdated(latestVersion) {
73 const iv = installedVersion();
74 if (semver.lt(iv, latestVersion)) {
75 console.log(ansi.format(`
76[bold magenta]{Update available!}
77The latest version of tachometer is [green]{${latestVersion}}
78You are running version [yellow]{${iv}}
79See what's new at [cyan]{https://github.com/Polymer/tachometer/blob/master/CHANGELOG.md}`));
80 }
81}
82async function realMain(argv) {
83 const opts = flags_1.parseFlags(argv);
84 if (opts.help) {
85 console.log(commandLineUsage([
86 {
87 header: 'tach',
88 content: `v${installedVersion()}\nhttps://github.com/PolymerLabs/tachometer`,
89 },
90 {
91 header: 'Usage',
92 content: `
93Run a benchmark from a local file:
94$ tach foo.html
95
96Compare a benchmark with different URL parameters:
97$ tach foo.html?i=1 foo.html?i=2
98
99Benchmark index.html in a directory:
100$ tach foo/bar
101
102Benchmark a remote URL's First Contentful Paint time:
103$ tach http://example.com
104`,
105 },
106 {
107 header: 'Options',
108 optionList: flags_1.optDefs,
109 },
110 ]));
111 return;
112 }
113 if (opts.version) {
114 console.log(installedVersion());
115 return;
116 }
117 const config = await config_1.makeConfig(opts);
118 if (config.legacyJsonFile) {
119 console.log(`Please use --json-file instead of --save. ` +
120 `--save will be removed in the next major version.`);
121 }
122 const { plans, gitInstalls } = await versions_1.makeServerPlans(config.root, opts['npm-install-dir'], config.benchmarks);
123 await Promise.all(gitInstalls.map((gitInstall) => versions_1.installGitDependency(gitInstall, config.forceCleanNpmInstall)));
124 const servers = new Map();
125 const promises = [];
126 for (const { npmInstalls, mountPoints, specs } of plans) {
127 promises.push(...npmInstalls.map((install) => versions_1.prepareVersionDirectory(install, config.forceCleanNpmInstall)));
128 promises.push((async () => {
129 const server = await server_1.Server.start({
130 host: opts.host,
131 ports: opts.port,
132 root: config.root,
133 npmInstalls,
134 mountPoints,
135 resolveBareModules: config.resolveBareModules,
136 cache: config.mode !== 'manual',
137 });
138 for (const spec of specs) {
139 servers.set(spec, server);
140 }
141 })());
142 }
143 await Promise.all(promises);
144 if (config.mode === 'manual') {
145 await manual_1.manualMode(config, servers);
146 }
147 else {
148 const runner = new runner_1.Runner(config, servers);
149 try {
150 return await runner.run();
151 }
152 finally {
153 const allServers = new Set([...servers.values()]);
154 await Promise.all([...allServers].map((server) => server.close()));
155 }
156 }
157}
158//# sourceMappingURL=cli.js.map
\No newline at end of file