-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·122 lines (103 loc) · 3.27 KB
/
index.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
const CURR_DIR = process.cwd();
const inquirer = require('inquirer');
const fs = require('fs');
const fse = require('fs-extra');
const klawSync = require('klaw-sync');
const CHOICES = fs.readdirSync(`${__dirname}/templates`)
.filter((fileName) => /^\w.*/.test(fileName));
const libFilesToDeploy = [
'/lib',
'/API.md',
];
let projectName;
let projectChoice;
let templatePath;
const QUESTIONS = [
{
name: 'project-choice',
type: 'list',
message: 'What project template would you like to generate?',
choices: CHOICES,
},
{
name: 'project-name',
type: 'input',
message: 'Project name:',
validate(input) {
if (/^([A-Za-z\-_\d])+$/.test(input)) return true;
return 'Project name may only include letters, numbers, underscores and hashes.';
},
},
];
const QUESTIONS_NEXT = [
{
name: 'project-name',
type: 'input',
message: 'Please choose another project name (already exists):',
validate(input) {
if (/^([A-Za-z\-_\d])+$/.test(input)) return true;
return 'Project name may only include letters, numbers, underscores and hashes.';
},
},
];
const addFilesKeyInLibrary = (path) => {
const content = fs.readFileSync(path, 'utf8');
try {
const dataToJson = JSON.parse(content);
dataToJson.files = libFilesToDeploy;
fs.writeFileSync(path, JSON.stringify(dataToJson, null, 2));
} catch (err) {
console.log(err);
}
};
const filterModules = (item) => !item.path.includes(`${templatePath}/node_modules`);
const readFilesAndWrite = () => {
try {
// return all files root path except directories
const files = klawSync(templatePath, {
traverseAll: true,
filter: filterModules,
nodir: true,
});
// change basePath to current directory
const newfilesPath = files.map((file) => file.path.replace(templatePath, `${CURR_DIR}/${projectName}`));
newfilesPath.forEach((filePath, index) => {
const content = fs.readFileSync(files[index].path, 'utf8');
// Almost the same as writeFileSync (i.e. it overwrites),
// except that if the parent directory does not exist, it's created.
// file must be a file path (a buffer or a file descriptor is not allowed).
// options are what you'd pass to fs.writeFileSync().
fse.outputFile(filePath, content, (err) => {
const regexlibPath = new RegExp(`${CURR_DIR}/${projectName}/package.json$`);
if (regexlibPath.test(filePath) && (/^library-/).test(projectChoice)) {
addFilesKeyInLibrary(filePath);
}
});
});
console.log(`Done: ${newfilesPath.length} has been created in ${CURR_DIR}/${projectName}`);
} catch (er) {
console.error(er);
}
};
const isDirAlreadyExist = (name) => {
const askQuestions = (questions) => {
inquirer.prompt(questions)
.then((answers) => {
projectName = answers['project-name'];
isDirAlreadyExist(projectName);
});
};
if (fs.existsSync(`${CURR_DIR}/${name}`)) {
askQuestions(QUESTIONS_NEXT);
} else {
readFilesAndWrite();
}
};
inquirer.prompt(QUESTIONS)
.then((answers) => {
projectName = answers['project-name'];
projectChoice = answers['project-choice'];
templatePath = `${__dirname}/templates/${projectChoice}`;
isDirAlreadyExist(projectName);
});