1 | const shell = require('shelljs')
|
2 | const fs = require('fs')
|
3 | const path = require('path')
|
4 |
|
5 | const treeKill = require('tree-kill');
|
6 | const Spawn = require('./spawn')
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | exports.getTemplates = () => {
|
12 | const templatePath = path.join(__dirname, '/template')
|
13 | const templateNames = fs.readdirSync(templatePath)
|
14 |
|
15 | let templates = []
|
16 | for (let template of templateNames) {
|
17 | const pkg = require(`./template/${template}/package.json`)
|
18 | templates.push({
|
19 | template,
|
20 | name: pkg.name,
|
21 | description: pkg.description
|
22 | })
|
23 | }
|
24 | return templates
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | exports.generate = (templateName, projectName, projectPostion) => {
|
31 |
|
32 |
|
33 | const projectPath = `${projectPostion}/${projectName}`
|
34 | if (fs.existsSync(projectPath)) {
|
35 | return {
|
36 | status: 0,
|
37 | message: `! project name already existed :( `
|
38 | }
|
39 | }
|
40 |
|
41 |
|
42 | shell.mkdir('-p', projectPath)
|
43 |
|
44 |
|
45 | shell.cp('-R', path.join(__dirname, `template/${templateName}`) + '/*', projectPath)
|
46 | shell.cp('-R', path.join(__dirname, `template/${templateName}`) + '/.*', projectPath)
|
47 |
|
48 | return {
|
49 | status: 1,
|
50 | message: `√ generate project succeed :) `
|
51 | }
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | exports.exec = (script, projectPath) => {
|
68 | return new Spawn(`cd ${projectPath} && npm run ${script}`)
|
69 |
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | exports.kill = (pid) => {
|
76 | treeKill(pid);
|
77 | }
|