-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileGenerator.js
More file actions
74 lines (63 loc) · 1.71 KB
/
fileGenerator.js
File metadata and controls
74 lines (63 loc) · 1.71 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
const fs = require('fs');
const shell = require('shelljs');
const {
compFileBoilerplate,
indexContents,
sfcBoilerplate,
sfcTestFileBoilerplate,
testFileBoilerplate
} = require('./fileContents');
const {
getComponentNameFromDirName,
writeToParent
} = require('./helpers');
const dirName = process.argv[2];
const shouldWriteToParentIndex = process.argv.includes('--parent');
const isSFC = process.argv.includes('--sfc');
const hasNoContentFlag = process.argv.includes('--no-content')
const componentName = getComponentNameFromDirName(dirName);
const fileConfig = [
{
extension: 'less',
},
{
extension: 'tsx',
contents: isSFC ? sfcBoilerplate(componentName, dirName) : compFileBoilerplate(componentName, dirName),
},
{
extension: 'test.tsx',
contents: isSFC ? sfcTestFileBoilerplate(componentName, dirName) : testFileBoilerplate(componentName, dirName),
},
{
extension: 'js',
contents: indexContents(dirName),
}
];
/******** Make folder and files, write contents ********/
console.log('Creating files ...');
shell.mkdir(`./${dirName}`);
shell.cd(dirName);
fileConfig.forEach(config => {
const fileName = config.extension === 'js' ? 'index.js' : `${dirName}.${config.extension}`;
shell.touch(fileName);
console.log(fileName);
if (!hasNoContentFlag) {
if (config.contents) {
fs.writeFile(fileName, config.contents, (err) => {
if (err) {
console.log('Something went wrong: ', err);
}
});
}
} else {
fs.writeFile('index.js', indexContents(dirName), (err) => {
if (err) {
console.log('Something went wrong: ', err);
}
});
}
});
if (shouldWriteToParentIndex) {
writeToParent(dirName);
}
console.log('Done');