Skip to content

Commit

Permalink
feat: Add CLI options for tsup with yargs
Browse files Browse the repository at this point in the history
This update introduces yargs options to selectively build 'core' or
'scripts' files. This enhancement allows for targeted builds, saving time
by avoiding unnecessary builds.

Tsup's automatic handling of dependencies eliminates the need for direct transpilation of core files for script builds and verbose pathing to core
and config files. This results in standalone output files, free from runtime external file dependencies.

Furthermore, the script files are now output to a 'bin' directory, maintaining a clean 'dist' directory and indicating that the bin files are standalone scripts executable from the CLI and are not part of the main app.
  • Loading branch information
ardelato committed Nov 30, 2023
1 parent 7744a3f commit 68fb3a3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ urls.json
lh-config.js

# TSC Output
dist
dist
bin
75 changes: 66 additions & 9 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,72 @@
import { build } from 'tsup';
import { build as tsupBuild } from 'tsup';
import yargs from 'yargs';

await build({
entry: {
index: 'src/core/index.ts',
createDashboard: 'src/scripts/createDashboard.ts',
updateDatadogMetricMetadata: 'src/scripts/updateDatadogMetricMetadata.ts',
const CORE_DIR = 'dist';
const SCRIPTS_DIR = 'bin';

const argv = yargs(process.argv.slice(2)).options({
target: {
alias: 't',
type: 'string',
description: 'Build target',
choices: ['all', 'core', 'scripts'],
default: 'all'
},
silent: {
alias: 's',
type: 'boolean',
description: 'Silent mode',
default: false
},
clean: {
alias: 'c',
type: 'boolean',
description: 'Clean out files before build',
default: false
},
}).strict().parseSync();

const config = {
splitting: false, // don't split code into chunks
clean: true,
platform: 'node', // environment to build for
target: 'node20',
outDir: 'dist',
format: 'esm',
});
silent: argv.silent,
clean: argv.clean,
}

const CORE_BUILD = {
...config,
outDir: CORE_DIR,
entry: {
index: 'src/core/index.ts',
},
}

const SCRIPTS_BUILD = {
...config,
outDir: SCRIPTS_DIR,
entry: ['src/scripts/*'],
}

async function build(buildConfig, buildTarget) {
try {
await tsupBuild(buildConfig);
} catch (e) {
console.error(`Failed to build ${buildTarget}:`, e);
process.exit(1);
}
}

switch(argv.target) {
case 'all':
await build(CORE_BUILD, 'core');
await build(SCRIPTS_BUILD, 'scripts');
break;
case 'core':
await build(CORE_BUILD, 'core');
break;
case 'scripts':
await build(SCRIPTS_BUILD, 'scripts');
break;
}

0 comments on commit 68fb3a3

Please sign in to comment.