Skip to content

Commit d069cd6

Browse files
authored
[WIP] Accurate CompleteMsg when not using autoInstall (fix vuejs-templates#1157) (vuejs-templates#1158)
* try and fix vuejs-templates#1157
1 parent 3b82f40 commit d069cd6

File tree

3 files changed

+119
-79
lines changed

3 files changed

+119
-79
lines changed

meta.js

+52-49
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
const path = require('path');
2-
const fs = require('fs');
1+
const path = require('path')
2+
const fs = require('fs')
33
const {
44
sortDependencies,
55
installDependencies,
66
runLintFix,
7-
printMessage
7+
printMessage,
88
} = require('./utils')
99

1010
module.exports = {
1111
helpers: {
12-
if_or: function (v1, v2, options) {
12+
if_or: function(v1, v2, options) {
1313
if (v1 || v2) {
14-
return options.fn(this);
14+
return options.fn(this)
1515
}
1616

17-
return options.inverse(this);
18-
}
17+
return options.inverse(this)
18+
},
1919
},
2020
prompts: {
2121
name: {
2222
type: 'string',
2323
required: true,
24-
message: 'Project name'
24+
message: 'Project name',
2525
},
2626
description: {
2727
type: 'string',
2828
required: false,
2929
message: 'Project description',
30-
default: 'A Vue.js project'
30+
default: 'A Vue.js project',
3131
},
3232
author: {
3333
type: 'string',
34-
message: 'Author'
34+
message: 'Author',
3535
},
3636
build: {
3737
type: 'list',
@@ -40,22 +40,23 @@ module.exports = {
4040
{
4141
name: 'Runtime + Compiler: recommended for most users',
4242
value: 'standalone',
43-
short: 'standalone'
43+
short: 'standalone',
4444
},
4545
{
46-
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',
46+
name:
47+
'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',
4748
value: 'runtime',
48-
short: 'runtime'
49-
}
50-
]
49+
short: 'runtime',
50+
},
51+
],
5152
},
5253
router: {
5354
type: 'confirm',
54-
message: 'Install vue-router?'
55+
message: 'Install vue-router?',
5556
},
5657
lint: {
5758
type: 'confirm',
58-
message: 'Use ESLint to lint your code?'
59+
message: 'Use ESLint to lint your code?',
5960
},
6061
lintConfig: {
6162
when: 'lint',
@@ -65,23 +66,23 @@ module.exports = {
6566
{
6667
name: 'Standard (https://github.com/standard/standard)',
6768
value: 'standard',
68-
short: 'Standard'
69+
short: 'Standard',
6970
},
7071
{
7172
name: 'Airbnb (https://github.com/airbnb/javascript)',
7273
value: 'airbnb',
73-
short: 'Airbnb'
74+
short: 'Airbnb',
7475
},
7576
{
7677
name: 'none (configure it yourself)',
7778
value: 'none',
78-
short: 'none'
79-
}
80-
]
79+
short: 'none',
80+
},
81+
],
8182
},
8283
unit: {
8384
type: 'confirm',
84-
message: 'Set up unit tests'
85+
message: 'Set up unit tests',
8586
},
8687
runner: {
8788
when: 'unit',
@@ -91,45 +92,46 @@ module.exports = {
9192
{
9293
name: 'Jest',
9394
value: 'jest',
94-
short: 'jest'
95+
short: 'jest',
9596
},
9697
{
9798
name: 'Karma and Mocha',
9899
value: 'karma',
99-
short: 'karma'
100+
short: 'karma',
100101
},
101102
{
102103
name: 'none (configure it yourself)',
103104
value: 'noTest',
104-
short: 'noTest'
105-
}
106-
]
105+
short: 'noTest',
106+
},
107+
],
107108
},
108109
e2e: {
109110
type: 'confirm',
110-
message: 'Setup e2e tests with Nightwatch?'
111+
message: 'Setup e2e tests with Nightwatch?',
111112
},
112113
autoInstall: {
113114
type: 'list',
114-
message: 'Should we run `npm install` for you after the project has been created? (recommended)',
115+
message:
116+
'Should we run `npm install` for you after the project has been created? (recommended)',
115117
choices: [
116118
{
117119
name: 'Yes, use NPM',
118120
value: 'npm',
119-
short: 'npm'
121+
short: 'npm',
120122
},
121123
{
122124
name: 'Yes, use Yarn',
123125
value: 'yarn',
124-
short: 'yarn'
126+
short: 'yarn',
125127
},
126128
{
127129
name: 'No, I will handle that myself',
128130
value: false,
129-
short: 'no'
130-
}
131-
]
132-
}
131+
short: 'no',
132+
},
133+
],
134+
},
133135
},
134136
filters: {
135137
'.eslintrc.js': 'lint',
@@ -143,27 +145,28 @@ module.exports = {
143145
'test/unit/specs/index.js': "unit && runner === 'karma'",
144146
'test/unit/setup.js': "unit && runner === 'jest'",
145147
'test/e2e/**/*': 'e2e',
146-
'src/router/**/*': 'router'
148+
'src/router/**/*': 'router',
147149
},
148-
'complete': function (data, { chalk }) {
149-
150+
complete: function(data, { chalk }) {
150151
const green = chalk.green
151152

152153
sortDependencies(data, green)
153154

154155
const cwd = path.join(process.cwd(), data.inPlace ? '' : data.destDirName)
155-
156+
156157
if (data.autoInstall) {
157158
installDependencies(cwd, data.autoInstall, green)
158-
.then(() => {
159-
return runLintFix(cwd, data, green)
160-
})
161-
.then(() => {
162-
printMessage(data, green)
163-
})
159+
.then(() => {
160+
return runLintFix(cwd, data, green)
161+
})
162+
.then(() => {
163+
printMessage(data, green)
164+
})
165+
.catch(e => {
166+
console.log(chalk.red('Error:'), e)
167+
})
164168
} else {
165169
printMessage(data, chalk)
166170
}
167-
168-
}
169-
};
171+
},
172+
}

template/test/e2e/custom-assertions/elementCount.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// A custom Nightwatch assertion.
2-
// the name of the method is the filename.
3-
// can be used in tests like this:
2+
// The assertion name is the filename.
3+
// Example usage:
44
//
55
// browser.assert.elementCount(selector, count)
66
//
7-
// for how to write custom assertions see
7+
// For more information on custom assertions see:
88
// http://nightwatchjs.org/guide#writing-custom-assertions
9+
910
exports.assertion = function (selector, count) {
1011
this.message = 'Testing if element <' + selector + '> has count: ' + count
1112
this.expected = count

utils/index.js

+63-27
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,29 @@ const lintStyles = ['standard', 'airbnb']
1111
*/
1212
exports.sortDependencies = function sortDependencies(data) {
1313
const packageJsonFile = path.join(
14-
data.inPlace ? "" : data.destDirName,
15-
"package.json"
14+
data.inPlace ? '' : data.destDirName,
15+
'package.json'
1616
)
1717
const packageJson = JSON.parse(fs.readFileSync(packageJsonFile))
1818
packageJson.devDependencies = sortObject(packageJson.devDependencies)
1919
packageJson.dependencies = sortObject(packageJson.dependencies)
20-
fs.writeFileSync(
21-
packageJsonFile,
22-
JSON.stringify(packageJson, null, 2) + "\n"
23-
);
20+
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson, null, 2) + '\n')
2421
}
2522

2623
/**
2724
* Runs `npm install` in the project directory
2825
* @param {string} cwd Path of the created project directory
2926
* @param {object} data Data from questionnaire
3027
*/
31-
exports.installDependencies = function installDependencies(cwd, executable = 'npm', color) {
28+
exports.installDependencies = function installDependencies(
29+
cwd,
30+
executable = 'npm',
31+
color
32+
) {
3233
console.log(`\n\n# ${color('Installing project dependencies ...')}`)
3334
console.log('# ========================\n')
3435
return runCommand(executable, ['install'], {
35-
cwd
36+
cwd,
3637
})
3738
}
3839

@@ -43,10 +44,18 @@ exports.installDependencies = function installDependencies(cwd, executable = 'np
4344
*/
4445
exports.runLintFix = function runLintFix(cwd, data, color) {
4546
if (data.lint && lintStyles.indexOf(data.lintConfig) !== -1) {
46-
console.log(`\n\n${color('Running eslint --fix to comply with chosen preset rules...')}`)
47+
console.log(
48+
`\n\n${color(
49+
'Running eslint --fix to comply with chosen preset rules...'
50+
)}`
51+
)
4752
console.log('# ========================\n')
48-
return runCommand('npm', ['run', 'lint', '--', '--fix'], {
49-
cwd
53+
const args =
54+
data.autoInstall === 'npm'
55+
? ['run', 'lint', '--', '--fix']
56+
: ['run', 'lint', '--fix']
57+
return runCommand(data.autoInstall, args, {
58+
cwd,
5059
})
5160
}
5261
return Promise.resolve()
@@ -63,35 +72,60 @@ exports.printMessage = function printMessage(data, { green, yellow }) {
6372
6473
To get started:
6574
66-
${yellow(`${data.inPlace ? '' : `cd ${data.destDirName}\n `}${requiresLint(data) ? 'npm run lint -- --fix\n ' : ''}npm run dev`)}
75+
${yellow(
76+
`${data.inPlace ? '' : `cd ${data.destDirName}\n `}${installMsg(
77+
data
78+
)}${lintMsg(data)}npm run dev`
79+
)}
6780
6881
Documentation can be found at https://vuejs-templates.github.io/webpack
6982
`
7083
console.log(message)
7184
}
7285

7386
/**
74-
* Returns true if the user will have to run lint --fix themselves.
87+
* If the user will have to run lint --fix themselves, it returns a string
88+
* containing the instruction for this step.
7589
* @param {Object} data Data from questionnaire.
7690
*/
77-
function requiresLint(data) {
78-
return !data.autoInstall && data.lint && lintStyles.indexOf(data.lintConfig) !== -1
91+
function lintMsg(data) {
92+
return !data.autoInstall &&
93+
data.lint &&
94+
lintStyles.indexOf(data.lintConfig) !== -1
95+
? 'npm run lint -- --fix (or for yarn: yarn run lint --fix)\n '
96+
: ''
97+
}
98+
99+
/**
100+
* If the user will have to run `npm install` or `yarn` themselves, it returns a string
101+
* containing the instruction for this step.
102+
* @param {Object} data Data from the questionnaire
103+
*/
104+
function installMsg(data) {
105+
return !data.autoInstall ? 'npm install (or if using yarn: yarn)\n ' : ''
79106
}
80107

81108
/**
82109
* Spawns a child process and runs the specified command
83110
* By default, runs in the CWD and inherits stdio
84111
* Options are the same as node's child_process.spawn
85-
* @param {string} cmd
86-
* @param {array<string>} args
112+
* @param {string} cmd
113+
* @param {array<string>} args
87114
* @param {object} options
88115
*/
89116
function runCommand(cmd, args, options) {
90117
return new Promise((resolve, reject) => {
91-
const spwan = spawn(cmd, args, Object.assign({
92-
cwd: process.cwd(),
93-
stdio: 'inherit',
94-
}, options))
118+
const spwan = spawn(
119+
cmd,
120+
args,
121+
Object.assign(
122+
{
123+
cwd: process.cwd(),
124+
stdio: 'inherit',
125+
},
126+
options
127+
)
128+
)
95129

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

102136
function sortObject(object) {
103137
// Based on https://github.com/yarnpkg/yarn/blob/v1.3.2/src/config.js#L79-L85
104-
const sortedObject = {};
105-
Object.keys(object).sort().forEach(item => {
106-
sortedObject[item] = object[item];
107-
});
108-
return sortedObject;
109-
}
138+
const sortedObject = {}
139+
Object.keys(object)
140+
.sort()
141+
.forEach(item => {
142+
sortedObject[item] = object[item]
143+
})
144+
return sortedObject
145+
}

0 commit comments

Comments
 (0)