From b1adb1be1c21c0b8097f1826bf5ec3d0c7fe9061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Tue, 29 Oct 2024 12:47:00 +0100 Subject: [PATCH] Update the build system [skip ci] --- gulpfile.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 4dbb9c6..67ede7c 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,18 +1,14 @@ import gulp from "gulp"; import {spawn} from "node:child_process"; import {readdir, readFile, rm, writeFile} from "node:fs/promises"; -import {delimiter, join, resolve} from "node:path"; -import {env} from "node:process"; +import {join} from "node:path"; import pkg from "./package.json" with {type: "json"}; -// Initialize the build system. -env.PATH = `${resolve("node_modules/.bin")}${delimiter}${env.PATH}`; - /** Builds the project. */ export async function build() { const file = "src/client.ts"; await writeFile(file, (await readFile(file, "utf8")).replace(/#version = "\d+(\.\d+){2}"/, `#version = "${pkg.version}"`)); - return $("tsc", ["--build", "src/tsconfig.json"]); + return exec("tsc", "--build", "src/tsconfig.json"); } /** Deletes all generated files. */ @@ -24,25 +20,25 @@ export async function clean() { /** Performs the static analysis of source code. */ export async function lint() { await build(); - await $("tsc", ["--build", "tsconfig.json"]); - return $("eslint", ["--config=etc/eslint.js", "gulpfile.js", "example", "src", "test"]); + await exec("tsc", "--build", "tsconfig.json"); + return exec("eslint", "--config=etc/eslint.js", "gulpfile.js", "example", "src", "test"); } /** Publishes the package. */ export async function publish() { - for (const registry of ["https://registry.npmjs.org", "https://npm.pkg.github.com"]) await $("npm", ["publish", `--registry=${registry}`]); - for (const action of [["tag"], ["push", "origin"]]) await $("git", [...action, `v${pkg.version}`]); + for (const registry of ["https://registry.npmjs.org", "https://npm.pkg.github.com"]) await run("npm", "publish", `--registry=${registry}`); + for (const action of [["tag"], ["push", "origin"]]) await run("git", ...action, `v${pkg.version}`); } /** Runs the test suite. */ export async function test() { await build(); - return $("node", ["--test", "--test-reporter=spec"]); + return run("node", "--test", "--test-reporter=spec"); } /** Watches for file changes. */ export function watch() { - return $("tsc", ["--build", "src/tsconfig.json", "--preserveWatchOutput", "--sourceMap", "--watch"]); + return exec("tsc", "--build", "src/tsconfig.json", "--preserveWatchOutput", "--sourceMap", "--watch"); } /** The default task. */ @@ -51,15 +47,24 @@ export default gulp.series( build ); +/** + * Executes a command from a local package. + * @param {string} command The command to run. + * @param {...string} args The command arguments. + * @return {Promise} Resolves when the command is terminated. + */ +function exec(command, ...args) { + return run("npm", "exec", "--", command, ...args); +} + /** * Spawns a new process using the specified command. * @param {string} command The command to run. - * @param {string[]} args The command arguments. - * @param {import("node:child_process").SpawnOptionsWithoutStdio} options The settings to customize how the process is spawned. + * @param {...string} args The command arguments. * @return {Promise} Resolves when the command is terminated. */ -function $(command, args = [], options = {}) { +function run(command, ...args) { const {promise, resolve: fulfill, reject} = /** @type {PromiseWithResolvers} */ (Promise.withResolvers()); - spawn(command, args, {shell: true, stdio: "inherit", ...options}).on("close", code => code ? reject(new Error(command)) : fulfill()); + spawn(command, args, {shell: true, stdio: "inherit"}).on("close", code => code ? reject(new Error(command)) : fulfill()); return promise; }