首頁 >web前端 >js教程 >TypeScript CLI:自動化建置與部署腳本

TypeScript CLI:自動化建置與部署腳本

Mary-Kate Olsen
Mary-Kate Olsen原創
2025-01-27 20:31:20212瀏覽

我想跟進我之前關於 TypeScript CLI 的文章。我想要繼續的方式如下:我計劃實施構建命令來構建 Vite 應用程序和部署命令來將應用程序部署到 Amazon S3 和 AWS CloudFront。

TypeScript CLI:自動化建置與部署腳本

為您的 Monorepo 創建 TypeScript CLI

克里斯·庫克 ・24 年 12 月 1 日

#打字稿 #javascript #webdev #程式設計

我們將使用 Listr2 作為任務運行程序來定義構建和部署應用程序所需的步驟。我們將使用 execa 運行 Vite 和 AWS 的 CLI 命令。由於我們正在運行 TypeScript 代碼,因此我們可以使用編程 API 而不是 CLI 命令,但讓我們保持簡單!

#!/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);
</void></void></ctx></void></ctx></ctx>

任務分為構建任務和部署任務。由於部署需要構建步驟,因此我們使用enabled 屬性根據 CLI 命令構建或部署有條件地啟用任務。每個任務執行相應的 CLI 命令並將其輸出傳輸到控制台。

將此腳本保存為 cli.ts 並使用 pnpm tsx cli 運行它:

TypeScript CLI:自動化建置與部署腳本

以上是TypeScript CLI:自動化建置與部署腳本的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn