From 2d747e97b0a84d7a0d0b65f5042dba9f63edab11 Mon Sep 17 00:00:00 2001 From: Ryan Christian <33403762+rschristian@users.noreply.github.com> Date: Tue, 20 Dec 2022 18:30:39 -0600 Subject: [PATCH] refactor: Bump Node version, remove `src` arg in build & watch, and misc housekeeping (#1753) * refactor: Drop rimraf for built-in fs.rm * refactor: Drop src argument in build & watch cmds * refactor: Extract info cmd from CLI entrypoint * refactor: Remove update-notifier * refactor: Switch from fs.promises to fs/promises * docs: Adding changeset --- .changeset/tender-lamps-boil.md | 9 + packages/cli/package.json | 6 +- packages/cli/src/commands/build.js | 26 +- packages/cli/src/commands/index.js | 7 - packages/cli/src/commands/info.js | 18 + packages/cli/src/commands/watch.js | 7 +- packages/cli/src/index.js | 38 +- .../cli/src/lib/webpack/transform-config.js | 2 +- .../src/lib/webpack/webpack-base-config.js | 16 +- packages/cli/src/util.js | 8 +- packages/cli/tests/build.test.js | 10 +- packages/cli/tests/config-formats.test.js | 2 +- packages/cli/tests/lib/cli.js | 9 +- packages/cli/tests/lib/output.js | 2 +- packages/cli/tests/lib/utils.js | 7 +- packages/cli/tests/service-worker.test.js | 2 +- packages/cli/tests/watch.test.js | 2 +- yarn.lock | 340 +----------------- 18 files changed, 98 insertions(+), 413 deletions(-) create mode 100644 .changeset/tender-lamps-boil.md delete mode 100644 packages/cli/src/commands/index.js create mode 100644 packages/cli/src/commands/info.js diff --git a/.changeset/tender-lamps-boil.md b/.changeset/tender-lamps-boil.md new file mode 100644 index 000000000..4e33286c2 --- /dev/null +++ b/.changeset/tender-lamps-boil.md @@ -0,0 +1,9 @@ +--- +'preact-cli': major +--- + +Minimum supported Node version for `preact-cli` is now v14.14.0. Please upgrade if you are on an older version. + +`build` and `watch` commands will no longer take an optional `src` directory argument; if you want to change the source directory from the default (`./src`), please instead use the `--src` flag (i.e., `--src differentSrc`). + +Upon rebuild, the output directory will no longer be outright deleted; instead, it will be emptied. This has the benefit of better supporting containerized environments where specific directories are mounted. Emptying the directory, rather than deleting and recreating it, ensures a stable reference for those tools. diff --git a/packages/cli/package.json b/packages/cli/package.json index e72de6063..0a176f2dc 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -64,7 +64,6 @@ "kleur": "^4.1.4", "mini-css-extract-plugin": "^2.5.3", "minimatch": "^3.0.3", - "native-url": "0.3.4", "optimize-plugin": "^1.3.1", "postcss": "^8.4.13", "postcss-load-config": "^3.1.4", @@ -72,7 +71,6 @@ "progress-bar-webpack-plugin": "^2.1.0", "promise-polyfill": "^8.2.3", "react-refresh": "0.11.0", - "rimraf": "^3.0.2", "sade": "^1.8.1", "size-plugin": "^2.0.2", "source-map": "^0.7.2", @@ -81,7 +79,6 @@ "style-loader": "^3.3.1", "terser-webpack-plugin": "^5.3.0", "typescript": "^4.6.4", - "update-notifier": "^5.1.0", "webpack": "^5.67.0", "webpack-bundle-analyzer": "^4.5.0", "webpack-dev-server": "^4.9.0", @@ -108,6 +105,7 @@ "preact-render-to-string": "^5.2.6", "preact-router": "^3.0.1", "puppeteer": "^17.1.3", + "rimraf": "^3.0.2", "sass": "^1.56.1", "sass-loader": "^12.4.0", "shelljs": "^0.8.5", @@ -134,6 +132,6 @@ } }, "engines": { - "node": ">=12" + "node": ">=14.14.0" } } diff --git a/packages/cli/src/commands/build.js b/packages/cli/src/commands/build.js index c103160b4..fc8a5e5ca 100644 --- a/packages/cli/src/commands/build.js +++ b/packages/cli/src/commands/build.js @@ -1,26 +1,32 @@ -const rimraf = require('rimraf'); -const { resolve } = require('path'); -const { promisify } = require('util'); +const { readdir, rm } = require('fs/promises'); +const { join, resolve } = require('path'); const runWebpack = require('../lib/webpack/run-webpack'); const { toBool } = require('../util'); -exports.build = async function buildCommand(src, argv) { - argv.src = src || argv.src; +exports.build = async function buildCommand(argv) { // add `default:true`s, `--no-*` disables argv.prerender = toBool(argv.prerender); let cwd = resolve(argv.cwd); + // Empties destination directory -- useful when mounted with Docker + // or similar situations where it's preferable to avoid directory deletion + let dest = resolve(cwd, argv.dest); + try { + await Promise.all( + ( + await readdir(dest) + ).map(item => rm(join(dest, item), { recursive: true })) + ); + } catch (e) { + if (e.code != 'ENOENT') throw e; + } + // we explicitly set the path as `dotenv` otherwise uses // `process.cwd()` -- this would cause issues in environments // like mono-repos or our test suite subjects where project root // and the current directory differ. require('dotenv').config({ path: resolve(cwd, '.env') }); - if (argv.clean === void 0) { - let dest = resolve(cwd, argv.dest); - await promisify(rimraf)(dest); - } - await runWebpack(argv, true); }; diff --git a/packages/cli/src/commands/index.js b/packages/cli/src/commands/index.js deleted file mode 100644 index 3131264d2..000000000 --- a/packages/cli/src/commands/index.js +++ /dev/null @@ -1,7 +0,0 @@ -const { build } = require('./build'); -const { watch } = require('./watch'); - -module.exports = { - build, - watch, -}; diff --git a/packages/cli/src/commands/info.js b/packages/cli/src/commands/info.js new file mode 100644 index 000000000..7c3aa9b15 --- /dev/null +++ b/packages/cli/src/commands/info.js @@ -0,0 +1,18 @@ +const envinfo = require('envinfo'); + +exports.info = async function infoCommand() { + const info = await envinfo.run({ + System: ['OS', 'CPU'], + Binaries: ['Node', 'Yarn', 'npm'], + Browsers: ['Chrome', 'Edge', 'Firefox', 'Safari'], + npmPackages: [ + 'preact', + 'preact-cli', + 'preact-router', + 'preact-render-to-string', + ], + npmGlobalPackages: ['preact-cli'], + }); + + process.stdout.write(`\nEnvironment Info:${info}\n`); +}; diff --git a/packages/cli/src/commands/watch.js b/packages/cli/src/commands/watch.js index 81a6962b8..69cbef28a 100644 --- a/packages/cli/src/commands/watch.js +++ b/packages/cli/src/commands/watch.js @@ -1,10 +1,9 @@ +const { resolve } = require('path'); +const getPort = require('get-port'); const runWebpack = require('../lib/webpack/run-webpack'); const { isPortFree, toBool, warn } = require('../util'); -const getPort = require('get-port'); -const { resolve } = require('path'); -exports.watch = async function watchCommand(src, argv) { - argv.src = src || argv.src; +exports.watch = async function watchCommand(argv) { if (argv.sw) { argv.sw = toBool(argv.sw); } diff --git a/packages/cli/src/index.js b/packages/cli/src/index.js index 3312b118d..b267ae77e 100755 --- a/packages/cli/src/index.js +++ b/packages/cli/src/index.js @@ -1,8 +1,9 @@ #!/usr/bin/env node -const envinfo = require('envinfo'); const sade = require('sade'); -const notifier = require('update-notifier'); const { green } = require('kleur'); +const { build } = require('./commands/build'); +const { info } = require('./commands/info'); +const { watch } = require('./commands/watch'); const { error } = require('./util'); const pkg = require('../package.json'); const { isNodeVersionGreater } = require('./util'); @@ -14,16 +15,10 @@ if (!isNodeVersionGreater(min)) { ); } -// Safe to load async-based funcs -const commands = require('./commands'); - -// installHooks(); -notifier({ pkg }).notify(); - const prog = sade('preact').version(pkg.version); prog - .command('build [src]') + .command('build') .describe( 'Create a production build. You can disable "default: true" flags by prefixing them with --no-