Skip to content

Commit ecd68c4

Browse files
authored
Automatically install Dependencies and fix lint errors (#1133)
* Batman! * removed special airbnb template strings since we can auto-fix them now. * fix string bug * remove last airbnb template string * * Make autoinstall optional * run lint-fix for airbnb AND standard * print message if user has to manually lint-fix * webpack.conf.js is not needed in jest and e2e (#1135) * fix filename of `.eslintignore` (#1136) * Batman! * removed special airbnb template strings since we can auto-fix them now. * fix string bug * remove last airbnb template string * * Make autoinstall optional * run lint-fix for airbnb AND standard * print message if user has to manually lint-fix * used chalk to color up console logs * add option for yarn * change double quotes to single quotes, remove where possible * generalize log message
1 parent 5751131 commit ecd68c4

File tree

12 files changed

+313
-181
lines changed

12 files changed

+313
-181
lines changed

meta.js

+124-101
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,169 @@
11
const path = require('path');
22
const fs = require('fs');
3-
4-
function sortObject(object) {
5-
// Based on https://github.com/yarnpkg/yarn/blob/v1.3.2/src/config.js#L79-L85
6-
const sortedObject = {};
7-
Object.keys(object).sort().forEach(item => {
8-
sortedObject[item] = object[item];
9-
});
10-
return sortedObject;
11-
}
3+
const {
4+
sortDependencies,
5+
installDependencies,
6+
runLintFix,
7+
printMessage
8+
} = require('./utils')
129

1310
module.exports = {
14-
"helpers": {
15-
"if_or": function (v1, v2, options) {
11+
helpers: {
12+
if_or: function (v1, v2, options) {
1613
if (v1 || v2) {
1714
return options.fn(this);
1815
}
1916

2017
return options.inverse(this);
2118
}
2219
},
23-
"prompts": {
24-
"name": {
25-
"type": "string",
26-
"required": true,
27-
"message": "Project name"
20+
prompts: {
21+
name: {
22+
type: 'string',
23+
required: true,
24+
message: 'Project name'
2825
},
29-
"description": {
30-
"type": "string",
31-
"required": false,
32-
"message": "Project description",
33-
"default": "A Vue.js project"
26+
description: {
27+
type: 'string',
28+
required: false,
29+
message: 'Project description',
30+
default: 'A Vue.js project'
3431
},
35-
"author": {
36-
"type": "string",
37-
"message": "Author"
32+
author: {
33+
type: 'string',
34+
message: 'Author'
3835
},
39-
"build": {
40-
"type": "list",
41-
"message": "Vue build",
42-
"choices": [
36+
build: {
37+
type: 'list',
38+
message: 'Vue build',
39+
choices: [
4340
{
44-
"name": "Runtime + Compiler: recommended for most users",
45-
"value": "standalone",
46-
"short": "standalone"
41+
name: 'Runtime + Compiler: recommended for most users',
42+
value: 'standalone',
43+
short: 'standalone'
4744
},
4845
{
49-
"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",
50-
"value": "runtime",
51-
"short": "runtime"
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',
47+
value: 'runtime',
48+
short: 'runtime'
5249
}
5350
]
5451
},
55-
"router": {
56-
"type": "confirm",
57-
"message": "Install vue-router?"
52+
router: {
53+
type: 'confirm',
54+
message: 'Install vue-router?'
5855
},
59-
"lint": {
60-
"type": "confirm",
61-
"message": "Use ESLint to lint your code?"
56+
lint: {
57+
type: 'confirm',
58+
message: 'Use ESLint to lint your code?'
6259
},
63-
"lintConfig": {
64-
"when": "lint",
65-
"type": "list",
66-
"message": "Pick an ESLint preset",
67-
"choices": [
60+
lintConfig: {
61+
when: 'lint',
62+
type: 'list',
63+
message: 'Pick an ESLint preset',
64+
choices: [
6865
{
69-
"name": "Standard (https://github.com/standard/standard)",
70-
"value": "standard",
71-
"short": "Standard"
66+
name: 'Standard (https://github.com/standard/standard)',
67+
value: 'standard',
68+
short: 'Standard'
7269
},
7370
{
74-
"name": "Airbnb (https://github.com/airbnb/javascript)",
75-
"value": "airbnb",
76-
"short": "Airbnb"
71+
name: 'Airbnb (https://github.com/airbnb/javascript)',
72+
value: 'airbnb',
73+
short: 'Airbnb'
7774
},
7875
{
79-
"name": "none (configure it yourself)",
80-
"value": "none",
81-
"short": "none"
76+
name: 'none (configure it yourself)',
77+
value: 'none',
78+
short: 'none'
8279
}
8380
]
8481
},
85-
"unit": {
86-
"type": "confirm",
87-
"message": "Set up unit tests"
82+
unit: {
83+
type: 'confirm',
84+
message: 'Set up unit tests'
8885
},
89-
"runner": {
90-
"when": "unit",
91-
"type": "list",
92-
"message": "Pick a test runner",
93-
"choices": [
86+
runner: {
87+
when: 'unit',
88+
type: 'list',
89+
message: 'Pick a test runner',
90+
choices: [
9491
{
95-
"name": "Jest",
96-
"value": "jest",
97-
"short": "jest"
92+
name: 'Jest',
93+
value: 'jest',
94+
short: 'jest'
9895
},
9996
{
100-
"name": "Karma and Mocha",
101-
"value": "karma",
102-
"short": "karma"
97+
name: 'Karma and Mocha',
98+
value: 'karma',
99+
short: 'karma'
103100
},
104101
{
105-
"name": "none (configure it yourself)",
106-
"value": "noTest",
107-
"short": "noTest"
102+
name: 'none (configure it yourself)',
103+
value: 'noTest',
104+
short: 'noTest'
108105
}
109106
]
110107
},
111-
"e2e": {
112-
"type": "confirm",
113-
"message": "Setup e2e tests with Nightwatch?"
108+
e2e: {
109+
type: 'confirm',
110+
message: 'Setup e2e tests with Nightwatch?'
111+
},
112+
autoInstall: {
113+
type: 'list',
114+
message: 'Should we run `npm install` for you after the project has been created? (recommended)',
115+
choices: [
116+
{
117+
name: 'Yes, use NPM',
118+
value: 'npm',
119+
short: 'npm'
120+
},
121+
{
122+
name: 'Yes, use Yarn',
123+
value: 'yarn',
124+
short: 'yarn'
125+
},
126+
{
127+
name: 'No, I will handle that myself',
128+
value: false,
129+
short: 'no'
130+
}
131+
]
114132
}
115133
},
116-
"filters": {
117-
".eslintrc.js": "lint",
118-
".eslintignore": "lint",
119-
"config/test.env.js": "unit || e2e",
120-
"build/webpack.test.conf.js": "unit && runner === 'karma'",
121-
"test/unit/**/*": "unit",
122-
"test/unit/index.js": "unit && runner === 'karma'",
123-
"test/unit/jest.conf.js": "unit && runner === 'jest'",
124-
"test/unit/karma.conf.js": "unit && runner === 'karma'",
125-
"test/unit/specs/index.js": "unit && runner === 'karma'",
126-
"test/unit/setup.js": "unit && runner === 'jest'",
127-
"test/e2e/**/*": "e2e",
128-
"src/router/**/*": "router"
134+
filters: {
135+
'.eslintrc.js': 'lint',
136+
'.eslintignore': 'lint',
137+
'config/test.env.js': 'unit || e2e',
138+
'build/webpack.test.conf.js': "unit && runner === 'karma'",
139+
'test/unit/**/*': 'unit',
140+
'test/unit/index.js': "unit && runner === 'karma'",
141+
'test/unit/jest.conf.js': "unit && runner === 'jest'",
142+
'test/unit/karma.conf.js': "unit && runner === 'karma'",
143+
'test/unit/specs/index.js': "unit && runner === 'karma'",
144+
'test/unit/setup.js': "unit && runner === 'jest'",
145+
'test/e2e/**/*': 'e2e',
146+
'src/router/**/*': 'router'
129147
},
130-
"complete": function (data) {
131-
const packageJsonFile = path.join(
132-
data.inPlace ? "" : data.destDirName,
133-
"package.json"
134-
);
135-
const packageJson = JSON.parse(fs.readFileSync(packageJsonFile));
136-
packageJson.devDependencies = sortObject(packageJson.devDependencies);
137-
packageJson.dependencies = sortObject(packageJson.dependencies);
138-
fs.writeFileSync(
139-
packageJsonFile,
140-
JSON.stringify(packageJson, null, 2) + "\n"
141-
);
148+
'complete': function (data, { chalk }) {
149+
150+
const green = chalk.green
142151

143-
const message = `To get started:\n\n ${data.inPlace ? '' : `cd ${data.destDirName}\n `}npm install\n npm run dev\n\nDocumentation can be found at https://vuejs-templates.github.io/webpack`;
144-
console.log("\n" + message.split(/\r?\n/g).map(line => " " + line).join("\n"));
152+
sortDependencies(data, green)
153+
154+
const cwd = path.join(process.cwd(), data.inPlace ? '' : data.destDirName)
155+
156+
if (data.autoInstall) {
157+
installDependencies(cwd, data.autoInstall, green)
158+
.then(() => {
159+
return runLintFix(cwd, data, green)
160+
})
161+
.then(() => {
162+
printMessage(data, green)
163+
})
164+
} else {
165+
printMessage(data, chalk)
166+
}
167+
145168
}
146169
};

template/src/App.vue

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
<script>
1313
{{#unless router}}
14-
import HelloWorld from './components/HelloWorld'{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
14+
import HelloWorld from './components/HelloWorld'
1515
1616
{{/unless}}
1717
export default {
18-
name: 'app'{{#router}}{{#if_eq lintConfig "airbnb"}},{{/if_eq}}{{else}},
18+
name: 'app'{{#router}}{{else}},
1919
components: {
20-
HelloWorld{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
21-
}{{#if_eq lintConfig "airbnb"}},{{/if_eq}}{{/router}}
22-
}{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
20+
HelloWorld
21+
}{{/router}}
22+
}
2323
</script>
2424

2525
<style>

template/src/components/HelloWorld.vue

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
<script>
2424
export default {
2525
name: 'HelloWorld',
26-
data{{#unless_eq lintConfig "airbnb"}} {{/unless_eq}}() {
26+
data () {
2727
return {
28-
msg: 'Welcome to Your Vue.js App'{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
29-
}{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
30-
}{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
31-
}{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
28+
msg: 'Welcome to Your Vue.js App'
29+
}
30+
}
31+
}
3232
</script>
3333

3434
<!-- Add "scoped" attribute to limit CSS to this component only -->

template/src/main.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// The Vue build version to load with the `import` command
33
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
44
{{/if_eq}}
5-
import Vue from 'vue'{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
6-
import App from './App'{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
5+
import Vue from 'vue'
6+
import App from './App'
77
{{#router}}
8-
import router from './router'{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
8+
import router from './router'
99
{{/router}}
1010

11-
Vue.config.productionTip = false{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
11+
Vue.config.productionTip = false
1212

1313
/* eslint-disable no-new */
1414
new Vue({
@@ -17,10 +17,10 @@ new Vue({
1717
router,
1818
{{/router}}
1919
{{#if_eq build "runtime"}}
20-
render: h => h(App){{#if_eq lintConfig "airbnb"}},{{/if_eq}}
20+
render: h => h(App)
2121
{{/if_eq}}
2222
{{#if_eq build "standalone"}}
2323
template: '<App/>',
24-
components: { App }{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
24+
components: { App }
2525
{{/if_eq}}
26-
}){{#if_eq lintConfig "airbnb"}};{{/if_eq}}
26+
})

template/src/router/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import Vue from 'vue'{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
2-
import Router from 'vue-router'{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
3-
import HelloWorld from '@/components/HelloWorld'{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
1+
import Vue from 'vue'
2+
import Router from 'vue-router'
3+
import HelloWorld from '@/components/HelloWorld'
44

5-
Vue.use(Router){{#if_eq lintConfig "airbnb"}};{{/if_eq}}
5+
Vue.use(Router)
66

77
export default new Router({
88
routes: [
99
{
1010
path: '/',
1111
name: 'HelloWorld',
12-
component: HelloWorld{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
13-
}{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
14-
]{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
15-
}){{#if_eq lintConfig "airbnb"}};{{/if_eq}}
12+
component: HelloWorld
13+
}
14+
]
15+
})

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
// for how to write custom assertions see
88
// http://nightwatchjs.org/guide#writing-custom-assertions
99
exports.assertion = function (selector, count) {
10-
this.message = 'Testing if element <' + selector + '> has count: ' + count{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
11-
this.expected = count{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
10+
this.message = 'Testing if element <' + selector + '> has count: ' + count
11+
this.expected = count
1212
this.pass = function (val) {
13-
return val === this.expected{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
13+
return val === this.expected
1414
}
1515
this.value = function (res) {
16-
return res.value{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
16+
return res.value
1717
}
1818
this.command = function (cb) {
19-
var self = this{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
19+
var self = this
2020
return this.api.execute(function (selector) {
21-
return document.querySelectorAll(selector).length{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
21+
return document.querySelectorAll(selector).length
2222
}, [selector], function (res) {
23-
cb.call(self, res){{#if_eq lintConfig "airbnb"}};{{/if_eq}}
24-
}){{#if_eq lintConfig "airbnb"}};{{/if_eq}}
23+
cb.call(self, res)
24+
})
2525
}
2626
}

0 commit comments

Comments
 (0)