mirror of
https://github.com/sern-handler/cli
synced 2026-06-06 01:16:53 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e66d6cfad | ||
|
|
58677cc15d | ||
| 1695956350 | |||
|
|
6798a762c7 | ||
|
|
cfc998b176 | ||
|
|
fca3c76016 | ||
|
|
7f4203370c | ||
|
|
23d70c6e03 | ||
|
|
90fae47ff4 |
2
.github/workflows/continuous-integration.yml
vendored
2
.github/workflows/continuous-integration.yml
vendored
@@ -42,7 +42,7 @@ jobs:
|
|||||||
- name: Set up Node.js
|
- name: Set up Node.js
|
||||||
uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3
|
uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3
|
||||||
with:
|
with:
|
||||||
node-version: 17
|
node-version: 20
|
||||||
|
|
||||||
- name: Install Node.js dependencies
|
- name: Install Node.js dependencies
|
||||||
run: npm i && npm run build
|
run: npm i && npm run build
|
||||||
|
|||||||
21
CHANGELOG.md
21
CHANGELOG.md
@@ -2,6 +2,27 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [1.4.0](https://github.com/sern-handler/cli/compare/v1.3.5...v1.4.0) (2025-02-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* watch command execute on successful build ([#151](https://github.com/sern-handler/cli/issues/151)) ([1695956](https://github.com/sern-handler/cli/commit/1695956350a55bb4a28839b427d1bf46cf518b01))
|
||||||
|
|
||||||
|
## [1.3.5](https://github.com/sern-handler/cli/compare/v1.3.4...v1.3.5) (2025-01-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* Update Dockerfile.TS.sern ([#149](https://github.com/sern-handler/cli/issues/149)) ([fca3c76](https://github.com/sern-handler/cli/commit/fca3c7601604215af9b6d66a137370064ae656e1))
|
||||||
|
|
||||||
|
## [1.3.4](https://github.com/sern-handler/cli/compare/v1.3.3...v1.3.4) (2025-01-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* tsconfig generator ([#145](https://github.com/sern-handler/cli/issues/145)) ([23d70c6](https://github.com/sern-handler/cli/commit/23d70c6e03860d1a69e3bffedd6910aba82071ab))
|
||||||
|
|
||||||
## [1.3.3](https://github.com/sern-handler/cli/compare/v1.3.2...v1.3.3) (2024-08-07)
|
## [1.3.3](https://github.com/sern-handler/cli/compare/v1.3.2...v1.3.3) (2024-08-07)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@sern/cli",
|
"name": "@sern/cli",
|
||||||
"version": "1.3.3",
|
"version": "1.4.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@sern/cli",
|
"name": "@sern/cli",
|
||||||
"version": "1.3.3",
|
"version": "1.4.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@esbuild-kit/cjs-loader": "^2.4.2",
|
"@esbuild-kit/cjs-loader": "^2.4.2",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@sern/cli",
|
"name": "@sern/cli",
|
||||||
"version": "1.3.3",
|
"version": "1.4.1",
|
||||||
"description": "Official CLI for @sern/handler",
|
"description": "Official CLI for @sern/handler",
|
||||||
"exports": "./dist/index.js",
|
"exports": "./dist/index.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
|||||||
@@ -7,10 +7,12 @@ import assert from 'node:assert';
|
|||||||
import defaultEsbuild from '../utilities/defaultEsbuildConfig';
|
import defaultEsbuild from '../utilities/defaultEsbuildConfig';
|
||||||
import { require } from '../utilities/require';
|
import { require } from '../utilities/require';
|
||||||
import { pathExists, pathExistsSync } from 'find-up';
|
import { pathExists, pathExistsSync } from 'find-up';
|
||||||
import { mkdir, writeFile, readFile } from 'fs/promises';
|
import { mkdir, writeFile } from 'fs/promises';
|
||||||
import * as Preprocessor from '../utilities/preprocessor';
|
import * as Preprocessor from '../utilities/preprocessor';
|
||||||
import { bold, magentaBright } from 'colorette';
|
import { bold, magentaBright } from 'colorette';
|
||||||
import { parseTsConfig } from '../utilities/parseTsconfig';
|
import { parseTsConfig } from '../utilities/parseTsconfig';
|
||||||
|
import { execa, type ExecaChildProcess } from 'execa';
|
||||||
|
import { InvalidArgumentError } from 'commander';
|
||||||
|
|
||||||
const VALID_EXTENSIONS = ['.ts', '.js' ];
|
const VALID_EXTENSIONS = ['.ts', '.js' ];
|
||||||
|
|
||||||
@@ -49,39 +51,107 @@ type BuildOptions = {
|
|||||||
*/
|
*/
|
||||||
sourcemap?: boolean;
|
sourcemap?: boolean;
|
||||||
|
|
||||||
|
watch?: {
|
||||||
|
/**
|
||||||
|
* command to run.
|
||||||
|
* defaults to your package
|
||||||
|
* manager's start command.
|
||||||
|
*/
|
||||||
|
command?: string;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const CommandHandlerPlugin = (buildConfig: Partial<BuildOptions>, ambientFilePath: string, sernTsConfigPath: string) => {
|
const CommandHandlerPlugin = (buildConfig: Partial<BuildOptions>, ambientFilePath: string, sernTsConfigPath: string) => {
|
||||||
return {
|
return {
|
||||||
name: "commandhandler",
|
name: "commandHandler",
|
||||||
setup(build) {
|
setup(build) {
|
||||||
|
|
||||||
const options = build.initialOptions
|
const options = build.initialOptions
|
||||||
const defVersion = () => JSON.stringify(require(p.resolve('package.json')).version);
|
const defVersion = () => JSON.stringify(require(p.resolve('package.json')).version);
|
||||||
|
// should fix a type error
|
||||||
options.define = {
|
options.define = {
|
||||||
...buildConfig.define ?? {},
|
...(buildConfig.define ?? {}),
|
||||||
__DEV__: `${buildConfig.mode === 'development'}`,
|
__DEV__: `${buildConfig.mode === 'development'}`,
|
||||||
__PROD__: `${buildConfig.mode === 'production'}`,
|
__PROD__: `${buildConfig.mode === 'production'}`,
|
||||||
__VERSION__: `${buildConfig.defineVersion ? `${defVersion()}` : 'undefined'}`
|
__VERSION__: `${buildConfig.defineVersion ? `${defVersion()}` : 'undefined'}`
|
||||||
} ?? {}
|
}
|
||||||
Preprocessor.writeTsConfig(buildConfig.format!, sernTsConfigPath, writeFile);
|
Preprocessor.writeTsConfig(buildConfig.format!, sernTsConfigPath, writeFile);
|
||||||
Preprocessor.writeAmbientFile(ambientFilePath, options.define!, writeFile);
|
Preprocessor.writeAmbientFile(ambientFilePath, options.define!, writeFile);
|
||||||
|
|
||||||
}
|
}
|
||||||
} as esbuild.Plugin
|
} as esbuild.Plugin
|
||||||
}
|
}
|
||||||
const resolveBuildConfig = (path: string|undefined, language: string) => {
|
const CommandOnEndPlugin = (watching: boolean, watchCommand?: string) => {
|
||||||
if(language === 'javascript') {
|
// for some reason it runs the command twice on first build
|
||||||
|
let isFirstBuild = true;
|
||||||
|
let currentProcess: ExecaChildProcess | null = null;
|
||||||
|
let restartTimeout: NodeJS.Timeout | null = null;
|
||||||
|
return {
|
||||||
|
name: 'watchRunCommand',
|
||||||
|
setup(build: esbuild.PluginBuild) {
|
||||||
|
build.onEnd(async (result) => {
|
||||||
|
if (!watching || result.errors.length !== 0) return;
|
||||||
|
if (isFirstBuild === true) {
|
||||||
|
isFirstBuild = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (watchCommand === '') {
|
||||||
|
console.log('[watch] no command provided, skipping');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentProcess) {
|
||||||
|
console.log('[watch] stopping previous process...');
|
||||||
|
currentProcess.cancel()
|
||||||
|
currentProcess = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cmd = watchCommand || (() => {
|
||||||
|
if (pathExistsSync('package-lock.json')) return 'npm start';
|
||||||
|
if (pathExistsSync('yarn.lock')) return 'yarn start';
|
||||||
|
if (pathExistsSync('pnpm-lock.yaml')) return 'pnpm start';
|
||||||
|
if (pathExistsSync('bun.lockb')) return 'bun start';
|
||||||
|
if (pathExistsSync('bun.lock')) return 'bun start';
|
||||||
|
|
||||||
|
throw new Error('[watch] default package manager start command not found');
|
||||||
|
})();
|
||||||
|
|
||||||
|
// Clear any pending restart
|
||||||
|
if (restartTimeout) clearTimeout(restartTimeout);
|
||||||
|
|
||||||
|
console.log('[watch] debouncing command for 1.5 seconds...');
|
||||||
|
|
||||||
|
// Set new debounced timeout
|
||||||
|
restartTimeout = setTimeout(() => {
|
||||||
|
console.log(`[watch] running command: ${cmd}`);
|
||||||
|
currentProcess = execa(cmd, { stdio: 'inherit', shell: true });
|
||||||
|
currentProcess.catch(error => {
|
||||||
|
if (error.isCanceled) return;
|
||||||
|
console.error(`[watch] command execution error: ${error.message}`);
|
||||||
|
});
|
||||||
|
}, 1500);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} as esbuild.Plugin;
|
||||||
|
};
|
||||||
|
const resolveBuildConfig = (path: string | undefined, language: string) => {
|
||||||
|
if (language === 'javascript') {
|
||||||
return path ?? 'jsconfig.json'
|
return path ?? 'jsconfig.json'
|
||||||
}
|
}
|
||||||
return path ?? 'tsconfig.json'
|
return path ?? 'tsconfig.json'
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function build(options: Record<string, any>) {
|
export async function build(options: Record<string, any>) {
|
||||||
|
//console.log(options)
|
||||||
if (!options.supressWarnings) {
|
if (!options.supressWarnings) {
|
||||||
console.info(`${magentaBright('EXPERIMENTAL')}: This API has not been stabilized. add -W or --suppress-warnings flag to suppress`);
|
console.info(`${magentaBright('EXPERIMENTAL')}: This API has not been stabilized. add -W or --suppress-warnings flag to suppress`);
|
||||||
}
|
}
|
||||||
console.log(options)
|
// if watch was not enabled and watchCommand is present
|
||||||
|
if(!options.watch && options.watchCommand) {
|
||||||
|
throw new InvalidArgumentError("enable watch to use --watch-command")
|
||||||
|
}
|
||||||
|
|
||||||
const sernConfig = await getConfig();
|
const sernConfig = await getConfig();
|
||||||
let buildConfig: BuildOptions;
|
let buildConfig: BuildOptions;
|
||||||
const buildConfigPath = p.resolve(options.project ?? 'sern.build.js');
|
const buildConfigPath = p.resolve(options.project ?? 'sern.build.js');
|
||||||
@@ -94,22 +164,34 @@ export async function build(options: Record<string, any>) {
|
|||||||
sourcemap: options.sourceMaps,
|
sourcemap: options.sourceMaps,
|
||||||
tsconfig: resolveBuildConfig(options.tsconfig, sernConfig.language),
|
tsconfig: resolveBuildConfig(options.tsconfig, sernConfig.language),
|
||||||
env: options.env ?? '.env',
|
env: options.env ?? '.env',
|
||||||
include: []
|
include: [],
|
||||||
|
watch: {
|
||||||
|
command: options.watchCommand
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// merging configuration with sern.build.js, if exists buildConfigPath
|
||||||
if (pathExistsSync(buildConfigPath)) {
|
if (pathExistsSync(buildConfigPath)) {
|
||||||
//throwable, buildConfigPath may not exist
|
let fileConfig;
|
||||||
buildConfig = { ...defaultBuildConfig, ...(await import('file:///' + buildConfigPath)).default };
|
try { fileConfig=await import('file:///' + buildConfigPath).then(r=>r.default) }
|
||||||
|
catch(e) {
|
||||||
|
console.error("Could not find buildConfigPath")
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
//throwable, buildConfigPath may not exist, todo, merge
|
||||||
|
buildConfig = { ...defaultBuildConfig, ...fileConfig };
|
||||||
} else {
|
} else {
|
||||||
buildConfig = defaultBuildConfig;
|
buildConfig = defaultBuildConfig;
|
||||||
console.log('No build config found, defaulting');
|
console.log('No build config found, defaulting');
|
||||||
}
|
}
|
||||||
|
|
||||||
configDotenv({ path: buildConfig.env });
|
configDotenv({ path: buildConfig.env });
|
||||||
|
|
||||||
if (process.env.NODE_ENV) {
|
if (process.env.NODE_ENV) {
|
||||||
buildConfig.mode = process.env.NODE_ENV as 'production' | 'development';
|
buildConfig.mode = process.env.NODE_ENV as 'production' | 'development';
|
||||||
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', 'NODE_ENV is not `production` or `development`');
|
||||||
try {
|
try {
|
||||||
let config = await parseTsConfig(buildConfig.tsconfig!);
|
let config = await parseTsConfig(buildConfig.tsconfig!);
|
||||||
config?.extends && console.warn("Extend the generated tsconfig")
|
config?.extends && console.warn("Extend the generated tsconfig")
|
||||||
@@ -130,7 +212,7 @@ export async function build(options: Record<string, any>) {
|
|||||||
console.log(' ', magentaBright('sourceMaps'), buildConfig.sourcemap);
|
console.log(' ', magentaBright('sourceMaps'), buildConfig.sourcemap);
|
||||||
|
|
||||||
const sernDir = p.resolve('.sern'),
|
const sernDir = p.resolve('.sern'),
|
||||||
[ambientFilePath, sernTsConfigPath, genDir] =
|
[ambientFilePath, sernTsConfigPath, genDir] = // resolves the file paths in the .sern dir
|
||||||
['ambient.d.ts', 'tsconfig.json', 'generated'].map(f => p.resolve(sernDir, f));
|
['ambient.d.ts', 'tsconfig.json', 'generated'].map(f => p.resolve(sernDir, f));
|
||||||
|
|
||||||
if (!(await pathExists(genDir))) {
|
if (!(await pathExists(genDir))) {
|
||||||
@@ -146,14 +228,17 @@ export async function build(options: Record<string, any>) {
|
|||||||
//https://esbuild.github.io/content-types/#tsconfig-json
|
//https://esbuild.github.io/content-types/#tsconfig-json
|
||||||
const ctx = await esbuild.context({
|
const ctx = await esbuild.context({
|
||||||
entryPoints,
|
entryPoints,
|
||||||
plugins: [CommandHandlerPlugin(buildConfig, ambientFilePath, sernTsConfigPath)],
|
plugins: [
|
||||||
|
CommandHandlerPlugin(buildConfig, ambientFilePath, sernTsConfigPath),
|
||||||
|
CommandOnEndPlugin(options.watch, buildConfig.watch?.command)
|
||||||
|
],
|
||||||
sourcemap: buildConfig.sourcemap,
|
sourcemap: buildConfig.sourcemap,
|
||||||
...defaultEsbuild(buildConfig.format!, buildConfig.tsconfig),
|
...defaultEsbuild(buildConfig.format!, buildConfig.tsconfig),
|
||||||
dropLabels: [buildConfig.mode === 'production' ? '__DEV__' : '__PROD__', ...buildConfig.dropLabels!],
|
dropLabels: [buildConfig.mode === 'production' ? '__DEV__' : '__PROD__', ...buildConfig.dropLabels!],
|
||||||
});
|
});
|
||||||
|
|
||||||
await ctx.rebuild()
|
await ctx.rebuild()
|
||||||
if(options.watch) {
|
if (options.watch) {
|
||||||
await ctx.watch()
|
await ctx.watch()
|
||||||
} else {
|
} else {
|
||||||
await ctx.dispose()
|
await ctx.dispose()
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ program
|
|||||||
.option('-f --format [fmt]', 'The module system of your application. `cjs` or `esm`', 'esm')
|
.option('-f --format [fmt]', 'The module system of your application. `cjs` or `esm`', 'esm')
|
||||||
.option('-m --mode [mode]', 'the mode for sern to build in. `production` or `development`', 'development')
|
.option('-m --mode [mode]', 'the mode for sern to build in. `production` or `development`', 'development')
|
||||||
.option('-w --watch')
|
.option('-w --watch')
|
||||||
|
.option('--watch-command [cmd]', 'the command for sern to watch. if watch is not enabled, an error is thrown', '')
|
||||||
.option('-W --suppress-warnings', 'suppress experimental warning')
|
.option('-W --suppress-warnings', 'suppress experimental warning')
|
||||||
.option('-p --project [filePath]', 'build with the provided sern.build file')
|
.option('-p --project [filePath]', 'build with the provided sern.build file')
|
||||||
.option('-e --env', 'path to .env file')
|
.option('-e --env', 'path to .env file')
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export const npmInit: PromptObject = {
|
|||||||
export const gitInit: PromptObject = {
|
export const gitInit: PromptObject = {
|
||||||
name: 'gitinit',
|
name: 'gitinit',
|
||||||
type: 'confirm',
|
type: 'confirm',
|
||||||
message: `Do you want to ${blueBright('me')} to initialize git?`,
|
message: `Do you want ${blueBright('me')} to initialize git?`,
|
||||||
initial: true,
|
initial: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ const writeTsConfig = async (format: 'cjs' | 'esm', configPath: string, fw: File
|
|||||||
compilerOptions: {
|
compilerOptions: {
|
||||||
//module determines top level await. CJS doesn't have that abliity afaik
|
//module determines top level await. CJS doesn't have that abliity afaik
|
||||||
module: format === 'cjs' ? 'node' : 'esnext',
|
module: format === 'cjs' ? 'node' : 'esnext',
|
||||||
moduleResolution: 'node16',
|
moduleResolution: 'node',
|
||||||
strict: true,
|
strict: true,
|
||||||
skipLibCheck: true,
|
skipLibCheck: true,
|
||||||
target: 'esnext',
|
target: 'esnext',
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
FROM node:latest
|
FROM node:latest
|
||||||
|
|
||||||
|
RUN npm install -g @sern/cli
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY package.json ./
|
COPY package.json ./
|
||||||
@@ -8,4 +10,6 @@ RUN npm install
|
|||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
CMD node src/index.js
|
RUN sern build
|
||||||
|
|
||||||
|
CMD node .
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
FROM node:latest
|
FROM node:latest
|
||||||
|
|
||||||
|
RUN npm install -g @sern/cli typescript@latest
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY package.json ./
|
COPY package.json ./
|
||||||
|
|
||||||
RUN npm install
|
RUN npm install
|
||||||
|
|
||||||
RUN npm install -D typescript
|
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
RUN tsc --build || npx -p typescript tsc --build
|
RUN sern build
|
||||||
|
|
||||||
CMD node dist/index.js
|
CMD node dist/index.js
|
||||||
|
|||||||
Reference in New Issue
Block a user