Skip to content

[WIP] Accurate CompleteMsg when not using autoInstall (fix #1157) #1158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 52 additions & 49 deletions meta.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
const path = require('path');
const fs = require('fs');
const path = require('path')
const fs = require('fs')
const {
sortDependencies,
installDependencies,
runLintFix,
printMessage
printMessage,
} = require('./utils')

module.exports = {
helpers: {
if_or: function (v1, v2, options) {
if_or: function(v1, v2, options) {
if (v1 || v2) {
return options.fn(this);
return options.fn(this)
}

return options.inverse(this);
}
return options.inverse(this)
},
},
prompts: {
name: {
type: 'string',
required: true,
message: 'Project name'
message: 'Project name',
},
description: {
type: 'string',
required: false,
message: 'Project description',
default: 'A Vue.js project'
default: 'A Vue.js project',
},
author: {
type: 'string',
message: 'Author'
message: 'Author',
},
build: {
type: 'list',
Expand All @@ -40,22 +40,23 @@ module.exports = {
{
name: 'Runtime + Compiler: recommended for most users',
value: 'standalone',
short: 'standalone'
short: 'standalone',
},
{
name: 'Runtime-only: about 6KB lighter min+gzip, but templates (or any Vue-specific HTML) are ONLY allowed in .vue files - render functions are required elsewhere',
name:
'Runtime-only: about 6KB lighter min+gzip, but templates (or any Vue-specific HTML) are ONLY allowed in .vue files - render functions are required elsewhere',
value: 'runtime',
short: 'runtime'
}
]
short: 'runtime',
},
],
},
router: {
type: 'confirm',
message: 'Install vue-router?'
message: 'Install vue-router?',
},
lint: {
type: 'confirm',
message: 'Use ESLint to lint your code?'
message: 'Use ESLint to lint your code?',
},
lintConfig: {
when: 'lint',
Expand All @@ -65,23 +66,23 @@ module.exports = {
{
name: 'Standard (https://github.com/standard/standard)',
value: 'standard',
short: 'Standard'
short: 'Standard',
},
{
name: 'Airbnb (https://github.com/airbnb/javascript)',
value: 'airbnb',
short: 'Airbnb'
short: 'Airbnb',
},
{
name: 'none (configure it yourself)',
value: 'none',
short: 'none'
}
]
short: 'none',
},
],
},
unit: {
type: 'confirm',
message: 'Set up unit tests'
message: 'Set up unit tests',
},
runner: {
when: 'unit',
Expand All @@ -91,45 +92,46 @@ module.exports = {
{
name: 'Jest',
value: 'jest',
short: 'jest'
short: 'jest',
},
{
name: 'Karma and Mocha',
value: 'karma',
short: 'karma'
short: 'karma',
},
{
name: 'none (configure it yourself)',
value: 'noTest',
short: 'noTest'
}
]
short: 'noTest',
},
],
},
e2e: {
type: 'confirm',
message: 'Setup e2e tests with Nightwatch?'
message: 'Setup e2e tests with Nightwatch?',
},
autoInstall: {
type: 'list',
message: 'Should we run `npm install` for you after the project has been created? (recommended)',
message:
'Should we run `npm install` for you after the project has been created? (recommended)',
choices: [
{
name: 'Yes, use NPM',
value: 'npm',
short: 'npm'
short: 'npm',
},
{
name: 'Yes, use Yarn',
value: 'yarn',
short: 'yarn'
short: 'yarn',
},
{
name: 'No, I will handle that myself',
value: false,
short: 'no'
}
]
}
short: 'no',
},
],
},
},
filters: {
'.eslintrc.js': 'lint',
Expand All @@ -143,27 +145,28 @@ module.exports = {
'test/unit/specs/index.js': "unit && runner === 'karma'",
'test/unit/setup.js': "unit && runner === 'jest'",
'test/e2e/**/*': 'e2e',
'src/router/**/*': 'router'
'src/router/**/*': 'router',
},
'complete': function (data, { chalk }) {

complete: function(data, { chalk }) {
const green = chalk.green

sortDependencies(data, green)

const cwd = path.join(process.cwd(), data.inPlace ? '' : data.destDirName)

if (data.autoInstall) {
installDependencies(cwd, data.autoInstall, green)
.then(() => {
return runLintFix(cwd, data, green)
})
.then(() => {
printMessage(data, green)
})
.then(() => {
return runLintFix(cwd, data, green)
})
.then(() => {
printMessage(data, green)
})
.catch(e => {
console.log(chalk.red('Error:'), e)
})
} else {
printMessage(data, chalk)
}

}
};
},
}
7 changes: 4 additions & 3 deletions template/test/e2e/custom-assertions/elementCount.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// A custom Nightwatch assertion.
// the name of the method is the filename.
// can be used in tests like this:
// The assertion name is the filename.
// Example usage:
//
// browser.assert.elementCount(selector, count)
//
// for how to write custom assertions see
// For more information on custom assertions see:
// http://nightwatchjs.org/guide#writing-custom-assertions

exports.assertion = function (selector, count) {
this.message = 'Testing if element <' + selector + '> has count: ' + count
this.expected = count
Expand Down
90 changes: 63 additions & 27 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@ const lintStyles = ['standard', 'airbnb']
*/
exports.sortDependencies = function sortDependencies(data) {
const packageJsonFile = path.join(
data.inPlace ? "" : data.destDirName,
"package.json"
data.inPlace ? '' : data.destDirName,
'package.json'
)
const packageJson = JSON.parse(fs.readFileSync(packageJsonFile))
packageJson.devDependencies = sortObject(packageJson.devDependencies)
packageJson.dependencies = sortObject(packageJson.dependencies)
fs.writeFileSync(
packageJsonFile,
JSON.stringify(packageJson, null, 2) + "\n"
);
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson, null, 2) + '\n')
}

/**
* Runs `npm install` in the project directory
* @param {string} cwd Path of the created project directory
* @param {object} data Data from questionnaire
*/
exports.installDependencies = function installDependencies(cwd, executable = 'npm', color) {
exports.installDependencies = function installDependencies(
cwd,
executable = 'npm',
color
) {
console.log(`\n\n# ${color('Installing project dependencies ...')}`)
console.log('# ========================\n')
return runCommand(executable, ['install'], {
cwd
cwd,
})
}

Expand All @@ -43,10 +44,18 @@ exports.installDependencies = function installDependencies(cwd, executable = 'np
*/
exports.runLintFix = function runLintFix(cwd, data, color) {
if (data.lint && lintStyles.indexOf(data.lintConfig) !== -1) {
console.log(`\n\n${color('Running eslint --fix to comply with chosen preset rules...')}`)
console.log(
`\n\n${color(
'Running eslint --fix to comply with chosen preset rules...'
)}`
)
console.log('# ========================\n')
return runCommand('npm', ['run', 'lint', '--', '--fix'], {
cwd
const args =
data.autoInstall === 'npm'
? ['run', 'lint', '--', '--fix']
: ['run', 'lint', '--fix']
return runCommand(data.autoInstall, args, {
cwd,
})
}
return Promise.resolve()
Expand All @@ -63,35 +72,60 @@ exports.printMessage = function printMessage(data, { green, yellow }) {

To get started:

${yellow(`${data.inPlace ? '' : `cd ${data.destDirName}\n `}${requiresLint(data) ? 'npm run lint -- --fix\n ' : ''}npm run dev`)}
${yellow(
`${data.inPlace ? '' : `cd ${data.destDirName}\n `}${installMsg(
data
)}${lintMsg(data)}npm run dev`
)}

Documentation can be found at https://vuejs-templates.github.io/webpack
`
console.log(message)
}

/**
* Returns true if the user will have to run lint --fix themselves.
* If the user will have to run lint --fix themselves, it returns a string
* containing the instruction for this step.
* @param {Object} data Data from questionnaire.
*/
function requiresLint(data) {
return !data.autoInstall && data.lint && lintStyles.indexOf(data.lintConfig) !== -1
function lintMsg(data) {
return !data.autoInstall &&
data.lint &&
lintStyles.indexOf(data.lintConfig) !== -1
? 'npm run lint -- --fix (or for yarn: yarn run lint --fix)\n '
: ''
}

/**
* If the user will have to run `npm install` or `yarn` themselves, it returns a string
* containing the instruction for this step.
* @param {Object} data Data from the questionnaire
*/
function installMsg(data) {
return !data.autoInstall ? 'npm install (or if using yarn: yarn)\n ' : ''
}

/**
* Spawns a child process and runs the specified command
* By default, runs in the CWD and inherits stdio
* Options are the same as node's child_process.spawn
* @param {string} cmd
* @param {array<string>} args
* @param {string} cmd
* @param {array<string>} args
* @param {object} options
*/
function runCommand(cmd, args, options) {
return new Promise((resolve, reject) => {
const spwan = spawn(cmd, args, Object.assign({
cwd: process.cwd(),
stdio: 'inherit',
}, options))
const spwan = spawn(
cmd,
args,
Object.assign(
{
cwd: process.cwd(),
stdio: 'inherit',
},
options
)
)

spwan.on('exit', () => {
resolve()
Expand All @@ -101,9 +135,11 @@ function runCommand(cmd, args, options) {

function sortObject(object) {
// Based on https://github.com/yarnpkg/yarn/blob/v1.3.2/src/config.js#L79-L85
const sortedObject = {};
Object.keys(object).sort().forEach(item => {
sortedObject[item] = object[item];
});
return sortedObject;
}
const sortedObject = {}
Object.keys(object)
.sort()
.forEach(item => {
sortedObject[item] = object[item]
})
return sortedObject
}