Skip to content

Commit

Permalink
feat: determine if yarn is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
KuangPF committed May 27, 2019
1 parent 2da4830 commit 9f03964
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Expand Up @@ -25,13 +25,15 @@
],
"arrow-parens": ["error", "as-needed"],
"arrow-body-style": "off",
"no-underscore-dangle":"off",
"no-else-return": [
"error",
{
"allowElseIf": true
}
],
"max-len": ["error", { "code": 200 }],
"no-param-reassign": "off"
"no-param-reassign": "off",
"no-return-assign":["error"]
}
}
40 changes: 35 additions & 5 deletions lib/Creator.js
Expand Up @@ -6,6 +6,7 @@ const { clearConsole } = require('./util/clearConsole')
const { render } = require('./util/render')
const writeFileTree = require('./util/writeFileTree')
const { log } = require('./util/logger')
const { hasYarn } = require('./util/hasYarn')

module.exports = class Creator {
constructor(name, context, promptModules) {
Expand All @@ -14,9 +15,9 @@ module.exports = class Creator {
const { featurePrompt } = this.resolveIntroPrompts()
this.featurePrompt = featurePrompt
this.injectedPrompts = [] // prompts
this.outroPrompts = this.resolveOutroPrompts() // packageManager

const promptAPI = new PromptModuleAPI(this)

promptModules.forEach(m => m(promptAPI))
}

Expand All @@ -29,18 +30,22 @@ module.exports = class Creator {
cssPreprocessor: 'sass',
eslintConfig: 'base',
stylelintConfig: 'recessOrder',
name: 'demo'
name: 'demo',
packageManager: 'yarn'
}
} else {
preset = await this.promptAndResolvePreset()
}
const file = await render('./template', Object.assign(preset, { name: this.name }))
const finalFile = await this.extractFinalFiles(preset, file)
await writeFileTree(this.context, finalFile)

const packageManager = (preset.packageManager || (hasYarn() ? 'yarn' : 'npm'))
log()
log(`🎉 Successfully created project ${chalk.yellow(this.name)}.`)
log(`👉 Get started with the following commands:\n\n${this.context === process.cwd() ? '' : chalk.cyan(` ${chalk.gray('$')} cd ${this.name}\n`)}`)
log(`👉 Get started with the following commands:\n\n
${this.context === process.cwd() ? '' : chalk.cyan(` ${chalk.gray('$')} cd ${this.name}\n`)}
${chalk.cyan(` ${chalk.gray('$')} ${packageManager === 'yarn' ? 'yarn' : 'npm install'}\n`)}
${chalk.cyan(` ${chalk.gray('$')} ${packageManager === 'yarn' ? 'yarn dev' : 'npm run dev'}`)}`)
log()
}

Expand All @@ -60,7 +65,7 @@ module.exports = class Creator {
return originalWhen(answers)
}
})
const prompts = [this.featurePrompt, ...this.injectedPrompts]
const prompts = [this.featurePrompt, ...this.injectedPrompts, ...this.outroPrompts]
return prompts
}

Expand All @@ -79,6 +84,31 @@ module.exports = class Creator {
}
}

// eslint-disable-next-line
resolveOutroPrompts() {
if (hasYarn()) {
const outroPrompts = [
{
name: 'packageManager',
type: 'list',
message: 'Pick the package manager to use when installing dependencies:',
choices: [
{
name: 'Use Yarn',
value: 'yarn',
short: 'Yarn'
},
{
name: 'Use NPM',
value: 'npm',
short: 'NPM'
}
]
}
]
return outroPrompts
}
}
// eslint-disable-next-line
async extractFinalFiles(preset, file) {
// delete .eslintrc and .eslintignore files if the features not include eslint
Expand Down
20 changes: 0 additions & 20 deletions lib/promptModules/base.js

This file was deleted.

13 changes: 13 additions & 0 deletions lib/util/hasYarn.js
@@ -0,0 +1,13 @@
const { execSync } = require('child_process')

exports.hasYarn = () => {
if (process.env.WEBPACK_PAGE_CLI_TEST) {
return true
}
try {
execSync('yarnpkg --version', { stdio: 'ignore' })
return true
} catch (e) {
return false
}
}

0 comments on commit 9f03964

Please sign in to comment.