|
| 1 | +import Command, { flags } from '@oclif/command'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import validFilename from 'valid-filename'; |
| 5 | + |
| 6 | +import recreateProject from '../project/recreateProject'; |
| 7 | + |
| 8 | + |
| 9 | +function mkdirDryRun(target: string) { |
| 10 | + const relative = path.relative(process.cwd(), target); |
| 11 | + process.stdout.write(`mkdir ${relative}\n`); |
| 12 | +} |
| 13 | + |
| 14 | +function findConfigFile(filePath: string): string { |
| 15 | + if (!fs.existsSync(filePath)) { |
| 16 | + throw new Error(`Path '${filePath}' does not exist.`); |
| 17 | + } |
| 18 | + |
| 19 | + if (fs.statSync(filePath).isDirectory()) { |
| 20 | + return findConfigFile(path.join(filePath, '.yo-rc.json')); |
| 21 | + } |
| 22 | + |
| 23 | + if (!fs.statSync(filePath).isFile()) { |
| 24 | + throw new Error(`Path '${filePath}' is not a file.`); |
| 25 | + } |
| 26 | + |
| 27 | + return filePath; |
| 28 | +} |
| 29 | + |
| 30 | +export default class Regen extends Command { |
| 31 | + static description = 'Regenerate a new project from an existing .yo-rc.json file.'; |
| 32 | + |
| 33 | + static flags = { |
| 34 | + help: flags.help({ char: 'h' }), |
| 35 | + 'dry-run': flags.boolean({ |
| 36 | + description: 'print command instead of running', |
| 37 | + }), |
| 38 | + next: flags.boolean({ |
| 39 | + description: 'use prerelease generator for testing', |
| 40 | + }), |
| 41 | + }; |
| 42 | + |
| 43 | + static args = [ |
| 44 | + { |
| 45 | + name: 'config', |
| 46 | + description: 'path of .yo-rc.json file or project directory', |
| 47 | + required: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'target', |
| 51 | + description: 'directory name to create', |
| 52 | + required: true, |
| 53 | + }, |
| 54 | + ]; |
| 55 | + |
| 56 | + async run() { |
| 57 | + const { args, flags } = this.parse(Regen); |
| 58 | + |
| 59 | + if (!validFilename(args.target)) { |
| 60 | + return this.error(`Argument '${args.target}' is not a valid filename.`, { |
| 61 | + exit: 1, |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + const directory = path.join(process.cwd(), args.target); |
| 66 | + const configPath = path.join(process.cwd(), args.config); |
| 67 | + |
| 68 | + let configFile; |
| 69 | + try { |
| 70 | + configFile = findConfigFile(configPath); |
| 71 | + } catch (e) { |
| 72 | + return this.error(e.getMessage(), { |
| 73 | + exit: 1, |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + const createDirectory = flags['dry-run'] ? mkdirDryRun : fs.mkdirSync; |
| 79 | + createDirectory(directory); |
| 80 | + } catch (error) { |
| 81 | + if (error.code === 'EEXIST') { |
| 82 | + return this.error( |
| 83 | + `Failed to create '${args.target}' because it already exists.`, |
| 84 | + { exit: 1 }, |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + return this.error(error, { exit: 1 }); |
| 89 | + } |
| 90 | + |
| 91 | + const command = recreateProject({ |
| 92 | + configFile, |
| 93 | + directory, |
| 94 | + next: flags.next, |
| 95 | + }); |
| 96 | + |
| 97 | + return flags['dry-run'] ? command.dryRun() : command.run(); |
| 98 | + } |
| 99 | +} |
0 commit comments