From 68fb3a3a77445e61ebe35bd45de08ad03e31fd94 Mon Sep 17 00:00:00 2001 From: Angel de la Torre Date: Thu, 30 Nov 2023 13:43:02 -0800 Subject: [PATCH] feat: Add CLI options for tsup with yargs 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. --- .gitignore | 3 ++- build.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index bbcb557..cd9141d 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,5 @@ urls.json lh-config.js # TSC Output -dist \ No newline at end of file +dist +bin diff --git a/build.js b/build.js index 55a2f2a..3efff20 100644 --- a/build.js +++ b/build.js @@ -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', -}); \ No newline at end of file + 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; +}