@cyklang/cli 0.5.3 → 0.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/AssetCommand.d.ts +24 -0
- package/build/AssetCommand.js +207 -178
- package/build/Cmd.d.ts +17 -12
- package/build/Cmd.js +82 -39
- package/build/DBClient.js +1 -0
- package/build/InstallCommand.d.ts +8 -7
- package/build/InstallCommand.js +148 -73
- package/build/ModuleCommand.d.ts +6 -0
- package/build/ModuleCommand.js +12 -1
- package/build/UninstallCommand.d.ts +20 -0
- package/build/UninstallCommand.js +164 -0
- package/build/index.js +3 -1
- package/package.json +2 -2
- package/src/AssetCommand.ts +212 -197
- package/src/Cmd.ts +59 -38
- package/src/DBClient.ts +1 -0
- package/src/InstallCommand.ts +156 -75
- package/src/ModuleCommand.ts +12 -0
- package/src/UninstallCommand.ts +145 -0
- package/src/index.ts +3 -1
package/src/AssetCommand.ts
CHANGED
|
@@ -7,7 +7,7 @@ import path from "path"
|
|
|
7
7
|
import mime from 'mime-types'
|
|
8
8
|
import inquirer from 'inquirer'
|
|
9
9
|
// import readline from 'readline'
|
|
10
|
-
import { DBAsset, DBExecuteRequest, DBManager, ObjectData, parseXML, PrimitiveData } from "@cyklang/core"
|
|
10
|
+
import { DBAsset, DBExecuteRequest, DBManager, DBRemote, ObjectData, parseXML, PrimitiveData } from "@cyklang/core"
|
|
11
11
|
import FormData from 'form-data'
|
|
12
12
|
|
|
13
13
|
const logger = getLogger('AssetCommand.ts')
|
|
@@ -112,7 +112,7 @@ class AssetDelete extends Cmd {
|
|
|
112
112
|
|
|
113
113
|
try {
|
|
114
114
|
await this.prologue(options)
|
|
115
|
-
if (!
|
|
115
|
+
if (!this.dbManager) throw 'dbManager undefined'
|
|
116
116
|
const dbClient = new DBClient(this.dbManager)
|
|
117
117
|
const list = await dbClient.selectFromTable('Assets to delete', 'cyk_asset',
|
|
118
118
|
{
|
|
@@ -121,9 +121,9 @@ class AssetDelete extends Cmd {
|
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
);
|
|
124
|
-
const reply = await inquirer.prompt({type: 'confirm', name: 'confirm', message: 'Do you want to delete these ' + list.length +
|
|
124
|
+
const reply = await inquirer.prompt({ type: 'confirm', name: 'confirm', message: 'Do you want to delete these ' + list.length + ' file(s)' })
|
|
125
125
|
if (reply.confirm) {
|
|
126
|
-
for(let ind = 0; ind < list.length; ind++) {
|
|
126
|
+
for (let ind = 0; ind < list.length; ind++) {
|
|
127
127
|
const asset = list[ind]
|
|
128
128
|
logger.info('delete ' + asset.asset_route)
|
|
129
129
|
const url = '/api/admin/assets/' + asset.asset_id
|
|
@@ -168,7 +168,7 @@ class AssetList extends Cmd {
|
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
interface FileDescriptor {
|
|
171
|
+
export interface FileDescriptor {
|
|
172
172
|
path: string
|
|
173
173
|
mtime: Date
|
|
174
174
|
id?: number
|
|
@@ -193,16 +193,7 @@ class AssetU extends Cmd {
|
|
|
193
193
|
})
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
|
|
197
|
-
let result: string | false
|
|
198
|
-
if (path.extname(filename) === '.cyk') {
|
|
199
|
-
result = 'application/xml'
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
result = mime.lookup(filename)
|
|
203
|
-
}
|
|
204
|
-
return result
|
|
205
|
-
}
|
|
196
|
+
|
|
206
197
|
|
|
207
198
|
/**
|
|
208
199
|
* method commandU
|
|
@@ -271,7 +262,7 @@ class AssetU extends Cmd {
|
|
|
271
262
|
if (dest.substring(dest.length - 1) !== '/') dest = dest + '/'
|
|
272
263
|
|
|
273
264
|
if (options.clean) {
|
|
274
|
-
await
|
|
265
|
+
await cleanDestination(dest, this.dbManager, this.dbRemote)
|
|
275
266
|
}
|
|
276
267
|
|
|
277
268
|
for (let ind = 0; ind < sources.length; ind++) {
|
|
@@ -279,12 +270,12 @@ class AssetU extends Cmd {
|
|
|
279
270
|
if (fs.existsSync(source) === false) throw 'source ' + source + ' does not exist'
|
|
280
271
|
const filestat = fs.lstatSync(source)
|
|
281
272
|
if (filestat.isDirectory() === true) {
|
|
282
|
-
await
|
|
273
|
+
await uploadDirectory(source, dest, options, this.dbManager, this.dbRemote)
|
|
283
274
|
}
|
|
284
275
|
else if (filestat.isFile() === true) {
|
|
285
276
|
const base = path.parse(source).base
|
|
286
277
|
logger.debug('upload file ' + source + ' to ' + dest + base)
|
|
287
|
-
await
|
|
278
|
+
await uploadAsset({ path: source, mtime: filestat.mtime }, dest + base, this.dbManager, this.dbRemote, options.auth)
|
|
288
279
|
}
|
|
289
280
|
}
|
|
290
281
|
}
|
|
@@ -292,212 +283,236 @@ class AssetU extends Cmd {
|
|
|
292
283
|
logger.error(err)
|
|
293
284
|
}
|
|
294
285
|
}
|
|
286
|
+
}
|
|
295
287
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
288
|
+
/**
|
|
289
|
+
* function cleanDestination
|
|
290
|
+
* @param dest
|
|
291
|
+
* @param dbManager
|
|
292
|
+
* @param dbRemote
|
|
293
|
+
*/
|
|
294
|
+
async function cleanDestination(dest: string, dbManager: DBManager | undefined, dbRemote: DBRemote | undefined) {
|
|
295
|
+
|
|
296
|
+
const remoteList = await scanAssets(dbManager)
|
|
297
|
+
for (let ind = 0; ind < remoteList.length; ind++) {
|
|
298
|
+
const fd = remoteList[ind]
|
|
299
|
+
if (fd.path.startsWith(dest)) {
|
|
300
|
+
const url = '/api/admin/assets/' + fd.id
|
|
301
|
+
await dbRemote?.apiServer.delete(url)
|
|
309
302
|
}
|
|
310
303
|
}
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* function uploadDirectory
|
|
307
|
+
* @param source
|
|
308
|
+
* @param dest
|
|
309
|
+
* @param options {yes: no confirmation, auth: basic/token/cookie/undefined}
|
|
310
|
+
* @param dbManager
|
|
311
|
+
* @param dbRemote
|
|
312
|
+
*/
|
|
313
|
+
export async function uploadDirectory(source: string, dest: string, options: any,
|
|
314
|
+
dbManager: DBManager | undefined, dbRemote: DBRemote | undefined) {
|
|
315
|
+
|
|
316
|
+
const localList: FileDescriptor[] = []
|
|
317
|
+
let dirName: string
|
|
318
|
+
if (source.substring(source.length - 1) === '/') {
|
|
319
|
+
// trailing directory separator
|
|
320
|
+
dirName = source
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
dirName = source + '/'
|
|
324
|
+
}
|
|
311
325
|
|
|
312
|
-
|
|
313
|
-
// uploadDirectory
|
|
314
|
-
//----------------------------------------------------------------------------------------------
|
|
315
|
-
|
|
316
|
-
async uploadDirectory(source: string, dest: string, options: any) {
|
|
317
|
-
|
|
318
|
-
const localList: FileDescriptor[] = []
|
|
319
|
-
let dirName: string
|
|
320
|
-
if (source.substring(source.length - 1) === '/') {
|
|
321
|
-
// trailing directory separator
|
|
322
|
-
dirName = source
|
|
323
|
-
}
|
|
324
|
-
else {
|
|
325
|
-
dirName = source + '/'
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
// logger.debug('options', options)
|
|
329
|
-
|
|
330
|
-
this.scanDir(dirName, localList)
|
|
331
|
-
|
|
332
|
-
const remoteList = await scanAssets(this.dbManager)
|
|
333
|
-
|
|
334
|
-
const uploadList = this.buildUploadList(dirName, dest, localList, remoteList)
|
|
326
|
+
// logger.debug('options', options)
|
|
335
327
|
|
|
336
|
-
|
|
337
|
-
if (uploadList.length === 0) throw 'None file to upload'
|
|
328
|
+
scanDir(dirName, localList)
|
|
338
329
|
|
|
339
|
-
|
|
330
|
+
const remoteList = await scanAssets(dbManager)
|
|
340
331
|
|
|
341
|
-
|
|
342
|
-
const reply = await inquirer.prompt({ type: 'confirm', name: 'confirm', message: 'Do you want to upload this(these) ' + uploadList.length + ' file(s) ?' })
|
|
343
|
-
if (reply.confirm === true) uploadConfirmed = true
|
|
344
|
-
}
|
|
332
|
+
const uploadList = buildUploadList(dirName, dest, localList, remoteList)
|
|
345
333
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
}
|
|
334
|
+
// logger.info(fileNames.join('\n'))
|
|
335
|
+
if (uploadList.length === 0) throw 'None file to upload'
|
|
349
336
|
|
|
350
|
-
|
|
351
|
-
// rl.question('Do you want to upload this(these) ' + uploadList.length + ' file(s) (Y/N/y/n) ?', async (reply) => {
|
|
352
|
-
// rl.close()
|
|
353
|
-
// if (reply !== 'Y' && reply !== 'y') {
|
|
354
|
-
// logger.info('upload cancelled')
|
|
355
|
-
// return
|
|
356
|
-
// }
|
|
337
|
+
let uploadConfirmed = (options.yes !== undefined)
|
|
357
338
|
|
|
358
|
-
|
|
359
|
-
|
|
339
|
+
if (uploadConfirmed === false) {
|
|
340
|
+
const reply = await inquirer.prompt({ type: 'confirm', name: 'confirm', message: 'Do you want to upload this(these) ' + uploadList.length + ' file(s) ?' })
|
|
341
|
+
if (reply.confirm === true) uploadConfirmed = true
|
|
360
342
|
}
|
|
361
343
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
344
|
+
if (uploadConfirmed === true) {
|
|
345
|
+
await uploadFiles(dirName, uploadList, dest, dbManager, dbRemote, options.auth)
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* function buildUploadList
|
|
350
|
+
* @param dirName
|
|
351
|
+
* @param dest
|
|
352
|
+
* @param localList
|
|
353
|
+
* @param remoteList
|
|
354
|
+
* @returns
|
|
355
|
+
*/
|
|
356
|
+
function buildUploadList(dirName: string, dest: string, localList: FileDescriptor[], remoteList: FileDescriptor[]): FileDescriptor[] {
|
|
357
|
+
const result: FileDescriptor[] = []
|
|
358
|
+
const remoteMap = new Map<string, FileDescriptor>()
|
|
359
|
+
for (let ind = 0; ind < remoteList.length; ind++) {
|
|
360
|
+
const remote = remoteList[ind]
|
|
361
|
+
remoteMap.set(remote.path, remote)
|
|
362
|
+
}
|
|
363
|
+
for (let ind = 0; ind < localList.length; ind++) {
|
|
364
|
+
const localDesc = localList[ind]
|
|
365
|
+
const route = dest + localDesc.path.substring(dirName.length)
|
|
366
|
+
const remoteDesc = remoteMap.get(route)
|
|
367
|
+
|
|
368
|
+
if (remoteDesc === undefined || remoteDesc.mtime.getTime() + 1000 < localDesc.mtime.getTime()) {
|
|
369
|
+
if (remoteDesc === undefined) logger.debug('remoteDesc undefined')
|
|
370
|
+
else logger.debug('remoteDesc.mtime ' + remoteDesc.mtime.getTime(), 'localDesc.mtime ' + localDesc.mtime.getTime())
|
|
371
|
+
logger.info(localDesc.path)
|
|
372
|
+
result.push(localDesc)
|
|
384
373
|
}
|
|
385
|
-
return result
|
|
386
374
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
result.push({ path: dirName + fileName, mtime: fstat.mtime })
|
|
408
|
-
// logger.debug(dirName + '/' + fileName)
|
|
375
|
+
return result
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* function scanDir
|
|
379
|
+
* @param dirName
|
|
380
|
+
* @param result
|
|
381
|
+
*/
|
|
382
|
+
function scanDir(dirName: string, result: FileDescriptor[]) {
|
|
383
|
+
const files = fs.readdirSync(dirName)
|
|
384
|
+
for (let ind = 0; ind < files.length; ind++) {
|
|
385
|
+
const fileName = files[ind]
|
|
386
|
+
if (file2exclude(fileName) === true) continue
|
|
387
|
+
const fstat = fs.statSync(dirName + fileName)
|
|
388
|
+
if (fstat.isDirectory() === true) {
|
|
389
|
+
scanDir(dirName + fileName + '/', result)
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
if (mimetypeLookup(fileName) === false) {
|
|
393
|
+
logger.info(fileName + ' file extension is unknown and will not be uploaded')
|
|
394
|
+
continue
|
|
409
395
|
}
|
|
396
|
+
//const dbAssetExist = this.dbManager?.dbAssetExist()
|
|
397
|
+
result.push({ path: dirName + fileName, mtime: fstat.mtime })
|
|
398
|
+
// logger.debug(dirName + '/' + fileName)
|
|
410
399
|
}
|
|
411
400
|
}
|
|
401
|
+
}
|
|
412
402
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
403
|
+
const ignorePatterns = ['.git', 'node_modules']
|
|
404
|
+
/**
|
|
405
|
+
* function file2exclude
|
|
406
|
+
* @param fileName
|
|
407
|
+
* @returns
|
|
408
|
+
*/
|
|
409
|
+
function file2exclude(fileName: string): boolean {
|
|
410
|
+
let result = false
|
|
411
|
+
ignorePatterns.forEach((pattern) => {
|
|
412
|
+
if (fileName === pattern) result = true
|
|
413
|
+
})
|
|
414
|
+
return result
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* function uploadFiles
|
|
418
|
+
* @param dirName
|
|
419
|
+
* @param uploadList
|
|
420
|
+
* @param dest
|
|
421
|
+
* @param dbManager
|
|
422
|
+
* @param dbRemote
|
|
423
|
+
* @param auth
|
|
424
|
+
*/
|
|
425
|
+
async function uploadFiles(dirName: string, uploadList: FileDescriptor[], dest: string, dbManager: DBManager | undefined, dbRemote: DBRemote | undefined, auth: string | undefined) {
|
|
426
|
+
for (let ind = 0; ind < uploadList.length; ind++) {
|
|
427
|
+
const upload = uploadList[ind]
|
|
428
|
+
const route = dest + upload.path.substring(dirName.length)
|
|
429
|
+
logger.debug('upload ' + upload.path + ' to ' + route)
|
|
430
|
+
await uploadAsset(upload, route, dbManager, dbRemote, auth)
|
|
425
431
|
}
|
|
432
|
+
}
|
|
426
433
|
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
434
|
+
/**
|
|
435
|
+
* function uploadAsset
|
|
436
|
+
* @param upload
|
|
437
|
+
* @param route
|
|
438
|
+
* @param dbManager
|
|
439
|
+
* @param dbRemote
|
|
440
|
+
* @param auth
|
|
441
|
+
*/
|
|
442
|
+
export async function uploadAsset(upload: FileDescriptor, route: string, dbManager: DBManager | undefined, dbRemote: DBRemote | undefined, auth?: string) {
|
|
443
|
+
// logger.debug('uploadAsset ' + upload.path + ' to route ' + route + ' with mimetype ' + this.mimetypeLookup(upload.path) + ', auth : ' + auth)
|
|
430
444
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
logger.debug('upload ' + upload.path + ' to ' + route)
|
|
436
|
-
await this.uploadAsset(upload, route, auth)
|
|
445
|
+
try {
|
|
446
|
+
let dbAsset: DBAsset | undefined = await dbManager?.dbAssetExist(route)
|
|
447
|
+
if (dbAsset === undefined) {
|
|
448
|
+
dbAsset = new DBAsset()
|
|
437
449
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
//
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
dbAsset.
|
|
455
|
-
dbAsset.route = route
|
|
456
|
-
dbAsset.auth = auth
|
|
457
|
-
if (dbAsset.auth === 'none') dbAsset.auth = undefined
|
|
458
|
-
|
|
459
|
-
// const fstat = fs.statSync(fileName)
|
|
460
|
-
dbAsset.last_update = upload.mtime
|
|
461
|
-
|
|
462
|
-
// const contentBuffer = fs.readFileSync(fileName)
|
|
463
|
-
// dbAsset.content = contentBuffer
|
|
464
|
-
// logger.debug('dbAsset.content length ' + dbAsset.content.length)
|
|
465
|
-
|
|
466
|
-
if (dbAsset.id === undefined) {
|
|
467
|
-
dbAsset.id = await this.dbManager?.dbAssetInsert(dbAsset)
|
|
468
|
-
logger.info('inserted asset ' + dbAsset.id + ' route ' + dbAsset.route)
|
|
469
|
-
}
|
|
470
|
-
else {
|
|
471
|
-
await this.dbManager?.dbAssetUpdate(dbAsset)
|
|
472
|
-
logger.info('updated asset ' + dbAsset.route)
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
await this.uploadAssetContent(dbAsset, upload.path)
|
|
450
|
+
const mimetype = mimetypeLookup(upload.path)
|
|
451
|
+
if (mimetype === false) throw 'mimetype false for ' + upload
|
|
452
|
+
dbAsset.mimetype = mimetype
|
|
453
|
+
dbAsset.route = route
|
|
454
|
+
dbAsset.auth = auth
|
|
455
|
+
if (dbAsset.auth === 'none') dbAsset.auth = undefined
|
|
456
|
+
|
|
457
|
+
// const fstat = fs.statSync(fileName)
|
|
458
|
+
dbAsset.last_update = upload.mtime
|
|
459
|
+
|
|
460
|
+
// const contentBuffer = fs.readFileSync(fileName)
|
|
461
|
+
// dbAsset.content = contentBuffer
|
|
462
|
+
// logger.debug('dbAsset.content length ' + dbAsset.content.length)
|
|
463
|
+
|
|
464
|
+
if (dbAsset.id === undefined) {
|
|
465
|
+
dbAsset.id = await dbManager?.dbAssetInsert(dbAsset)
|
|
466
|
+
logger.info('inserted asset ' + dbAsset.id + ' route ' + dbAsset.route)
|
|
476
467
|
}
|
|
477
|
-
|
|
478
|
-
|
|
468
|
+
else {
|
|
469
|
+
await dbManager?.dbAssetUpdate(dbAsset)
|
|
470
|
+
logger.info('updated asset ' + dbAsset.route)
|
|
479
471
|
}
|
|
472
|
+
|
|
473
|
+
await uploadAssetContent(dbAsset, upload.path, dbRemote)
|
|
474
|
+
}
|
|
475
|
+
catch (err) {
|
|
476
|
+
logger.error(err)
|
|
480
477
|
}
|
|
478
|
+
}
|
|
481
479
|
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
480
|
+
/**
|
|
481
|
+
* function mimetypeLookup
|
|
482
|
+
* @param filename
|
|
483
|
+
* @returns
|
|
484
|
+
*/
|
|
485
|
+
function mimetypeLookup(filename: string): string | false {
|
|
486
|
+
let result: string | false
|
|
487
|
+
if (path.extname(filename) === '.cyk') {
|
|
488
|
+
result = 'application/xml'
|
|
489
|
+
}
|
|
490
|
+
else {
|
|
491
|
+
result = mime.lookup(filename)
|
|
492
|
+
}
|
|
493
|
+
return result
|
|
494
|
+
}
|
|
485
495
|
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
496
|
+
/**
|
|
497
|
+
* function uploadAssetContent
|
|
498
|
+
* @param dbAsset
|
|
499
|
+
* @param fileName
|
|
500
|
+
* @param dbRemote
|
|
501
|
+
*/
|
|
502
|
+
async function uploadAssetContent(dbAsset: DBAsset, fileName: string, dbRemote: DBRemote | undefined) {
|
|
503
|
+
logger.debug('uploadAssetContent file ' + fileName + ' to path ' + path)
|
|
504
|
+
try {
|
|
505
|
+
const file = fs.createReadStream(fileName)
|
|
506
|
+
const form = new FormData()
|
|
507
|
+
form.append('uploadFile', file)
|
|
508
|
+
|
|
509
|
+
const route = '/api/upload/cyk_asset/asset_content?asset_id=' + dbAsset.id
|
|
510
|
+
const resp = await dbRemote?.apiServer.post(route, form)
|
|
511
|
+
if (resp.status === 200) {
|
|
512
|
+
logger.info(fileName + ' uploaded')
|
|
501
513
|
}
|
|
502
514
|
}
|
|
515
|
+
catch (err) {
|
|
516
|
+
logger.error(err)
|
|
517
|
+
}
|
|
503
518
|
}
|
package/src/Cmd.ts
CHANGED
|
@@ -5,7 +5,8 @@ import loglevel from 'loglevel'
|
|
|
5
5
|
import { NodeCrypto } from "./NodeCrypto";
|
|
6
6
|
import { DBClient } from "./DBClient";
|
|
7
7
|
import { exec, spawn } from 'child_process';
|
|
8
|
-
import {red, green, bold} from 'kolorist'
|
|
8
|
+
import { red, green, bold } from 'kolorist'
|
|
9
|
+
import * as fs from 'fs'
|
|
9
10
|
|
|
10
11
|
const logger = loglevel.getLogger('Cmd.ts')
|
|
11
12
|
logger.setLevel('debug')
|
|
@@ -115,44 +116,64 @@ export class Cmd extends Command {
|
|
|
115
116
|
});
|
|
116
117
|
}
|
|
117
118
|
|
|
118
|
-
/**
|
|
119
|
-
* method runShellCommand
|
|
120
|
-
* @param command
|
|
121
|
-
*/
|
|
122
|
-
async runShellCommand(command: string, args: string[]) {
|
|
123
|
-
try {
|
|
124
|
-
await this.spawnCommand(command, args)
|
|
125
|
-
// console.log(command)
|
|
126
|
-
// const result = await this.executeCommand(command);
|
|
127
|
-
// console.log(result.stdout);
|
|
128
|
-
// if (result.stderr && result.stderr.trim() !== '')
|
|
129
|
-
// console.error(result.stderr);
|
|
130
|
-
} catch (error) {
|
|
131
|
-
console.error('Error running "' + command + '":', error);
|
|
132
|
-
throw error
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
119
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
120
|
+
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* method runShellCommand
|
|
126
|
+
* @param command
|
|
127
|
+
*/
|
|
128
|
+
export async function runShellCommand(command: string, args: string[]) {
|
|
129
|
+
try {
|
|
130
|
+
await spawnCommand(command, args)
|
|
131
|
+
// console.log(command)
|
|
132
|
+
// const result = await this.executeCommand(command);
|
|
133
|
+
// console.log(result.stdout);
|
|
134
|
+
// if (result.stderr && result.stderr.trim() !== '')
|
|
135
|
+
// console.error(result.stderr);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.error('Error running "' + command + '":', error);
|
|
138
|
+
throw error
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* function spawnCommand
|
|
144
|
+
* @param command
|
|
145
|
+
* @param args
|
|
146
|
+
* @returns
|
|
147
|
+
*/
|
|
148
|
+
export function spawnCommand(command: string, args: string[]): Promise<void> {
|
|
149
|
+
console.log()
|
|
150
|
+
console.log([command, ...args, '...'].join(' '))
|
|
151
|
+
const child_process = spawn(command, args, { stdio: 'inherit' })
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
child_process.on('close', (code) => {
|
|
154
|
+
if (code === 0) {
|
|
155
|
+
console.log(green('*') + ' ' + [command, ...args].join(' '))
|
|
156
|
+
resolve()
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
reject(new Error([command, ...args].join(' ') + ' --> Error ' + code))
|
|
160
|
+
}
|
|
156
161
|
})
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* function readPackageJson
|
|
167
|
+
* @returns
|
|
168
|
+
*/
|
|
169
|
+
export async function readPackageJson(): Promise<any> {
|
|
170
|
+
try {
|
|
171
|
+
if (!fs.existsSync("package.json")) throw 'package.json not found'
|
|
172
|
+
const fileContent = fs.readFileSync("package.json")
|
|
173
|
+
return JSON.parse(fileContent.toString())
|
|
174
|
+
}
|
|
175
|
+
catch (err) {
|
|
176
|
+
console.error(err)
|
|
177
|
+
throw err
|
|
157
178
|
}
|
|
158
179
|
}
|
package/src/DBClient.ts
CHANGED