Skip to content

Commit a324d26

Browse files
committedMar 31, 2023
feat: add --bail command parameter.
1 parent da2c83a commit a324d26

File tree

17 files changed

+66
-19
lines changed

17 files changed

+66
-19
lines changed
 

‎examples/babel-transform-ts/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "Transform Typescript Example.",
66
"scripts": {
77
"watch": "tsbb watch \"src/**/*.ts\" --use-babel --no-esm",
8-
"build": "tsbb build \"src/**/*.ts\" --use-babel --no-esm",
8+
"build": "tsbb build \"src/**/*.ts\" --use-babel --no-esm --bail",
99
"test": "tsbb test",
1010
"coverage": "tsbb test --coverage --bail"
1111
},

‎examples/basic/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"start": "node lib/index.js",
99
"watch": "tsbb watch",
10-
"build": "tsbb build",
10+
"build": "tsbb build --bail",
1111
"test": "tsbb test",
1212
"coverage": "tsbb test --coverage --bail"
1313
},

‎examples/express/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dev:ts": "ts-node-dev --inspect -- src/app.ts",
99
"dev": "npm run build && nodemon --inspect lib/app.js",
1010
"watch": "tsbb watch",
11-
"build": "tsbb build",
11+
"build": "tsbb build --bail",
1212
"test": "tsbb test",
1313
"coverage": "tsbb test --coverage --bail"
1414
},

‎examples/hapi/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"start": "npm run build && node lib/app.js",
99
"dev": "nodemon --inspect lib/app.js",
1010
"watch": "tsbb watch",
11-
"build": "tsbb build",
11+
"build": "tsbb build --bail",
1212
"test": "tsbb test",
1313
"coverage": "tsbb test --coverage --bail"
1414
},

‎examples/koa/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"start": "npm run build && node lib/app.js",
88
"dev": "nodemon --inspect lib/app.js",
99
"watch": "tsbb watch --entry ./src/app.ts --no-esm",
10-
"build": "tsbb build --entry ./src/app.ts --no-esm",
10+
"build": "tsbb build --entry ./src/app.ts --no-esm --bail",
1111
"test": "tsbb test",
1212
"coverage": "tsbb test --coverage --bail"
1313
},

‎examples/react-component-tsx/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"doc": "kkt build --app-src ./website",
1010
"start": "kkt start --app-src ./website",
1111
"watch": "tsbb watch src/*.{tsx,ts} --useBabel & npm run css:watch",
12-
"build": "tsbb build src/*.{tsx,ts} --useBabel && npm run css:build",
12+
"build": "tsbb build src/*.{tsx,ts} --useBabel --bail && npm run css:build",
1313
"css:build": "compile-less -d src -o esm",
1414
"css:watch": "compile-less -d src -o esm --watch",
1515
"css:build:dist": "compile-less -d src --combine lib/dist.css --rm-global",

‎examples/react-component/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "React Component Example.",
66
"scripts": {
77
"watch": "tsbb watch src/*.{jsx,js} --useBabel",
8-
"build": "tsbb build src/*.{jsx,js} --useBabel && npm run css:build && npm run css:build:dist",
8+
"build": "tsbb build src/*.{jsx,js} --useBabel --bail && npm run css:build && npm run css:build:dist",
99
"css:build": "compile-less -d src -o esm",
1010
"css:watch": "compile-less -d src -o esm --watch",
1111
"css:build:dist": "compile-less -d src --combine dist/markdown-editor.css --rm-global",

‎examples/typenexus/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"start": "npm run build && node dist/main.js",
99
"dev": "nodemon --inspect dist/main.js",
1010
"watch": "tsbb watch",
11-
"build": "tsbb build",
11+
"build": "tsbb build --bail",
1212
"test": "tsbb test",
1313
"coverage": "tsbb test --coverage --bail"
1414
},

‎examples/umd/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "UMD Example.",
66
"scripts": {
77
"watch": "tsbb watch",
8-
"build": "tsbb build",
8+
"build": "tsbb build --bail",
99
"test": "tsbb test",
1010
"coverage": "tsbb test --coverage --bail"
1111
},

‎examples/vue/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"license": "ISC",
1010
"scripts": {
1111
"watch": "tsbb watch src/*.tsx --use-vue --use-babel --cjs cjs",
12-
"build": "tsbb build src/*.tsx --use-vue --use-babel --cjs cjs"
12+
"build": "tsbb build src/*.tsx --use-vue --use-babel --cjs cjs --bail"
1313
},
1414
"dependencies": {
1515
"vue": "^3.0.0"

‎packages/babel/src/index.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getCjsTransformOption, getESMTransformOption } from './config.js';
1010
export * from './utils.js';
1111

1212
export interface BabelCompileOptions {
13+
watch?: boolean;
1314
/**
1415
* The specified entry file, for example:
1516
* @example
@@ -64,10 +65,15 @@ export interface BabelCompileOptions {
6465
* @default `false`
6566
*/
6667
sourceMaps?: TransformOptions['sourceMaps'];
68+
/**
69+
* Exit the compile as soon as the compile fails(default: true).
70+
* @default `true`
71+
*/
72+
bail?: boolean;
6773
}
6874

6975
export default async function compile(fileName: string, options: BabelCompileOptions = {}) {
70-
const { cjs = 'lib', esm = 'esm', envName, useVue = false } = options;
76+
const { cjs = 'lib', esm = 'esm', envName, useVue = false, bail, watch } = options;
7177
const dt = getOutputPath(fileName, options);
7278
const log = new Log();
7379
log.name();
@@ -89,7 +95,16 @@ export default async function compile(fileName: string, options: BabelCompileOpt
8995
esmBabelOptions.cwd = dt.projectDirectory;
9096

9197
if (typeof esm === 'string') {
92-
transformFile(fileName, dt.esm.path, dt.folderFilePath, dt.projectDirectory, dt.esm.fileName, esmBabelOptions);
98+
transformFile(
99+
fileName,
100+
dt.esm.path,
101+
dt.folderFilePath,
102+
dt.projectDirectory,
103+
dt.esm.fileName,
104+
esmBabelOptions,
105+
bail,
106+
watch,
107+
);
93108
}
94109

95110
let cjsBabelOptions = getCjsTransformOption();
@@ -106,7 +121,16 @@ export default async function compile(fileName: string, options: BabelCompileOpt
106121
cjsBabelOptions.cwd = dt.projectDirectory;
107122

108123
if (typeof cjs === 'string') {
109-
transformFile(fileName, dt.cjs.path, dt.folderFilePath, dt.projectDirectory, dt.cjs.fileName, cjsBabelOptions);
124+
transformFile(
125+
fileName,
126+
dt.cjs.path,
127+
dt.folderFilePath,
128+
dt.projectDirectory,
129+
dt.cjs.fileName,
130+
cjsBabelOptions,
131+
bail,
132+
watch,
133+
);
110134
}
111135
}
112136

@@ -117,6 +141,8 @@ function transformFile(
117141
projectDirectory: string,
118142
outFileName: string,
119143
options: TransformOptions,
144+
bail?: boolean,
145+
isWatch?: boolean,
120146
) {
121147
const log = new Log();
122148
log.name();
@@ -149,5 +175,8 @@ function transformFile(
149175
} else {
150176
log.icon('\n🚨').error(`\x1b[33;1m ${JSON.stringify(error)}\x1b[0m\n`);
151177
}
178+
if (bail && isWatch !== true) {
179+
process.exitCode = 1;
180+
}
152181
});
153182
}

‎packages/core/src/compile.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { watcherCopyFiles } from './watcher/copyFiles.js';
55

66
export interface CompileOptions extends BabelCompileOptions {
77
watch?: boolean;
8+
bail?: boolean;
89
build?: boolean;
910
entry?: string[];
1011
[key: string]: any;
@@ -13,6 +14,7 @@ export interface CompileOptions extends BabelCompileOptions {
1314
export async function compile(options: CompileOptions = {}) {
1415
if (!options.useBabel) {
1516
return tsCompile({
17+
bail: options.bail,
1618
watch: options.watch,
1719
onCopyFiles: watcherCopyFiles,
1820
});

‎packages/core/src/helpStr.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ Usage:\x1b[34;1m tsbb\x1b[0m <command>
99
1010
Options:[build|watch]
1111
12-
\x1b[35;1m--use-babel\x1b[0m Use Babel.(works in babel)
13-
\x1b[35;1m--source-maps\x1b[0m Enables the generation of sourcemap files.(works in babel)
14-
\x1b[35;1m--env-name\x1b[0m The current active environment used during configuration loading.(works in babel)
15-
\x1b[35;1m--esm\x1b[0m Output "esm" directory.(works in babel)
16-
\x1b[35;1m--cjs\x1b[0m Output "cjs" directory.(works in babel)
12+
\x1b[35;1m--bail\x1b[0m Exit the compile as soon as the compile fails(default: true).
13+
\x1b[35;1m--use-babel\x1b[0m Use Babel.(works in babel)
14+
\x1b[35;1m--source-maps\x1b[0m Enables the generation of sourcemap files.(works in babel)
15+
\x1b[35;1m--env-name\x1b[0m The current active environment used during configuration loading.(works in babel)
16+
\x1b[35;1m--esm\x1b[0m Output "esm" directory.(works in babel)
17+
\x1b[35;1m--cjs\x1b[0m Output "cjs" directory.(works in babel)
1718
1819
Options:
1920

‎packages/core/src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export async function tsbb() {
2020
sourceMaps: {
2121
default: false,
2222
},
23+
bail: {
24+
type: 'boolean',
25+
default: true,
26+
},
2327
},
2428
});
2529
const flags: CamelCase<TypedFlags<CompileOptions>> & Record<string, unknown> = cli.flags;

‎packages/core/src/watcher/babelTransform.ts

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function babelTransform(options: CompileOptions = {}) {
7171
const tsConfigPath = findConfigFile();
7272
if (tsConfigPath) {
7373
tsCompile({
74+
bail: options.bail,
7475
emitDeclarationOnly: true,
7576
watch: options.watch,
7677
isCopyFiles: false,

‎packages/tsbb/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ Commands:
180180

181181
Options:[build|watch]
182182

183+
--bail Exit the compile as soon as the compile fails(default: true).
183184
--use-babel Use Babel.(works in babel)
184185
--source-maps Enables the generation of sourcemap files.(works in babel)
185186
--env-name The current active environment used during configuration loading.(works in babel)

‎packages/typescript/src/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export interface CopyFilesOptions {
3939

4040
export interface TsCompileOptions {
4141
watch?: boolean;
42+
/**
43+
* Exit the compile as soon as the compile fails(default: true).
44+
* @default `true`
45+
*/
46+
bail?: boolean;
4247
emitDeclarationOnly?: boolean;
4348
/**
4449
* @default true
@@ -52,7 +57,7 @@ export interface TsCompileOptions {
5257
export const findConfigFile = () => ts.findConfigFile('.', ts.sys.fileExists, 'tsconfig.json');
5358

5459
export default async function compile(options: TsCompileOptions = {}) {
55-
const { isCopyFiles = true, onWriteFile, onCopyFiles } = options;
60+
const { isCopyFiles = true, onWriteFile, onCopyFiles, bail } = options;
5661
const tsConfigPath = findConfigFile();
5762
const log = new Log();
5863
if (!tsConfigPath) {
@@ -147,6 +152,10 @@ export default async function compile(options: TsCompileOptions = {}) {
147152
const emitResult = program.emit();
148153
const diagnostics = ts.getPreEmitDiagnostics(program);
149154
diagnostics.forEach(reportDiagnostic);
155+
if (bail && diagnostics.length) {
156+
diagnostics.forEach(reportDiagnostic);
157+
process.exitCode = 1;
158+
}
150159
if (isCopyFiles && onCopyFiles) {
151160
await onCopyFiles(rootDirs, { isWatch: options.watch, outputDir, currentDir, rootDirsRelative });
152161
}

0 commit comments

Comments
 (0)
Please sign in to comment.