|
| 1 | +import { checkbox, confirm } from "@inquirer/prompts"; |
| 2 | +import fs from "fs"; |
| 3 | +import { Ora } from "ora"; |
| 4 | +import path from "path"; |
| 5 | + |
| 6 | +type Platform = "github" | "bitbucket" | "gitlab"; |
| 7 | + |
| 8 | +const platforms: Platform[] = ["github", "bitbucket", "gitlab"]; |
| 9 | + |
| 10 | +export default async function initCICD(spinner: Ora) { |
| 11 | + const initializers = getPlatformInitializers(spinner); |
| 12 | + |
| 13 | + const init = await confirm({ |
| 14 | + message: "Would you like to use Lingo.dev in your CI/CD?", |
| 15 | + }); |
| 16 | + |
| 17 | + if (!init) { |
| 18 | + spinner.warn("CI/CD not initialized. To set it up later, see docs: https://docs.lingo.dev/ci-action/overview"); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const selectedPlatforms: Platform[] = await checkbox({ |
| 23 | + message: "Please select CI/CD platform(s) you want to use:", |
| 24 | + choices: platforms.map((platform) => ({ |
| 25 | + name: initializers[platform].name, |
| 26 | + value: platform, |
| 27 | + checked: initializers[platform].isEnabled(), |
| 28 | + })), |
| 29 | + }); |
| 30 | + |
| 31 | + for (const platform of selectedPlatforms) { |
| 32 | + await initializers[platform].init(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function getPlatformInitializers(spinner: Ora) { |
| 37 | + return { |
| 38 | + github: makeGithubInitializer(spinner), |
| 39 | + bitbucket: makeBitbucketInitializer(spinner), |
| 40 | + gitlab: makeGitlabInitializer(spinner), |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +type PlatformConfig = { |
| 45 | + name: string; |
| 46 | + checkPath: string; |
| 47 | + ciConfigPath: string; |
| 48 | + ciConfigContent: string; |
| 49 | +}; |
| 50 | + |
| 51 | +function makePlatformInitializer(config: PlatformConfig, spinner: Ora) { |
| 52 | + return { |
| 53 | + name: config.name, |
| 54 | + isEnabled: () => { |
| 55 | + const filePath = path.join(process.cwd(), config.checkPath); |
| 56 | + return fs.existsSync(filePath); |
| 57 | + }, |
| 58 | + init: async () => { |
| 59 | + const filePath = path.join(process.cwd(), config.ciConfigPath); |
| 60 | + const dirPath = path.dirname(filePath); |
| 61 | + if (!fs.existsSync(dirPath)) { |
| 62 | + fs.mkdirSync(dirPath, { recursive: true }); |
| 63 | + } |
| 64 | + let canWrite = true; |
| 65 | + if (fs.existsSync(filePath)) { |
| 66 | + canWrite = await confirm({ |
| 67 | + message: `File ${filePath} already exists. Do you want to overwrite it?`, |
| 68 | + default: false, |
| 69 | + }); |
| 70 | + } |
| 71 | + if (canWrite) { |
| 72 | + fs.writeFileSync(filePath, config.ciConfigContent); |
| 73 | + spinner.succeed(`CI/CD initialized for ${config.name}`); |
| 74 | + } else { |
| 75 | + spinner.warn(`CI/CD not initialized for ${config.name}`); |
| 76 | + } |
| 77 | + }, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +function makeGithubInitializer(spinner: Ora) { |
| 82 | + return makePlatformInitializer( |
| 83 | + { |
| 84 | + name: "GitHub Action", |
| 85 | + checkPath: ".github", |
| 86 | + ciConfigPath: ".github/workflows/i18n.yml", |
| 87 | + ciConfigContent: `name: Lingo.dev i18n |
| 88 | +
|
| 89 | +on: |
| 90 | + push: |
| 91 | + branches: |
| 92 | + - main |
| 93 | +
|
| 94 | +permissions: |
| 95 | + contents: write |
| 96 | + pull-requests: write |
| 97 | +
|
| 98 | +jobs: |
| 99 | + i18n: |
| 100 | + name: Run i18n |
| 101 | + runs-on: ubuntu-latest |
| 102 | + steps: |
| 103 | + - uses: actions/checkout@v4 |
| 104 | + - uses: lingodotdev/lingo.dev@main |
| 105 | + with: |
| 106 | + api-key: \${{ secrets.LINGODOTDEV_API_KEY }} |
| 107 | +`, |
| 108 | + }, |
| 109 | + spinner, |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +function makeBitbucketInitializer(spinner: Ora) { |
| 114 | + return makePlatformInitializer( |
| 115 | + { |
| 116 | + name: "Bitbucket Pipeline", |
| 117 | + checkPath: "bitbucket-pipelines.yml", |
| 118 | + ciConfigPath: "bitbucket-pipelines.yml", |
| 119 | + ciConfigContent: `pipelines: |
| 120 | + branches: |
| 121 | + main: |
| 122 | + - step: |
| 123 | + name: Run i18n |
| 124 | + script: |
| 125 | + - pipe: lingodotdev/lingo.dev:main`, |
| 126 | + }, |
| 127 | + spinner, |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +function makeGitlabInitializer(spinner: Ora) { |
| 132 | + return makePlatformInitializer( |
| 133 | + { |
| 134 | + name: "Gitlab CI", |
| 135 | + checkPath: ".gitlab-ci.yml", |
| 136 | + ciConfigPath: ".gitlab-ci.yml", |
| 137 | + ciConfigContent: `lingodotdev: |
| 138 | + image: lingodotdev/ci-action:latest |
| 139 | + script: |
| 140 | + - echo "Done" |
| 141 | +`, |
| 142 | + }, |
| 143 | + spinner, |
| 144 | + ); |
| 145 | +} |
0 commit comments