mirror of
https://github.com/sern-handler/cli
synced 2026-06-06 01:16:53 +00:00
start watch server
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import esbuild from 'esbuild';
|
import esbuild from 'esbuild';
|
||||||
import { getConfig } from '../utilities/getConfig';
|
import { getConfig, type sernConfig } from '../utilities/getConfig';
|
||||||
import { resolve } from 'node:path';
|
import { resolve } from 'node:path';
|
||||||
import { glob } from 'glob';
|
import { glob } from 'glob';
|
||||||
import { configDotenv } from 'dotenv';
|
import { configDotenv } from 'dotenv';
|
||||||
@@ -48,51 +48,58 @@ type BuildOptions = {
|
|||||||
env?: string;
|
env?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function build(options: Record<string, any>) {
|
/*
|
||||||
if (!options.supressWarnings) {
|
* locates jsconfig or tsconfig depending on language.
|
||||||
console.info(`${magentaBright('EXPERIMENTAL')}: This API has not been stabilized. add -W or --suppress-warnings flag to suppress`);
|
* If path is not given, default to the current working directory's `(js|ts)config.json`
|
||||||
}
|
*/
|
||||||
const sernConfig = await getConfig();
|
function resolveConfigPath (path: string|undefined, language: string) {
|
||||||
let buildConfig: Partial<BuildOptions> = {};
|
if(language === 'javascript') {
|
||||||
const entryPoints = await glob(`./src/**/*{${validExtensions.join(',')}}`, {
|
return path ?? resolve('jsconfig.json')
|
||||||
//for some reason, my ignore glob wasn't registering correctly'
|
}
|
||||||
ignore: {
|
return path ?? resolve('tsconfig.json')
|
||||||
ignored: (p) => p.name.endsWith('.d.ts'),
|
}
|
||||||
},
|
|
||||||
});
|
async function createBuildConfig(options: Record<string,any>, sernConfig: sernConfig) {
|
||||||
const buildConfigPath = resolve(options.project ?? 'sern.build.js');
|
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')
|
|
||||||
}
|
|
||||||
const defaultBuildConfig = {
|
const defaultBuildConfig = {
|
||||||
defineVersion: true,
|
defineVersion: true,
|
||||||
format: options.format ?? 'esm',
|
format: options.format ?? 'esm',
|
||||||
mode: options.mode ?? 'development',
|
mode: options.mode ?? 'development',
|
||||||
dropLabels: [],
|
dropLabels: [],
|
||||||
tsconfig: resolveBuildConfig(options.tsconfig, sernConfig.language),
|
esbuildPlugins: [],
|
||||||
|
tsconfig: resolveConfigPath(options.tsconfig, sernConfig.language),
|
||||||
env: options.env ?? resolve('.env'),
|
env: options.env ?? resolve('.env'),
|
||||||
};
|
} satisfies BuildOptions;
|
||||||
if (pathExistsSync(buildConfigPath)) {
|
|
||||||
try {
|
if(pathExistsSync(buildConfigPath)) {
|
||||||
buildConfig = {
|
return {
|
||||||
...defaultBuildConfig,
|
...defaultBuildConfig,
|
||||||
...(await import('file:///' + buildConfigPath)).default,
|
...(await import('file:///' + buildConfigPath)).default,
|
||||||
};
|
|
||||||
} catch (e) {
|
|
||||||
console.log(e);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
buildConfig = {
|
return defaultBuildConfig;
|
||||||
...defaultBuildConfig,
|
|
||||||
};
|
|
||||||
console.log('No build config found, defaulting');
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function fillEnv(options: Record<string, any>) {
|
||||||
let env = {} as Record<string, string>;
|
let env = {} as Record<string, string>;
|
||||||
configDotenv({ path: buildConfig.env, processEnv: env });
|
configDotenv({ path: options.env, processEnv: env });
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function build(options: Record<string, any>) {
|
||||||
|
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) {
|
if (env.MODE && !env.NODE_ENV) {
|
||||||
console.warn('Use NODE_ENV instead of MODE');
|
console.warn('Use NODE_ENV instead of MODE');
|
||||||
@@ -105,7 +112,8 @@ export async function build(options: Record<string, any>) {
|
|||||||
console.log(magentaBright('NODE_ENV:'), 'Found NODE_ENV variable, setting `mode` to this.');
|
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 {
|
try {
|
||||||
|
|||||||
5
src/commands/watch.ts
Normal file
5
src/commands/watch.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import esbuild from 'esbuild'
|
||||||
|
|
||||||
|
export async function watch(argv: unknown) {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -60,5 +60,8 @@ program
|
|||||||
.option('-e --env', 'path to .env file')
|
.option('-e --env', 'path to .env file')
|
||||||
.option('--tsconfig [filePath]', 'Use this tsconfig')
|
.option('--tsconfig [filePath]', 'Use this tsconfig')
|
||||||
.action(async (...args) => importDynamic('build.js').then((m) => m.build(...args)));
|
.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();
|
program.parse();
|
||||||
|
|||||||
Reference in New Issue
Block a user