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

cli: add list-locales flag #12983

Merged
merged 11 commits into from
Oct 12, 2021
5 changes: 5 additions & 0 deletions lighthouse-cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ async function begin() {
commands.listAudits();
}

// Process terminating command
if (cliFlags.listLocales) {
commands.listLocales();
}

// Process terminating command
if (cliFlags.listTraceCategories) {
commands.listTraceCategories();
Expand Down
9 changes: 7 additions & 2 deletions lighthouse-cli/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ function getFlags(manualArgv, options = {}) {
default: false,
describe: 'Prints a list of all available audits and exits',
},
'list-locales': {
type: 'boolean',
default: false,
describe: 'Prints a list of all supported locales and exits',
},
'list-trace-categories': {
type: 'boolean',
default: false,
Expand Down Expand Up @@ -210,7 +215,7 @@ function getFlags(manualArgv, options = {}) {
},
})
.group([
'save-assets', 'list-all-audits', 'list-trace-categories', 'print-config', 'additional-trace-categories',
'save-assets', 'list-all-audits', 'list-locales', 'list-trace-categories', 'print-config', 'additional-trace-categories',
'config-path', 'preset', 'chrome-flags', 'port', 'hostname', 'form-factor', 'screenEmulation', 'emulatedUserAgent',
'max-wait-for-load', 'enable-error-reporting', 'gather-mode', 'audit-mode',
'only-audits', 'only-categories', 'skip-audits', 'budget-path',
Expand Down Expand Up @@ -314,7 +319,7 @@ function getFlags(manualArgv, options = {}) {
// - We're just printing the config.
// - We're in auditMode (and we have artifacts already)
// If one of these don't apply, if no URL, stop the program and ask for one.
const isPrintSomethingMode = argv.listAllAudits || argv.listTraceCategories || argv.printConfig;
const isPrintSomethingMode = argv.listAllAudits || argv.listLocales || argv.listTraceCategories || argv.printConfig;
const isOnlyAuditMode = !!argv.auditMode && !argv.gatherMode;
if (isPrintSomethingMode || isOnlyAuditMode) {
return true;
Expand Down
1 change: 1 addition & 0 deletions lighthouse-cli/commands/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

export {listAudits} from './list-audits.js';
export {listTraceCategories} from './list-trace-categories.js';
export {listLocales} from './list-locales.js';
16 changes: 16 additions & 0 deletions lighthouse-cli/commands/list-locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @license Copyright 2021 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

import locales from '../../shared/localization/locales.js';

function listLocales() {
const localesList = Object.keys(locales);
process.stdout.write(JSON.stringify({locales: localesList}, null, 2));
process.exit(0);
}

export {listLocales};
1 change: 1 addition & 0 deletions lighthouse-cli/test/cli/bin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ beforeEach(async () => {
hostname: '',
// Command modes
listAllAudits: false,
listLocales: false,
listTraceCategories: false,
printConfig: false,
};
Expand Down
11 changes: 11 additions & 0 deletions lighthouse-cli/test/cli/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ describe('CLI Tests', function() {
assert.ok(output.traceCategories.length > 0);
});

it('accepts just the list-locales flag and exit immediately after', () => {
const ret = spawnSync('node', [indexPath, '--list-locales'], {encoding: 'utf8'});

const output = JSON.parse(ret.stdout);
assert.ok(Array.isArray(output.locales));
assert.ok(output.locales.length > 52);
for (const lang of ['en', 'es', 'ru', 'zh']) {
assert.ok(output.locales.includes(lang));
}
});

describe('extra-headers', () => {
it('should exit with a error if the path is not valid', () => {
const ret = spawnSync('node', [indexPath, 'https://www.google.com',
Expand Down
2 changes: 2 additions & 0 deletions types/externs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ export interface CliFlags extends Flags {
enableErrorReporting?: boolean;
/** Flag to print a list of all audits + categories. */
listAllAudits: boolean;
/** Flag to print a list of all supported locales. */
listLocales: boolean;
/** Flag to print a list of all required trace categories. */
listTraceCategories: boolean;
/** A preset audit of selected audit categories to run. */
Expand Down