-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·61 lines (57 loc) · 2.26 KB
/
main.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
#!/usr/bin/env node
var mkdirp = require('mkdirp'),
minimist = require('minimist'),
path = require('path'),
fs = require('fs'),
fse = require('fs-extra');
var config = minimist(process.argv.slice(2));
var projectName = config._[0] || path.basename(process.cwd());
var jinja = config.jinja || config.j;
if (!projectName) {
console.log('No project name!');
return;
}
var targetName = config.local || config.l ? '.' : projectName;
var modulesPath = path.dirname(require.resolve('.'));
var fileNames = ['common.jade', 'config.scss', 'ui.reset.scss', jinja ? 'index.jinja.jade' : 'index.jade', 'index.scss',
'index.json', jinja ? 'index.jinja.js' : 'index.js', 'index.debug.js', 'data.imgs'];
var projectFiles = ['gulpfile.js', 'package.json', '.eslintrc', '.eslintignore'];
fs.stat(path.join(process.cwd(), targetName), function (err, stats) {
if ((!(config.f || config.force)) && stats && stats.isDirectory()) {
console.log('Project exist in current direcotry');
} else {
if(targetName === '.') {
copyFiles();
} else {
mkdirp(projectName, function (err) {
if (err) { console.log(err); }
copyFiles();
});
}
}
});
function copyFiles() {
fileNames.forEach(function (item) {
var targetDir = item.split('.').pop(),
targetFile = item.replace(/index.(jinja.)?/, projectName + '.');
copyTo(path.join(modulesPath, 'templates', item), path.join(process.cwd(), targetName, targetDir, targetFile));
});
projectFiles.forEach(function (item) {
if (item === 'package.json') {
fse.readJson(path.join(modulesPath, 'templates/package.json'), function (err, packageObj) {
if (err) { console.log(err); }
packageObj.name = projectName;
fse.writeJson(path.join(process.cwd(), targetName, './package.json'), packageObj, function (err) {
if (err) { console.log(err); }
});
});
} else {
copyTo(path.join(modulesPath, 'templates', item), path.join(process.cwd(), targetName, item));
}
});
}
function copyTo(src, target) {
fse.copy(src, target, function (err) {
if (err) { console.log(err); }
});
}