Skip to content

Commit

Permalink
refactor: Simplifying arg validation using sade's builtin mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Nov 19, 2022
1 parent 82662af commit 7b6b007
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 312 deletions.
88 changes: 1 addition & 87 deletions packages/cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,8 @@ const { resolve } = require('path');
const { promisify } = require('util');
const runWebpack = require('../lib/webpack/run-webpack');
const { toBool } = require('../util');
const { validateArgs } = require('./validate-args');

const options = [
{
name: '--src',
description: 'Specify source directory',
default: 'src',
},
{
name: '--dest',
description: 'Specify output directory',
default: 'build',
},
{
name: '--cwd',
description: 'A directory to use instead of $PWD',
default: '.',
},
{
name: '--esm',
description: 'Builds ES-2015 bundles for your code',
default: true,
},
{
name: '--sw',
description: 'Generate and attach a Service Worker',
default: true,
},
{
name: '--babelConfig',
description: 'Path to custom Babel config',
default: '.babelrc',
},
{
name: '--json',
description: 'Generate build stats for bundle analysis',
},
{
name: '--template',
description: 'Path to custom HTML template (default "src/template.html")',
},
{
name: '--preload',
description: 'Adds preload tags to the document its assets',
default: false,
},
{
name: '--analyze',
description: 'Launch interactive Analyzer to inspect production bundle(s)',
},
{
name: '--prerender',
description: 'Renders route(s) into generated static HTML',
default: true,
},
{
name: '--prerenderUrls',
description: 'Path to pre-rendered routes config',
default: 'prerender-urls.json',
},
{
name: '--brotli',
description: 'Builds brotli compressed bundles of javascript',
default: false,
},
{
name: '--inline-css',
description: 'Adds critical css to the prerendered markup',
default: true,
},
{
name: '-c, --config',
description: 'Path to custom CLI config',
default: 'preact.config.js',
},
{
name: '-v, --verbose',
description: 'Verbose output',
},
];

async function command(src, argv) {
validateArgs(argv, options, 'build');
exports.build = async function buildCommand(src, argv) {
argv.src = src || argv.src;
// add `default:true`s, `--no-*` disables
argv.prerender = toBool(argv.prerender);
Expand All @@ -109,9 +28,4 @@ async function command(src, argv) {
if (argv.json) {
await runWebpack.writeJsonStats(cwd, stats);
}
}

module.exports = {
command,
options,
};
54 changes: 2 additions & 52 deletions packages/cli/src/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,56 +28,13 @@ const {
FALLBACK_TEMPLATE_OPTIONS,
} = require('../constants');
const { addScripts, install, initGit } = require('../lib/setup');
const { validateArgs } = require('./validate-args');

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

const options = [
{
name: '--name',
description: 'The application name',
},
{
name: '--cwd',
description: 'A directory to use instead of $PWD',
default: '.',
},
{
name: '--force',
description: 'Force destination output; will override!',
default: false,
},
{
name: '--install',
description: 'Install dependencies',
default: true,
},
{
name: '--yarn',
description: 'Use `yarn` instead of `npm`',
default: false,
},
{
name: '--git',
description: 'Initialize git repository',
default: false,
},
{
name: '--license',
description: 'License type',
default: 'MIT',
},
{
name: '-v, --verbose',
description: 'Verbose output',
default: false,
},
];

// Formulate Questions if `create` args are missing
function requestParams(repo, dest, argv, templates) {
const cwd = resolve(argv.cwd);
Expand Down Expand Up @@ -203,15 +160,13 @@ async function copyFileToDestination(srcPath, destPath, force = false) {
}
}

async function command(repo, dest, argv) {
validateArgs(argv, options, 'create');
exports.create = async function createCommand(repo, dest, argv) {
// Prompt if incomplete data
if (!repo || !dest) {
const templates = await fetchTemplates();
const questions = requestParams(repo, dest, argv, templates);
const onCancel = () => {
info('Aborting execution');
process.exit();
info('Aborting execution', 0);
};
const response = await prompt(questions, { onCancel });

Expand Down Expand Up @@ -427,9 +382,4 @@ async function command(repo, dest, argv) {
${green(pfx + ' serve')}
`) + '\n\n'
);
}

module.exports = {
command,
options,
};
11 changes: 4 additions & 7 deletions packages/cli/src/commands/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const { command: build, options: buildOptions } = require('./build');
const { command: create, options: createOptions } = require('./create');
const list = require('./list');
const { command: watch, options: watchOptions } = require('./watch');
const { build } = require('./build');
const { create } = require('./create');
const { list } = require('./list');
const { watch } = require('./watch');

module.exports = {
build,
buildOptions,
create,
createOptions,
list,
watch,
watchOptions,
};
2 changes: 1 addition & 1 deletion packages/cli/src/commands/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { error, info } = require('../util');

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

module.exports = async function () {
exports.list = async function listCommand() {
try {
const repos = await fetch(REPOS_URL).then(r => r.json());

Expand Down
31 changes: 0 additions & 31 deletions packages/cli/src/commands/validate-args.js

This file was deleted.

106 changes: 4 additions & 102 deletions packages/cli/src/commands/watch.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,9 @@
const runWebpack = require('../lib/webpack/run-webpack');
const { isPortFree, toBool, warn } = require('../util');
const { validateArgs } = require('./validate-args');
const getPort = require('get-port');
const { resolve } = require('path');

const options = [
{
name: '--src',
description: 'Specify source directory',
default: 'src',
},
{
name: '--cwd',
description: 'A directory to use instead of $PWD',
default: '.',
},
{
name: '--esm',
description: 'Builds ES-2015 bundles for your code',
default: false,
},
{
name: '--clear',
description: 'Clear the console',
default: true,
},
{
name: '--sw',
description: 'Generate and attach a Service Worker',
default: undefined,
},
{
name: '--babelConfig',
description: 'Path to custom Babel config',
default: '.babelrc',
},
{
name: '--rhl',
description: '(Deprecated) use --refresh instead',
default: false,
},
{
name: '--json',
description: 'Generate build stats for bundle analysis',
},
{
name: '--https',
description: 'Run server with HTTPS protocol',
},
{
name: '--key',
description: 'Path to PEM key for custom SSL certificate',
},
{
name: '--cert',
description: 'Path to custom SSL certificate',
},
{
name: '--cacert',
description: 'Path to optional CA certificate override',
},
{
name: '--prerender',
description: 'Pre-render static content on first run',
},
{
name: '--prerenderUrls',
description: 'Path to pre-rendered routes config',
default: 'prerender-urls.json',
},
{
name: '--template',
description: 'Path to custom HTML template (default "src/template.html")',
},
{
name: '--refresh',
description: 'Enables experimental preact-refresh functionality',
default: false,
},
{
name: '-c, --config',
description: 'Path to custom CLI config',
default: 'preact.config.js',
},
{
name: '-H, --host',
description: 'Set server hostname',
default: '0.0.0.0',
},
{
name: '-p, --port',
description: 'Set server port (default 8080)',
},
];

async function command(src, argv) {
validateArgs(argv, options, 'watch');
exports.watch = async function watchCommand(src, argv) {
if (argv.rhl) {
delete argv.rhl;
argv.refresh = argv.rhl;
Expand Down Expand Up @@ -126,9 +34,9 @@ async function command(src, argv) {
}

return runWebpack(argv, true);
}
};

async function determinePort(port) {
const determinePort = (exports.determinePort = async function (port) {
port = parseInt(port, 10);
if (port) {
if (!(await isPortFree(port))) {
Expand All @@ -141,10 +49,4 @@ async function determinePort(port) {
}

return port;
}

module.exports = {
command,
options,
determinePort,
};
});

0 comments on commit 7b6b007

Please sign in to comment.