-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
280 lines (234 loc) · 7.69 KB
/
app.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
var _ = require("lodash"),
colors = require("colors"),
watch = require("watch"),
domain = require("domain"),
fs = require("fs"),
path = require("path"),
util = require("util"),
ShopifySyncer = require('./lib/shopify-theme-sync'),
config = require("./config.json"),
defaults = {
"compress": {
// do not compress JavaScript or JSON by default.
"js": false
},
"ignoreDotFiles": true,
"interval": 500
},
// Merge the base options in config.json with our `defaults` object.
// Note: We can't use `_.defaults` here directly because it will not perform a "deep" merge. The _.merge method
// below was suggested by @jdalton himself: https://github.com/bestiejs/lodash/issues/154#issuecomment-12310052
configOptions = _.merge( {}, config.options, defaults, _.defaults ),
/**
* A black list of file/directory names.
*
* Shopify's filesystem is case insensitive, so our filter function is too.
* Make sure all filenames in this list are lowercase since `filter` only checks against lowercase.
*/
blacklist = [
"thumbs.db"
],
/**
* A whitelist of all valid shopify directories.
*
* Shopify's filesystem is case insensitive, so our filter function is too.
* Make sure all filenames in this list are lowercase since `filter` only checks against lowercase.
*/
validDirectories = [
"assets",
"config",
"layout",
"snippets",
"templates"
],
/**
* walk options. We want to apply our blacklist filter and always ignore dot files.
*/
options = {
"filter": filter
},
appTitle = "Shopify Theme Syncer";
process.title = appTitle;
_.extend( options, configOptions );
if ( config && Array.isArray( config.shops ) && config.shops.length > 0 ) {
config.shops.forEach(function( shopConfig ) {
// set `options` on our shopConfig and make sure defaults are applied (deep).
_.merge( shopConfig.options = shopConfig.options || {}, options, _.defaults );
var shopDomain = domain.create();
shopDomain.on( "error", function( error ) {
console.error( ( "An error occurred in shop: %s. Details below:", shopConfig.name ) );
console.error( util.inspect( error ) );
console.error( "\n" );
});
shopDomain.run(function() {
// TODO: Investigate any potential optimizations by spinning these up on separate workers.
watchShop( shopConfig );
});
});
}
else {
console.log("No shops to watch. :-(\n");
}
/**
* Sets up the methods to watch and sync to a Shopify shop.
*
* @param {Object} shopConfig The config object for the Shopify shop.
*/
function watchShop ( shopConfig ) {
var directory = shopConfig.directory;
if ( directory ) {
if ( fs.existsSync( directory ) ) {
var shopify = new ShopifySyncer( shopConfig ),
shopOptions = shopConfig.options;
console.log( util.format( "Walking directory tree: %s\n", directory) );
watch.watchTree( directory, shopOptions, function sync( f, curr, prev ) {
if ( typeof f == "object" && prev === null && curr === null ) {
// we're done walking!
console.log( util.format( "Now watching directory tree: %s\n", directory ).rainbow );
}
// we can't actually delete the root (at Shopify), so don't try.
else if ( f !== directory ) {
// `walk` sometimes lets dot files through (usually on creation), so we need to double check.
if ( shopOptions.ignoreDotFiles && path.basename(f)[0] === "." ) {
console.log( util.format( "dotFile file ignored: %s\n", f ) );
return;
}
// `walk` sometimes lets filtered files through (usually on creation), so we need to double check.
if ( !filter( f, curr, blacklist ) ){
console.log( util.format( "filtered file ignored: %s\n", f ) );
return;
}
if ( !filter( f, curr, blacklist ) ){
console.log( util.format( "filtered file ignored: %s\n", f ) );
return;
}
if ( prev === null ) {
// f is a new file or directory
// if we are a file, we can be synced with Shopify (probably)
if ( curr.isFile() ) {
updateTitle( util.format( "Creating: %s\n", f ) );
shopify.create( f, wrap( handleResponse, f + " created" ) );
}
// only some directories can be synced, make sure we are one of these before trying.
else if ( curr.isDirectory() && !filter( f, curr, validDirectories ) ) {
// a directory was created (or just renamed), sync its files.
watch.walk( f, options, function( err, files ) {
for ( var _f in files ){
if ( _f !== f ) {
sync( _f, files[_f], null );
}
}
});
}
}
else if ( curr.nlink === 0 ) {
// f was removed
updateTitle( util.format( "Deleting: %s\n", f ) );
shopify.delete( f, wrap( handleResponse, f + " deleted\n" ) );
}
else {
// f was changed.
// We *should* never need to deal with directories here, as renamed directories
// are treated as delete oldname + create newname
updateTitle( util.format( "Modifying: %s\n", f ) );
shopify.modify( f, wrap( handleResponse, f + " modified\n" ) );
}
}
});
}
else {
console.error( util.format( "Specified directory %s does not exist.\n", directory ) );
}
}
else {
console.error("You must specify a directory.\n");
}
}
/**
* Checks if a file/directory name is in an array.
*
* @param {String} f The pathname of the file being walked.
* @param {Object} stat Not used, but required.
* @param {Array} arr The array to filter on (optional, defaults to blacklist). All entries should be lowercased.
*
* @return {Boolean} False if the file was found in the list, true if it wasn't.
*/
function filter( f, stat, arr ) {
return (arr || blacklist).indexOf( path.basename( f ).toLowerCase() ) === -1;
}
/**
* Applies all arguments after the first to the passed `fn`, appending those arguments on the end.
* arguments.
*
* e.g. wrap( function(){ return arguments; }, "3", "4" )( "1", "2" ) returns [ 1, 2, 3, 4 ] (as an `arguments` object)
*
* @param {Function} fn
* @return {Function} The wrapped fn.
*/
function wrap( fn ) {
var args = [].slice.call( arguments, 0 );
args.shift();
return function(){
fn.apply( this, [].slice.call(arguments, 0).concat( args ) );
};
}
/**
* Handles a shopify response
*
* @param {Object} error
* @param {Object} data
* @param {String} message
*/
function handleResponse ( error, data, message ) {
var titleMsg = "",
consoleMsg = "",
consoleData = null;
if ( error || data.errors ) {
titleMsg = "Failed: " + message;
consoleMsg = titleMsg.red;
consoleData = error || data.errors;
}
else if ( data ){
titleMsg = "Success: " + message;
consoleMsg = titleMsg.green;
consoleData = data;
}
else {
titleMsg = "Failed?: " + message;
consoleMsg = titleMsg.yellow;
consoleData = "[No data]";
}
updateTitle( titleMsg, consoleMsg );
console.log( consoleData );
}
/**
* Updates the node process' title to `titleMsg` and then blinks it, also writes to the console.
*
* After blinking for 5 times the title is reset to its original value.
*
* @param {String} titleMsg The message to display as the title.
* @param {String} consoleMsg The message to write to the console. Defaults to `titleMsg` (optional).
*/
function updateTitle( titleMsg, consoleMsg ) {
// make sure we aren't running multiple timeouts
clearTimeout( updateTitle.interval );
console.log( consoleMsg || titleMsg );
// now, blink the title!
process.title = titleMsg;
(function blink(i){
if ( i > 4 ) {
// reset the title to its orginal value
updateTitle.interval = setTimeout(function(){
process.title = appTitle;
}, 5000);
return;
}
updateTitle.interval = setTimeout(function(){
process.title = "";
updateTitle.interval = setTimeout(function(){
process.title = titleMsg;
blink(++i);
}, 200);
}, 1250);
}(0));
}