Skip to content

Commit 8cc739b

Browse files
author
coderwhy
committed
实现create addcpn功能
1 parent 46fafb1 commit 8cc739b

File tree

850 files changed

+95895
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

850 files changed

+95895
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env node
2+
const program = require('commander');
3+
4+
const helpOptions = require('./lib/core/help');
5+
const createCommands = require('./lib/core/create');
6+
7+
const log = require('./lib/utils/log');
8+
9+
// 定义显示模块的版本号
10+
program.version(require('./package.json').version);
11+
12+
// 给help增加其他选项
13+
helpOptions();
14+
15+
// 创建命令
16+
createCommands();
17+
18+
// 解析终端指令
19+
program.parse(process.argv);
20+
21+
22+
23+

lib/config/repo_config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const vueGitRepo = "direct:https://github.com/coderwhy/hy-vue-temp.git";
2+
3+
4+
module.exports = {
5+
vueGitRepo
6+
}

lib/core/actions.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { promisify } = require('util');
2+
const path = require('path');
3+
4+
const program = require('commander');
5+
const downloadRepo = promisify(require('download-git-repo'));
6+
const open = require('open');
7+
8+
const log = require('../utils/log');
9+
const terminal = require('../utils/terminal');
10+
const { ejsCompile, writeFile } = require('../utils/file');
11+
const repoConfig = require('../config/repo_config');
12+
13+
const createProject = async (project, otherArg) => {
14+
// 1.提示信息
15+
log.hint('coderwhy helps you create your project, please wait a moment~');
16+
17+
// 2.clone项目从仓库
18+
await downloadRepo(repoConfig.vueGitRepo, project, { clone: true });
19+
20+
// 3.执行终端命令npm install
21+
// terminal.exec('npm install', {cwd: `./${project}`});
22+
const npm = project.platform === 'win32' ? 'npm.cmd' : 'npm';
23+
await terminal.spawn(npm, ['install'], { cwd: `./${project}` });
24+
25+
// 4.打开浏览器
26+
open('http://localhost:8080/');
27+
28+
// 5.运行项目
29+
await terminal.spawn(npm, ['run', 'serve'], { cwd: `./${project}` });
30+
}
31+
32+
const addComponent = async (name) => {
33+
console.log(name, program.dest);
34+
35+
// 1.获取模块引擎的路径
36+
const templatePath = path.resolve(__dirname, '../template/component.vue.ejs');
37+
const result = await ejsCompile(templatePath, {name});
38+
39+
// 2.写入文件中
40+
const targetPath = path.resolve(program.dest, `${name}.vue`);
41+
writeFile(targetPath, result);
42+
}
43+
44+
module.exports = {
45+
createProject,
46+
addComponent
47+
}

lib/core/create.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const program = require('commander');
2+
3+
const {
4+
createProject,
5+
addComponent
6+
} = require('./actions');
7+
8+
const createCommands = () => {
9+
// 创建项目指令
10+
program
11+
.command('create <project> [otherArgs...]')
12+
.description('clone a repository into a newly created directory')
13+
.action(createProject);
14+
15+
program
16+
.command('addcpn <name>')
17+
.description('add vue component, 例如: coderwhy addcpn NavBar -d src/components')
18+
.action(addComponent)
19+
20+
program.command('test').action(() => {
21+
// terminal.spawn("npm", ['--version']);
22+
// terminal.exec("npm --version");
23+
// open('http://localhost:8080/');
24+
})
25+
}
26+
27+
module.exports = createCommands;

lib/core/help.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const program = require('commander');
2+
3+
const helpOptions = () => {
4+
program.option('-w --why', 'a coderwhy option');
5+
6+
program.option('-s --src <src>', 'a source folder');
7+
program.option('-d --dest <dest>', 'a destination folder, 例如: -d src/pages, 错误/src/pages');
8+
program.option('-f --framework <framework>', 'your framework name');
9+
10+
program.on('--help', function() {
11+
console.log("");
12+
console.log("usage");
13+
console.log(" coderwhy -v");
14+
console.log(" coderwhy -version");
15+
})
16+
}
17+
18+
module.exports = helpOptions;

lib/template/component.vue.ejs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<%_ if(data) { _%>
2+
<template>
3+
<div class="<%= data.name %>">
4+
<h2>{{ message }}</h2>
5+
</div>
6+
</template>
7+
8+
<script>
9+
export default {
10+
name: "<%= data.name %>",
11+
components: {
12+
13+
},
14+
mixins: [],
15+
props: {
16+
17+
},
18+
data: function() {
19+
return {
20+
message: "Hello <%= data.name %>"
21+
}
22+
},
23+
created: function() {
24+
25+
},
26+
mounted: function() {
27+
28+
},
29+
computed: {
30+
31+
},
32+
methods: {
33+
34+
}
35+
}
36+
</script>
37+
38+
<style scoped>
39+
.<%= data.name %> {
40+
41+
}
42+
</style>
43+
44+
<%_ } _%>

lib/template/test.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<div>
3+
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
10+
}
11+
</script>
12+
13+
<style scoped>
14+
15+
</style>

lib/utils/file.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const fs = require('fs');
2+
const ejs = require('ejs');
3+
4+
const log = require('./log');
5+
6+
const ejsCompile = (templatePath, data={}, options = {}) => {
7+
return new Promise((resolve, reject) => {
8+
ejs.renderFile(templatePath, {data}, options, (err, str) => {
9+
if (err) {
10+
reject(err);
11+
return;
12+
}
13+
resolve(str);
14+
})
15+
})
16+
}
17+
18+
const writeFile = (path, content) => {
19+
if (fs.existsSync(path)) {
20+
log.error("the file already exists~")
21+
return;
22+
}
23+
return fs.promises.writeFile(path, content);
24+
}
25+
26+
module.exports = {
27+
ejsCompile,
28+
writeFile
29+
}

lib/utils/log.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const chalk = require('chalk');
2+
3+
const hint = (...info) => {
4+
console.log(chalk.blue(info));
5+
}
6+
7+
const error = (...info) => {
8+
console.log(chalk.red(info));
9+
}
10+
11+
const clear = () => {
12+
console.clear();
13+
}
14+
15+
module.exports = {
16+
hint,
17+
error,
18+
clear
19+
}

lib/utils/terminal.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { spawn, exec } = require('child_process');
2+
3+
4+
const spawnCommand = (...args) => {
5+
return new Promise((resole, reject) => {
6+
const childProcess = spawn(...args);
7+
childProcess.stdout.pipe(process.stdout);
8+
childProcess.stderr.pipe(process.stderr);
9+
childProcess.on('close', () => {
10+
resole();
11+
})
12+
})
13+
}
14+
15+
const execCommand = (...args) => {
16+
return new Promise((resolve, reject) => {
17+
exec(...args, (err, stdout, stderr) => {
18+
if (err) {
19+
reject(err);
20+
return;
21+
}
22+
console.log(stdout.replace('\n', ''));
23+
// console.log(stderr);
24+
resolve();
25+
})
26+
})
27+
}
28+
29+
module.exports = {
30+
spawn: spawnCommand,
31+
exec: execCommand
32+
};

node_modules/.bin/ejs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/is-docker

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/jake

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/rimraf

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/seek-bunzip

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/seek-table

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@sindresorhus/is/dist/example.d.ts

Whitespace-only changes.

node_modules/@sindresorhus/is/dist/example.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@sindresorhus/is/dist/example.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@sindresorhus/is/dist/index.d.ts

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)