|
| 1 | +import inquirer from "inquirer" |
| 2 | +import fs from "node:fs" |
| 3 | +import path from "node:path" |
| 4 | +import { fileURLToPath } from "node:url" |
| 5 | + |
| 6 | + |
| 7 | +const idIgnorePrefix = [ |
| 8 | + 'com', |
| 9 | + 'net', |
| 10 | + 'java', |
| 11 | + 'org', |
| 12 | + 'top', |
| 13 | + 'cn', |
| 14 | + 'io', |
| 15 | + 'co', |
| 16 | + 'cc', |
| 17 | + 'site', |
| 18 | + 'git', |
| 19 | + 'github', |
| 20 | + 'gitee', |
| 21 | + 'gitea', |
| 22 | + 'gitlab', |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +let useJS = false |
| 27 | +let id = '' |
| 28 | +let extensionDir = '' |
| 29 | +let author = '' |
| 30 | +let extensionName = '' |
| 31 | + |
| 32 | +function leftPath(left: string) { |
| 33 | + return fileURLToPath(import.meta.resolve('./template/' + left)) |
| 34 | +} |
| 35 | + |
| 36 | +function rightPath(right: string) { |
| 37 | + return path.join(extensionDir, right) |
| 38 | +} |
| 39 | + |
| 40 | +function cp(left: string, right?: string) { |
| 41 | + fs.cpSync( |
| 42 | + leftPath(left), |
| 43 | + rightPath(right || left), |
| 44 | + { recursive: true } |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +function checkInputID(id: string): true | string { |
| 49 | + // 1. `your.extension.id` must be an valid id, |
| 50 | + // which only contains a-z, 0-9 and `_`, and is split by `.`. |
| 51 | + // To avoid id conflict, a recommended id is `name.extension`, |
| 52 | + // containing both your extension name and your own name |
| 53 | + // (or your team/organization's name) with lower letters |
| 54 | + // (numbers and `_` shouldn't be used if not necessary), |
| 55 | + // like `alexcui.random`, `clipteam.community`, etc. |
| 56 | + // https://www.npmjs.com/package/clipcc-extension-cli |
| 57 | + let re = /^([a-z0-9_]+\.)+[a-z0-9_]+$/ |
| 58 | + if (!re.test(id)) |
| 59 | + return 'ID must match ' + re |
| 60 | + |
| 61 | + // 如果我 id 以 com 开头该怎么办 |
| 62 | + // —— Simon Shiki |
| 63 | + if (idIgnorePrefix.includes(id.split('.', 2)[0])) |
| 64 | + return 'ID prefix must not in ' + JSON.stringify(idIgnorePrefix) |
| 65 | + |
| 66 | + return true |
| 67 | +} |
| 68 | + |
| 69 | +try { |
| 70 | + { |
| 71 | + const answerLang = await inquirer.prompt([{ |
| 72 | + type: 'list', |
| 73 | + name: 'lang', |
| 74 | + message: 'Choose your development language:', |
| 75 | + choices: ['TypeScript', 'JavaScript'] |
| 76 | + } |
| 77 | + ]) |
| 78 | + useJS = answerLang.lang == 'JavaScript' |
| 79 | + } { |
| 80 | + const answerID = await inquirer.prompt([{ |
| 81 | + type: 'input', |
| 82 | + name: 'id', |
| 83 | + message: 'Extension ID:', |
| 84 | + validate: (id) => checkInputID(id) |
| 85 | + }]) |
| 86 | + id = answerID.id; |
| 87 | + [author, extensionName] = id.split('.', 2) |
| 88 | + extensionDir = 'clipcc-extension-' + extensionName |
| 89 | + } { |
| 90 | + const answerDir = await inquirer.prompt([{ |
| 91 | + type: 'input', |
| 92 | + name: 'dir', |
| 93 | + message: 'Directory Name:', |
| 94 | + default: extensionDir, |
| 95 | + validate: (v) => (!fs.existsSync(v) || 'Directory exists!') |
| 96 | + }]) |
| 97 | + if (answerDir.dir) { |
| 98 | + extensionDir = answerDir.dir |
| 99 | + } |
| 100 | + } { |
| 101 | + const answerAuthor = await inquirer.prompt([{ |
| 102 | + type: 'input', |
| 103 | + name: 'author', |
| 104 | + message: 'Author:', |
| 105 | + default: author |
| 106 | + }]) |
| 107 | + if (answerAuthor.author) { |
| 108 | + author = answerAuthor.author |
| 109 | + } |
| 110 | + } { |
| 111 | + const answerExtensionName = await inquirer.prompt([{ |
| 112 | + type: 'input', |
| 113 | + name: 'extensionName', |
| 114 | + message: 'Extension Name:', |
| 115 | + default: extensionName |
| 116 | + }]) |
| 117 | + if (answerExtensionName.extensionName) { |
| 118 | + extensionName = answerExtensionName.extensionName |
| 119 | + } |
| 120 | + } |
| 121 | +} catch (e) { |
| 122 | + if (e == 'ExitPromptError: User force closed the prompt with SIGINT') { |
| 123 | + console.error('^C') |
| 124 | + process.exit(1) |
| 125 | + } |
| 126 | + throw e |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +if (extensionDir == '') { |
| 131 | + console.error('Reject.') |
| 132 | + process.exit(1) |
| 133 | +} |
| 134 | + |
| 135 | +fs.mkdirSync(extensionDir) |
| 136 | + |
| 137 | +cp('.github') |
| 138 | +cp('.vscode') |
| 139 | +cp('.gitattributes') |
| 140 | +cp('.gitignore') |
| 141 | +cp('src-public/assets', 'src/assets') |
| 142 | + |
| 143 | +if (useJS) { |
| 144 | + cp('src-js', 'src') |
| 145 | + cp('makeccx.config.js') |
| 146 | + cp('package-js.json', 'package.json') |
| 147 | +} else { |
| 148 | + cp('src-ts', 'src') |
| 149 | + cp('makeccx.config.js', 'makeccx.config.ts') |
| 150 | + cp('package-ts.json', 'package.json') |
| 151 | + cp('tsconfig.json') |
| 152 | + cp('tsconfig.makeccx.json') |
| 153 | + cp('tsconfig.src.json') |
| 154 | +} |
| 155 | + |
| 156 | +{ |
| 157 | + const lp = leftPath('src-public/info.json') |
| 158 | + const info = JSON.parse(fs.readFileSync(lp).toString()) |
| 159 | + info.id = id |
| 160 | + info.author = author |
| 161 | + |
| 162 | + const rp = rightPath('src/info.json') |
| 163 | + fs.writeFileSync(rp, JSON.stringify(info, null, " ")) |
| 164 | +} |
| 165 | + |
| 166 | +fs.mkdirSync(rightPath(`src/locales`)) |
| 167 | + |
| 168 | +for (const name of fs.readdirSync(leftPath(`src-public/locales`))) { |
| 169 | + const lp = leftPath(`src-public/locales/${name}`) |
| 170 | + const json = JSON.parse(fs.readFileSync(lp).toString()) |
| 171 | + json.name = extensionName |
| 172 | + |
| 173 | + const rp = rightPath(`src/locales/${name}`) |
| 174 | + fs.writeFileSync(rp, JSON.stringify(json, null, " ")) |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +console.log(` |
| 179 | + cd ${extensionDir} |
| 180 | + npm i |
| 181 | + npm run build |
| 182 | +`) |
0 commit comments