diff --git a/.gitignore b/.gitignore index 18558b1..0ccd7a7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ node_modules dist bundle +build .DS_Store # Yarn related files diff --git a/package.json b/package.json index 72ee5b3..4dc08a4 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ ] }, "scripts": { - "build": "tsc", + "build": "node scripts/build.js", "watch": "tsc --watch", "lint": "eslint src", "format": "prettier --write src && eslint --fix src", @@ -39,6 +39,16 @@ }, "contributors": [], "license": "Apache-2.0", + "optionalDependencies": { + "better-sqlite3": "*", + "@json2csv/plainjs": "*", + "@json2csv/transforms": "*", + "@napi-rs/clipboard": "*", + "@napi-rs/keyring": "*", + "@node-rs/argon2": "*", + "playwright-core": "*", + "node-mac-auth": "*" + }, "devDependencies": { "@types/async": "^3.2.24", "@types/better-sqlite3": "^7.6.9", @@ -50,6 +60,7 @@ "@typescript-eslint/parser": "^7.7.0", "@yao-pkg/pkg": "^5.11.5", "chai": "^4.4.1", + "esbuild": "^0.21.2", "eslint": "^8.57.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-import": "^2.29.1", diff --git a/scripts/build.js b/scripts/build.js new file mode 100644 index 0000000..05bd649 --- /dev/null +++ b/scripts/build.js @@ -0,0 +1,81 @@ +#!/usr/bin/env node + +const os = require('os'); +const fs = require('fs'); +const path = require('path'); +const process = require('process'); +const childProcess = require('child_process'); +const esbuild = require('esbuild'); +const packageJSON = require('../package.json'); + +const platform = os.platform(); + +/* eslint-disable no-console */ +async function main(argv = process.argv) { + argv = argv.slice(2); + const projectRoot = path.join(__dirname, '..'); + const buildPath = path.join(projectRoot, 'build'); + const distPath = path.join(projectRoot, 'dist'); + const gitPath = process.env.GIT_DIR ?? path.join(projectRoot, '.git'); + await fs.promises.rm(distPath, { + recursive: true, + force: true, + }); + const buildArgs = ['-p', './tsconfig.build.json', ...argv]; + console.error('Running tsc:'); + console.error(['tsc', ...buildArgs].join(' ')); + childProcess.execFileSync('tsc', buildArgs, { + stdio: ['inherit', 'inherit', 'inherit'], + windowsHide: true, + encoding: 'utf-8', + shell: platform === 'win32' ? true : false, + }); + // This collects the build metadata and adds it to the build folder so that dynamic imports to it will resolve correctly. + let gitHead = process.env.COMMIT_HASH; + if (gitHead == null) { + gitHead = await fs.promises.readFile(path.join(gitPath, 'HEAD'), 'utf-8'); + if (gitHead.startsWith('ref: ')) { + const refPath = gitHead.slice(5).trim(); + gitHead = await fs.promises + .readFile(path.join(gitPath, refPath), 'utf-8') + .then((ref) => ref.trim()); + } + } + const buildJSON = { + versionMetadata: { + version: packageJSON.version, + commitHash: gitHead, + }, + }; + console.error('Writing build metadata (build.json):'); + console.error(buildJSON); + await fs.promises.writeFile( + path.join(buildPath, 'build.json'), + JSON.stringify(buildJSON, null, 2), + ); + // This specifies import paths that is left as an external require + // This is kept to packages that have a native binding + const externalDependencies = Object.keys(packageJSON.optionalDependencies ?? {}); + const esbuildOptions = { + entryPoints: [ + path.join(buildPath, 'index.js'), + ], + sourceRoot: buildPath, + bundle: true, + platform: 'node', + outdir: distPath, + external: externalDependencies, + treeShaking: true, + // External source map for debugging + sourcemap: true, + // Minify and keep the original names + minify: true, + keepNames: true, + }; + console.error('Running esbuild:'); + console.error(esbuildOptions); + await esbuild.build(esbuildOptions); +} +/* eslint-enable no-console */ + +void main(); \ No newline at end of file diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..3e49c73 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "rootDir": "./src", + "noEmit": false, + "stripInternal": true + }, + "exclude": ["./documentation/**/*", "./scripts/**/*"] +} diff --git a/tsconfig.json b/tsconfig.json index b66cc01..83cd21f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -48,7 +48,7 @@ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ - "outDir": "dist" /* Specify an output folder for all emitted files. */, + "outDir": "build" /* Specify an output folder for all emitted files. */, // "removeComments": true, /* Disable emitting comments. */ // "noEmit": true, /* Disable emitting files from a compilation. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ diff --git a/yarn.lock b/yarn.lock index 072e415..33b5c67 100644 --- a/yarn.lock +++ b/yarn.lock @@ -149,6 +149,7 @@ __metadata: better-sqlite3: "npm:^9.5.0" chai: "npm:^4.4.1" commander: "npm:^11.1.0" + esbuild: "npm:^0.21.2" eslint: "npm:^8.57.0" eslint-config-prettier: "npm:^9.1.0" eslint-plugin-import: "npm:^2.29.1" @@ -190,6 +191,167 @@ __metadata: languageName: node linkType: hard +"@esbuild/aix-ppc64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/aix-ppc64@npm:0.21.2" + conditions: os=aix & cpu=ppc64 + languageName: node + linkType: hard + +"@esbuild/android-arm64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/android-arm64@npm:0.21.2" + conditions: os=android & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/android-arm@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/android-arm@npm:0.21.2" + conditions: os=android & cpu=arm + languageName: node + linkType: hard + +"@esbuild/android-x64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/android-x64@npm:0.21.2" + conditions: os=android & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/darwin-arm64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/darwin-arm64@npm:0.21.2" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/darwin-x64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/darwin-x64@npm:0.21.2" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/freebsd-arm64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/freebsd-arm64@npm:0.21.2" + conditions: os=freebsd & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/freebsd-x64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/freebsd-x64@npm:0.21.2" + conditions: os=freebsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/linux-arm64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/linux-arm64@npm:0.21.2" + conditions: os=linux & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/linux-arm@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/linux-arm@npm:0.21.2" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + +"@esbuild/linux-ia32@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/linux-ia32@npm:0.21.2" + conditions: os=linux & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/linux-loong64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/linux-loong64@npm:0.21.2" + conditions: os=linux & cpu=loong64 + languageName: node + linkType: hard + +"@esbuild/linux-mips64el@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/linux-mips64el@npm:0.21.2" + conditions: os=linux & cpu=mips64el + languageName: node + linkType: hard + +"@esbuild/linux-ppc64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/linux-ppc64@npm:0.21.2" + conditions: os=linux & cpu=ppc64 + languageName: node + linkType: hard + +"@esbuild/linux-riscv64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/linux-riscv64@npm:0.21.2" + conditions: os=linux & cpu=riscv64 + languageName: node + linkType: hard + +"@esbuild/linux-s390x@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/linux-s390x@npm:0.21.2" + conditions: os=linux & cpu=s390x + languageName: node + linkType: hard + +"@esbuild/linux-x64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/linux-x64@npm:0.21.2" + conditions: os=linux & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/netbsd-x64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/netbsd-x64@npm:0.21.2" + conditions: os=netbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/openbsd-x64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/openbsd-x64@npm:0.21.2" + conditions: os=openbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/sunos-x64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/sunos-x64@npm:0.21.2" + conditions: os=sunos & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/win32-arm64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/win32-arm64@npm:0.21.2" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/win32-ia32@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/win32-ia32@npm:0.21.2" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/win32-x64@npm:0.21.2": + version: 0.21.2 + resolution: "@esbuild/win32-x64@npm:0.21.2" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + "@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0": version: 4.4.0 resolution: "@eslint-community/eslint-utils@npm:4.4.0" @@ -2239,6 +2401,86 @@ __metadata: languageName: node linkType: hard +"esbuild@npm:^0.21.2": + version: 0.21.2 + resolution: "esbuild@npm:0.21.2" + dependencies: + "@esbuild/aix-ppc64": "npm:0.21.2" + "@esbuild/android-arm": "npm:0.21.2" + "@esbuild/android-arm64": "npm:0.21.2" + "@esbuild/android-x64": "npm:0.21.2" + "@esbuild/darwin-arm64": "npm:0.21.2" + "@esbuild/darwin-x64": "npm:0.21.2" + "@esbuild/freebsd-arm64": "npm:0.21.2" + "@esbuild/freebsd-x64": "npm:0.21.2" + "@esbuild/linux-arm": "npm:0.21.2" + "@esbuild/linux-arm64": "npm:0.21.2" + "@esbuild/linux-ia32": "npm:0.21.2" + "@esbuild/linux-loong64": "npm:0.21.2" + "@esbuild/linux-mips64el": "npm:0.21.2" + "@esbuild/linux-ppc64": "npm:0.21.2" + "@esbuild/linux-riscv64": "npm:0.21.2" + "@esbuild/linux-s390x": "npm:0.21.2" + "@esbuild/linux-x64": "npm:0.21.2" + "@esbuild/netbsd-x64": "npm:0.21.2" + "@esbuild/openbsd-x64": "npm:0.21.2" + "@esbuild/sunos-x64": "npm:0.21.2" + "@esbuild/win32-arm64": "npm:0.21.2" + "@esbuild/win32-ia32": "npm:0.21.2" + "@esbuild/win32-x64": "npm:0.21.2" + dependenciesMeta: + "@esbuild/aix-ppc64": + optional: true + "@esbuild/android-arm": + optional: true + "@esbuild/android-arm64": + optional: true + "@esbuild/android-x64": + optional: true + "@esbuild/darwin-arm64": + optional: true + "@esbuild/darwin-x64": + optional: true + "@esbuild/freebsd-arm64": + optional: true + "@esbuild/freebsd-x64": + optional: true + "@esbuild/linux-arm": + optional: true + "@esbuild/linux-arm64": + optional: true + "@esbuild/linux-ia32": + optional: true + "@esbuild/linux-loong64": + optional: true + "@esbuild/linux-mips64el": + optional: true + "@esbuild/linux-ppc64": + optional: true + "@esbuild/linux-riscv64": + optional: true + "@esbuild/linux-s390x": + optional: true + "@esbuild/linux-x64": + optional: true + "@esbuild/netbsd-x64": + optional: true + "@esbuild/openbsd-x64": + optional: true + "@esbuild/sunos-x64": + optional: true + "@esbuild/win32-arm64": + optional: true + "@esbuild/win32-ia32": + optional: true + "@esbuild/win32-x64": + optional: true + bin: + esbuild: bin/esbuild + checksum: 0f6663800686f06dc55a701e0a97648cc52aacfdb618bb48bcb6d6d1881e96152b9ac2729d8408db6263344dd07732210944527e50d874ee3a2c41bae74f7239 + languageName: node + linkType: hard + "escalade@npm:^3.1.1": version: 3.1.1 resolution: "escalade@npm:3.1.1"