UNPKG

1.77 kBJavaScriptView Raw
1const shell = require('shelljs')
2const fs = require('fs')
3const path = require('path')
4
5const treeKill = require('tree-kill');
6const Spawn = require('./spawn')
7
8/**
9 * get templates
10 */
11exports.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 * generate project
29 */
30exports.generate = (templateName, projectName, projectPostion) => {
31
32 // detect project path
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 // mkdir project path
42 shell.mkdir('-p', projectPath)
43
44 // copy template to project path
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// function runCommand(command, position, name) {
55// shell.cd(`${position}/${name}`)
56// shell.config.execPath = shell.which('node');
57// if (shell.exec(command).code !== 0) {
58// shell.echo(`Error: ${command} failed.`)
59// return shell.exit(1)
60// }
61// return true
62// }
63
64/**
65 * exec script
66 */
67exports.exec = (script, projectPath) => {
68 return new Spawn(`cd ${projectPath} && npm run ${script}`)
69 // return runCommand('npm run dev', position, name)
70}
71
72/**
73 * kill progress
74 */
75exports.kill = (pid) => {
76 treeKill(pid);
77}