Skip to content

Commit a3b8814

Browse files
committed
feat(init): 提供初始化模板
1 parent 506ac8c commit a3b8814

File tree

17 files changed

+327
-31
lines changed

17 files changed

+327
-31
lines changed

lib/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var cli = module.exports = {
3737
return command.name === option || command.abbr === option;
3838
})[0];
3939
if (!command) {
40-
error('请确认是否存在 ' + option + ' 命令');
40+
error('Command ' + option + ' not found.');
4141
return;
4242
}
4343
var module = command.module;

lib/commands/init.js

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
'use strict';
22

3+
/* eslint-disable */
4+
5+
var shell = require('shelljs');
6+
var async = require('async');
7+
38
var replaceStream = require('replacestream');
49

510
var Manager = require('../modules/manager.js');
@@ -26,24 +31,101 @@ exports.run = function (options) {
2631
defaultName = sysPath.basename(cwd);
2732
}
2833

34+
spinner.start();
35+
2936
// TODO 也许可以改成通过命令行让用户输入
3037
var projectName = defaultName;
31-
3238
var initTmplPath = sysPath.resolve(__dirname, '../config/initTmpl/');
3339
var writePackageJsonStream = void 0;
3440

35-
// 如果没有package.json,先添加package.json
36-
if (!UtilFs.fileExists(packageJsonPath)) {
37-
writePackageJsonStream = createPackageJson();
38-
}
39-
40-
if (!writePackageJsonStream) {
41-
createConfigFile();
41+
// 如果初始化时带着初始化类型
42+
if (typeof process.argv[3] === 'string') {
43+
(function () {
44+
var checkInitCmd = function checkInitCmd(callback) {
45+
shell.exec('node ~/Desktop/pro/ykit/bin/ykit ' + initParam + ' init', { silent: true }, function (code, stdout, stderr) {
46+
if (stdout.indexOf('X') > -1) {
47+
// do nothing
48+
} else {
49+
isInitReady = true;
50+
}
51+
callback(null, 'one');
52+
});
53+
};
54+
55+
var checkConfigPkg = function checkConfigPkg(callback, packageName, registry) {
56+
if (!isInitReady) {
57+
(function () {
58+
var timeout = void 0;
59+
var child = shell.exec('npm view ' + packageName + ' --registry https://registry.npm.' + registry + '.org', { silent: true }, function (code, stdout, stderr) {
60+
if (stdout) {
61+
isInitReady = true;
62+
initProject();
63+
}
64+
clearTimeout(timeout);
65+
callback(null);
66+
});
67+
68+
// 防止超时
69+
timeout = setTimeout(function () {
70+
child.kill('SIGINT');
71+
}, 5000);
72+
})();
73+
} else {
74+
callback(null, 'two');
75+
}
76+
};
77+
78+
var initParam = process.argv[3];
79+
var isInitReady = false;
80+
81+
async.series([
82+
// 首先寻找是否存在 ykit xxx init 命令
83+
function (callback) {
84+
spinner.text = 'checking cmd ykit ' + initParam + ' init';
85+
checkInitCmd(callback);
86+
}
87+
// TODO
88+
// 寻找是否存在 ykit-config-xxx 的插件
89+
// (callback) => {
90+
// spinner.text =`checking package ykit-config-${initParam}`;
91+
// checkConfigPkg(callback, `ykit-config-${initParam}`, 'taobao')
92+
// },
93+
// // 寻找是否存在 @qnpm/ykit-config-xxx 的插件
94+
// (callback) => {
95+
// checkConfigPkg(callback, `@qnpm/ykit-config-${initParam}`, 'corp.qunar')
96+
// },
97+
], function (err) {
98+
// results is now equal to ['one', 'two']
99+
isInitReady ? spinner.succeed() : spinner.fail();
100+
});
101+
})();
42102
} else {
43-
writePackageJsonStream.on('finish', function () {
44-
log('Successfully created package.json file in ' + cwd);
103+
// 如果没有package.json,先添加package.json
104+
initProject();
105+
}
45106

107+
function initProject() {
108+
async.series([
109+
// 首先寻找是否存在 ykit xxx init 命令
110+
function (callback) {
111+
if (!UtilFs.fileExists(packageJsonPath)) {
112+
writePackageJsonStream = createPackageJson();
113+
writePackageJsonStream.on('finish', function () {
114+
log('Saved package.json file in ' + cwd);
115+
callback(null);
116+
});
117+
} else {
118+
callback(null);
119+
}
120+
}, function (callback) {
46121
createConfigFile();
122+
callback(null);
123+
}, function (callback) {
124+
createTmpl();
125+
callback(null);
126+
}], function (err, results) {
127+
// results is now equal to ['one', 'two']
128+
spinner.succeed();
47129
});
48130
}
49131

@@ -60,8 +142,17 @@ exports.run = function (options) {
60142
var stream = fs.createReadStream(configFilePath).pipe(replaceStream('#_name', projectName)).pipe(writeStream);
61143

62144
stream.on('finish', function () {
63-
log('Successfully created ' + configFileName + ' in ' + cwd);
145+
log('Saved ' + configFileName + ' in ' + cwd);
64146
});
65147
}
66148
}
149+
150+
function createTmpl() {
151+
fs.move(sysPath.resolve(initTmplPath, './src'), sysPath.resolve(cwd, './src'), function (err) {
152+
if (err) return console.error(err);
153+
});
154+
fs.move(sysPath.resolve(initTmplPath, './index.html'), sysPath.resolve(cwd, './index.html'), function (err) {
155+
if (err) return console.error(err);
156+
});
157+
}
67158
};

lib/config/initTmpl/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Ykit-Starter</title>
8+
<link rel="stylesheet" href="./prd/styles/index.css" />
9+
</head>
10+
<body>
11+
<div class="app">
12+
<h1>Ykit-Starter</h1>
13+
<p>
14+
See <a href="http://ued.qunar.com/ykit/docs.html" target="_blank">documentation</a> for more detailed usage.
15+
</p>
16+
</div>
17+
<script src="./prd/scripts/index.js"></script>
18+
</body>
19+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* eslint-disable */
2+
module.exports = (function() {
3+
console.log('index.js loaded');
4+
})();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
body {
2+
background-color: #f2f2f2;
3+
}
4+
5+
.app{
6+
position: absolute;
7+
top: 50%;
8+
left: 0;
9+
width: 100%;
10+
color: #444;
11+
text-align: center;
12+
margin: -70px 0 0;
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Ykit-Starter</title>
8+
</head>
9+
<body>
10+
11+
</body>
12+
</html>

lib/config/initTmpl/ykit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ exports.config = function() {
22
return {
33
export: [
44
'./scripts/index.js',
5-
'./styles/index.scss'
5+
'./styles/index.css'
66
],
77
modifyWebpackConfig: function(baseConfig) {
88
// edit ykit's Webpack configs

lib/global.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var moment = require('moment');
55
var logSymbols = require('log-symbols');
66

77
global.spinner = require('./utils/ora')();
8-
global.fs = require('fs');
8+
global.fs = require('fs-extra');
99
global.sysPath = require('path');
1010
global.async = require('async');
1111
global.childProcess = require('child_process');

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"css-loader": "^0.23.1",
2828
"extend": "^3.0.0",
2929
"extract-text-webpack-plugin": "^1.0.1",
30+
"fs-extra": "^1.0.0",
3031
"globby": "^5.0.0",
3132
"html-loader": "^0.4.3",
3233
"jerryproxy": "^1.0.68",
@@ -47,6 +48,7 @@
4748
"semver": "^5.3.0",
4849
"serve-index": "^1.8.0",
4950
"serve-static": "^1.11.1",
51+
"shelljs": "^0.7.5",
5052
"style-loader": "^0.13.1",
5153
"through2": "^2.0.1",
5254
"uglify-js": "^1.3.5",
@@ -70,7 +72,6 @@
7072
"mocha": "^3.1.2",
7173
"ps-tree": "^1.1.0",
7274
"semver-regex": "^1.0.0",
73-
"shelljs": "^0.7.5",
7475
"ydoc": "^2.1.1"
7576
},
7677
"config": {

src/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let cli = module.exports = {
3535
let project = Manager.getProject(process.cwd());
3636
let command = project.commands.filter((command) => command.name === option || command.abbr === option)[0];
3737
if (!command) {
38-
error('请确认是否存在 ' + option + ' 命令');
38+
error('Command ' + option + ' not found.');
3939
return;
4040
}
4141
let module = command.module;

0 commit comments

Comments
 (0)