|
| 1 | +import { Flags } from '@oclif/core' |
| 2 | +import { execSync } from 'child_process' |
| 3 | +import { compile } from 'ejs' |
| 4 | +import { access, mkdir, readFile, rm, stat, writeFile } from 'fs/promises' |
| 5 | +import { prompt } from 'inquirer' |
| 6 | +import { basename, dirname, join, resolve } from 'path' |
| 7 | +import { Command } from '../../utils/command/command' |
| 8 | +import { computePackageJSON } from '../../utils/package-json/plugin' |
| 9 | +import { toCamelCase } from '../../utils/string' |
| 10 | + |
| 11 | +export default class Plugin extends Command { |
| 12 | + static description = 'Generate fastify plugin' |
| 13 | + |
| 14 | + static args = [ |
| 15 | + { name: 'name', required: false, description: 'Name of the plugin' } |
| 16 | + ] |
| 17 | + |
| 18 | + static flags = { |
| 19 | + location: Flags.string({ description: 'Location to place the project.' }), |
| 20 | + overwrite: Flags.boolean({ description: 'Force to overwrite the project location when it exist.', default: false }), |
| 21 | + repo: Flags.string({ description: 'Git repository url of the project.' }), |
| 22 | + language: Flags.string({ description: 'Programming Language you would like to use in this project.' }), |
| 23 | + lint: Flags.string({ description: 'Lint Tools you would like to use in this project.' }), |
| 24 | + test: Flags.string({ description: 'Test Framework you would like to use in this project.' }), |
| 25 | + help: Flags.help() |
| 26 | + } |
| 27 | + |
| 28 | + shouldOverwrite = false |
| 29 | + |
| 30 | + async run (): Promise<void> { |
| 31 | + const { args, flags } = await this.parse(Plugin) |
| 32 | + |
| 33 | + // validate |
| 34 | + if (flags.language !== undefined) this.corceLanguage(flags.language) |
| 35 | + |
| 36 | + const answer: any = {} |
| 37 | + |
| 38 | + Object.assign(answer, await prompt([ |
| 39 | + { type: 'input', name: 'name', message: 'What is your project name?', validate: this.questionNameValidate }, |
| 40 | + { type: 'input', name: 'location', message: 'Where do you want to place your project?', default: this.questionLocationDefault }, |
| 41 | + { type: 'confirm', name: 'overwrite', message: 'The folder already exist. Do you want to overwrite?', default: flags.overwrite ?? false, when: this.questionOverwriteWhen, askAnswered: true } |
| 42 | + ], { |
| 43 | + name: args.name, |
| 44 | + location: flags.location, |
| 45 | + overwrite: flags.overwrite |
| 46 | + })) |
| 47 | + |
| 48 | + if (this.shouldOverwrite) this.questionOverwriteValidate(answer.overwrite) |
| 49 | + |
| 50 | + Object.assign(answer, await prompt([ |
| 51 | + { type: 'input', name: 'repo', message: 'What is the repo url?' }, |
| 52 | + { type: 'list', name: 'language', message: 'Which language will you use?', default: 'JavaScript', choices: ['JavaScript'] }, |
| 53 | + { type: 'list', name: 'lint', message: 'Which linter would you like to use?', default: this.questionLintDefault, choices: this.questionLintChoices }, |
| 54 | + { type: 'list', name: 'test', message: 'Which test framework would you like to use?', default: 'tap', choices: ['tap'] } |
| 55 | + ], { |
| 56 | + repo: flags.repo, |
| 57 | + language: flags.language, |
| 58 | + lint: flags.lint, |
| 59 | + test: flags.test |
| 60 | + })) |
| 61 | + |
| 62 | + answer.camelCaseName = toCamelCase(answer.name) |
| 63 | + |
| 64 | + const fileList = await this.computeFileList(answer) |
| 65 | + const files = await this.prepareFiles(fileList, answer) |
| 66 | + await this.writeFiles(files, answer) |
| 67 | + await this.npmInstall(answer) |
| 68 | + this.log(`project "${answer.name as string}" initialized in "${answer.location as string}"`) |
| 69 | + } |
| 70 | + |
| 71 | + questionNameValidate = (input: string): true | string => { |
| 72 | + if (String(input).trim() === '') { |
| 73 | + return 'Project Name cannot be empty.' |
| 74 | + } |
| 75 | + return true |
| 76 | + } |
| 77 | + |
| 78 | + questionLocationDefault = (answer: any): string => { |
| 79 | + return this.toLocation(answer.name) |
| 80 | + } |
| 81 | + |
| 82 | + questionOverwriteWhen = async (answer: any): Promise<boolean> => { |
| 83 | + this.shouldOverwrite = !(await this.validateProjectLocation(answer.location)) |
| 84 | + return this.shouldOverwrite |
| 85 | + } |
| 86 | + |
| 87 | + questionOverwriteValidate = (input: boolean): true | undefined => { |
| 88 | + if (input) return true |
| 89 | + this.log('Terminated because location cannot be overwrite.') |
| 90 | + this.exit(0) |
| 91 | + } |
| 92 | + |
| 93 | + questionLintDefault =(answer: any): string => { |
| 94 | + switch (this.corceLanguage(answer.language)) { |
| 95 | + case 'JavaScript': |
| 96 | + return 'standard' |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + questionLintChoices = (answer: any): string[] => { |
| 101 | + switch (this.corceLanguage(answer.language)) { |
| 102 | + case 'JavaScript': |
| 103 | + return ['standard', 'eslint', 'eslint + standard'] |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + corceLanguage (str: string): 'JavaScript' { |
| 108 | + switch (str.trim().toLowerCase()) { |
| 109 | + case 'js': |
| 110 | + case 'javascript': |
| 111 | + return 'JavaScript' |
| 112 | + default: |
| 113 | + throw new Error(`Programming Language expected to be "JavaScript", but recieved "${str}"`) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + toLocation (name: string): string { |
| 118 | + return name.trim().toLowerCase().replace(/\s+/g, '-') |
| 119 | + } |
| 120 | + |
| 121 | + async isFileExist (path: string): Promise<boolean> { |
| 122 | + try { |
| 123 | + const stats = await stat(path) |
| 124 | + return stats.isFile() |
| 125 | + } catch { |
| 126 | + return false |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + async validateProjectLocation (location: string): Promise<boolean> { |
| 131 | + const path = resolve(location) |
| 132 | + try { |
| 133 | + await access(path) |
| 134 | + return false |
| 135 | + } catch { |
| 136 | + return true |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + computeFileList (answer: any): string[] { |
| 141 | + // we do not add .ejs in here |
| 142 | + // we should find the file if .ejs exist first and then compile to the destination |
| 143 | + const files: string[] = [ |
| 144 | + 'README.md', |
| 145 | + '__gitignore', |
| 146 | + '.github/dependabot.yml', |
| 147 | + '.github/workflows/ci.yml' |
| 148 | + ] |
| 149 | + |
| 150 | + if (answer.language === 'JavaScript') { |
| 151 | + files.push('tsconfig.json') |
| 152 | + files.push('index.js') |
| 153 | + files.push('index.d.ts') |
| 154 | + files.push('test/index.test.js') |
| 155 | + files.push('test/index.test-d.ts') |
| 156 | + } |
| 157 | + |
| 158 | + return files |
| 159 | + } |
| 160 | + |
| 161 | + async resolveFile (file: string): Promise<{ template: boolean, content: string }> { |
| 162 | + const o = { template: false, content: '' } |
| 163 | + const ejsPath = resolve(join('templates', 'plugin', `${file}.ejs`)) |
| 164 | + const isEJSTemplate = await this.isFileExist(ejsPath) |
| 165 | + if (isEJSTemplate) { |
| 166 | + o.template = true |
| 167 | + const data = await readFile(ejsPath) |
| 168 | + o.content = data.toString() |
| 169 | + return o |
| 170 | + } |
| 171 | + const filePath = resolve(join('templates', 'plugin', file)) |
| 172 | + const isFileExist = await this.isFileExist(filePath) |
| 173 | + if (isFileExist) { |
| 174 | + const data = await readFile(filePath) |
| 175 | + o.content = data.toString() |
| 176 | + return o |
| 177 | + } |
| 178 | + throw new Error(`File ${file} is missing, please check if the module installed properly.`) |
| 179 | + } |
| 180 | + |
| 181 | + async prepareFiles (files: string[], answer: any): Promise<{ [path: string]: string }> { |
| 182 | + const o: { [path: string]: string } = {} |
| 183 | + o['package.json'] = computePackageJSON(answer) |
| 184 | + for (let file of files) { |
| 185 | + const { template, content } = await this.resolveFile(file) |
| 186 | + const dir = dirname(file) |
| 187 | + file = `${dir}/${basename(file).replace('__', '.')}` |
| 188 | + if (template) { |
| 189 | + const render = compile(content, { async: true }) |
| 190 | + o[file] = await render(answer) |
| 191 | + } else { |
| 192 | + o[file] = content |
| 193 | + } |
| 194 | + } |
| 195 | + return o |
| 196 | + } |
| 197 | + |
| 198 | + async writeFiles (files: { [path: string]: string }, answer: any): Promise<void> { |
| 199 | + if (this.shouldOverwrite) { |
| 200 | + this.log(`remove folder "${answer.location as string}"`) |
| 201 | + await rm(resolve(answer.location as string), { recursive: true, force: true }) |
| 202 | + } |
| 203 | + for (const [path, content] of Object.entries(files)) { |
| 204 | + const realpath = join(answer.location, path) |
| 205 | + const fullpath = resolve(realpath) |
| 206 | + await mkdir(dirname(fullpath), { recursive: true }) |
| 207 | + await writeFile(fullpath, content) |
| 208 | + this.log(`write file "${path}" to "${realpath}"`) |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + async npmInstall (answer: any): Promise<void> { |
| 213 | + this.log('run "npm install"') |
| 214 | + const result: any = await prompt([ |
| 215 | + { type: 'confirm', name: 'npm', message: 'Do you want to run "npm install"?', default: true } |
| 216 | + ]) |
| 217 | + if (result.npm === true) { |
| 218 | + execSync('npm install', { |
| 219 | + cwd: resolve(answer.location), |
| 220 | + stdio: 'inherit' |
| 221 | + }) |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments