-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaffold.js
115 lines (82 loc) · 2.47 KB
/
scaffold.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
const fs = require( 'fs' );
// index starts on 2 because process.argv contains the whole command-line invocation:
// process.argv = ['node', 'yourscript.js', <your string here>]
const flag = process.argv[2];
const scaffoldName = process.argv[3].toLowerCase();
// ensure flag is either -v or -c
if( flag !== '-v' && flag !== '-c' ){
console.log( 'you did not include any flags')
console.log( 'include "-c" to scaffold a controller, or "-v" to scaffold a view ')
return;
}
// generate view
const scaffoldView = async () => {
const templates = {
pug : {
name : 'view.pug',
path : `${__dirname}/views/${scaffoldName}.pug`,
},
scss : {
name : 'stylesheet.scss',
path : `${__dirname}/public/sass/${scaffoldName}.scss`,
},
js : {
name : 'viewModel.js',
path : `${__dirname}/public/js/${scaffoldName}.js`,
}
};
Object.keys( templates ).forEach( ( key ) => {
const file = templates[key];
const template = fs.readFileSync(`scaffold/${file.name}`, 'utf8' );
const templateResult = template.replace( /TEMPLATE/g, scaffoldName );
fs.writeFileSync( file.path, templateResult );
} );
};
// generate controllers
const scaffoldController = async () => {
const capitalizedName = scaffoldName.charAt(0).toUpperCase() + scaffoldName.slice(1);
const directories = [
`${__dirname}/controllers`,
`${__dirname}/lib`,
`${__dirname}/lib/${scaffoldName}`,
`${__dirname}/test`,
`${__dirname}/test/${scaffoldName}`
];
directories.forEach( ( directory ) => {
if ( !fs.existsSync( directory ) ) {
fs.mkdirSync( directory );
}
} );
const templates = {
validator: {
name : 'validator.js',
path : `${__dirname}/lib/${scaffoldName}/validator.js`
},
service : {
name : 'service.js',
path : `${__dirname}/lib/${scaffoldName}/service.js`
},
controller : {
name : 'controller.js',
path : `${__dirname}/controllers/${scaffoldName}.js`
},
test : {
name : 'service_test.js',
path : `${__dirname}/test/${scaffoldName}/service_test.js`
}
}
Object.keys( templates ).forEach( ( key ) => {
const file = templates[key];
const template = fs.readFileSync(`scaffold/${file.name}`, 'utf8' );
const templateResult = template.replace( /CAPITALIZEDTEMPLATE/g, capitalizedName )
.replace( /TEMPLATE/g, scaffoldName )
fs.writeFileSync( file.path, templateResult );
} );
}
if( flag === "-v" ){
scaffoldView();
}
if( flag === '-c' ){
scaffoldController();
}
return console.log(`scaffold successful for ${scaffoldName}`)