UNPKG

@spotify/web-scripts

Version:

Build, lint, test, format, and release your JS/TS library.

251 lines (250 loc) 7.77 kB
import { program } from 'commander'; import { COMMITLINT_CONIFG } from './Paths'; import { auditTask } from './Tasks/AuditTasks'; import { testTask } from './Tasks/TestTask'; import { buildTask } from './Tasks/BuildTask'; import { lintTask } from './Tasks/LintTask'; import { formatTask } from './Tasks/FormatTask'; import { commitTask, commitMsgTask, releaseTask, precommitTask, } from './Tasks/CommitTasks'; program .command('init') .description('initialize your package to use web-scripts') .action(function () { throw new Error('unimplemented'); }); program .command('build') .allowUnknownOption() .description('Build your project into esm, cjs, and types folders') .option('--no-esm', 'do not build esm target') .option('--no-cjs', 'do not build cjs target') .option('--no-types', 'do not build types target') .action(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var cmd = getCommand(args); var _a = getOpts(cmd), esm = _a.esm, types = _a.types, cjs = _a.cjs; var t = { name: 'build', esm: esm, types: types, cjs: cjs, restOptions: cmd.args, }; handlePromiseResult(buildTask(t)); }); program .command('test') .allowUnknownOption() .description('Run tests via jest') .option('--config [path]', 'path to jest config') .action(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var cmd = getCommand(args); var config = getOpts(cmd).config; var t = { name: 'test', config: config, restOptions: cmd.args, }; var result = testTask(t); handleSpawnResult(result); }); program .command('lint') .allowUnknownOption() .description('Run ESLint and TypeScript to statically analyze your code') .option('--config [path]', 'path to ESLint config') .option('--typecheck', 'run a TypeScript type check', true) .option('--no-typecheck', 'do not run a TypeScript type check') .option('--stylecheck', "run Prettier's style check", true) .option('--no-stylecheck', "do not run Prettier's style check") .action(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var cmd = getCommand(args); var _a = getOpts(cmd), stylecheck = _a.stylecheck, typecheck = _a.typecheck, config = _a.config; var t = { name: 'lint', config: config, stylecheck: stylecheck, typecheck: typecheck, restOptions: cmd.args, }; handlePromiseResult(lintTask(t)); }); program .command('format') .allowUnknownOption() .description('Run Prettier to format your code') .option('--config [path]', 'path to Prettier config') .option('--path [path]', 'path to project folder to format. targets the `src` directory within this path only.') .action(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var cmd = getCommand(args); var _a = getOpts(cmd), config = _a.config, path = _a.path; var t = { name: 'format', config: config, path: path, restOptions: cmd.args, }; handleSpawnResult(formatTask(t)); }); program .command('precommit') .allowUnknownOption() .description('Locally validate the repo before committing') .option('--jest-config [path]', 'path to jest config') .option('--prettier-config [path]', 'path to prettier config') .option('--eslint-config [path]', 'path to eslint config') .option('--no-tests', 'Do not run Jest tests') .option('--no-typecheck', 'Do not type check using TypeScript') .action(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var cmd = getCommand(args); var _a = getOpts(cmd), tests = _a.tests, typecheck = _a.typecheck, jestConfig = _a.jestConfig, eslintConfig = _a.eslintConfig, prettierConfig = _a.prettierConfig; var t = { name: 'precommit', tests: tests, typecheck: typecheck, jestConfig: jestConfig, eslintConfig: eslintConfig, prettierConfig: prettierConfig, restOptions: cmd.args, }; handlePromiseResult(precommitTask(t)); }); function thresholdLimiter(value, defaultValue) { switch (value) { case 'info': case 'low': case 'moderate': case 'high': case 'critical': return value; default: return defaultValue; } } program .command('audit') .alias('postinstall') .allowUnknownOption() .description("Run yarn audit and exit non-zero if the security vulnerability threshold is breached") .option('--threshold [info|low|moderate|high|critical]', 'The amount of permissible vulnerabilities', thresholdLimiter, 'none') .action(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var cmd = getCommand(args); var threshold = getOpts(cmd).threshold; var t = { name: 'audit', threshold: threshold, restOptions: cmd.args, }; handlePromiseResult(auditTask(t)); }); program .command('commit') .allowUnknownOption() .description('Create Commitizen commit from staged files') .option('--path [path]', 'path for commitizen adapter to use', 'cz-conventional-changelog') .action(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var cmd = getCommand(args); var path = getOpts(cmd).path; var t = { name: 'commit', path: path, restOptions: cmd.args, }; try { commitTask(t); } catch (err) { handleError(err); } }); program .command('commitmsg') .allowUnknownOption() .description('Run commitizen commit message validation hook') .option('--config [path]', 'path to the commitlint config.', COMMITLINT_CONIFG) .option('--edit [path]', 'read last commit message from the specified file (only necessary when using Husky v6).') .action(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var cmd = getCommand(args); var _a = getOpts(cmd), config = _a.config, edit = _a.edit; var t = { name: 'commitmsg', config: config, edit: edit, restOptions: cmd.args, }; handleSpawnResult(commitMsgTask(t)); }); program .command('release') .allowUnknownOption() .description('Run semantic-release') .action(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var cmd = getCommand(args); var t = { name: 'release', restOptions: cmd.args, }; handleSpawnResult(releaseTask(t)); }); function handlePromiseResult(result) { result.catch(handleError); } function handleError(error) { if (error.message && error.message.indexOf('Error: Exited with status') < 0) { console.error(error); } process.exit(error.exitStatus || 1); } function handleSpawnResult(result) { if (result.error) { throw result.error; } if (result.status !== 0) { process.exit(result.status === null ? 0 : result.status); } } function getCommand(args) { return args[1]; } function getOpts(cmd) { return cmd.opts(); } program.parse(process.argv); if (!process.argv.slice(2).length) { program.help(); }