From 5c06df12de67c2a46a1f73201f9c9aa0ec7bb349 Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 7 Nov 2023 11:36:24 -0600 Subject: [PATCH] start watch server --- src/commands/build.ts | 82 ++++++++++++++++++++++++------------------- src/commands/watch.ts | 5 +++ src/index.ts | 5 ++- 3 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 src/commands/watch.ts diff --git a/src/commands/build.ts b/src/commands/build.ts index 1b6e879..b3e5b51 100644 --- a/src/commands/build.ts +++ b/src/commands/build.ts @@ -1,5 +1,5 @@ import esbuild from 'esbuild'; -import { getConfig } from '../utilities/getConfig'; +import { getConfig, type sernConfig } from '../utilities/getConfig'; import { resolve } from 'node:path'; import { glob } from 'glob'; import { configDotenv } from 'dotenv'; @@ -48,51 +48,58 @@ type BuildOptions = { env?: string; }; -export async function build(options: Record) { - if (!options.supressWarnings) { - console.info(`${magentaBright('EXPERIMENTAL')}: This API has not been stabilized. add -W or --suppress-warnings flag to suppress`); - } - const sernConfig = await getConfig(); - let buildConfig: Partial = {}; - const entryPoints = await glob(`./src/**/*{${validExtensions.join(',')}}`, { - //for some reason, my ignore glob wasn't registering correctly' - ignore: { - ignored: (p) => p.name.endsWith('.d.ts'), - }, - }); - const buildConfigPath = resolve(options.project ?? 'sern.build.js'); - const resolveBuildConfig = (path: string|undefined, language: string) => { - if(language === 'javascript') { - return path ?? resolve('jsconfig.json') - } - return path ?? resolve('tsconfig.json') - } +/* + * locates jsconfig or tsconfig depending on language. + * If path is not given, default to the current working directory's `(js|ts)config.json` + */ +function resolveConfigPath (path: string|undefined, language: string) { + if(language === 'javascript') { + return path ?? resolve('jsconfig.json') + } + return path ?? resolve('tsconfig.json') +} + +async function createBuildConfig(options: Record, sernConfig: sernConfig) { + const buildConfigPath = resolve(options.project ?? 'sern.build.js'); const defaultBuildConfig = { defineVersion: true, format: options.format ?? 'esm', mode: options.mode ?? 'development', dropLabels: [], - tsconfig: resolveBuildConfig(options.tsconfig, sernConfig.language), + esbuildPlugins: [], + tsconfig: resolveConfigPath(options.tsconfig, sernConfig.language), env: options.env ?? resolve('.env'), - }; - if (pathExistsSync(buildConfigPath)) { - try { - buildConfig = { - ...defaultBuildConfig, - ...(await import('file:///' + buildConfigPath)).default, - }; - } catch (e) { - console.log(e); - process.exit(1); + } satisfies BuildOptions; + + if(pathExistsSync(buildConfigPath)) { + return { + ...defaultBuildConfig, + ...(await import('file:///' + buildConfigPath)).default, } } else { - buildConfig = { - ...defaultBuildConfig, - }; - console.log('No build config found, defaulting'); + return defaultBuildConfig; } +} + +function fillEnv(options: Record) { let env = {} as Record; - configDotenv({ path: buildConfig.env, processEnv: env }); + configDotenv({ path: options.env, processEnv: env }); + return env; +} + +export async function build(options: Record) { + if (!options.supressWarnings) { + console.info(`${magentaBright('EXPERIMENTAL')}: This API has not been stabilized. add -W or --suppress-warnings flag to suppress`); + } + const sernConfig = await getConfig(); + const entryPoints = await glob(`./src/**/*{${validExtensions.join(',')}}`, { + ignore: { + ignored: (p) => p.name.endsWith('.d.ts'), + }, + }); + + const buildConfig = await createBuildConfig(options, sernConfig); + let env = fillEnv(options); if (env.MODE && !env.NODE_ENV) { console.warn('Use NODE_ENV instead of MODE'); @@ -105,7 +112,8 @@ export async function build(options: Record) { console.log(magentaBright('NODE_ENV:'), 'Found NODE_ENV variable, setting `mode` to this.'); } - assert(buildConfig.mode === 'development' || buildConfig.mode === 'production', 'Mode is not `production` or `development`'); + assert(buildConfig.mode === 'development' + || buildConfig.mode === 'production', 'Mode is not `production` or `development`'); try { diff --git a/src/commands/watch.ts b/src/commands/watch.ts new file mode 100644 index 0000000..f2fdabf --- /dev/null +++ b/src/commands/watch.ts @@ -0,0 +1,5 @@ +import esbuild from 'esbuild' + +export async function watch(argv: unknown) { + +} diff --git a/src/index.ts b/src/index.ts index 0bfab5a..9d9fc13 100644 --- a/src/index.ts +++ b/src/index.ts @@ -60,5 +60,8 @@ program .option('-e --env', 'path to .env file') .option('--tsconfig [filePath]', 'Use this tsconfig') .action(async (...args) => importDynamic('build.js').then((m) => m.build(...args))); - +program + .command('watch') + .description('start a hmr session') + .action(async (...args) => importDynamic('watch.js').then((m) => m.watch(...args))) program.parse();