TypeScript CLI:自动化构建和部署脚本
时间:2025-01-28
浏览:474
作者:佚名
“纵有疾风来,人生不言弃”,这句话送给正在学习文章的朋友们,也希望在阅读本文《TypeScript CLI:自动化构建和部署脚本》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新文章相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!
>我想跟进我以前的有关打字稿cli的文章。我要继续进行以下操作:我计划实现构建命令以构建vite应用程序和deploy命令,以将应用程序部署到amazon s3和aws cloudfront。
>为您的monorepo创建打字稿cli
>我们将使用listr2作为任务跑步者来定义构建和部署应用所需的步骤。我们将使用execa运行vite和aws的cli命令。由于我们正在运行打字稿代码,因此我们可以使用程序化api而不是cli命令,但让我们保持简单!克里斯·库克(chris cook
#typescript #javascript #webdev #编程
#!/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);
任务分为构建任务和部署任务。由于部署需要一个构建步骤,因此我们使用启用属性来有条件地启用基于cli命令构建或部署的任务。每个任务都执行相应的cli命令并将其输出输出到控制台。 >将此脚本保存为cli.ts,并使用pnpm tsx cli运行:

理论要掌握,实操不能落!以上关于《TypeScript CLI:自动化构建和部署脚本》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.28
热门专题
