|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import prompts from 'prompts'; |
| 4 | +import { colors, copyFolderSync, log, normalizePath, run } from './utils'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Prompts user for projectName if not defined, returns the information required related to project |
| 8 | + */ |
| 9 | +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types |
| 10 | +export const getProjectInfo = async (projectNameArg: string | undefined) => { |
| 11 | + let projectName = ''; |
| 12 | + if (!projectNameArg) { |
| 13 | + projectName = ( |
| 14 | + await prompts({ |
| 15 | + type: 'text', |
| 16 | + message: 'Enter Name of your project', |
| 17 | + name: 'projectName', |
| 18 | + initial: 'hello-abell' |
| 19 | + }) |
| 20 | + ).projectName; |
| 21 | + } else { |
| 22 | + projectName = projectNameArg; |
| 23 | + } |
| 24 | + |
| 25 | + if (!projectName) { |
| 26 | + throw new Error(log.failure('Project name is required', false)); |
| 27 | + } |
| 28 | + |
| 29 | + const projectSlugName = projectName.toLowerCase().replace(/ |_/g, '-'); |
| 30 | + const projectPath = path.join(process.cwd(), projectSlugName); |
| 31 | + const projectDisplayName = path.basename(projectPath); |
| 32 | + |
| 33 | + if (fs.existsSync(projectPath)) { |
| 34 | + // oops. Can be an issue |
| 35 | + if (fs.readdirSync(projectPath).length !== 0) { |
| 36 | + // Not an empty directory so break! |
| 37 | + console.error( |
| 38 | + `${colors.red( |
| 39 | + '>> ' |
| 40 | + )} The directory already exists and is not an empty directory` |
| 41 | + ); |
| 42 | + process.exit(0); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return { projectDisplayName, projectPath }; |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * Prompts user to choose package installer if not defined |
| 51 | + */ |
| 52 | +export const getInstallCommand = async ( |
| 53 | + installerVal: 'npm' | 'yarn' | undefined |
| 54 | +): Promise<'npm install' | 'yarn'> => { |
| 55 | + if (!installerVal) { |
| 56 | + // if installer flag is undefined, ask user. |
| 57 | + const answers = await prompts({ |
| 58 | + type: 'select', |
| 59 | + message: 'Select Installer', |
| 60 | + name: 'installer', |
| 61 | + choices: [ |
| 62 | + { |
| 63 | + title: 'npm', |
| 64 | + value: 'npm install' |
| 65 | + }, |
| 66 | + { |
| 67 | + title: 'yarn', |
| 68 | + value: 'yarn' |
| 69 | + } |
| 70 | + ] |
| 71 | + }); |
| 72 | + |
| 73 | + installerVal = answers.installer; |
| 74 | + } |
| 75 | + |
| 76 | + if (installerVal === 'yarn') { |
| 77 | + return 'yarn'; |
| 78 | + } else { |
| 79 | + return 'npm install'; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * Some validations on top of template names |
| 85 | + */ |
| 86 | +export const getTemplate = (templateVal: string | undefined): string => { |
| 87 | + // return default when value is not defined |
| 88 | + if (!templateVal) return 'default'; |
| 89 | + |
| 90 | + if (templateVal === 'default' || templateVal === 'minimal') { |
| 91 | + // 'default' and 'minimal' are valid templates. Return them as it is |
| 92 | + return templateVal; |
| 93 | + } |
| 94 | + |
| 95 | + // when `--template abelljs/abell-starter-portfolio` |
| 96 | + if (!templateVal.startsWith('https://github.com/')) { |
| 97 | + // If template value is `abelljs/abell-starter-portfolio`, add https://github.com before it. |
| 98 | + return 'https://github.com/' + templateVal; |
| 99 | + } |
| 100 | + |
| 101 | + // when `--template https://github.com/abelljs/abell-starter-portfolio` |
| 102 | + return templateVal; |
| 103 | +}; |
| 104 | + |
| 105 | +export const scaffoldTemplate = async ({ |
| 106 | + projectPath, |
| 107 | + template |
| 108 | +}: { |
| 109 | + projectPath: string; |
| 110 | + template: string; |
| 111 | +}): Promise<void> => { |
| 112 | + if (template === 'default' || template === 'minimal') { |
| 113 | + // copy default template from templates directory |
| 114 | + const templatesDir = path.join(__dirname, '..', 'templates'); |
| 115 | + const templatePath = path.join(templatesDir, template); |
| 116 | + copyFolderSync(templatePath, projectPath, [ |
| 117 | + path.join(templatePath, 'node_modules'), |
| 118 | + path.join(templatePath, 'yarn.lock'), |
| 119 | + path.join(templatePath, 'dist') |
| 120 | + ]); |
| 121 | + } else { |
| 122 | + // Execute git clone |
| 123 | + try { |
| 124 | + const errorCode = await run(`git clone ${template} ${projectPath}`); |
| 125 | + if (errorCode === 1) { |
| 126 | + throw new Error(log.failure('Git clone failed', false)); |
| 127 | + } |
| 128 | + } catch (err) { |
| 129 | + throw err; |
| 130 | + } |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | +export const setNameInPackageJSON = ( |
| 135 | + packagePath: string, |
| 136 | + appName: string |
| 137 | +): void => { |
| 138 | + try { |
| 139 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 140 | + const packageJSON = require(normalizePath(packagePath)); |
| 141 | + packageJSON.name = appName; |
| 142 | + fs.writeFileSync(packagePath, JSON.stringify(packageJSON, null, 2)); |
| 143 | + } catch (err) { |
| 144 | + // Do nothing. Skip the step if error. |
| 145 | + } |
| 146 | +}; |
0 commit comments