-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdz.js
executable file
·297 lines (250 loc) · 9.83 KB
/
kdz.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env node
// Run this task with Node
"use strict"; // use ES5 strict mode
// Bring in core and plugin-like Node modules
var fs = require( 'fs' ), // Read files with Node's fs module
exec = require( 'child_process' ).exec, // execute line commands
program = require( 'commander' ), // Fires off commands and options
Q = require( 'q' ), // Use Q to manage Promises
Decompress = require('decompress'),
chalk = require( 'chalk' ); // Colorize console messages
// Bring in Node modules that kaidez created
var data = require( './config/data.js' ), // JSON file data is visible
dlFiles = require( './config/dlFiles.js' ), // Download GitHub files
buildFolder = require( './config/buildFolder.js' ), // Build folders
unzip = require( './config/unzip.js' ), // Unzip files
goToTest = require( './config/goToTest.js' ), // Run a test build
touchCoffee = require( './config/touchCoffee.js' ), // "main.coffee"
doneMsg = require( './config/doneMessage.js' ); // ".done()" message
// Stop "kdz app" if "less" & "sass" flags are passed at the same time
// Passes error as under Node proccess exit code 9 ("Invalid Argument")
function flagCheck() {
if ( program.less && program.scss ) {
console.log(chalk.red( 'Invalid Argument: "less" and "scss" flags cannot be passed at the same time\nExiting task....\n' ) )
process.exit( 9 );
}
} // end "flagCheck()"
/*
* "getAllFiles()"
* =====================================================================
*
* Uses the above "dlFiles()" method to download an array of files
* "array" param points to an array listed in "config/data.js"
* "folder" param points to which folder to download the files
*/
function getAllFiles( array, folder ) {
// Loop through the array to find files
// "coreFile" represents one item in an array
array.forEach( function( coreFile ) {
// Use "dlFiles()" to file-check & download the files in the array
// Pass "array" & "folder" params above, "dlFiles()" does the rest
dlFiles( coreFile, folder );
})
} // end "getAllFiles()"
/*
* "getSingleFile()"
* =====================================================================
*
* Uses the above "dlFiles()" method to download a single file
* "file" param points to a file listed in "config/data.js"
* "folder" param points to which folder to download the files
*/
function getSingleFile( file, folder ) {
// Use "dlFiles()" to file-check & download the single file
// Pass "file" & "folder" params above, "dlFiles()" does the rest
dlFiles( file, folder );
} // end "getSingleFile()"
/*
* "preProcess()"
* =====================================================================
*
* Helper function for downloading CSS preprocessors files
* "whatType" is a preprocessor file type: either "less" or "scss"
* Don't create it if it doesn't and pass a message saying so
* Stop the fs process folder
* "ifFile" is a file to look for before downloading files...
* ...either "style.less" or "style.scss"
* Internally uses the above "getAllFiles()" function
* Downloads .zip files to be later unzipped with the "unzip()" function
*/
function preProcess( whatType, ifFile ) {
// Create a reference for a file to look for before downloading files
// Look in the "css-build/" folder
var coreFile = "css-build/" + ifFile;
// Use Node fs.open to check if "style" file exists in "css-build/"
fs.open( coreFile, 'rs', function( err, fd ) {
if ( err && err.code == 'ENOENT' ) {
// If "style" file DOES NOT exist, download the files
// When "wordpress" flag is passed, download WordPress-type files
// When "build" flag is passed, download SPA-type files
if( program.wordpress ) {
getAllFiles( whatType, "wordpress" );
} else {
getAllFiles( whatType, "spa" );
}
} else {
// If "style" file DOES exist, don't download files
// Pass a console message saying it exists & stop the fs process
console.log( chalk.red( '"CSS preprocessor files may exists...new ones won\'t be created.\n' ) );
fs.close( fd );
}
});
return Q.delay( 1500 ); // Return a Promise
} // end "preProcess()"
// "app" command: scaffolds out a SPA-like project
program
.command( 'app' )
.description( 'scaffold a basic web application' )
.action(function() {
flagCheck(); // does not return a promise
Q.fcall(goToTest)
.then(function() {
console.log( chalk.green.underline( '>> Create preprocess folders...\n' ) );
return Q.delay( 1500 );
})
.then(function() {
buildFolder( data.source_build, "css-build" );
return Q.delay( 1500 );
}, function() { console.log( chalk.red( '✘ preprocess folders failed to be created!' ) );} )
.then(function() {
if( program.wordpress ) {
return false;
} else {
console.log( chalk.green.underline( '>> Create build folders...\n' ) );
buildFolder( data.build_folder, "build" );
}
return Q.delay( 1500 );
}, function() { console.log( chalk.red( '✘ build folders failed to be created!' ) ) ;} )
.then(function(){
console.log( chalk.green.underline( '>> Create "coffee/main.coffee"...\n' ) );
return Q.delay( 500 );
})
.then(function(){
process.chdir( 'coffee' );
return Q.delay( 1500 );
})
.then(function(){
touchCoffee();
return Q.delay( 1500 );
}, function() { console.log( chalk.red( '✘ "coffee/main.coffee" failed to be created!' ) ) ;})
.then(function(){
process.chdir( '../' );
return Q.delay( 1500 );
})
.then(function(){
if ( program.wordpress ){
console.log( chalk.green.underline( '>> Download "functions.php & "wp-comment-block.css"...\n' ) );
} else {
return false;
}
return Q.delay( 1500 );
})
.then(function() {
if( program.wordpress ) {
var coreWpArray = data.wp_files.slice(-2);
getAllFiles( coreWpArray, "wordpress" );
}
return Q.delay( 1500 );
}, function() { console.log( chalk.red( '✘ Either "functions.php" or "wp-comment-block.css" failed to download!' ) );} )
.then(function(){
console.log( chalk.green.underline( '>> Download common project files...\n' ) );
return Q.delay( 1500 );
})
.then(function(){
getAllFiles( data.shared, "shared-files" );
return Q.delay( 1500 );
}, function() { console.log( chalk.red( '✘ Common project files failed to download!' ) );} )
.then(function(){
console.log( chalk.green.underline( '>> Download task runner project files & package.json...\n' ) );
return Q.delay( 1500 );
})
.then(function() {
if( program.wordpress ) {
getAllFiles( data.core, "wordpress" );
} else {
getAllFiles( data.core, "spa" );
}
return Q.delay( 1500 );
}, function() { console.log( chalk.red( '✘ Task runner project files & package.jsons failed to download!' ) );} )
.then(function(){
if( program.gitignore ) {
console.log( chalk.green.underline( '>> Download ".gitignore"...\n' ) );
return Q.delay( 1500 );
}
})
.then(function() {
if ( program.gitignore && program.wordpress ) {
getSingleFile( data.wp_files[0], "wordpress" );
} else if (( program.gitignore ) || ( program.gitignore && !program.wordpress)) {
getSingleFile( data.spa_files, "spa" );
}
return Q.delay( 1500 );
}, function() { console.log( chalk.red( '✘ .gitignore failed to download!' ) );} )
.then(function(){
if( program.less || program.scss ) {
console.log( chalk.green.underline( '>> Download CSS preprocessor project files...\n' ) );
return Q.delay( 1500 );
}
})
.then(function() {
if( program.less ) {
preProcess( data.less, "style.less" )
} else if( program.scss ) {
preProcess( data.sass, "style.scss" )
} else {
return false;
}
return Q.delay( 1500 );
}, function() { console.log( chalk.red( '✘ Preprocessor files failed to download!' ) );} )
.then(function(){
fs.open( 'wp-comment-block.css', 'rs', function( err, fd ) {
if ( err && err.code == 'ENOENT' ) {
return false;
} else {
console.log( chalk.cyan.green( '>> Move "wp-comment-block.css" to "css-build/...\n' ) );
exec( "mv wp-comment-block.css css-build/" );
}
});
})
.then(function() {
return unzip()
.then(function(){
var zip, file;
if ( program.less ) {
zip = "rm -rf less.zip";
file = "mv style.less css-build/";
} else if ( program.scss ) {
zip = "rm -rf sass.zip";
file = "mv style.scss css-build/";
}
exec( zip );
exec( file );
}, function() { console.log( chalk.red( '✘ Preprocess files failed to copy over!' ) );})
}, function() { console.log( chalk.red( '✘ Preprocess files failed unzip!' ) );})
.done( doneMsg );
}) // end "app" command
// delete "test-build" folder
program
.command( 'dt' )
.description( 'delete "test-build" folder' )
.action(function() {
fs.open( 'test-build', 'rs', function( err, fd ) {
if ( err && err.code == 'ENOENT' ) {
console.log( chalk.cyan.underline( '>> "test-build" doesn\'t exist...nothing to delete\n' ) );
} else {
exec( "rm -rf test-build/" );
}
});
})
// options
program
.version( '0.0.1' )
.option( '-w, --wordpress', 'create a WordPress project' )
.option( '-g, --gitignore', 'download ".gitignore" file' )
.option( '-l, --less', 'download LESS files in "css-build"' )
.option( '-s, --scss', 'download Sass files in "css-build"' )
.option( '-t, --test', 'do a test scaffold in "test-build"' );
// Pass options to commands
program.parse( process.argv );
// If no arguments or commands are passed, display "help"
if ( !program.args.length ) program.help();