Skip to content

Commit

Permalink
rewrote build script in typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
z1glr committed Feb 28, 2024
1 parent f97040d commit 05401d6
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"project": [
"src/server/tsconfig.json",
"src/client/tsconfig.json",
"src/templates/tsconfig.json"
"src/templates/tsconfig.json",
"build/scripts/tsconfig.json"
]
},
"extends": [
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ dist
client/*.js
casparcg-templates/JohnCG/*.js
*.map
client/webfonts/bahnschrift.ttf
client/webfonts/bahnschrift.ttf
build/scripts/*.js
build/scripts/3rdpartylicenses.json
52 changes: 0 additions & 52 deletions build.ps1

This file was deleted.

119 changes: 119 additions & 0 deletions build/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { execSync } from "child_process";
import fs from "fs";
import path from "path";
import tar from "tar";

import { ConfigJSON } from "../../src/server/config";

// load the package.json
const package_json = JSON.parse(fs.readFileSync("package.json", "utf-8")) as { version: string; dependencies: string[] };

const build_name = "JohnCG_" + package_json.version;
// temporary method until there is a solution for packaging sharp
// const exec_name = build_name + ".exe";
const exec_name = "node.exe";
const build_dir = "dist/build";
const release_dir = path.join("dist", build_name);

// clear the build-directory
fs.rmSync("dist", { recursive: true, force: true });
fs.mkdirSync(build_dir, { recursive: true });
fs.mkdirSync(path.join("dist", build_name), { recursive: true });

const copy_build_file = (file: string, dest?: string) => fs.copyFileSync(file, path.join(build_dir, dest ?? path.basename(file)));
// const copy_build_dir = (dir: string, dest?: string, args?: fs.CopySyncOptions) => fs.cpSync(dir, path.join(build_dir, dest ?? path.basename(dir)), { recursive: true, ...args });
const copy_release_file = (file: string, dest?: string) => fs.copyFileSync(file, path.join(release_dir, dest ?? path.basename(file)));
const copy_release_dir = (dir: string, dest?: string, args?: fs.CopySyncOptions) => fs.cpSync(dir, path.join(release_dir, dest ?? path.basename(dir)), { recursive: true, ...args });

// bundle the different scripts
execSync("npm run build-server");
execSync("npm run build-client");
execSync("npm run build-templates");

// temporary method until there is a solution for packaging sharp
// // create sea-prep.blob
// execSync("node --experimental-sea-config sea-config.json");

// get the node executable
copy_build_file(process.execPath, exec_name);

// temporary method until there is a solution for packaging sharp
// // remove the signature from the node executable
// execSync(`'C:\\Program Files (x86)\\Windows Kits\\10\\bin\\10.0.22621.0\\x64\\signtool.exe' remove /s dist/build/${exec_name}`);
// // modify the node executable
// execSync(`npx postject dist/build/${exec_name} NODE_SEA_BLOB dist/build/sea-prep.blob --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2`);

// load the config-file, censor the file-paths and store it for the relase
const config_file = JSON.parse(fs.readFileSync("config.json", "utf-8")) as ConfigJSON;
config_file.path = {
background_image: "C:/path/to/image/directory",
song: "D:/path/to/song/directory"
};
config_file.casparcg.templates = "e:/path/to/the/casparcg/templates/directory";
fs.writeFileSync(path.join(release_dir, "config.json"), JSON.stringify(config_file, undefined, "\t"));

// copy the file to the output
copy_release_file(path.join(build_dir, exec_name));
copy_release_file(path.join(build_dir, "main.js"));

copy_release_dir("casparcg-templates", undefined, { filter: (src) => {
switch (true) {
case path.basename(src) === ".eslintrc":
case [".js", ".map"].includes(path.extname(src)):
return false;
default:
return true;
}
} });
copy_release_dir("client", undefined, { filter: (src) => {
switch (true) {
case [".eslintrc", "bahnschrift.ttf"].includes(path.basename(src)):
case [".js", ".map"].includes(path.extname(src)):
return false;
default:
return true;
}
} });
copy_release_dir("node_modules/@img", path.join(release_dir, "node_modules/@img/"));

// temporary method until there is a solution for packaging sharp
// create a batch file, that start node with the main.js
fs.writeFileSync(path.join(release_dir, build_name + ".bat"), `${exec_name} main.js\npause`);

// create and copy the licenses
// void lr.cli(["--config=build/scripts/license-reporter.config.ts"]);
try {
execSync("npx license-reporter --config build/scripts/license-reporter.config.ts");
} catch (e) { /* empty */ }

// eslint-disable-next-line @typescript-eslint/naming-convention
interface License { name: string; licenseText: string }
const licenses_orig = JSON.parse(fs.readFileSync("build/scripts/3rdpartylicenses.json", "utf-8")) as License[];

const licenses: Record<string, License> = {};

licenses_orig.forEach((pack) => {
licenses[pack.name] = pack;
});

fs.mkdirSync("dist/build/licenses");

Object.keys(package_json.dependencies).forEach((pack) => {
const lic = licenses[pack];

try {
fs.writeFileSync(`dist/build/licenses/${lic.name}.txt`, lic.licenseText, "utf-8");
} catch (e) {
if (lic.licenseText === undefined) {
throw new EvalError(`ERROR: no license was found for the package '${lic.name}'`);
}
}
});

copy_release_file("LICENSE", "LICENSE.txt");

// copy the licenses
copy_release_dir(path.join(build_dir, "licenses"));

// pack the files in a .tar.gz-file
void tar.c({ gzip: true, file: release_dir + ".tar.gz" }, [release_dir]);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { IReporterConfiguration } from "@weichwarenprojekt/license-reporter";

export const configuration: Partial<IReporterConfiguration> = {
// defaultLicenseText: undefined,
output: "dist/build/3rdpartylicenses.json",
output: "build/scripts/3rdpartylicenses.json",
ignore: ["dist/*"],
overrides: [
{
name: "osc"
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions build/scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"removeComments": true
}
}
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@types/mime-types": "^2.1.4",
"@types/node": "^20.11.16",
"@types/node-osc": "^6.0.3",
"@types/tar": "^6.1.11",
"@types/tmp": "^0.2.6",
"@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "^6.21.0",
Expand All @@ -28,12 +29,13 @@
"esbuild": "^0.20.0",
"eslint": "^8.56.0",
"postject": "^1.0.0-alpha.6",
"tar": "^6.2.0",
"typescript": "^5.3.3"
},
"scripts": {
"lint": "eslint src/**/*.ts",
"build-release": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ./build.ps1",
"build-server": "esbuild src/server/main.ts --outfile=dist/build/main.js --tsconfig=src/server/tsconfig.json --platform=node --minify --bundle",
"build-release": "esbuild build/scripts/build.ts --outfile=build/scripts/build.js --tsconfig=build/scripts/tsconfig.json --platform=node --bundle && node build/scripts/build.js",
"build-server": "esbuild src/server/main.ts --outfile=dist/build/main.js --tsconfig=src/server/tsconfig.json --platform=node --minify --bundle --external:pdfjs-dist --external:canvas",
"build-client": "esbuild src/client/main.ts --outfile=client/main.js --tsconfig=src/client/tsconfig.json --bundle --minify",
"watch-client": "esbuild src/client/main.ts --outfile=client/main.js --tsconfig=src/client/tsconfig.json --bundle --sourcemap --watch",
"build-templates": "esbuild src/templates/*.ts --outdir=casparcg-templates/JohnCG/ --tsconfig=src/templates/tsconfig.json --target=chrome117 --minify",
Expand Down
2 changes: 1 addition & 1 deletion src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface CasparCGConnectionSettings {
layers: [number, number];
}

interface ConfigJSON {
export interface ConfigJSON {
behaviour: {
show_on_load: boolean;
};
Expand Down

0 comments on commit 05401d6

Please sign in to comment.