Skip to content

Commit

Permalink
Update the build system [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Oct 29, 2024
1 parent 3a73ad1 commit b1adb1b
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -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. */
Expand All @@ -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. */
Expand All @@ -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<void>} 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<void>} Resolves when the command is terminated.
*/
function $(command, args = [], options = {}) {
function run(command, ...args) {
const {promise, resolve: fulfill, reject} = /** @type {PromiseWithResolvers<void>} */ (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;
}

0 comments on commit b1adb1b

Please sign in to comment.