diff --git a/.changeset/late-snakes-joke.md b/.changeset/late-snakes-joke.md new file mode 100644 index 000000000..cfa754517 --- /dev/null +++ b/.changeset/late-snakes-joke.md @@ -0,0 +1,5 @@ +--- +'sku': patch +--- + +Replace `command-line-args` with `minimist` for parsing CLI arguments diff --git a/packages/sku/entry/server/index.js b/packages/sku/entry/server/index.js index 27f3eb541..3fbd68f8b 100644 --- a/packages/sku/entry/server/index.js +++ b/packages/sku/entry/server/index.js @@ -1,20 +1,14 @@ import fs from 'node:fs'; import http from 'node:http'; import https from 'node:https'; -import commandLineArgs from 'command-line-args'; import { app, onStart } from './server'; +import minimist from 'minimist'; -const { port } = commandLineArgs( - [ - { - name: 'port', - alias: 'p', - type: Number, - defaultValue: __SKU_DEFAULT_SERVER_PORT__, // eslint-disable-line no-undef - }, - ], - { partial: true }, -); +const { port } = minimist(process.argv.slice(2), { + alias: { p: 'port' }, + // eslint-disable-next-line no-undef + default: { port: __SKU_DEFAULT_SERVER_PORT__ }, +}); const startCallback = () => { console.log('Server started on port', port); diff --git a/packages/sku/lib/parseArgs.js b/packages/sku/lib/parseArgs.js index 8a5897aa4..34ce3a145 100644 --- a/packages/sku/lib/parseArgs.js +++ b/packages/sku/lib/parseArgs.js @@ -1,58 +1,4 @@ -const commandLineArgs = require('command-line-args'); - -const optionDefinitions = [ - { - name: 'script', - defaultOption: true, - }, - { - name: 'env', - alias: 'e', - type: String, - defaultValue: 'production', - }, - { - name: 'tenant', - alias: 't', - type: String, - defaultValue: '', - }, - { - name: 'build', - alias: 'b', - type: String, - }, - { - name: 'config', - alias: 'c', - type: String, - }, - { - name: 'debug', - alias: 'D', - type: Boolean, - }, - { - name: 'environment', - type: String, - }, - { - name: 'packageManager', - type: String, - }, - { - name: 'port', - type: Number, - }, - { - name: 'site', - type: String, - }, - { - name: 'stats', - type: String, - }, -]; +const minimist = require('minimist'); /** * Supports parsing args that look like: @@ -60,16 +6,53 @@ const optionDefinitions = [ * * @param {string[]} args - should look like process.argv */ -module.exports = (args) => { - const options = commandLineArgs(optionDefinitions, { +module.exports = (processArgv) => { + /** + * @type {string[]} + */ + const unknown = []; + + // We are tracking unknown arguments ourselves, so we ignore `minimist`'s unknown property `_` + const { _, ...options } = minimist( // The first 2 items in process.argv are /path/to/node and /path/to/sku. - // We need the first arg we give to command-line-args to be the script name. - argv: args.slice(2), - stopAtFirstUnknown: false, - partial: true, - }); + // We need the first arg we give to minimist to be the script name. + processArgv.slice(2), + { + string: [ + 'env', + 'tenant', + 'build', + 'config', + 'environment', + 'packageManager', + 'site', + 'stats', + ], + default: { + env: 'production', + tenant: '', + }, + alias: { + e: 'env', + t: 'tenant', + b: 'build', + c: 'config', + D: 'debug', + }, + boolean: [ + 'debug', + // Passed to Vocab in the `translations` script + 'delete-unused-keys', + ], + // `minimist` does not push unknown flags to `_` even if this function returns `true`, so we + // need to track them ourselves + unknown: (arg) => { + unknown.push(arg); + }, + }, + ); - const { script, _unknown: argv = [] } = options; + const [script, ...argv] = unknown; // Backwards compatibility for unnamed build name argument, to be deprecated const buildName = () => { @@ -78,11 +61,13 @@ module.exports = (args) => { } else if (argv.length) { return argv[0]; } - return undefined; // eslint-disable-line no-undefined + + return undefined; }; return { ...options, + script, buildName: script === 'start' ? buildName() : null, env: script === 'start' ? 'development' : options.env, argv, diff --git a/packages/sku/lib/parseArgs.test.js b/packages/sku/lib/parseArgs.test.js index a7cb66a4b..c6d38190b 100644 --- a/packages/sku/lib/parseArgs.test.js +++ b/packages/sku/lib/parseArgs.test.js @@ -1,3 +1,7 @@ +/** + * @jest-environment node + */ + const parseArgs = require('./parseArgs'); describe('parseArgs', () => { @@ -52,7 +56,7 @@ describe('parseArgs', () => { test('debug', () => { expect( parseArgs(['/path/to/node', '/path/to/bin/sku', 'build']).debug, - ).toBeUndefined(); + ).toBe(false); expect( parseArgs(['/path/to/node', '/path/to/bin/sku', '-D', 'build']).debug, diff --git a/packages/sku/lib/toPosixPath.test.js b/packages/sku/lib/toPosixPath.test.js index 2806304d6..b119b5c14 100644 --- a/packages/sku/lib/toPosixPath.test.js +++ b/packages/sku/lib/toPosixPath.test.js @@ -1,3 +1,7 @@ +/** + * @jest-environment node + */ + const toPosixPath = require('./toPosixPath'); describe('toPosixPath', () => { diff --git a/packages/sku/package.json b/packages/sku/package.json index 980c53a72..388928641 100644 --- a/packages/sku/package.json +++ b/packages/sku/package.json @@ -67,7 +67,6 @@ "browserslist": "^4.16.1", "browserslist-config-seek": "^2.1.0", "chalk": "^4.1.0", - "command-line-args": "^5.1.1", "cross-spawn": "^7.0.3", "css-loader": "^6.7.1", "css-modules-typescript-loader": "4.0.1", @@ -101,6 +100,7 @@ "lint-staged": "^11.1.1", "memoizee": "^0.4.15", "mini-css-extract-plugin": "^2.6.1", + "minimist": "^1.2.8", "node-html-parser": "^6.1.1", "open": "^7.3.1", "path-to-regexp": "^6.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0c94371b1..6ad80d215 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -639,9 +639,6 @@ importers: chalk: specifier: ^4.1.0 version: 4.1.2 - command-line-args: - specifier: ^5.1.1 - version: 5.2.1 cross-spawn: specifier: ^7.0.3 version: 7.0.3 @@ -741,6 +738,9 @@ importers: mini-css-extract-plugin: specifier: ^2.6.1 version: 2.7.7(webpack@5.90.0) + minimist: + specifier: ^1.2.8 + version: 1.2.8 node-html-parser: specifier: ^6.1.1 version: 6.1.12 @@ -6122,11 +6122,6 @@ packages: deep-equal: 2.2.3 dev: true - /array-back@3.1.0: - resolution: {integrity: sha512-TkuxA4UCOvxuDK6NZYXCalszEzj+TLszyASooky+i742l9TqsOdYCMJJupxRic61hwquNtppB3hgcuq9SVSH1Q==} - engines: {node: '>=6'} - dev: false - /array-buffer-byte-length@1.0.0: resolution: {integrity: sha512-LPuwb2P+NrQw3XhxGc36+XSvuBPopovXYTR9Ew++Du9Yb/bx5AzBfrIsBoj0EZUifjQU+sHL21sseZ3jerWO/A==} dependencies: @@ -7015,16 +7010,6 @@ packages: dependencies: delayed-stream: 1.0.0 - /command-line-args@5.2.1: - resolution: {integrity: sha512-H4UfQhZyakIjC74I9d34fGYDwk3XpSr17QhEd0Q3I9Xq1CETHo4Hcuo87WyWHpAF1aSLjLRf5lD9ZGX2qStUvg==} - engines: {node: '>=4.0.0'} - dependencies: - array-back: 3.1.0 - find-replace: 3.0.0 - lodash.camelcase: 4.3.0 - typical: 4.0.0 - dev: false - /commander@10.0.1: resolution: {integrity: sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==} engines: {node: '>=14'} @@ -9118,13 +9103,6 @@ packages: - supports-color dev: true - /find-replace@3.0.0: - resolution: {integrity: sha512-6Tb2myMioCAgv5kfvP5/PkZZ/ntTpVK39fHY7WkWBgvbeE+VHd/tZuZ4mrC+bxh4cfOZeYKVPaJIZtZXV7GNCQ==} - engines: {node: '>=4.0.0'} - dependencies: - array-back: 3.1.0 - dev: false - /find-root@1.1.0: resolution: {integrity: sha512-NKfW6bec6GfKc0SGx1e07QZY9PE99u0Bft/0rzSD5k3sO/vwkVUpDUKVm5Gpp5Ue3YfShPFTX2070tDs5kB9Ng==} @@ -11417,10 +11395,6 @@ packages: p-locate: 6.0.0 dev: false - /lodash.camelcase@4.3.0: - resolution: {integrity: sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==} - dev: false - /lodash.debounce@4.0.8: resolution: {integrity: sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==} dev: false @@ -14942,11 +14916,6 @@ packages: engines: {node: '>=14.17'} hasBin: true - /typical@4.0.0: - resolution: {integrity: sha512-VAH4IvQ7BDFYglMd7BPRDfLgxZZX4O4TFcRDA6EN5X7erNJJq+McIEp8np9aVtxrCJ6qx4GTYVfOWNjcqwZgRw==} - engines: {node: '>=8'} - dev: false - /ufo@1.3.2: resolution: {integrity: sha512-o+ORpgGwaYQXgqGDwd+hkS4PuZ3QnmqMMxRuajK/a38L6fTpcE5GPIfrf+L/KemFzfUpeUQc1rRS1iDBozvnFA==} dev: false diff --git a/tests/storybook-config.test.js b/tests/storybook-config.test.js index 9bbfd1e6b..72f7ab3eb 100644 --- a/tests/storybook-config.test.js +++ b/tests/storybook-config.test.js @@ -28,6 +28,7 @@ describe('storybook-config', () => { beforeAll(async () => { server = await runSkuScriptInDir('storybook', appDir, [ '--ci', + '--quiet', '--config', skuConfigFileName, ]); diff --git a/tests/styling.test.ts b/tests/styling.test.ts index 75e849611..8b2fd376c 100644 --- a/tests/styling.test.ts +++ b/tests/styling.test.ts @@ -92,7 +92,10 @@ describe('styling', () => { let storybookFrame: Frame; beforeAll(async () => { - server = await runSkuScriptInDir('storybook', appDir, ['--ci']); + server = await runSkuScriptInDir('storybook', appDir, [ + '--ci', + '--quiet', + ]); await waitForUrls(storybookUrl); storybookFrame = await getStorybookFrame(storybookUrl); }, 200000);