Skip to content

Commit 44a083a

Browse files
committedSep 13, 2021
feat(tsbb): Add --file-names options.
1 parent cbf39ae commit 44a083a

File tree

10 files changed

+92
-47
lines changed

10 files changed

+92
-47
lines changed
 

‎example/basic/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"description": "Basic Example.",
66
"scripts": {
77
"start": "node lib/index.js",
8-
"watch": "tsbb watch --no-esm --disable-babel",
9-
"build": "tsbb build --no-esm --disable-babel",
8+
"watch": "tsbb watch --no-esm --disable-babel --file-names src/index.ts --file-names src/main.ts",
9+
"build": "tsbb build --no-esm --disable-babel --file-names src/index.ts --file-names src/main.ts",
1010
"test": "npm run build && tsbb test",
1111
"coverage": "npm run build && tsbb test --coverage"
1212
},

‎example/basic/src/main.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function abs(a: number, b: number) {
2+
return a + b;
3+
}

‎example/basic/test/index.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import fs from 'fs';
22
import path from 'path';
33
import { sum } from '../src/utils/sum';
4+
import { abs } from '../src/main';
45

56
const dirSrc = path.resolve('lib');
67

78
it('sum test case', async () => {
89
expect(sum(1, 1)).toEqual(4);
10+
expect(abs(1, 1)).toEqual(2);
911
});
1012

1113
it('output files test case.', async () => {
1214
const dirs = await fs.promises.readdir(dirSrc);
13-
expect(dirs).toEqual(expect.arrayContaining(["index.d.ts", "index.js", "index.js.map", "utils"]));
15+
expect(dirs).toEqual(expect.arrayContaining(["index.d.ts", "index.js", "index.js.map", "utils", "main.js", "main.d.ts", "main.js.map"]));
1416
});

‎packages/tsbb/README.md

+17-14
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,24 @@ Version 3.0.0-rc.14
9797

9898
Commands:
9999

100-
tsbb build [options] Build your project once and exit.
101-
tsbb watch [options] Recompile files on changes.
102-
tsbb test [options] Run jest test runner in watch mode.
100+
tsbb build [options] Build your project once and exit.
101+
tsbb watch [options] Recompile files on changes.
102+
tsbb test [options] Run jest test runner in watch mode.
103103

104104
Options:[build|watch]
105105

106-
--entry, -e Specify the entry directory.
107-
--envName The current active environment used during configuration loading.
108-
--disable-babel Disable Babel.
109-
--disable-babel-option Disable Babel Option.
110-
--esm Output "esm" directory.
111-
--cjs Output "cjs" directory.
106+
--entry, -e Specify the entry directory.
107+
--env-name The current active environment used during configuration loading.
108+
--disable-babel Disable Babel.
109+
--disable-babel-option Disable Babel Option.
110+
--file-names, -f A set of root files.
111+
--esm Output "esm" directory.
112+
--cjs Output "cjs" directory.
113+
114+
Options:
115+
116+
--version, -v Show version number
117+
--help, -h Show help
112118

113119
Examples:
114120

@@ -118,14 +124,11 @@ Examples:
118124
$ tsbb watch --disable-babel-option Disable Babel Option.
119125
$ tsbb watch --disable-babel Disable Babel.
120126
$ tsbb watch --cjs ./cjs Watch Output directory.
127+
$ tsbb build --disable-babel --file-names src/index.ts --file-names src/main.ts
128+
A set of root files.
121129
$ tsbb test Run test suites related
122130
$ tsbb test --coverage Test coverage information should be collected
123131

124-
Options:
125-
126-
--version, -v Show version number
127-
--help, -h Show help
128-
129132
Copyright 2021
130133
```
131134

‎packages/tsbb/src/build.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ export interface BuildOptions extends Arguments {
2828
* Output ESM directory.
2929
* @example `--no-esm`
3030
*/
31-
esm?: string;
31+
esm?: boolean | string;
3232
}
3333

3434
export async function build(options: BuildOptions, compilerOptions?: ts.CompilerOptions) {
3535
try {
36-
await compile([options.entry], compilerOptions, options);
36+
await compile(options.fileNames, compilerOptions, options);
3737
} catch (error) {
3838
console.error('ERROR', error);
3939
process.exit(1);

‎packages/tsbb/src/cli.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { help } from './help';
1010
import { jest } from './jest';
1111

1212
interface ArgvArguments extends Arguments {
13+
disableBabel?: boolean;
14+
esm?: boolean | string;
15+
cjs?: string;
1316
entry?: string;
17+
fileNames?: string | string[];
1418
}
1519

1620
const argv: ArgvArguments = parser(process.argv.slice(2), {
@@ -24,7 +28,6 @@ const argv: ArgvArguments = parser(process.argv.slice(2), {
2428

2529
(() => {
2630
const version = require('../package.json').version;
27-
2831
if (argv.v) {
2932
console.log();
3033
console.log(` Version \x1b[32;1m ${version}\x1b[0m`);
@@ -40,6 +43,26 @@ const argv: ArgvArguments = parser(process.argv.slice(2), {
4043
argv.entry = argv.entry.replace(/\.tsx$/, '.ts');
4144
}
4245

46+
if (typeof argv.fileNames === 'string') {
47+
argv.fileNames = [argv.fileNames];
48+
}
49+
50+
if (argv.fileNames && Array.isArray(argv.fileNames)) {
51+
argv.fileNames = argv.fileNames.map((filename: string) => path.resolve(process.cwd(), filename));
52+
argv.fileNames = [argv.entry, ...argv.fileNames];
53+
argv.fileNames = Array.from(new Set(argv.fileNames));
54+
}
55+
56+
if (!argv.fileNames) {
57+
argv.fileNames = [argv.entry];
58+
}
59+
60+
argv.fileNames = argv.fileNames.map((item) => item.replace(/\.tsx$/, '.ts'));
61+
62+
if (argv.disableBabel) {
63+
argv.esm = false;
64+
}
65+
4366
const configPath = ts.findConfigFile(path.dirname(argv.entry), ts.sys.fileExists);
4467
let tsConf = { compilerOptions: {} as ts.CompilerOptions };
4568

‎packages/tsbb/src/help.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@ export function help() {
66
console.log();
77
console.log(' Commands:');
88
console.log();
9-
console.log(' \x1b[35;1m tsbb\x1b[0m build [options] Build your project once and exit.');
10-
console.log(' \x1b[35;1m tsbb\x1b[0m watch [options] Recompile files on changes.');
11-
console.log(' \x1b[35;1m tsbb\x1b[0m test [options] Run jest test runner in watch mode.');
9+
console.log(' \x1b[35;1m tsbb\x1b[0m build [options] Build your project once and exit.');
10+
console.log(' \x1b[35;1m tsbb\x1b[0m watch [options] Recompile files on changes.');
11+
console.log(' \x1b[35;1m tsbb\x1b[0m test [options] Run jest test runner in watch mode.');
1212
console.log();
1313
console.log(` Options:[build|watch]`);
1414
console.log();
15-
console.log(` \x1b[35;1m--entry, -e\x1b[0m Specify the entry directory.`);
16-
console.log(` \x1b[35;1m--env-name\x1b[0m The current active environment used during configuration loading.`);
17-
console.log(` \x1b[35;1m--disable-babel\x1b[0m Disable Babel.`);
18-
console.log(` \x1b[35;1m--disable-babel-option\x1b[0m Disable Babel Option.`);
19-
console.log(` \x1b[35;1m--esm\x1b[0m Output "esm" directory.`);
20-
console.log(` \x1b[35;1m--cjs\x1b[0m Output "cjs" directory.`);
15+
console.log(` \x1b[35;1m--entry, -e\x1b[0m Specify the entry directory.`);
16+
console.log(
17+
` \x1b[35;1m--env-name\x1b[0m The current active environment used during configuration loading.`,
18+
);
19+
console.log(` \x1b[35;1m--disable-babel\x1b[0m Disable Babel.`);
20+
console.log(` \x1b[35;1m--disable-babel-option\x1b[0m Disable Babel Option.`);
21+
console.log(` \x1b[35;1m--file-names, -f\x1b[0m A set of root files.`);
22+
console.log(` \x1b[35;1m--esm\x1b[0m Output "esm" directory.`);
23+
console.log(` \x1b[35;1m--cjs\x1b[0m Output "cjs" directory.`);
2124
console.log();
2225
console.log(` Options:`);
2326
console.log();
24-
console.log(' --version, -v Show version number');
25-
console.log(' --help, -h Show help');
27+
console.log(' \x1b[35;1m--version, -v\x1b[0m Show version number');
28+
console.log(' \x1b[35;1m--help, -h\x1b[0m Show help');
2629
console.log();
2730
console.log(` Examples:`);
2831
console.log();
@@ -32,8 +35,12 @@ export function help() {
3235
console.log(` $\x1b[35;1m tsbb\x1b[0m watch --disable-babel-option Disable Babel Option.`);
3336
console.log(` $\x1b[35;1m tsbb\x1b[0m watch --disable-babel Disable Babel.`);
3437
console.log(` $\x1b[35;1m tsbb\x1b[0m watch --cjs ./cjs Watch Output directory.`);
38+
console.log(` $\x1b[35;1m tsbb\x1b[0m build --disable-babel --file-names src/index.ts --file-names src/main.ts`);
39+
console.log(` A set of root files.`);
3540
console.log(` $\x1b[35;1m tsbb\x1b[0m test Run test suites related`);
36-
console.log(` $\x1b[35;1m tsbb\x1b[0m test --coverage Test coverage information should be collected`);
41+
console.log(
42+
` $\x1b[35;1m tsbb\x1b[0m test --coverage Test coverage information should be collected`,
43+
);
3744
console.log();
3845
console.log(' Copyright 2021');
3946
console.log();

‎packages/tsbb/src/utils/compile.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function compile(
2222
if (tsOptions.outDir || cjs) {
2323
await FS.remove(outDir);
2424
}
25-
if (esm) {
25+
if (typeof esm === 'string') {
2626
await FS.remove(path.resolve(process.cwd(), esm));
2727
}
2828
const dirToFiles = await recursiveReaddirFiles(path.dirname(entry), {
@@ -42,7 +42,7 @@ export async function compile(
4242
copyFiles(item.path, cjsPath);
4343
}
4444
}
45-
if (esm) {
45+
if (typeof esm === 'string') {
4646
const esmPath = item.path.replace(entryDir, esm);
4747
if (
4848
!disableBabel &&
@@ -59,7 +59,11 @@ export async function compile(
5959

6060
// Create a Program with an in-memory emit
6161
const createdFiles: Record<string, string> = {};
62-
tsOptions = { ...tsOptions, outDir: cjs || esm, target: tsOptions.target || ts.ScriptTarget.ESNext };
62+
const outDirPath = cjs || esm;
63+
tsOptions = { ...tsOptions, target: tsOptions.target || ts.ScriptTarget.ESNext };
64+
if (typeof outDirPath === 'string') {
65+
tsOptions.outDir = outDirPath;
66+
}
6367
if (tsOptions.noEmit) {
6468
resolve();
6569
return;
@@ -90,21 +94,21 @@ export async function compile(
9094
await Promise.all(
9195
Object.keys(createdFiles).map(async (filepath) => {
9296
try {
93-
if (disableBabel) {
97+
if (disableBabel && !/\.d\.ts$/.test(filepath)) {
9498
ts.sys.writeFile(filepath, createdFiles[filepath]);
9599
outputLog(filepath);
96100
}
97101
if (/\.d\.ts$/.test(filepath)) {
98102
if (new RegExp(`${esm}`).test(filepath)) {
99103
outputFiles(filepath, createdFiles[filepath]);
100-
if (cjs) {
104+
if (cjs && typeof esm === 'string') {
101105
const fileCjs = filepath.replace(esm, cjs);
102106
outputFiles(fileCjs, createdFiles[filepath]);
103107
}
104108
}
105109
if (new RegExp(`${cjs}`).test(filepath)) {
106110
outputFiles(filepath, createdFiles[filepath]);
107-
if (esm) {
111+
if (esm && typeof esm === 'string') {
108112
const fileEsm = filepath.replace(cjs, esm);
109113
outputFiles(fileEsm, createdFiles[filepath]);
110114
}

‎packages/tsbb/src/utils/watchCompile.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ export async function watchCompile(
1616
let { entry, cjs = tsOptions.outDir || 'lib', esm = 'esm', disableBabel, ...other } = options || {};
1717
const entryDir = path.dirname(entry);
1818
cjs = path.relative(ts.sys.getCurrentDirectory(), cjs);
19-
const compilerOptions: ts.CompilerOptions = {
20-
...tsOptions,
21-
outDir: cjs || esm,
22-
};
19+
20+
const compilerOptions: ts.CompilerOptions = { ...tsOptions };
21+
const outDirPath = cjs || esm;
22+
if (typeof outDirPath === 'string') {
23+
tsOptions.outDir = outDirPath;
24+
}
2325

2426
if (!disableBabel) {
2527
await compile([options.entry], tsOptions, options);
2628
}
29+
2730
const watcher = chokidar.watch(path.dirname(entry), {
2831
persistent: true,
2932
});
3033
watcher.on('change', async (filepath) => {
31-
if (esm) {
34+
if (typeof esm === 'string') {
3235
const output = filepath.replace(entryDir, esm);
3336
if (
3437
!disableBabel &&
@@ -41,7 +44,7 @@ export async function watchCompile(
4144
outputFiles(output, result);
4245
}
4346
}
44-
if (cjs) {
47+
if (typeof cjs === 'string') {
4548
const output = filepath.replace(entryDir, cjs);
4649
if (
4750
!disableBabel &&
@@ -67,14 +70,14 @@ export async function watchCompile(
6770
if (/\.d\.ts$/.test(file)) {
6871
if (new RegExp(`${esm}`).test(file)) {
6972
outputFiles(file, content);
70-
if (cjs) {
73+
if (cjs && typeof esm === 'string') {
7174
const fileCjs = file.replace(esm, cjs);
7275
outputFiles(fileCjs, content);
7376
}
7477
}
7578
if (new RegExp(`${cjs}`).test(file)) {
7679
outputFiles(file, content);
77-
if (esm) {
80+
if (esm && typeof esm === 'string') {
7881
const fileEsm = file.replace(cjs, esm);
7982
outputFiles(fileEsm, content);
8083
}

‎packages/tsbb/src/watch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { BuildOptions } from './build';
55
export interface WatchOptions extends BuildOptions {}
66
export async function watch(options: WatchOptions, compilerOptions?: ts.CompilerOptions) {
77
try {
8-
await watchCompile([options.entry], compilerOptions, options);
8+
await watchCompile(options.fileNames, compilerOptions, options);
99
} catch (error) {
1010
console.error('ERROR', error);
1111
process.exit(1);

0 commit comments

Comments
 (0)
Please sign in to comment.