|
| 1 | +const path = require('path'); |
| 2 | +const execa = require('execa'); |
| 3 | +const GitUtilities = require('@4c/cli-core/GitUtilities'); |
| 4 | + |
| 5 | +const addHelpers = require('./addHelpers'); |
| 6 | +const { getPackageNameFromPath, templatePath } = require('./utils'); |
| 7 | + |
| 8 | +const ignore = () => {}; |
| 9 | + |
| 10 | +const prompts = [ |
| 11 | + { |
| 12 | + name: 'location', |
| 13 | + type: 'input', |
| 14 | + message: 'package location', |
| 15 | + filter: location => |
| 16 | + path.isAbsolute(location) ? location : path.resolve(location), |
| 17 | + }, |
| 18 | + { |
| 19 | + name: 'scopePackage', |
| 20 | + type: 'confirm', |
| 21 | + message: 'Create a scoped package?', |
| 22 | + default: true, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'scope', |
| 26 | + type: 'input', |
| 27 | + message: 'package scope', |
| 28 | + default: '@4c', |
| 29 | + when: _ => !!_.scopePackage, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: 'isPrivate', |
| 33 | + type: 'confirm', |
| 34 | + default: false, |
| 35 | + message: 'Is this a private package?', |
| 36 | + when: _ => !!_.scope, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: 'name', |
| 40 | + type: 'input', |
| 41 | + message: 'name', |
| 42 | + default: _ => getPackageNameFromPath(_.scope, _.location), |
| 43 | + }, |
| 44 | + { |
| 45 | + name: 'type', |
| 46 | + type: 'list', |
| 47 | + default: 'node', |
| 48 | + choices: ['node', 'web'], |
| 49 | + message: 'What type of library is this?', |
| 50 | + }, |
| 51 | + { |
| 52 | + name: 'babel', |
| 53 | + type: 'confirm', |
| 54 | + default: false, |
| 55 | + message: 'Do you need babel (maybe not?)', |
| 56 | + when: _ => _.type === 'node', |
| 57 | + }, |
| 58 | + { |
| 59 | + name: 'semanticRelease', |
| 60 | + type: 'confirm', |
| 61 | + default: true, |
| 62 | + message: 'Do you want to use semantic-release to handle releases?', |
| 63 | + }, |
| 64 | +]; |
| 65 | + |
| 66 | +module.exports = plop => { |
| 67 | + addHelpers(plop); |
| 68 | + |
| 69 | + plop.setHelper('gitUrl', name => GitUtilities.remoteUrl(name)); |
| 70 | + |
| 71 | + // controller generator |
| 72 | + plop.setGenerator('new-package', { |
| 73 | + description: 'create a new package', |
| 74 | + prompts, |
| 75 | + actions({ type, babel, location }) { |
| 76 | + if (type === 'web') babel = true; // eslint-disable-line no-param-reassign |
| 77 | + |
| 78 | + return [ |
| 79 | + { |
| 80 | + type: 'add', |
| 81 | + path: '{{location}}/package.json', |
| 82 | + templateFile: `${templatePath}/package.json.hbs`, |
| 83 | + }, |
| 84 | + { |
| 85 | + type: 'add', |
| 86 | + path: '{{location}}/.gitignore', |
| 87 | + templateFile: `${templatePath}/gitignore`, |
| 88 | + skipIfExists: true, |
| 89 | + }, |
| 90 | + { |
| 91 | + type: 'add', |
| 92 | + path: `{{location}}/.travis.yml`, |
| 93 | + templateFile: `${templatePath}/.travis.yml.hbs`, |
| 94 | + skipIfExists: true, |
| 95 | + }, |
| 96 | + { |
| 97 | + type: 'add', |
| 98 | + path: `{{location}}/.eslintrc`, |
| 99 | + templateFile: `${templatePath}/.eslintrc.hbs`, |
| 100 | + skipIfExists: true, |
| 101 | + }, |
| 102 | + { |
| 103 | + type: 'add', |
| 104 | + path: `{{location}}/.eslintignore`, |
| 105 | + templateFile: `${templatePath}/.eslintignore`, |
| 106 | + skipIfExists: true, |
| 107 | + }, |
| 108 | + { |
| 109 | + type: 'add', |
| 110 | + path: `{{location}}/LICENSE`, |
| 111 | + templateFile: `${templatePath}/LICENSE.hbs`, |
| 112 | + skipIfExists: true, |
| 113 | + }, |
| 114 | + () => GitUtilities.init(location).catch(ignore), |
| 115 | + _ => GitUtilities.addRemote(location, _.name).catch(ignore), |
| 116 | + babel |
| 117 | + ? { |
| 118 | + type: 'add', |
| 119 | + path: `{{location}}/.babelrc.js`, |
| 120 | + templateFile: `${templatePath}/babelrc.js.hbs`, |
| 121 | + skipIfExists: true, |
| 122 | + } |
| 123 | + : { |
| 124 | + type: 'add', |
| 125 | + path: `{{location}}/index.js`, |
| 126 | + skipIfExists: true, |
| 127 | + }, |
| 128 | + () => |
| 129 | + execa( |
| 130 | + 'prettier', |
| 131 | + [ |
| 132 | + `${location}/**/*.{js,json,md}`, |
| 133 | + `${location}/.eslintrc`, |
| 134 | + '--write', |
| 135 | + ], |
| 136 | + { cwd: location }, |
| 137 | + ).catch(ignore), |
| 138 | + () => execa('yarn', ['install'], { cwd: location, stdio: 'inherit' }), |
| 139 | + () => |
| 140 | + execa('yarn', ['upgrade-interactive', '--latest'], { |
| 141 | + cwd: location, |
| 142 | + stdio: 'inherit', |
| 143 | + }), |
| 144 | + |
| 145 | + ({ semanticRelease }) => { |
| 146 | + if (semanticRelease) { |
| 147 | + console.log( |
| 148 | + '\nRun `npx semantic-release-cli setup` after pushing to github for the first time to setup semantic release\n', |
| 149 | + ); |
| 150 | + } else { |
| 151 | + console.log('Done!'); |
| 152 | + } |
| 153 | + }, |
| 154 | + ]; |
| 155 | + }, |
| 156 | + }); |
| 157 | +}; |
0 commit comments