Skip to content

Commit

Permalink
Replace command-line-args with minimist for parsing CLI arguments (
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis authored Mar 25, 2024
1 parent 01c0015 commit a6f2f9e
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 113 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-snakes-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sku': patch
---

Replace `command-line-args` with `minimist` for parsing CLI arguments
18 changes: 6 additions & 12 deletions packages/sku/entry/server/index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
113 changes: 49 additions & 64 deletions packages/sku/lib/parseArgs.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,58 @@
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:
* [/path/to/node/node, /path/to/sku, scriptName, arg1, arg2]
*
* @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 = () => {
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion packages/sku/lib/parseArgs.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @jest-environment node
*/

const parseArgs = require('./parseArgs');

describe('parseArgs', () => {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions packages/sku/lib/toPosixPath.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @jest-environment node
*/

const toPosixPath = require('./toPosixPath');

describe('toPosixPath', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/sku/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
37 changes: 3 additions & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/storybook-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('storybook-config', () => {
beforeAll(async () => {
server = await runSkuScriptInDir('storybook', appDir, [
'--ci',
'--quiet',
'--config',
skuConfigFileName,
]);
Expand Down
5 changes: 4 additions & 1 deletion tests/styling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a6f2f9e

Please sign in to comment.