-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·101 lines (75 loc) · 2.54 KB
/
index.js
File metadata and controls
executable file
·101 lines (75 loc) · 2.54 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const clear = require('clear');
const chalk = require('chalk');
const figlet = require('figlet');
const program = require('commander');
const { prompt } = require('inquirer');
const shell = require('shelljs');
const Spinner = require('cli-spinner').Spinner;
const questions = require('./questions');
clear();
console.log(
chalk.green(
figlet.textSync('EXY CLI', { horizontalLayout: 'full' })
)
);
program
.version('0.0.1')
.description('EXY CLI');
program
.command('init')
.action(prompt(questions).then(answers => {
const { template, ...config } = answers;
const templatePath = `src/${template}`;
const packagePath = path.resolve(__dirname, templatePath, 'package.json');
fs.readFile(packagePath, 'utf8', (err, data) => {
const baseConfig = JSON.parse(data);
const packageJSONBaseConfig = { ...baseConfig, ...config };
copyFolderRecursiveSync(path.resolve(__dirname, templatePath), process.cwd());
const spinner = new Spinner('Installing dependencies.. %s');
spinner.setSpinnerString('|/-\\');
spinner.start();
fs.writeFile(`${template}/package.json`, JSON.stringify(packageJSONBaseConfig, null, 2), (err) => {
if (err) {
console.log(err);
}
shell.cd(template);
const install = shell.exec('npm install && npm start', { async: true });
install.stdout.on('data', () => {
spinner.stop();
});
});
});
}));
program.parse(process.argv);
function copyFileSync( source, target ) {
let targetFile = target;
//if target is a directory a new file with the same name will be created
if ( fs.existsSync( target ) ) {
if ( fs.lstatSync( target ).isDirectory() ) {
targetFile = path.join( target, path.basename( source ) );
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync( source, target ) {
let files = [];
//check if folder needs to be created or integrated
const targetFolder = path.join( target, path.basename( source ) );
if ( !fs.existsSync( targetFolder ) ) {
fs.mkdirSync( targetFolder );
}
if ( fs.lstatSync( source ).isDirectory() ) {
files = fs.readdirSync( source );
files.forEach( function ( file ) {
const curSource = path.join( source, file );
if ( fs.lstatSync( curSource ).isDirectory() ) {
copyFolderRecursiveSync( curSource, targetFolder );
} else {
copyFileSync( curSource, targetFolder );
}
} );
}
}