Skip to content

Commit

Permalink
feat(tsbb): Add --file-names options.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 13, 2021
1 parent cbf39ae commit 44a083a
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 47 deletions.
4 changes: 2 additions & 2 deletions example/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"description": "Basic Example.",
"scripts": {
"start": "node lib/index.js",
"watch": "tsbb watch --no-esm --disable-babel",
"build": "tsbb build --no-esm --disable-babel",
"watch": "tsbb watch --no-esm --disable-babel --file-names src/index.ts --file-names src/main.ts",
"build": "tsbb build --no-esm --disable-babel --file-names src/index.ts --file-names src/main.ts",
"test": "npm run build && tsbb test",
"coverage": "npm run build && tsbb test --coverage"
},
Expand Down
3 changes: 3 additions & 0 deletions example/basic/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function abs(a: number, b: number) {
return a + b;
}
4 changes: 3 additions & 1 deletion example/basic/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import fs from 'fs';
import path from 'path';
import { sum } from '../src/utils/sum';
import { abs } from '../src/main';

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

it('sum test case', async () => {
expect(sum(1, 1)).toEqual(4);
expect(abs(1, 1)).toEqual(2);
});

it('output files test case.', async () => {
const dirs = await fs.promises.readdir(dirSrc);
expect(dirs).toEqual(expect.arrayContaining(["index.d.ts", "index.js", "index.js.map", "utils"]));
expect(dirs).toEqual(expect.arrayContaining(["index.d.ts", "index.js", "index.js.map", "utils", "main.js", "main.d.ts", "main.js.map"]));
});
31 changes: 17 additions & 14 deletions packages/tsbb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,24 @@ Version 3.0.0-rc.14

Commands:

tsbb build [options] Build your project once and exit.
tsbb watch [options] Recompile files on changes.
tsbb test [options] Run jest test runner in watch mode.
tsbb build [options] Build your project once and exit.
tsbb watch [options] Recompile files on changes.
tsbb test [options] Run jest test runner in watch mode.

Options:[build|watch]

--entry, -e Specify the entry directory.
--envName The current active environment used during configuration loading.
--disable-babel Disable Babel.
--disable-babel-option Disable Babel Option.
--esm Output "esm" directory.
--cjs Output "cjs" directory.
--entry, -e Specify the entry directory.
--env-name The current active environment used during configuration loading.
--disable-babel Disable Babel.
--disable-babel-option Disable Babel Option.
--file-names, -f A set of root files.
--esm Output "esm" directory.
--cjs Output "cjs" directory.

Options:

--version, -v Show version number
--help, -h Show help

Examples:

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

Options:

--version, -v Show version number
--help, -h Show help

Copyright 2021
```

Expand Down
4 changes: 2 additions & 2 deletions packages/tsbb/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export interface BuildOptions extends Arguments {
* Output ESM directory.
* @example `--no-esm`
*/
esm?: string;
esm?: boolean | string;
}

export async function build(options: BuildOptions, compilerOptions?: ts.CompilerOptions) {
try {
await compile([options.entry], compilerOptions, options);
await compile(options.fileNames, compilerOptions, options);
} catch (error) {
console.error('ERROR', error);
process.exit(1);
Expand Down
25 changes: 24 additions & 1 deletion packages/tsbb/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { help } from './help';
import { jest } from './jest';

interface ArgvArguments extends Arguments {
disableBabel?: boolean;
esm?: boolean | string;
cjs?: string;
entry?: string;
fileNames?: string | string[];
}

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

(() => {
const version = require('../package.json').version;

if (argv.v) {
console.log();
console.log(` Version \x1b[32;1m ${version}\x1b[0m`);
Expand All @@ -40,6 +43,26 @@ const argv: ArgvArguments = parser(process.argv.slice(2), {
argv.entry = argv.entry.replace(/\.tsx$/, '.ts');
}

if (typeof argv.fileNames === 'string') {
argv.fileNames = [argv.fileNames];
}

if (argv.fileNames && Array.isArray(argv.fileNames)) {
argv.fileNames = argv.fileNames.map((filename: string) => path.resolve(process.cwd(), filename));
argv.fileNames = [argv.entry, ...argv.fileNames];
argv.fileNames = Array.from(new Set(argv.fileNames));
}

if (!argv.fileNames) {
argv.fileNames = [argv.entry];
}

argv.fileNames = argv.fileNames.map((item) => item.replace(/\.tsx$/, '.ts'));

if (argv.disableBabel) {
argv.esm = false;
}

const configPath = ts.findConfigFile(path.dirname(argv.entry), ts.sys.fileExists);
let tsConf = { compilerOptions: {} as ts.CompilerOptions };

Expand Down
31 changes: 19 additions & 12 deletions packages/tsbb/src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@ export function help() {
console.log();
console.log(' Commands:');
console.log();
console.log(' \x1b[35;1m tsbb\x1b[0m build [options] Build your project once and exit.');
console.log(' \x1b[35;1m tsbb\x1b[0m watch [options] Recompile files on changes.');
console.log(' \x1b[35;1m tsbb\x1b[0m test [options] Run jest test runner in watch mode.');
console.log(' \x1b[35;1m tsbb\x1b[0m build [options] Build your project once and exit.');
console.log(' \x1b[35;1m tsbb\x1b[0m watch [options] Recompile files on changes.');
console.log(' \x1b[35;1m tsbb\x1b[0m test [options] Run jest test runner in watch mode.');
console.log();
console.log(` Options:[build|watch]`);
console.log();
console.log(` \x1b[35;1m--entry, -e\x1b[0m Specify the entry directory.`);
console.log(` \x1b[35;1m--env-name\x1b[0m The current active environment used during configuration loading.`);
console.log(` \x1b[35;1m--disable-babel\x1b[0m Disable Babel.`);
console.log(` \x1b[35;1m--disable-babel-option\x1b[0m Disable Babel Option.`);
console.log(` \x1b[35;1m--esm\x1b[0m Output "esm" directory.`);
console.log(` \x1b[35;1m--cjs\x1b[0m Output "cjs" directory.`);
console.log(` \x1b[35;1m--entry, -e\x1b[0m Specify the entry directory.`);
console.log(
` \x1b[35;1m--env-name\x1b[0m The current active environment used during configuration loading.`,
);
console.log(` \x1b[35;1m--disable-babel\x1b[0m Disable Babel.`);
console.log(` \x1b[35;1m--disable-babel-option\x1b[0m Disable Babel Option.`);
console.log(` \x1b[35;1m--file-names, -f\x1b[0m A set of root files.`);
console.log(` \x1b[35;1m--esm\x1b[0m Output "esm" directory.`);
console.log(` \x1b[35;1m--cjs\x1b[0m Output "cjs" directory.`);
console.log();
console.log(` Options:`);
console.log();
console.log(' --version, -v Show version number');
console.log(' --help, -h Show help');
console.log(' \x1b[35;1m--version, -v\x1b[0m Show version number');
console.log(' \x1b[35;1m--help, -h\x1b[0m Show help');
console.log();
console.log(` Examples:`);
console.log();
Expand All @@ -32,8 +35,12 @@ export function help() {
console.log(` $\x1b[35;1m tsbb\x1b[0m watch --disable-babel-option Disable Babel Option.`);
console.log(` $\x1b[35;1m tsbb\x1b[0m watch --disable-babel Disable Babel.`);
console.log(` $\x1b[35;1m tsbb\x1b[0m watch --cjs ./cjs Watch Output directory.`);
console.log(` $\x1b[35;1m tsbb\x1b[0m build --disable-babel --file-names src/index.ts --file-names src/main.ts`);
console.log(` A set of root files.`);
console.log(` $\x1b[35;1m tsbb\x1b[0m test Run test suites related`);
console.log(` $\x1b[35;1m tsbb\x1b[0m test --coverage Test coverage information should be collected`);
console.log(
` $\x1b[35;1m tsbb\x1b[0m test --coverage Test coverage information should be collected`,
);
console.log();
console.log(' Copyright 2021');
console.log();
Expand Down
16 changes: 10 additions & 6 deletions packages/tsbb/src/utils/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function compile(
if (tsOptions.outDir || cjs) {
await FS.remove(outDir);
}
if (esm) {
if (typeof esm === 'string') {
await FS.remove(path.resolve(process.cwd(), esm));
}
const dirToFiles = await recursiveReaddirFiles(path.dirname(entry), {
Expand All @@ -42,7 +42,7 @@ export async function compile(
copyFiles(item.path, cjsPath);
}
}
if (esm) {
if (typeof esm === 'string') {
const esmPath = item.path.replace(entryDir, esm);
if (
!disableBabel &&
Expand All @@ -59,7 +59,11 @@ export async function compile(

// Create a Program with an in-memory emit
const createdFiles: Record<string, string> = {};
tsOptions = { ...tsOptions, outDir: cjs || esm, target: tsOptions.target || ts.ScriptTarget.ESNext };
const outDirPath = cjs || esm;
tsOptions = { ...tsOptions, target: tsOptions.target || ts.ScriptTarget.ESNext };
if (typeof outDirPath === 'string') {
tsOptions.outDir = outDirPath;
}
if (tsOptions.noEmit) {
resolve();
return;
Expand Down Expand Up @@ -90,21 +94,21 @@ export async function compile(
await Promise.all(
Object.keys(createdFiles).map(async (filepath) => {
try {
if (disableBabel) {
if (disableBabel && !/\.d\.ts$/.test(filepath)) {
ts.sys.writeFile(filepath, createdFiles[filepath]);
outputLog(filepath);
}
if (/\.d\.ts$/.test(filepath)) {
if (new RegExp(`${esm}`).test(filepath)) {
outputFiles(filepath, createdFiles[filepath]);
if (cjs) {
if (cjs && typeof esm === 'string') {
const fileCjs = filepath.replace(esm, cjs);
outputFiles(fileCjs, createdFiles[filepath]);
}
}
if (new RegExp(`${cjs}`).test(filepath)) {
outputFiles(filepath, createdFiles[filepath]);
if (esm) {
if (esm && typeof esm === 'string') {
const fileEsm = filepath.replace(cjs, esm);
outputFiles(fileEsm, createdFiles[filepath]);
}
Expand Down
19 changes: 11 additions & 8 deletions packages/tsbb/src/utils/watchCompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ export async function watchCompile(
let { entry, cjs = tsOptions.outDir || 'lib', esm = 'esm', disableBabel, ...other } = options || {};
const entryDir = path.dirname(entry);
cjs = path.relative(ts.sys.getCurrentDirectory(), cjs);
const compilerOptions: ts.CompilerOptions = {
...tsOptions,
outDir: cjs || esm,
};

const compilerOptions: ts.CompilerOptions = { ...tsOptions };
const outDirPath = cjs || esm;
if (typeof outDirPath === 'string') {
tsOptions.outDir = outDirPath;
}

if (!disableBabel) {
await compile([options.entry], tsOptions, options);
}

const watcher = chokidar.watch(path.dirname(entry), {
persistent: true,
});
watcher.on('change', async (filepath) => {
if (esm) {
if (typeof esm === 'string') {
const output = filepath.replace(entryDir, esm);
if (
!disableBabel &&
Expand All @@ -41,7 +44,7 @@ export async function watchCompile(
outputFiles(output, result);
}
}
if (cjs) {
if (typeof cjs === 'string') {
const output = filepath.replace(entryDir, cjs);
if (
!disableBabel &&
Expand All @@ -67,14 +70,14 @@ export async function watchCompile(
if (/\.d\.ts$/.test(file)) {
if (new RegExp(`${esm}`).test(file)) {
outputFiles(file, content);
if (cjs) {
if (cjs && typeof esm === 'string') {
const fileCjs = file.replace(esm, cjs);
outputFiles(fileCjs, content);
}
}
if (new RegExp(`${cjs}`).test(file)) {
outputFiles(file, content);
if (esm) {
if (esm && typeof esm === 'string') {
const fileEsm = file.replace(cjs, esm);
outputFiles(fileEsm, content);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tsbb/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BuildOptions } from './build';
export interface WatchOptions extends BuildOptions {}
export async function watch(options: WatchOptions, compilerOptions?: ts.CompilerOptions) {
try {
await watchCompile([options.entry], compilerOptions, options);
await watchCompile(options.fileNames, compilerOptions, options);
} catch (error) {
console.error('ERROR', error);
process.exit(1);
Expand Down

0 comments on commit 44a083a

Please sign in to comment.