>我想跟进我以前的有关打字稿cli的文章。我要继续进行以下操作:我计划实现构建命令以构建vite应用程序和deploy命令,以将应用程序部署到amazon s3和aws cloudfront。
#!/usr/bin/env -S pnpm tsx
import chalk from 'chalk';
import { Command } from 'commander';
import { Listr } from 'listr2';
import { $ } from 'execa';
interface Ctx {
command: 'build' | 'deploy';
}
const tasks = new Listr<Ctx>(
[
/**
* Build tasks
*/
{
enabled: (ctx) => ctx.command === 'build' || ctx.command === 'deploy',
title: 'Build',
task: (ctx, task): Listr =>
task.newListr<Ctx>([
/**
* Runs `vite build`.
*/
{
title: `Run ${chalk.magenta('vite build')}`,
task: async (ctx, task): Promise<void> => {
const cmd = $({ all: true })`vite build`;
cmd.all.pipe(task.stdout());
await cmd;
task.output = `Build completed: ${chalk.dim('./dist')}`;
},
rendererOptions: { persistentOutput: true },
},
]),
},
/**
* Deploy tasks
*/
{
enabled: (ctx) => ctx.command === 'deploy',
title: 'Deploy',
task: (ctx, task): Listr =>
task.newListr<Ctx>([
/**
* Runs `aws s3 sync`.
*/
{
title: `Run ${chalk.magenta('aws s3 sync')}`,
task: async (ctx, task): Promise<void> => {
const build = './dist';
const bucket = 's3://my-bucket';
const cmd = $({ all: true })`aws s3 sync ${build} ${bucket} --delete`;
cmd.all.pipe(task.stdout());
await cmd;
task.output = `S3 sync completed: ${chalk.dim(bucket)}`;
},
rendererOptions: { persistentOutput: true },
},
/**
* Runs `aws cloudfront create-invalidation`.
*/
{
title: `Run ${chalk.magenta('aws cloudfront create-invalidation')}`,
task: async (ctx, task): Promise<void> => {
const distributionId = 'E1234567890ABC';
const cmd = $({ all: true })`aws cloudfront create-invalidation --distribution-id ${distributionId} --paths /* --no-cli-pager`;
cmd.all.pipe(task.stdout());
await cmd;
task.output = `CloudFront invalidation completed: ${chalk.dim(distributionId)}`;
},
rendererOptions: { persistentOutput: true },
},
]),
},
],
{
rendererOptions: {
collapseSubtasks: false,
},
},
);
const program = new Command()
.name('monorepo')
.description('CLI for Monorepo')
.version('1.0.0');
program
.command('build')
.description('Build the monorepo')
.action(async () => {
await tasks.run({ command: 'build' });
});
program
.command('deploy')
.description('Deploy the monorepo')
.action(async () => {
await tasks.run({ command: 'deploy' });
});
await program.parseAsync(process.argv);

以上就是TypeScript CLI:自动化构建和部署脚本的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号