Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: update yargs to latest #11794

Merged
merged 5 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions lighthouse-cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,6 @@ async function begin() {
' Please use "--emulated-form-factor=none" instead.');
}

if (typeof cliFlags.extraHeaders === 'string') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the coerce() functions from yargs gave a perfect place to do this loading/parsing and avoid the awkward type dance. Just moved into cli-flags

// TODO: LH.Flags.extraHeaders is sometimes actually a string at this point, but needs to be
// copied over to LH.Settings.extraHeaders, which is LH.Crdp.Network.Headers. Force
// the conversion here, but long term either the CLI flag or the setting should have
// a different name.
/** @type {string} */
let extraHeadersStr = cliFlags.extraHeaders;
// If not a JSON object, assume it's a path to a JSON file.
if (extraHeadersStr.substr(0, 1) !== '{') {
extraHeadersStr = fs.readFileSync(extraHeadersStr, 'utf-8');
}

cliFlags.extraHeaders = JSON.parse(extraHeadersStr);
}

if (cliFlags.precomputedLanternDataPath) {
const lanternDataStr = fs.readFileSync(cliFlags.precomputedLanternDataPath, 'utf8');
/** @type {LH.PrecomputedLanternData} */
Expand Down
458 changes: 327 additions & 131 deletions lighthouse-cli/cli-flags.js

Large diffs are not rendered by default.

19 changes: 0 additions & 19 deletions lighthouse-cli/test/cli/bin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,6 @@ describe('CLI bin', function() {
});
});

describe('extraHeaders', () => {
it('should convert extra headers to object', async () => {
// @ts-expect-error - see TODO: in bin.js
cliFlags = {...cliFlags, extraHeaders: '{"foo": "bar"}'};
await bin.begin();

expect(getRunLighthouseArgs()[1]).toHaveProperty('extraHeaders', {foo: 'bar'});
});

it('should read extra headers from file', async () => {
const headersFile = require.resolve('../fixtures/extra-headers/valid.json');
// @ts-expect-error - see TODO: in bin.js
cliFlags = {...cliFlags, extraHeaders: headersFile};
await bin.begin();

expect(getRunLighthouseArgs()[1]).toHaveProperty('extraHeaders', require(headersFile));
});
});

describe('precomputedLanternData', () => {
it('should read lantern data from file', async () => {
const lanternDataFile = require.resolve('../fixtures/lantern-data.json');
Expand Down
21 changes: 21 additions & 0 deletions lighthouse-cli/test/cli/cli-flags-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,25 @@ describe('CLI bin', function() {
]);
expect(flags.blockedUrlPatterns).toEqual(['.*x,y\\.png']);
});

describe('extraHeaders', () => {
it('should convert extra headers to object', async () => {
const flags = getFlags([
'http://www.example.com',
'--extra-headers="{"foo": "bar"}"',
].join(' '));

expect(flags).toHaveProperty('extraHeaders', {foo: 'bar'});
});

it('should read extra headers from file', async () => {
const headersFile = require.resolve('../fixtures/extra-headers/valid.json');
const flags = getFlags([
'http://www.example.com',
`--extra-headers=${headersFile}`,
].join(' '));

expect(flags).toHaveProperty('extraHeaders', require(headersFile));
});
});
});
55 changes: 39 additions & 16 deletions lighthouse-cli/test/smokehouse/frontends/smokehouse-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,54 @@ function getDefinitionsToRun(allTestDefns, requestedIds, {invertMatch}) {
* CLI entry point.
*/
async function begin() {
const argv = yargs
const rawArgv = yargs
.help('help')
.usage('node $0 [<options>] <test-ids>')
.example('node $0 -j=1 pwa seo', 'run pwa and seo tests serially')
.example('node $0 --invert-match byte', 'run all smoke tests but `byte`')
.describe({
'debug': 'Save test artifacts and output verbose logs',
'jobs': 'Manually set the number of jobs to run at once. `1` runs all tests serially',
'retries': 'The number of times to retry failing tests before accepting. Defaults to 0',
'runner': 'The method of running Lighthouse',
'tests-path': 'The path to a set of test definitions to run. Defaults to core smoke tests.',
'invert-match': 'Run all available tests except the ones provided',
.option('_', {
array: true,
type: 'string',
})
.boolean(['debug', 'invert-match'])
.alias({
'jobs': 'j',
.options({
'debug': {
type: 'boolean',
default: false,
describe: 'Save test artifacts and output verbose logs',
},
'jobs': {
type: 'number',
alias: 'j',
describe: 'Manually set the number of jobs to run at once. `1` runs all tests serially',
},
'retries': {
type: 'number',
describe: 'The number of times to retry failing tests before accepting. Defaults to 0',
},
'runner': {
default: 'cli',
choices: ['cli', 'bundle'],
describe: 'The method of running Lighthouse',
},
'tests-path': {
type: 'string',
describe: 'The path to a set of test definitions to run. Defaults to core smoke tests.',
},
'invert-match': {
type: 'boolean',
default: false,
describe: 'Run all available tests except the ones provided',
},
})
.choices('runner', ['cli', 'bundle'])
.default('runner', 'cli')
.wrap(yargs.terminalWidth())
.argv;

// TODO: use .number() when yargs is updated
const jobs = argv.jobs !== undefined ? Number(argv.jobs) : undefined;
const retries = argv.retries !== undefined ? Number(argv.retries) : undefined;
// Augmenting yargs type with auto-camelCasing breaks in tsc@4.1.2 and @types/yargs@15.0.11,
// so for now cast to add yarg's camelCase properties to type.
const argv = /** @type {typeof rawArgv & CamelCasify<typeof rawArgv>} */ (rawArgv);

const jobs = Number.isFinite(argv.jobs) ? argv.jobs : undefined;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a big deal, but yargs.number() is less helpful than I'd hoped. --jobs texttext gives argv = { _: [], jobs: NaN} instead of throwing an error to the user like I hoped it would...

const retries = Number.isFinite(argv.retries) ? argv.retries : undefined;

const runnerPath = runnerPaths[/** @type {keyof typeof runnerPaths} */ (argv.runner)];
if (argv.runner === 'bundle') {
Expand Down
25 changes: 20 additions & 5 deletions lighthouse-core/scripts/compare-runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {ProgressLogger} = require('./lantern/collect/common.js');
const LH_ROOT = `${__dirname}/../..`;
const ROOT_OUTPUT_DIR = `${LH_ROOT}/timings-data`;

const argv = yargs
const rawArgv = yargs
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@connorjclark I tried to minimize the changes needed in here. I don't think I changed any semantics

.help('help')
.describe({
// common flags
Expand All @@ -51,12 +51,14 @@ const argv = yargs
'delta-property-sort': 'Property to sort by its delta',
'desc': 'Set to override default ascending sort',
})
.option('n', {type: 'number', default: 1})
.string('filter')
.string('url-filter')
.alias({'gather': 'G', 'audit': 'A'})
.default('report-exclude', 'key|min|max|stdev|^n$')
.default('delta-property-sort', 'mean')
.default('output', 'table')
.array('urls')
.option('urls', {array: true, type: 'string'})
.string('lh-flags')
.default('desc', false)
.default('sort-by-absolute-value', false)
Expand All @@ -65,6 +67,10 @@ const argv = yargs
.wrap(yargs.terminalWidth())
.argv;

// Augmenting yargs type with auto-camelCasing breaks in tsc@4.1.2 and @types/yargs@15.0.11,
// so for now cast to add yarg's camelCase properties to type.
const argv = /** @type {typeof rawArgv & CamelCasify<typeof rawArgv>} */ (rawArgv);

const reportExcludeRegex =
argv.reportExclude !== 'none' ? new RegExp(argv.reportExclude, 'i') : null;

Expand Down Expand Up @@ -120,12 +126,15 @@ function round(value) {
* @param {number} total
* @return {string}
*/
function getProgressBar(i, total = argv.n * argv.urls.length) {
function getProgressBar(i, total = argv.n * (argv.urls || []).length) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no way to do required arrays?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no way to do required arrays?

But I think we only want a required array of urls if doing --collect? yargs.implies() can do that, I believe, but it's a little too indirect for the typescript type to infer off of. It might benefit the CLI experience, but a JS check or a default will still have to go somewhere.

I was trying to leave everything mostly untouched so it would all be recognizable when you were next back in here, but happy to commit any suggestions

const bars = new Array(Math.round(i * 40 / total)).fill('▄').join('').padEnd(40);
return `${i + 1} / ${total} [${bars}]`;
}

async function gather() {
if (typeof argv.name !== 'string') {
throw new Error('expected entry for name option');
}
const outputDir = dir(argv.name);
if (fs.existsSync(outputDir)) {
console.log('Collection already started - resuming.');
Expand All @@ -136,7 +145,7 @@ async function gather() {
progress.log('Gathering…');

let progressCount = 0;
for (const url of argv.urls) {
for (const url of argv.urls || []) {
const urlFolder = `${outputDir}/${urlToFolder(url)}`;
await mkdir(urlFolder, {recursive: true});

Expand All @@ -161,12 +170,15 @@ async function gather() {
}

async function audit() {
if (typeof argv.name !== 'string') {
throw new Error('expected entry for name option');
}
const outputDir = dir(argv.name);
const progress = new ProgressLogger();
progress.log('Auditing…');

let progressCount = 0;
for (const url of argv.urls) {
for (const url of argv.urls || []) {
const urlDir = `${outputDir}/${urlToFolder(url)}`;
for (let i = 0; i < argv.n; i++) {
const gatherDir = `${urlDir}/${i}`;
Expand Down Expand Up @@ -309,6 +321,9 @@ function isNumber(value) {
}

function summarize() {
if (typeof argv.name !== 'string') {
throw new Error('expected entry for name option');
}
const results = aggregateResults(argv.name);
print(filter(results));
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"@types/semver": "^5.5.0",
"@types/update-notifier": "^4.1.0",
"@types/ws": "^4.0.1",
"@types/yargs": "^8.0.2",
"@types/yargs": "^15.0.11",
"@types/yargs-parser": "^15.0.0",
"@typescript-eslint/parser": "^4.7.0",
"@wardpeet/brfs": "2.1.0",
Expand Down Expand Up @@ -188,8 +188,8 @@
"third-party-web": "^0.12.2",
"update-notifier": "^4.1.0",
"ws": "3.3.2",
"yargs": "3.32.0",
"yargs-parser": "^18.1.3"
"yargs": "^16.1.1",
"yargs-parser": "^20.2.4"
},
"repository": "GoogleChrome/lighthouse",
"keywords": [
Expand Down
Loading