Skip to content

Commit 735b72e

Browse files
authored
refactor: Simplifying arg validation using sade's builtin mechanism (#1699)
1 parent 82662af commit 735b72e

9 files changed

Lines changed: 108 additions & 312 deletions

File tree

packages/cli/src/commands/build.js

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,89 +3,8 @@ const { resolve } = require('path');
33
const { promisify } = require('util');
44
const runWebpack = require('../lib/webpack/run-webpack');
55
const { toBool } = require('../util');
6-
const { validateArgs } = require('./validate-args');
76

8-
const options = [
9-
{
10-
name: '--src',
11-
description: 'Specify source directory',
12-
default: 'src',
13-
},
14-
{
15-
name: '--dest',
16-
description: 'Specify output directory',
17-
default: 'build',
18-
},
19-
{
20-
name: '--cwd',
21-
description: 'A directory to use instead of $PWD',
22-
default: '.',
23-
},
24-
{
25-
name: '--esm',
26-
description: 'Builds ES-2015 bundles for your code',
27-
default: true,
28-
},
29-
{
30-
name: '--sw',
31-
description: 'Generate and attach a Service Worker',
32-
default: true,
33-
},
34-
{
35-
name: '--babelConfig',
36-
description: 'Path to custom Babel config',
37-
default: '.babelrc',
38-
},
39-
{
40-
name: '--json',
41-
description: 'Generate build stats for bundle analysis',
42-
},
43-
{
44-
name: '--template',
45-
description: 'Path to custom HTML template (default "src/template.html")',
46-
},
47-
{
48-
name: '--preload',
49-
description: 'Adds preload tags to the document its assets',
50-
default: false,
51-
},
52-
{
53-
name: '--analyze',
54-
description: 'Launch interactive Analyzer to inspect production bundle(s)',
55-
},
56-
{
57-
name: '--prerender',
58-
description: 'Renders route(s) into generated static HTML',
59-
default: true,
60-
},
61-
{
62-
name: '--prerenderUrls',
63-
description: 'Path to pre-rendered routes config',
64-
default: 'prerender-urls.json',
65-
},
66-
{
67-
name: '--brotli',
68-
description: 'Builds brotli compressed bundles of javascript',
69-
default: false,
70-
},
71-
{
72-
name: '--inline-css',
73-
description: 'Adds critical css to the prerendered markup',
74-
default: true,
75-
},
76-
{
77-
name: '-c, --config',
78-
description: 'Path to custom CLI config',
79-
default: 'preact.config.js',
80-
},
81-
{
82-
name: '-v, --verbose',
83-
description: 'Verbose output',
84-
},
85-
];
86-
87-
async function command(src, argv) {
88-
validateArgs(argv, options, 'build');
7+
exports.build = async function buildCommand(src, argv) {
898
argv.src = src || argv.src;
909
// add `default:true`s, `--no-*` disables
9110
argv.prerender = toBool(argv.prerender);
@@ -109,9 +28,4 @@ async function command(src, argv) {
10928
if (argv.json) {
11029
await runWebpack.writeJsonStats(cwd, stats);
11130
}
112-
}
113-
114-
module.exports = {
115-
command,
116-
options,
11731
};

packages/cli/src/commands/create.js

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,56 +28,13 @@ const {
2828
FALLBACK_TEMPLATE_OPTIONS,
2929
} = require('../constants');
3030
const { addScripts, install, initGit } = require('../lib/setup');
31-
const { validateArgs } = require('./validate-args');
3231

3332
const ORG = 'preactjs-templates';
3433
const RGX =
3534
/\.(woff2?|ttf|eot|jpe?g|ico|png|gif|webp|avif|mp4|mov|ogg|webm)(\?.*)?$/i;
3635
const isMedia = str => RGX.test(str);
3736
const capitalize = str => str.charAt(0).toUpperCase() + str.substring(1);
3837

39-
const options = [
40-
{
41-
name: '--name',
42-
description: 'The application name',
43-
},
44-
{
45-
name: '--cwd',
46-
description: 'A directory to use instead of $PWD',
47-
default: '.',
48-
},
49-
{
50-
name: '--force',
51-
description: 'Force destination output; will override!',
52-
default: false,
53-
},
54-
{
55-
name: '--install',
56-
description: 'Install dependencies',
57-
default: true,
58-
},
59-
{
60-
name: '--yarn',
61-
description: 'Use `yarn` instead of `npm`',
62-
default: false,
63-
},
64-
{
65-
name: '--git',
66-
description: 'Initialize git repository',
67-
default: false,
68-
},
69-
{
70-
name: '--license',
71-
description: 'License type',
72-
default: 'MIT',
73-
},
74-
{
75-
name: '-v, --verbose',
76-
description: 'Verbose output',
77-
default: false,
78-
},
79-
];
80-
8138
// Formulate Questions if `create` args are missing
8239
function requestParams(repo, dest, argv, templates) {
8340
const cwd = resolve(argv.cwd);
@@ -203,15 +160,13 @@ async function copyFileToDestination(srcPath, destPath, force = false) {
203160
}
204161
}
205162

206-
async function command(repo, dest, argv) {
207-
validateArgs(argv, options, 'create');
163+
exports.create = async function createCommand(repo, dest, argv) {
208164
// Prompt if incomplete data
209165
if (!repo || !dest) {
210166
const templates = await fetchTemplates();
211167
const questions = requestParams(repo, dest, argv, templates);
212168
const onCancel = () => {
213-
info('Aborting execution');
214-
process.exit();
169+
info('Aborting execution', 0);
215170
};
216171
const response = await prompt(questions, { onCancel });
217172

@@ -427,9 +382,4 @@ async function command(repo, dest, argv) {
427382
${green(pfx + ' serve')}
428383
`) + '\n\n'
429384
);
430-
}
431-
432-
module.exports = {
433-
command,
434-
options,
435385
};

packages/cli/src/commands/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
const { command: build, options: buildOptions } = require('./build');
2-
const { command: create, options: createOptions } = require('./create');
3-
const list = require('./list');
4-
const { command: watch, options: watchOptions } = require('./watch');
1+
const { build } = require('./build');
2+
const { create } = require('./create');
3+
const { list } = require('./list');
4+
const { watch } = require('./watch');
55

66
module.exports = {
77
build,
8-
buildOptions,
98
create,
10-
createOptions,
119
list,
1210
watch,
13-
watchOptions,
1411
};

packages/cli/src/commands/list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { error, info } = require('../util');
44

55
const REPOS_URL = 'https://api.github.com/users/preactjs-templates/repos';
66

7-
module.exports = async function () {
7+
exports.list = async function listCommand() {
88
try {
99
const repos = await fetch(REPOS_URL).then(r => r.json());
1010

packages/cli/src/commands/validate-args.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/cli/src/commands/watch.js

Lines changed: 4 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,9 @@
11
const runWebpack = require('../lib/webpack/run-webpack');
22
const { isPortFree, toBool, warn } = require('../util');
3-
const { validateArgs } = require('./validate-args');
43
const getPort = require('get-port');
54
const { resolve } = require('path');
65

7-
const options = [
8-
{
9-
name: '--src',
10-
description: 'Specify source directory',
11-
default: 'src',
12-
},
13-
{
14-
name: '--cwd',
15-
description: 'A directory to use instead of $PWD',
16-
default: '.',
17-
},
18-
{
19-
name: '--esm',
20-
description: 'Builds ES-2015 bundles for your code',
21-
default: false,
22-
},
23-
{
24-
name: '--clear',
25-
description: 'Clear the console',
26-
default: true,
27-
},
28-
{
29-
name: '--sw',
30-
description: 'Generate and attach a Service Worker',
31-
default: undefined,
32-
},
33-
{
34-
name: '--babelConfig',
35-
description: 'Path to custom Babel config',
36-
default: '.babelrc',
37-
},
38-
{
39-
name: '--rhl',
40-
description: '(Deprecated) use --refresh instead',
41-
default: false,
42-
},
43-
{
44-
name: '--json',
45-
description: 'Generate build stats for bundle analysis',
46-
},
47-
{
48-
name: '--https',
49-
description: 'Run server with HTTPS protocol',
50-
},
51-
{
52-
name: '--key',
53-
description: 'Path to PEM key for custom SSL certificate',
54-
},
55-
{
56-
name: '--cert',
57-
description: 'Path to custom SSL certificate',
58-
},
59-
{
60-
name: '--cacert',
61-
description: 'Path to optional CA certificate override',
62-
},
63-
{
64-
name: '--prerender',
65-
description: 'Pre-render static content on first run',
66-
},
67-
{
68-
name: '--prerenderUrls',
69-
description: 'Path to pre-rendered routes config',
70-
default: 'prerender-urls.json',
71-
},
72-
{
73-
name: '--template',
74-
description: 'Path to custom HTML template (default "src/template.html")',
75-
},
76-
{
77-
name: '--refresh',
78-
description: 'Enables experimental preact-refresh functionality',
79-
default: false,
80-
},
81-
{
82-
name: '-c, --config',
83-
description: 'Path to custom CLI config',
84-
default: 'preact.config.js',
85-
},
86-
{
87-
name: '-H, --host',
88-
description: 'Set server hostname',
89-
default: '0.0.0.0',
90-
},
91-
{
92-
name: '-p, --port',
93-
description: 'Set server port (default 8080)',
94-
},
95-
];
96-
97-
async function command(src, argv) {
98-
validateArgs(argv, options, 'watch');
6+
exports.watch = async function watchCommand(src, argv) {
997
if (argv.rhl) {
1008
delete argv.rhl;
1019
argv.refresh = argv.rhl;
@@ -126,9 +34,9 @@ async function command(src, argv) {
12634
}
12735

12836
return runWebpack(argv, true);
129-
}
37+
};
13038

131-
async function determinePort(port) {
39+
const determinePort = (exports.determinePort = async function (port) {
13240
port = parseInt(port, 10);
13341
if (port) {
13442
if (!(await isPortFree(port))) {
@@ -141,10 +49,4 @@ async function determinePort(port) {
14149
}
14250

14351
return port;
144-
}
145-
146-
module.exports = {
147-
command,
148-
options,
149-
determinePort,
150-
};
52+
});

0 commit comments

Comments
 (0)