Skip to content

Commit

Permalink
scripts: update and align CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Mar 3, 2025
1 parent f89efa9 commit 07485d3
Show file tree
Hide file tree
Showing 9 changed files with 498 additions and 169 deletions.
159 changes: 111 additions & 48 deletions scripts/build_demo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,75 +23,73 @@ const DEMO_OUT_FILE = join(rootDirectory, "demo/bundle.js");
const WORKER_OUT_FILE = join(rootDirectory, "demo/worker.js");
const WASM_FILE_DEPENDENCY = join(rootDirectory, "dist/mpd-parser.wasm");

// If true, this script is called directly
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
const { argv } = process;
if (argv.includes("-h") || argv.includes("--help")) {
displayHelp();
process.exit(0);
}
const shouldWatch = argv.includes("-w") || argv.includes("--watch");
const shouldMinify = argv.includes("-m") || argv.includes("--minify");
const production = argv.includes("-p") || argv.includes("--production-mode");
const includeWasmParser = argv.includes("--include-wasm");
buildDemo({
watch: shouldWatch,
minify: shouldMinify,
includeWasmParser,
production,
});
}

/**
* Build the demo with the given options.
* @param {Object} options
* @param {Object} [options]
* @param {boolean} [options.minify] - If `true`, the output will be minified.
* Defaults to `false`.
* @param {boolean} [options.production] - If `false`, the code will be compiled
* in "development" mode, which has supplementary assertions.
* Defaults to `true`.
* @param {boolean} [options.watch] - If `true`, the RxPlayer's files involve
* will be watched and the code re-built each time one of them changes.
* Defaults to `false`.
* @param {boolean} [options.silent] - If `true`, no logs will be outputed to
* signal the advancement of the build.
* Defaults to `false`.
* @param {boolean} [options.includeWasmParser] - If `true`, the WebAssembly MPD
* parser of the RxPlayer will be used (if it can be requested).
* Defaults to `false`.
* @returns {Promise} - Promise which resolves when the intial build is done or
* rejects with an error when it cannot be done.
*/
export default function buildDemo(options) {
export default function buildDemo(options = {}) {
const verbose = !options.silent;
const minify = !!options.minify;
const watch = !!options.watch;
const isDevMode = !options.production;
const includeWasmParser = !!options.includeWasmParser;
const outfile = DEMO_OUT_FILE;

stat(WASM_FILE_DEPENDENCY, (err) => {
if (err != null && err.code === "ENOENT") {
console.warn(
"\x1b[31m[NOTE]\x1b[0m No built WebAssembly file detected. " +
"If needed, please build it separately.",
);
} else {
console.warn(
"\x1b[33m[NOTE]\x1b[0m The WebAssembly file won't be re-built by " +
"this script. If needed, ensure its build is up-to-date.",
);
}
});
if (verbose && includeWasmParser) {
stat(WASM_FILE_DEPENDENCY, (err) => {
if (err != null && err.code === "ENOENT") {
console.warn(
"\x1b[31m[NOTE]\x1b[0m No built WebAssembly file detected. " +
"If needed, please build it separately.",
);
} else {
console.warn(
"\x1b[33m[NOTE]\x1b[0m The WebAssembly file won't be re-built by " +
"this script. If needed, ensure its build is up-to-date.",
);
}
});
}

runBundler(WORKER_IN_FILE, {
const promWorkerBuild = runBundler(WORKER_IN_FILE, {
watch,
minify,
production: !isDevMode,
outfile: WORKER_OUT_FILE,
silent: false,
silent: !verbose,
});

/** Declare a plugin to anounce when a build begins and ends */
const consolePlugin = {
name: "onEnd",
setup(build) {
build.onStart(() => {
console.log(
`\x1b[33m[${getHumanReadableHours()}]\x1b[0m ` + "New demo build started",
);
if (verbose) {
console.log(
`\x1b[33m[${getHumanReadableHours()}]\x1b[0m ` + "New demo build started",
);
}
});
build.onEnd((result) => {
if (!verbose) {
return;
}
if (result.errors.length > 0 || result.warnings.length > 0) {
const { errors, warnings } = result;
console.log(
Expand All @@ -111,7 +109,7 @@ export default function buildDemo(options) {
const meth = watch ? "context" : "build";

// Create a context for incremental builds
esbuild[meth]({
const promDemoBuild = esbuild[meth]({
entryPoints: [join(rootDirectory, "demo/scripts/index.tsx")],
bundle: true,
target: "es2017",
Expand All @@ -138,12 +136,73 @@ export default function buildDemo(options) {
}
})
.catch((err) => {
console.error(
`\x1b[31m[${getHumanReadableHours()}]\x1b[0m Demo build failed:`,
err,
);
if (verbose) {
console.error(
`\x1b[31m[${getHumanReadableHours()}]\x1b[0m Demo build failed:`,
err,
);
}
});

return Promise.all([promWorkerBuild, promDemoBuild]);
}

// If true, this script is called directly
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
const args = process.argv.slice(2);
let shouldWatch = false;
let shouldMinify = false;
let production = false;
let silent = false;
let includeWasmParser = false;
for (let argOffset = 0; argOffset < args.length; argOffset++) {
const currentArg = args[argOffset];
switch (currentArg) {
case "-h":
case "--help":
displayHelp();
process.exit(0);
case "-w":
case "--watch":
shouldWatch = true;
break;
case "-m":
case "--minify":
shouldMinify = true;
break;
case "-p":
case "--production-mode":
production = true;
break;
case "-s":
case "--silent":
silent = true;
break;
case "--include-wasm":
includeWasmParser = true;
break;
default: {
console.error('ERROR: unknown option: "' + currentArg + '"\n');
displayHelp();
process.exit(1);
}
}
}
try {
buildDemo({
silent,
watch: shouldWatch,
minify: shouldMinify,
includeWasmParser,
production,
}).catch((err) => {
console.error(`ERROR: ${err}\n`);
process.exit(1);
});
} catch (err) {
console.error(`ERROR: ${err}\n`);
process.exit(1);
}
}

/**
Expand All @@ -152,12 +211,16 @@ export default function buildDemo(options) {
*/
function displayHelp() {
console.log(
`Usage: node build_demo.mjs [options]
`build_demo.mjs: Build the RxPlayer's demo in the "demo/" directory.
Usage: node build_demo.mjs [OPTIONS]
Options:
-h, --help Display this help
-m, --minify Minify the built demo
-h, --help Display this help.
-m, --minify Minify the built demo.
-p, --production-mode Build all files in production mode (less runtime checks, mostly).
-w, --watch Re-build each time either the demo or library files change
-w, --watch Re-build each time either the demo or library files change.
-s, --silent Don't log to stdout/stderr when bundling.
--include-wasm The demo will be able to request the WebAssembly MPD parser (if available).`,
);
}
71 changes: 53 additions & 18 deletions scripts/generate_build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,8 @@ const BUILD_ARTEFACTS_TO_REMOVE = [
const WORKER_IN_FILE = path.join(ROOT_DIR, "src/worker_entry_point.ts");
const WORKER_OUT_FILE = path.join(ROOT_DIR, "dist/worker.js");

// If true, this script is called directly
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
const { argv } = process;
if (argv.includes("-h") || argv.includes("--help")) {
displayHelp();
process.exit(0);
}
const devMode = argv.includes("-d") || argv.includes("--dev-mode");
const noCheck = argv.includes("-n") || argv.includes("--no-check");
generateBuild({
devMode,
noCheck,
});
}

/**
* @param {Object|undefined} options
* @param {Object|undefined} [options]
* @param {boolean|undefined} [options.devMode]
* @param {boolean|undefined} [options.noCheck]
* @returns {Promise}
Expand Down Expand Up @@ -153,9 +138,15 @@ async function compile(opts) {
}

/**
* Spawn a shell with the command given in argument, alongside that command's
* arguments.
* Return a Promise that resolves if the command exited with the exit code `0`
* or rejects if the exit code is not zero.
* @param {string} command
* @param {Array.<string>} args
* @param {Function} errorOnCode
* @param {Function} errorOnCode - Callback which will be called if the command
* has an exit code different than `0`, with the exit code in argument. The
* value returned by that callback will be the value rejected by the Promise.
*/
function spawnProm(command, args, errorOnCode) {
return new Promise((res, rej) => {
Expand All @@ -168,13 +159,57 @@ function spawnProm(command, args, errorOnCode) {
});
}

// If true, this script is called directly
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
const args = process.argv.slice(2);
let devMode = false;
let noCheck = false;
for (let argOffset = 0; argOffset < args.length; argOffset++) {
const currentArg = args[argOffset];
switch (currentArg) {
case "-h":
case "--help":
displayHelp();
process.exit(0);
case "-d":
case "--dev-mode":
devMode = true;
break;
case "-n":
case "--no-check":
noCheck = true;
break;
default: {
console.error('ERROR: unknown option: "' + currentArg + '"\n');
displayHelp();
process.exit(1);
}
}
}
try {
generateBuild({
devMode,
noCheck,
}).catch((err) => {
console.error(`ERROR: ${err}\n`);
process.exit(1);
});
} catch (err) {
console.error(`ERROR: ${err}\n`);
process.exit(1);
}
}

/**
* Display through `console.log` an helping message relative to how to run this
* script.
*/
function displayHelp() {
console.log(
`Usage: node build_worker.mjs [options]
`generate_build.mjs: Produce the main RxPlayer's builds (do not produce RxPlayer bundles).
Usage: node build_worker.mjs [OPTIONS]
Options:
-h, --help Display this help
-p, --dev-mode Build all files in development mode (more runtime checks, worker not minified)
Expand Down
39 changes: 37 additions & 2 deletions scripts/generate_demo_list.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,55 @@
*
* To run this:
*
* 1. Be sure you are in the gh-pages branch
* 1. Be sure you are in the `gh-pages` git branch
*
* 2. Call this script directly
*
* 3. a new file, `demo_page_by_version.html` should have been generated with
* 3. A new file, `demo_page_by_version.html` should have been generated with
* all the right links.
*/

import { execSync } from "child_process";
import { lstatSync, readdirSync, existsSync, writeFileSync } from "fs";
import { join } from "path";
import { encode } from "html-entities";
import * as semver from "semver";

const INITIAL_PATH = "./versions";
const TARGET_BRANCH = "gh-pages";

const currentBranch = executeCommand(
"git branch | sed -n -e 's/^\\* \\(.*\\)/\\1/p'",
).trim();
if (currentBranch !== TARGET_BRANCH) {
console.error(
"Error: You're not on the right git branch to execute this script.\n" +
'Current Branch: "' +
currentBranch +
'"\n' +
'Expected Branch: "' +
TARGET_BRANCH +
'"',
);
process.exit(1);
}

if (!existsSync(INITIAL_PATH)) {
console.error(`Error: Missing "${INITIAL_PATH}" directory.`);
process.exit(1);
}

/**
* Execute the given shell command and return the output.
* @param {string} cmd
* @returns {string}
*/
function executeCommand(cmd) {
return execSync(cmd, {
encoding: "utf8",
shell: true,
});
}

function sortVersions(versions) {
return versions
Expand Down
Loading

0 comments on commit 07485d3

Please sign in to comment.