Skip to content

Instantly share code, notes, and snippets.

@bkniffler
Created November 5, 2023 09:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkniffler/9fe91f6a1a8ba05f1eeb993441c7cdb5 to your computer and use it in GitHub Desktop.
Save bkniffler/9fe91f6a1a8ba05f1eeb993441c7cdb5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const { build } = require('esbuild');
// const { fileURLToPath } = require('url');
const { existsSync } = require('fs');
const { resolve } = require('path');
const arg = process.argv;
const pkgJsonPath = resolve(process.cwd(), 'package.json');
const srcDir = resolve(process.cwd(), arg[2] || 'src');
const wfSrcDir = resolve(process.cwd(), arg[3] || arg[2] || 'src');
const outDir = resolve(process.cwd(), 'build');
(async () => {
const paths = {
packageJson: pkgJsonPath,
indexTS: resolve(srcDir, 'index.ts'),
workflowsFolderTS: resolve(wfSrcDir, 'workflows/index.ts'),
};
const workerTS = existsSync(paths.indexTS) ? paths.indexTS : null;
const workflowsTS = existsSync(paths.workflowsFolderTS)
? paths.workflowsFolderTS
: null;
const packages = Object.keys(require(paths.packageJson).dependencies || {});
if (!workerTS) throw new Error(`No worker found in ${srcDir}`);
if (!workflowsTS) throw new Error(`No workflows found in ${srcDir}`);
try {
const t1 = build({
bundle: true,
sourcemap: true,
entryPoints: [workerTS],
outfile: resolve(outDir, 'index.js'),
platform: 'node',
external: packages.concat(['./workflows', '@temporalio/worker', '*.png']),
treeShaking: true,
target: 'node18',
keepNames: true,
format: 'cjs',
});
const t2 = build({
bundle: true,
sourcemap: true,
format: 'esm',
entryPoints: [workflowsTS],
outfile: resolve(outDir, 'workflows.js'),
platform: 'node',
external: packages.concat(['@temporalio/workflow', '*.png']),
treeShaking: true,
target: 'node18',
keepNames: true,
});
await Promise.all([t1, t2]);
console.log('Build success');
} catch (err) {
console.error(err);
process.exitCode = 1;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment