-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
114 lines (103 loc) · 3.29 KB
/
utils.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
const fs = require('fs');
const path = require('path');
const camelCase = require('camelcase');
const mustache = require('mustache');
const emoji = require('node-emoji');
const tree = require('tree-node-cli');
const rimraf = require('rimraf');
const chalk = require('chalk');
const checkDirExistence = dirPath => {
return fs.existsSync(dirPath);
}
const ensureDirectoryExistence = filePath => {
const dirname = path.dirname(filePath);
if (checkDirExistence(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
const writeToFile = (filePath, content) => {
try {
ensureDirectoryExistence(filePath);
fs.writeFileSync(filePath, content, 'utf8');
} catch (e) {
throw e;
}
}
const walkSync = (baseDir, dirPath, handler) => {
fs.readdirSync(dirPath, 'utf8').forEach(file => {
if (fs.statSync(dirPath + file).isDirectory()) {
walkSync(baseDir, dirPath + file + '/', handler);
} else {
handler(path.relative(baseDir, dirPath + file));
}
});
return;
}
const mustached = (filePath, tags) => {
const file = fs.readFileSync(path.resolve(filePath), 'utf8');
return mustache.render(file, tags);
}
const fileHandler = (writeDir, moduleDir, tags) => fileRelativePath => {
let content;
let normalizedFileRelativePath;
const hbsRegex = /\.hbs$/;
if (hbsRegex.test(fileRelativePath)) {
content = mustached(path.resolve(moduleDir, fileRelativePath), tags);
normalizedFileRelativePath = fileRelativePath.replace(hbsRegex, '');
} else {
content = fs.readFileSync(path.resolve(moduleDir, fileRelativePath), 'utf8');
normalizedFileRelativePath = fileRelativePath;
}
writeToFile(path.resolve(writeDir, normalizedFileRelativePath), content);
}
const kebab2Camel = kebab => {
return camelCase(kebab.replace('react-native-', ''), { pascalCase: true });
}
const log = (type, params) => {
if (type === 'ERR_DIR_EXIST') {
console.log(emoji.get('x'), ` Aborted: The directory <${params.name}> has already existed.`);
console.log(emoji.get('point_right'), ' Please try again with a different directory name.');
} else if (type === 'ERR_REASON') {
console.log(emoji.get('x'), `Aborted: ${params.reason}`);
} else if (type === 'DONE') {
console.log(tree(params.writeDir, {
allFiles: true,
dirsFirst: true
}));
console.log(emoji.get('sparkles'), ` ${chalk.bold('Done in ')}${chalk.green(params.time)}`);
console.log();
console.log(emoji.get('point_right'), ` ${chalk.bold('Quick Tips')}`);
console.log();
console.log(` ${chalk.bold('1.')} ${chalk.green('Install')}`);
console.log(` • $ cd ${params.name}`);
console.log(' • $ npm install');
console.log();
console.log(` ${chalk.bold('2.')} ${chalk.green('Run Tests')}`);
console.log(' • $ npm test');
console.log();
console.log(` ${chalk.bold('3.')} ${chalk.green('Dev / View Demo')}`);
console.log(' • $ npm start');
console.log();
}
}
const hrtime2ms = hrtime => {
return (((hrtime[0] * 1e9) + hrtime[1]) / 1e6).toFixed(2);
}
const deleteDir = dirPath => {
if (checkDirExistence(dirPath)) {
rimraf.sync(dirPath);
}
}
module.exports = {
checkDirExistence,
writeToFile,
walkSync,
mustached,
fileHandler,
kebab2Camel,
log,
hrtime2ms,
deleteDir
}