-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-component.js
executable file
·95 lines (76 loc) · 2.21 KB
/
create-component.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
// node create-component.js componentName
const cssExt = 'styl';
const fs = require('fs');
function createComponent() {
const componentName = process.argv.slice(2);
// --- CHECK PARAM - COMPONENT NAME
if (componentName.length == 0) {
console.error('ERROR: Set Component Name');
return;
}
const fName = componentName[0];
const path = './src/components/' + fName;
// --- CHECK FOLDER
if ( fs.existsSync(path) ) {
console.log('ERROR: Folder "src/components/' + fName + '" exists');
} else {
// Create Dir
fs.mkdirSync(path, {recursive: true}, (err) => {
if (err) throw err;
});
}
// --- ADD CSS FILE
// Check file
if ( fs.existsSync(path +'/'+ fName +'.'+ cssExt) ) {
console.log('ERROR: File "'+ path +'/'+ fName +'.'+ cssExt +'" exists');
} else {
// Create css file
try {
fs.appendFileSync(
path + '/' + fName + '.' + cssExt,
'/*!\n\t'+ fName + '\n\t==\n*/'
);
} catch (err) {
console.log(err);
}
}
// --- ADD PUG FILE
// Check file
if ( fs.existsSync(path + '/' + fName +'.pug') ) {
console.log('ERROR: File "'+ path + '/' + fName +'.pug" exists');
return;
} else {
// Create pug file
try {
fs.appendFileSync(
path + '/' + fName +'.pug',
"mixin "+ fName +"()\n .new "+ fName
);
} catch (err) {
console.log(err);
}
}
let fd;
// --- PUSH @IMPORT TO MAIN
try {
fd = fs.openSync('./src/stylus/main.' + cssExt, 'a');
fs.appendFileSync(fd, '\n@import "../components/'+ fName +'/'+ fName +'";', 'utf8');
} catch (err) {
console.log(err);
} finally {
if (fd !== undefined)
fs.closeSync(fd);
}
// --- PUSH INCLUDE TO COMPONENTS.PUG
try {
fd = fs.openSync('./src/pug/layout/components.pug', 'a');
fs.appendFileSync(fd, '\ninclude ../../components/'+ fName +'/'+ fName, 'utf8');
} catch (err) {
console.log(err);
} finally {
if (fd !== undefined)
fs.closeSync(fd);
}
console.log('COMPONENT "' + fName + '" HAS BEEN CREATED!');
}
createComponent();