Skip to content

Commit

Permalink
feat: add copy command.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 9, 2023
1 parent 0108ebf commit 8275abb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
39 changes: 39 additions & 0 deletions packages/core/src/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import path from 'node:path';
import fs from 'fs-extra';
import chokidar from 'chokidar';
import { Log, CopyFilesOptions } from '@tsbb/typescript';
import { watcherCopyFiles } from './watcher/copyFiles.js';

export interface CopyOption {
entry?: string[];
output?: string;
watch?: boolean;
}

export async function copy(options: CopyOption = {}) {
const { watch, entry, output } = options;
if (!output) {
throw new Error('Please enter the \x1b[33;1moutput\x1b[0m directory');
}
const watcher = chokidar.watch(entry || [], {
persistent: true,
});
const log = new Log();
log.name();
watcher.on('all', async (eventName, filepath, stats) => {
const outputPath = path.resolve(process.cwd(), output, filepath);
fs.ensureDirSync(path.dirname(outputPath));
fs.copyFile(filepath, path.resolve(process.cwd(), output, filepath));
log
.icon('🗞️')
.success(`Copy \x1b[32;1m${filepath}\x1b[0m ┈┈▶ \x1b[35;1m${path.relative(process.cwd(), outputPath)}\x1b[0m`);
});
watcher.on('error', (error) => {
log.error(`\n \x1b[33;1m${error.message}\x1b[0m.`);
});
watcher.on('ready', () => {
if (!watch) {
watcher.close();
}
});
}
5 changes: 4 additions & 1 deletion packages/core/src/helpStr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Usage:\x1b[34;1m tsbb\x1b[0m <command>
\x1b[35;1m tsbb\x1b[0m build [source…] [options] Build your project once and exit.
\x1b[35;1m tsbb\x1b[0m watch [source…] [options] Recompile files on changes.
\x1b[35;1m tsbb\x1b[0m test [options] Run jest test runner in watch mode.
\x1b[35;1m tsbb\x1b[0m test [options] Run jest test runner in watch mode.
\x1b[35;1m tsbb\x1b[0m copy|cpy <source …> [options] Copy files.
Options:[build|watch]
Expand All @@ -31,6 +32,8 @@ Examples:
$\x1b[35;1m tsbb\x1b[0m build src/*.ts --use-babel --no-source-maps No ".js.map" file is generated. (works in babel)
$\x1b[35;1m tsbb\x1b[0m test Run test suites related
$\x1b[35;1m tsbb\x1b[0m test --coverage --bail Test coverage information should be collected
$\x1b[35;1m tsbb\x1b[0m copy 'src/*.png' 'src/goat.{js,d.ts}' --output dist Copy files.
Copyright 2023
`;
16 changes: 11 additions & 5 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import jest, { JestOptions } from '@tsbb/jest';
import { Log } from '@tsbb/typescript';
import { helpStr } from './helpStr.js';
import { compile, CompileOptions } from './compile.js';
import { copy, CopyOption } from './copy.js';

export * from './watcher/copyFiles.js';

Expand Down Expand Up @@ -38,16 +39,21 @@ export async function tsbb() {
}
try {
if (cli.input.length === 0) {
throw new Error('Please enter command parameters, such as: (build, watch)');
throw new Error('Please enter command parameters, such as: (build, watch, copy)');
}
let entry = [...cli.input];
entry.shift();
entry = await glob(entry, { ignore: 'node_modules/**' });
if (cli.input[0] === 'build') {

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 } as CompileOptions);
} else if (/^(watch|start|dev)/i.test(cli.input[0])) {
} else if (/^(copy|cpy)/i.test(commandName)) {
copy({ ...flags, entry } as CopyOption);
} else if (/^(watch|start|dev)/i.test(commandName)) {
compile({ ...flags, watch: true, entry } as CompileOptions);
} else if (/^(test)/i.test(cli.input[0])) {
} else if (/^(test)/i.test(commandName)) {
// @ts-ignore
jest.default(flags as unknown as JestOptions);
} else {
Expand Down
2 changes: 2 additions & 0 deletions packages/tsbb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ Commands:
tsbb build [source…] [options] Build your project once and exit.
tsbb watch [source…] [options] Recompile files on changes.
tsbb test [options] Run jest test runner in watch mode.
tsbb copy|cpy <source …> [options] Copy files.

Options:[build|watch]

Expand All @@ -202,6 +203,7 @@ Examples:
$ tsbb build src/*.ts --use-babel --use-vue To add Vue JSX support.
$ tsbb test Run test suites related
$ tsbb test --coverage --bail Test coverage information should be collected
$ tsbb copy 'src/*.png' '!src/goat.png' --output dist Copy files.

Copyright 2023

Expand Down

0 comments on commit 8275abb

Please sign in to comment.