@tsbb/core
Version:
TSBB is a zero-config CLI that helps you develop, test, and publish modern TypeScript project.
75 lines (74 loc) • 2.28 kB
JavaScript
import meow from 'meow';
import { glob } from 'glob';
import jest from '@tsbb/jest';
import { Log } from '@tsbb/typescript';
import { helpStr } from './helpStr.js';
import { compile } from './compile.js';
import { copy } from './copy.js';
export * from './watcher/copyFiles.js';
export async function tsbb() {
const cli = meow(helpStr(), {
importMeta: import.meta,
flags: {
useBabel: {
type: 'boolean',
shortFlag: 'b',
default: false,
},
sourceMaps: {
default: false,
},
bail: {
type: 'boolean',
default: true,
},
},
});
const flags = cli.flags;
const log = new Log();
if (flags.h || flags.help) {
cli.showHelp();
process.exitCode = 0;
}
if (flags.v || flags.version) {
cli.showVersion();
process.exitCode = 0;
}
try {
if (cli.input.length === 0) {
throw new Error('Please enter command parameters, such as: (build, watch, copy)');
}
let entry = [...cli.input];
entry.shift();
const commandName = cli.input[0];
entry = await glob(entry, /^(copy|cpy)/i.test(commandName) ? {} : { ignore: 'node_modules/**' });
if (commandName === 'build') {
compile({ ...flags, build: true, entry });
}
else if (/^(copy|cpy)/i.test(commandName)) {
copy({ ...flags, entry });
}
else if (/^(watch|start|dev)/i.test(commandName)) {
compile({ ...flags, watch: true, entry });
}
else if (/^(test)/i.test(commandName)) {
jest(flags);
}
else {
console.error('\n \x1b[31;1m The build/watch/jest parameter must be passed in\x1b[0m\n');
process.exitCode = 1;
}
}
catch (error) {
if (error instanceof Error) {
log.name().icon('🚨').success(`\x1b[31;1m${error.message}\x1b[0m\n`, error.stack);
}
else {
log
.name()
.icon('🚨')
.success(`\x1b[31;1m${JSON.stringify(error)}\x1b[0m\n`);
}
process.exitCode = 1;
}
}