Skip to content

Commit 753b293

Browse files
committed
init
0 parents  commit 753b293

File tree

7 files changed

+367
-0
lines changed

7 files changed

+367
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
node_modules

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# vue-cli
2+
3+
A simple CLI for scaffolding Vue.js projects.
4+
5+
### Installation
6+
7+
``` bash
8+
$ npm install -g vue-cli
9+
```
10+
11+
### Usage
12+
13+
``` bash
14+
$ vue init webpack my-project
15+
$ cd my-project
16+
$ npm install
17+
$ npm run dev
18+
```
19+
20+
The above command pulls the template from [vuejs-templates/webpack](https://github.com/vuejs-templates/webpack), prompts for some information, and generates the project at `.my-project/`.
21+
22+
When a new template is added to the `vuejs-templates` organization, you will be able to run `vue init <template-name> <project-name>` to use that template.
23+
24+
You can run `vue list` to see all available official templates.
25+
26+
### Custom Templates
27+
28+
It's unlikely to make everyone happy with the official templates. You can simply fork an official template and then use it via `vue-cli` with:
29+
30+
``` bash
31+
vue init username/repo my-project
32+
```
33+
34+
Where `username/repo` is the GitHub repo shorthand for your fork.
35+
36+
You can also create your own template from scratch:
37+
38+
- A template repo **must** have a `template` directory that holds the template files.
39+
40+
- All template files will be piped through Handlebars for simple templating - `vue-cli` will automatically infer the prompts based on `{{}}` interpolations found in the files.
41+
42+
- A template repo **may** have a `meta.json` file that provides a schema for the prompts. The schema will be passed to [prompt-for](https://github.com/segmentio/prompt-for#prompt-for) as options. See [example](https://github.com/vuejs-templates/webpack/blob/master/meta.json).

bin/vue

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env node
2+
3+
var exists = require('fs').existsSync
4+
var join = require('path').join
5+
var logger = require('../lib/logger')
6+
var program = require('commander')
7+
var resolve = require('path').resolve
8+
var spawn = require('child_process').spawn
9+
var stat = require('fs').statSync
10+
11+
/**
12+
* Usage.
13+
*/
14+
15+
program
16+
.version(require('../package').version)
17+
.usage('<command> [options]')
18+
19+
/**
20+
* Help.
21+
*/
22+
23+
program.on('--help', function () {
24+
console.log(' Commands:')
25+
console.log()
26+
console.log(' init generate a new project from a template')
27+
console.log(' list list available official templates')
28+
console.log()
29+
})
30+
31+
/**
32+
* Parse.
33+
*/
34+
35+
program.parse(process.argv)
36+
if (!program.args.length) program.help()
37+
38+
/**
39+
* Padding.
40+
*/
41+
42+
console.log()
43+
process.on('exit', function () {
44+
console.log()
45+
})
46+
47+
/**
48+
* Settings.
49+
*/
50+
51+
var cmd = program.args[0]
52+
var args = process.argv.slice(3)
53+
var name = 'vue-' + cmd
54+
55+
/**
56+
* Resolve a local or remote executable.
57+
*/
58+
59+
var bin = join(__dirname, name)
60+
if (!exists(bin)) {
61+
bin = process.env.PATH.split(':').reduce(function (binary, path) {
62+
path = resolve(path, bin)
63+
return exists(path) && stat(path).isFile() ? path : binary
64+
}, bin)
65+
}
66+
67+
if (!exists(bin)) logger.fatal('The "%s" command does not exist.', name)
68+
69+
/**
70+
* Spawn a new, forwarded child process with the executable.
71+
*/
72+
73+
spawn(bin, args, { stdio: 'inherit' })
74+
.on('close', process.exit.bind(process))

bin/vue-init

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/usr/bin/env node
2+
3+
var Khaos = require('khaos')
4+
var metadata = require('read-metadata')
5+
var download = require('download-github-repo')
6+
var logger = require('../lib/logger')
7+
var program = require('commander')
8+
var exists = require('fs').existsSync
9+
var join = require('path').join
10+
var resolve = require('path').resolve
11+
var rm = require('rimraf').sync
12+
var uid = require('uid')
13+
14+
/**
15+
* Usage.
16+
*/
17+
18+
program
19+
.usage('<template> <project-name>')
20+
21+
/**
22+
* Help.
23+
*/
24+
25+
program.on('--help', function () {
26+
console.log(' Examples:')
27+
console.log()
28+
console.log(' # create a new project with an official template')
29+
console.log(' $ vue init webpack my-project')
30+
console.log()
31+
console.log(' # create a new project straight from a github template')
32+
console.log(' $ vue init username/repo my-project')
33+
console.log()
34+
})
35+
36+
/**
37+
* Help.
38+
*/
39+
40+
program.parse(process.argv)
41+
if (program.args.length < 2) return program.help()
42+
43+
/**
44+
* Padding.
45+
*/
46+
47+
console.log()
48+
process.on('exit', function () {
49+
console.log()
50+
})
51+
52+
/**
53+
* Settings.
54+
*/
55+
56+
var template = program.args[0]
57+
var name = program.args[1]
58+
var dir = program.directory
59+
var to = resolve(name)
60+
if (exists(to)) logger.fatal('"%s" already exists.', name)
61+
62+
/**
63+
* Detect official template.
64+
*/
65+
66+
if (!~template.indexOf('/')) {
67+
template = 'vuejs-templates/' + template
68+
}
69+
70+
/**
71+
* Download and generate.
72+
*/
73+
74+
var tmp = '/tmp/vue-template-' + uid()
75+
download(template, tmp, function (err) {
76+
if (err) logger.fatal(err)
77+
generate(tmp, to, function (err) {
78+
if (err) logger.fatal(err)
79+
rm(tmp)
80+
console.log()
81+
logger.success('Generated "%s".', name)
82+
})
83+
})
84+
85+
/**
86+
* Generate a template given a `src` and `dest`.
87+
*
88+
* @param {String} src
89+
* @param {String} dest
90+
* @param {Function} fn
91+
*/
92+
93+
function generate (src, dest, fn) {
94+
var template = join(src, 'template')
95+
var khaos = new Khaos(template)
96+
var opts = options(src)
97+
98+
// work around prompt-for bug...
99+
// which ignores default value for strings
100+
// otherwise we can just use khaos.generate :(
101+
khaos.schema(opts.schema)
102+
khaos.read(function (err, files) {
103+
if (err) logger.fatal(err)
104+
khaos.parse(files, function (err, schema) {
105+
if (err) logger.fatal(err)
106+
khaos.prompt(schema, function (err, answers) {
107+
if (err) logger.fatal(err)
108+
if (!answers.name) answers.name = name
109+
khaos.write(dest, files, answers, fn)
110+
})
111+
})
112+
})
113+
}
114+
115+
/**
116+
* Read prompts metadata.
117+
*
118+
* @param {String} dir
119+
* @return {Object}
120+
*/
121+
122+
function options (dir) {
123+
var file = join(dir, 'meta.json')
124+
var opts = exists(file)
125+
? metadata.sync(file)
126+
: {}
127+
defaultName(opts)
128+
return opts
129+
}
130+
131+
/**
132+
* Automatically infer the default project name
133+
*
134+
* @param {Object} opts
135+
*/
136+
137+
function defaultName (opts) {
138+
var schema = opts.schema || (opts.schema = {})
139+
if (!schema.name || typeof schema.name !== 'object') {
140+
schema.name = {
141+
'type': 'string',
142+
'default': name
143+
}
144+
} else {
145+
schema.name['default'] = name
146+
}
147+
}

bin/vue-list

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env node
2+
3+
var logger = require('../lib/logger')
4+
var request = require('request')
5+
var chalk = require('chalk')
6+
7+
request({
8+
url: 'https://api.github.com/users/vuejs-templates/repos',
9+
headers: {
10+
'User-Agent': 'vue-cli'
11+
}
12+
}, function (err, res, body) {
13+
if (err) logger.fatal(err)
14+
console.log(' Available official templates:')
15+
console.log()
16+
JSON.parse(body).forEach(function (repo) {
17+
console.log(' ' + chalk.yellow('★') + ' ' + chalk.blue(repo.name) + ' - ' + repo.description)
18+
})
19+
})

lib/logger.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var chalk = require('chalk')
2+
var format = require('util').format
3+
4+
/**
5+
* Prefix.
6+
*/
7+
8+
var prefix = ' vue-cli'
9+
var sep = chalk.gray('·')
10+
11+
/**
12+
* Log a `message` to the console.
13+
*
14+
* @param {String} message
15+
*/
16+
17+
exports.log = function () {
18+
var msg = format.apply(format, arguments)
19+
console.log(chalk.white(prefix), sep, msg)
20+
}
21+
22+
/**
23+
* Log an error `message` to the console and exit.
24+
*
25+
* @param {String} message
26+
*/
27+
28+
exports.fatal = function (message) {
29+
if (message instanceof Error) message = message.message.trim()
30+
var msg = format.apply(format, arguments)
31+
console.error(chalk.red(prefix), sep, msg)
32+
process.exit(1)
33+
}
34+
35+
/**
36+
* Log a success `message` to the console and exit.
37+
*
38+
* @param {String} message
39+
*/
40+
41+
exports.success = function () {
42+
var msg = format.apply(format, arguments)
43+
console.log(chalk.white(prefix), sep, msg)
44+
process.exit(0)
45+
}

package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "vue-cli",
3+
"version": "0.1.0",
4+
"description": "Opinionated CLI for starting Vue.js single page applications",
5+
"preferGlobal": true,
6+
"bin": {
7+
"vue": "bin/vue"
8+
},
9+
"scripts": {
10+
"test": "mocha"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/vuejs/vue-cli.git"
15+
},
16+
"keywords": [
17+
"vue",
18+
"cli",
19+
"spa"
20+
],
21+
"author": "Evan You",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/vuejs/vue-cli/issues"
25+
},
26+
"homepage": "https://github.com/vuejs/vue-cli#readme",
27+
"main": "lib/index.js",
28+
"dependencies": {
29+
"chalk": "^1.1.1",
30+
"commander": "^2.9.0",
31+
"download-github-repo": "^0.1.3",
32+
"khaos": "^0.9.3",
33+
"read-metadata": "^1.0.0",
34+
"request": "^2.67.0",
35+
"rimraf": "^2.5.0",
36+
"uid": "0.0.2"
37+
}
38+
}

0 commit comments

Comments
 (0)