-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
456 lines (376 loc) · 13.4 KB
/
index.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/env node
const s3 = require('s3');
const AWS = require('aws-sdk');
const fs = require('fs');
var fse = require('fs-extra');
const request = require('request');
const archiver = require('archiver');
const md5File = require('md5-file');
var authEndpoint = "https://api1.noodlapp.com/auth"
var domainTemplateName = 'template-beta'
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function uploadFiles(opts) {
// Copy files to a temporary dir
var tmpdir = require('os').tmpdir() + '/' + guid();
fs.mkdirSync(tmpdir);
opts.files.forEach((f) => {
fs.copyFileSync((opts.path ? opts.path : '') + f, tmpdir + '/' + f);
})
// Upload tmp dir
function reportProgress(amount, total) {
process.stdout.write((total > 0 ? Math.round(amount / total * 100) : 0) + '%\r');
}
return new Promise(function (resolve, reject) {
var client = s3.createClient({
maxAsyncS3: 20, // this is the default
s3RetryCount: 3, // this is the default
s3RetryDelay: 1000, // this is the default
multipartUploadThreshold: 20971520, // this is the default (20 MB)
multipartUploadSize: 15728640, // this is the default (15 MB)
s3Options: {
accessKeyId: keys.accessKeyId,
secretAccessKey: keys.secretAccessKey,
sessionToken: keys.sessionToken,
// any other options are passed to new AWS.S3()
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property
},
});
var params = {
localDir: tmpdir,
deleteRemoved: false, // default false, whether to remove s3 objects
// that have no corresponding local file.
s3Params: {
Bucket: opts.bucket,
Prefix: opts.prefix,
// other options supported by putObject, except Body and ContentLength.
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
},
};
var uploader = client.uploadDir(params);
uploader.on('error', function (err) {
fse.removeSync(tmpdir);
reject(new Error("Unable to upload dir " + dir));
});
uploader.on('progress', function () {
reportProgress(uploader.progressAmount, uploader.progressTotal);
});
uploader.on('end', function () {
fse.removeSync(tmpdir);
resolve();
});
})
}
var keys,domain;
function getDomainInfo(domain) {
return new Promise(function (resolve, reject) {
request({
method: 'GET',
url: 'http://domains.noodlcloud.com/' + domain + '.json'
}, function (error, response, body) {
if (error || response.statusCode !== 200) reject(error || Error('Could not get domain.'))
else resolve(JSON.parse(response.body))
})
})
}
function getWorkspaceCredentials(name, key) {
return new Promise(function (resolve, reject) {
request({
method: 'GET',
url: authEndpoint + '/workspace/' + encodeURIComponent(name) + '/credentials',
headers: {
'Authorization': key
}
}, function (error, response, body) {
if (error || response.statusCode !== 200) reject(error || Error('Could not get workspace credentials.'))
else resolve(JSON.parse(response.body))
})
})
}
function zipFolderContentWithHash(options) {
var zipTarget = options.cwd + '/' + options.folder + '.zip';
var output = fs.createWriteStream(zipTarget);
var archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
function _readDirRec(dir,_res) {
var res = _res || [];
var files = fs.readdirSync(dir);
files.forEach((f) => {
var stats = fs.statSync(dir + '/' + f);
if(stats && stats.isDirectory()) {
_readDirRec(dir + '/' + f,res)
}
else {
res.push(dir + '/' + f);
}
})
return res;
}
return new Promise(function (resolve, reject) {
var folderPath = options.cwd + '/' + options.folder;
var files = _readDirRec(folderPath);
var f = files.pop();
archive.file(f,{name:f.substring(folderPath.length+1)});
// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function (err) {
console.log(err);
});
// good practice to catch this error explicitly
archive.on('error', function (err) {
reject(err);
});
archive.on('entry', function (arg) {
//console.log(arg.name,arg.sourcePath);
if(files.length > 0) {
var f = files.pop();
archive.file(f,{name:f.substring(folderPath.length+1)});
}
else {
archive.finalize();
}
})
output.on('close', function () {
const hash = md5File.sync(zipTarget);
var zipWithHashTarget = options.cwd + '/' + (options.target || options.folder) + '-' + hash + '.zip';
fs.renameSync(zipTarget, zipWithHashTarget);
//console.log(zipTarget,zipWithHashTarget,hash);
resolve((options.target || options.folder) + '-' + hash + '.zip');
})
// pipe archive data to the file
archive.pipe(output);
})
}
function putJSONObject(options) {
var s3 = new AWS.S3({
accessKeyId: keys.accessKeyId,
secretAccessKey: keys.secretAccessKey,
sessionToken: keys.sessionToken
});
var params = {
Body: JSON.stringify(options.json),
Bucket: options.bucket,
Key: options.key,
};
return new Promise(function (resolve, reject) {
s3.putObject(params, function (err, data) {
if (err) { reject(err) }
else resolve({ json: options.json })
});
})
}
function getJSONObject(options, fn) {
const s3 = new AWS.S3({
accessKeyId: keys.accessKeyId,
secretAccessKey: keys.secretAccessKey,
sessionToken: keys.sessionToken
});
var getParams = {
Bucket: options.bucket,
Key: options.key
}
return new Promise(function (resolve, reject) {
s3.getObject(getParams, function (err, data) {
if (err) return reject(err);
try {
var objectData = data.Body.toString('utf-8'); // Use the encoding necessary
var o = JSON.parse(objectData);
}
catch (e) {
reject(Error('Could not parse JSON.'));
return;
}
resolve(o);
});
})
}
async function putConfig(userConfig) {
// Write user config
try {
await putJSONObject({ bucket: keys.bucket, key: keys.prefix + 'config.json', json: { config: userConfig } });
}
catch (e) {
console.log('Could not write workspace config.');
return;
}
}
function updateTemplateFile(path,args) {
var fileContent = fs.readFileSync(path,'utf8');
if(args.name) fileContent = fileContent.replace(/\{\{name\}\}/g, args.name);
if(args.id) fileContent = fileContent.replace(/\{\{id\}\}/g, args.id);
fs.writeFileSync(path,fileContent);
}
function CreateReactLib(args) {
const dir = argv._[1];
if(!dir) return console.log('Must specify target directory name')
if(fs.existsSync(dir)) return console.log(`Directory ${dir} already exists.`)
try {
fs.mkdirSync(dir);
const template = __dirname + '/templates/create-react-lib/.';
fse.copySync(template,dir);
}
catch(e) {
console.log('Failed to initialize from template',e);
}
if(!args.name) args.name = dir.replace(/\s+/g, '-').toLowerCase();
args.id = guid() + '-' + args.name;
updateTemplateFile(dir + '/module/package.json',args);
updateTemplateFile(dir + '/module.json',args);
console.log('Success');
}
function CreateLib(args) {
const dir = argv._[1];
if(!dir) return console.log('Must specify target directory name')
if(fs.existsSync(dir)) return console.log(`Directory ${dir} already exists.`)
try {
fs.mkdirSync(dir);
const template = __dirname + '/templates/create-lib/.';
fse.copySync(template,dir);
}
catch(e) {
console.log('Failed to initialize from template',e);
}
if(!args.name) args.name = dir.replace(/\s+/g, '-').toLowerCase();
args.id = guid() + '-' + args.name;
updateTemplateFile(dir + '/module/package.json',args);
updateTemplateFile(dir + '/module.json',args);
console.log('Success');
}
function UpdateDesc(args) {
if(!fs.existsSync('./module.json')) return console.log('Must be in library module directory.');
try {
module = JSON.parse(fs.readFileSync('module.json'));
}
catch (e) {
console.log('Could not read module config file.');
return;
}
module.label = args.label || module.label;
module.desc = args.desc || module.desc;
module.docs = args.docs || module.docs;
fs.writeFileSync('module.json',JSON.stringify(module,null,2));
console.log('Success');
}
async function UploadLib(args) {
if(!args.accessKey) return console.log('Must provide workspace access key with --accessKey "XYZ". ')
const _parts = args.accessKey.split('/');
const workspaceId = _parts[0];
const accessKey = _parts[1];
if(accessKey === undefined || workspaceId === undefined)
return console.log('Malformed access key');
try {
domain = await getDomainInfo(domainTemplateName);
keys = await getWorkspaceCredentials(workspaceId, accessKey);
}
catch (e) {
console.log('Could not get workspace credentials, is the access key correct?');
return;
}
try {
module = JSON.parse(fs.readFileSync('module.json'));
}
catch (e) {
console.log('Could not read module config file.');
return;
}
const archive = await zipFolderContentWithHash({ cwd: '.', target: module.name, folder: 'project' })
fs.copyFileSync('icon.png',module.name + '-icon.png');
const files = [archive,module.name + '-icon.png'];
await uploadFiles({
files: files,
path: './',
bucket: keys.bucket,
prefix: keys.prefix + 'library',
})
files.forEach((f) => fs.unlinkSync('./' + f))
try {
var userConfig = await getJSONObject({ bucket: keys.bucket, key: keys.prefix + 'config.json' });
userConfig = userConfig.config;
}
catch (e) {
console.log('Could not retrieve config from workspace.');
console.log(e);
return;
}
if(!userConfig.library) userConfig.library = [];
var lib = userConfig.library.find((l) => l._id === module._id)
if(!lib) {
lib = {_id:module._id}
userConfig.library.push(lib);
}
lib.label = module.label;
lib.desc = module.desc;
lib.url = domain.resourcesEndpoint + '/' + keys.prefix + 'library/' + archive;
lib.thumbURL = domain.resourcesEndpoint + '/' + keys.prefix + 'library/' + module.name + '-icon.png';
lib.docs = module.docs;
await putConfig(userConfig);
console.log('Success');
}
async function RemoveLib(args) {
if(!args.accessKey) return console.log('Must provide workspace access key with --accessKey "XYZ". ')
const _parts = args.accessKey.split('/');
const workspaceId = _parts[0];
const accessKey = _parts[1];
if(accessKey === undefined || workspaceId === undefined)
return console.log('Malformed access key');
try {
keys = await getWorkspaceCredentials(workspaceId, accessKey);
}
catch (e) {
console.log('Could not get workspace credentials, is the access key correct?');
return;
}
try {
module = JSON.parse(fs.readFileSync('module.json'));
}
catch (e) {
console.log('Could not read module config file.');
return;
}
try {
var userConfig = await getJSONObject({ bucket: keys.bucket, key: keys.prefix + 'config.json' });
userConfig = userConfig.config;
}
catch (e) {
console.log('Could not retrieve config from workspace.');
console.log(e);
return;
}
if(userConfig.library === undefined) {
console.log('Workspace has no library.');
return;
}
var libIdx = userConfig.library.findIndex((l) => l._id === module._id)
if(libIdx === -1) {
console.log('Module is not part of this workspace library.')
return;
}
userConfig.library.splice(libIdx,1);
await putConfig(userConfig);
console.log('Success');
}
const commands = {
'create-react-lib':CreateReactLib,
'create-lib':CreateLib,
'push':UploadLib,
'desc':UpdateDesc,
'remove':RemoveLib
}
var argv = require('minimist')(process.argv.slice(2))
const cmd = argv._[0];
if(argv.dev === true) {
// Switch to dev backend
authEndpoint = "https://api2.noodlapp.com/auth"
domainTemplateName = 'template-dev'
}
if(!commands[cmd]) {
console.log('Unknown command');
}
else commands[cmd](argv);