-
Notifications
You must be signed in to change notification settings - Fork 0
/
cscratch.js
71 lines (54 loc) · 2.07 KB
/
cscratch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
module.exports = function createProject(callback) {
var fs = require('fs'),
path = require('path'),
program = require('commander');
var packageJSON = path.join(__dirname, 'package.json');
program
.version(JSON.parse(fs.readFileSync(packageJSON, {encoding: 'utf8'})).version)
.usage('[options] <dest>')
.option('-n, --name [project]', 'Project name')
.option('-d, --desc [description]', 'Project description')
.option('-r, --repo [repository]', 'Git repository link')
.option('-a, --author [author]', 'Author name')
.parse(process.argv);
if (program.args.length !== 1) {
program.help();
}
var dest = program.args[0],
name = program.name,
description = program.desc,
repo = program.repo,
author = program.author;
var gulp = require('gulp'),
template = require('lodash').template;
require('colors');
/* eslint no-process-exit:0 */
if (!dest) {
console.log(('Error: destination is empty').red);
process.exit(1);
}
dest = path.resolve(dest);
if (fs.existsSync(dest)) {
console.log(('Error: "' + dest + '" already exists').red);
process.exit(1);
}
gulp.src([path.join(__dirname, 'template/**/*'),
path.join(__dirname, 'template/.gitignore'),
'!**/package.json.tpl'])
.pipe(gulp.dest(dest))
.on('end', function() {
// Workaround for https://github.com/tj/commander.js/issues/284.
name = (typeof name === 'function' ? undefined : name) || path.basename(path.resolve(dest));
var packageJSONTpl = fs.readFileSync(path.join(__dirname, 'template/package.json.tpl'), {encoding: 'utf8'});
fs.writeFileSync(path.join(dest, 'package.json'), template(packageJSONTpl)({
name: name,
description: description,
repo: repo,
author: author
}));
if (callback) {
callback(dest);
}
});
};