# All files / dist/systemTools fileutils.js

22.76% Statements 66/290
0.59% Branches 1/169
2.22% Functions 1/45
25.2% Lines 62/246

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    3984x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x
    12x
    12x
    4x

    4x

    4x

    4x

    4x
     var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.getFileListSync=exports.replaceHomeFolder=exports.updateConfigFile=exports.mergeObjects=exports.sanitizeDynamicProps=exports.sanitizeDynamicRefs=exports.arrayMerge=exports.getRealPath=exports.updateObjectSync=exports.readObjectSync=exports.writeObjectSync=exports.removeDirSync=exports.removeDirs=exports.removeDirsSync=exports.removeFilesSync=exports.cleanFolder=exports.mkdirSync=exports.removeDir=exports.saveAsJs=exports.copyFolderContentsRecursive=exports.copyFolderContentsRecursiveSync=exports.copyFolderRecursiveSync=exports.copyFileSync=exports.isRunningOnWindows=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _fs=_interopRequireDefault(require("fs"));
    var _path=_interopRequireDefault(require("path"));
    var _rimraf=_interopRequireDefault(require("rimraf"));
    var _svg2js=_interopRequireDefault(require("svg2js"));
    var _shelljs=_interopRequireDefault(require("shelljs"));
    var _deepmerge=_interopRequireDefault(require("deepmerge"));
    var _chalk=_interopRequireDefault(require("chalk"));
    var _ncp=_interopRequireDefault(require("ncp"));
    var _logger=require("./logger");var _this=this;

    var isRunningOnWindows=process.platform==='win32';exports.isRunningOnWindows=isRunningOnWindows;

    var copyFileSync=function copyFileSync(source,target){
    (0,_logger.logDebug)('copyFileSync',source,target);
    var targetFile=target;

    if(source.indexOf('.DS_Store')!==-1)return;

    if(_fs.default.existsSync(target)){
    if(_fs.default.lstatSync(target).isDirectory()){
    targetFile=_path.default.join(target,_path.default.basename(source));
    }
    }
    _fs.default.writeFileSync(targetFile,_fs.default.readFileSync(source));
    };exports.copyFileSync=copyFileSync;

    var copyFolderRecursiveSync=function copyFolderRecursiveSync(source,target){var convertSvg=arguments.length>2&&arguments[2]!==undefined?arguments[2]:true;var skipPaths=arguments.length>3?arguments[3]:undefined;
    (0,_logger.logDebug)('copyFolderRecursiveSync',source,target);
    if(!_fs.default.existsSync(source))return;

    var files=[];

    var targetFolder=_path.default.join(target,_path.default.basename(source));
    if(!_fs.default.existsSync(targetFolder)){
    mkdirSync(targetFolder);
    }

    if(_fs.default.lstatSync(source).isDirectory()){
    files=_fs.default.readdirSync(source);
    files.forEach(function(file){
    var curSource=_path.default.join(source,file);
    if(_fs.default.lstatSync(curSource).isDirectory()){
    copyFolderRecursiveSync(curSource,targetFolder);
    }else if(_path.default.extname(curSource)==='.svg'&&convertSvg===true){
    var jsDest=_path.default.join(targetFolder,_path.default.basename(curSource)+".js");
    (0,_logger.logDebug)("file "+curSource+" is svg and convertSvg is set to true. converitng to "+jsDest);
    saveAsJs(curSource,jsDest);
    }else{
    copyFileSync(curSource,targetFolder);
    }
    });
    }
    };exports.copyFolderRecursiveSync=copyFolderRecursiveSync;

    var copyFolderContentsRecursiveSync=function copyFolderContentsRecursiveSync(source,target){var convertSvg=arguments.length>2&&arguments[2]!==undefined?arguments[2]:true;var skipPaths=arguments.length>3?arguments[3]:undefined;
    (0,_logger.logDebug)('copyFolderContentsRecursiveSync',source,target,skipPaths);
    if(!_fs.default.existsSync(source))return;
    var files=[];
    var targetFolder=_path.default.join(target);
    if(!_fs.default.existsSync(targetFolder)){
    mkdirSync(targetFolder);
    }
    if(_fs.default.lstatSync(source).isDirectory()){
    files=_fs.default.readdirSync(source);
    files.forEach(function(file){
    var curSource=_path.default.join(source,file);
    if(!skipPaths||skipPaths&&!skipPaths.includes(curSource)){
    if(_fs.default.lstatSync(curSource).isDirectory()){
    copyFolderRecursiveSync(curSource,targetFolder,convertSvg,skipPaths);
    }else{
    copyFileSync(curSource,targetFolder);
    }
    }
    });
    }
    };exports.copyFolderContentsRecursiveSync=copyFolderContentsRecursiveSync;

    var copyFolderContentsRecursive=function copyFolderContentsRecursive(source,target){var convertSvg=arguments.length>2&&arguments[2]!==undefined?arguments[2]:true;var skipPaths=arguments.length>3?arguments[3]:undefined;return new Promise(function(resolve,reject){
    (0,_logger.logDebug)('copyFolderContentsRecursive',source,target,skipPaths);
    if(!_fs.default.existsSync(source))return;
    var targetFolder=_path.default.resolve(target);
    if(!_fs.default.existsSync(targetFolder)){
    mkdirSync(targetFolder);
    }
    (0,_ncp.default)(source,targetFolder,function(err){
    if(err){
    return reject(err);
    }
    return resolve();
    });
    });};exports.copyFolderContentsRecursive=copyFolderContentsRecursive;

    var saveAsJs=function saveAsJs(source,dest){
    _svg2js.default.createSync({
    source:source,
    destination:dest});

    };exports.saveAsJs=saveAsJs;

    var removeDir=function removeDir(path,callback){
    (0,_rimraf.default)(path,callback);
    };exports.removeDir=removeDir;

    var mkdirSync=function mkdirSync(dir){
    try{
    _shelljs.default.mkdir('-p',dir);
    }catch(e){
    (0,_logger.logWarning)("shelljs.mkdir failed for dir: "+dir+" with error: "+e);
    }
    };exports.mkdirSync=mkdirSync;

    var cleanFolder=function cleanFolder(d){return new Promise(function(resolve,reject){
    (0,_logger.logDebug)('cleanFolder',d);
    removeDir(d,function(){
    mkdirSync(d);
    resolve();
    });
    });};exports.cleanFolder=cleanFolder;

    var removeFilesSync=function removeFilesSync(filePaths){
    (0,_logger.logDebug)('removeFilesSync',filePaths);
    filePaths.forEach(function(filePath){
    try{
    if(_fs.default.existsSync(filePath)){
    _fs.default.unlinkSync(filePath);
    }else{
    (0,_logger.logDebug)("Path "+filePath+" does not exist");
    }
    }catch(e){
    (0,_logger.logError)(e);
    }
    });
    };exports.removeFilesSync=removeFilesSync;

    var removeDirsSync=function removeDirsSync(dirPaths){
    (0,_logger.logDebug)('removeDirsSync',dirPaths);

    for(var i=0;i<dirPaths.length;i++){
    try{
    removeDirSync(dirPaths[i]);
    }catch(e){
    (0,_logger.logError)(e);
    }
    }
    };exports.removeDirsSync=removeDirsSync;

    var removeDirs=function removeDirs(dirPaths){return new Promise(function(resolve,reject){
    (0,_logger.logDebug)('removeDirs',dirPaths);
    var allFolders=dirPaths.length;
    var deletedFolders=0;
    for(var i=0;i<allFolders;i++){
    (0,_rimraf.default)(dirPaths[i],function(e){
    if(e){
    (0,_logger.logError)(e);
    }
    deletedFolders++;
    if(deletedFolders>=allFolders)resolve();
    });
    }
    if(allFolders===0)resolve();
    });};exports.removeDirs=removeDirs;

    var removeDirSync=function removeDirSync(dir,rmSelf){
    var files;
    rmSelf=rmSelf===undefined?true:rmSelf;
    dir+='/';
    try{files=_fs.default.readdirSync(dir);}catch(e){(0,_logger.logDebug)('!Oops, directory not exist.');return;}
    if(files.length>0){
    files.forEach(function(x,i){
    if(_fs.default.statSync(dir+x).isDirectory()){
    removeDirSync(dir+x);
    }else{
    _fs.default.unlinkSync(dir+x);
    }
    });
    }
    if(rmSelf){

    _fs.default.rmdirSync(dir);
    }
    };exports.removeDirSync=removeDirSync;

    var writeObjectSync=function writeObjectSync(filePath,obj,spaces){var addNewLine=arguments.length>3&&arguments[3]!==undefined?arguments[3]:true;
    if(addNewLine){
    _fs.default.writeFileSync(filePath,JSON.stringify(obj,null,spaces||4)+"\n");
    }else{
    _fs.default.writeFileSync(filePath,JSON.stringify(obj,null,spaces||4));
    }
    };exports.writeObjectSync=writeObjectSync;

    var readObjectSync=function readObjectSync(filePath){var sanitize=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;var c=arguments.length>2?arguments[2]:undefined;
    (0,_logger.logDebug)("readObjectSync:"+sanitize+":"+filePath);
    if(!filePath){
    (0,_logger.logWarning)('readObjectSync: filePath is undefined');
    return null;
    }
    if(!_fs.default.existsSync(filePath)){
    (0,_logger.logWarning)("readObjectSync: File at "+filePath+" does not exist");
    return null;
    }
    var obj;
    try{
    obj=JSON.parse(_fs.default.readFileSync(filePath));
    if(sanitize){
    (0,_logger.logDebug)("readObjectSync: will sanitize file at: "+filePath);
    if(c){
    obj=sanitizeDynamicRefs(c,obj);
    }
    if(obj._refs){
    obj=sanitizeDynamicProps(obj,obj._refs);
    }
    }
    }catch(e){
    (0,_logger.logWarning)("readObjectSync: Parsing of "+_chalk.default.white(filePath)+" failed with "+e);
    return null;
    }
    return obj;
    };exports.readObjectSync=readObjectSync;

    var updateObjectSync=function updateObjectSync(filePath,updateObj){
    var output;
    var obj=readObjectSync(filePath);
    if(obj){
    output=(0,_deepmerge.default)(obj,updateObj);
    writeObjectSync(filePath,output);
    }
    return output;
    };exports.updateObjectSync=updateObjectSync;

    var getRealPath=function getRealPath(c,p){var key=arguments.length>2&&arguments[2]!==undefined?arguments[2]:'undefined';var original=arguments.length>3?arguments[3]:undefined;
    if(!p){
    (0,_logger.logInfo)("Path "+_chalk.default.white(key)+" is not defined. using default: "+_chalk.default.white(original));
    return original;
    }
    if(p.startsWith('./')){
    return _path.default.join(c.paths.project.dir,p);
    }
    return p.replace(/RNV_HOME/g,c.paths.rnv.dir).
    replace(/~/g,c.paths.home.dir).
    replace(/USER_HOME/g,c.paths.home.dir).
    replace(/PROJECT_HOME/g,c.paths.project.dir);
    };exports.getRealPath=getRealPath;

    var _refToValue=function _refToValue(c,ref,key){
    var val=ref.replace('$REF$:','').split('$...');

    var realPath=getRealPath(c,val[0],key);

    if(realPath&&realPath.includes('.json')&&val.length===2){
    if(_fs.default.existsSync(realPath)){
    var obj=readObjectSync(realPath);

    try{
    var output=val[1].split('.').reduce(function(o,i){return o[i];},obj);
    return output;
    }catch(e){
    (0,_logger.logWarning)("_refToValue: "+e);
    }
    }else{
    (0,_logger.logWarning)("_refToValue: "+_chalk.default.white(realPath)+" does not exist!");
    }
    }
    return ref;
    };

    var arrayMerge=function arrayMerge(destinationArray,sourceArray,mergeOptions){
    var jointArray=destinationArray.concat(sourceArray);
    var uniqueArray=jointArray.filter(function(item,index){return jointArray.indexOf(item)===index;});
    return uniqueArray;
    };exports.arrayMerge=arrayMerge;

    var _arrayMergeOverride=function _arrayMergeOverride(destinationArray,sourceArray,mergeOptions){return sourceArray;};

    var sanitizeDynamicRefs=function sanitizeDynamicRefs(c,obj){
    if(!obj)return obj;
    if(Array.isArray(obj)){
    obj.forEach(function(v){
    sanitizeDynamicRefs(c,v);
    });
    }
    Object.keys(obj).forEach(function(key){
    var val=obj[key];
    if(val){
    if(typeof val==='string'){
    if(val.startsWith('$REF$:')){
    obj[key]=_refToValue(c,val,key);
    }
    }else{
    sanitizeDynamicRefs(c,val);
    }
    }
    });
    return obj;
    };exports.sanitizeDynamicRefs=sanitizeDynamicRefs;

    var sanitizeDynamicProps=function sanitizeDynamicProps(obj,props){
    if(!obj||!props)return obj;
    if(Array.isArray(obj)){
    obj.forEach(function(v){
    if(typeof val==='string'){
    Object.keys(props).forEach(function(pk){
    val=val.replace("@"+pk+"@",props[pk]);
    obj[key]=val;
    });
    }else{
    sanitizeDynamicProps(v,props);
    }
    });
    }
    Object.keys(obj).forEach(function(key){
    var val=obj[key];
    if(val){
    if(typeof val==='string'){
    Object.keys(props).forEach(function(pk){
    val=val.replace("@"+pk+"@",props[pk]);
    obj[key]=val;
    });
    }else{
    sanitizeDynamicProps(val,props);
    }
    }
    });
    return obj;
    };exports.sanitizeDynamicProps=sanitizeDynamicProps;

    var mergeObjects=function mergeObjects(c,obj1,obj2){var dynamicRefs=arguments.length>3&&arguments[3]!==undefined?arguments[3]:true;var replaceArrays=arguments.length>4&&arguments[4]!==undefined?arguments[4]:false;
    if(!obj2)return obj1;
    if(!obj1)return obj2;
    var obj=(0,_deepmerge.default)(obj1,obj2,{arrayMerge:replaceArrays?_arrayMergeOverride:arrayMerge});
    return dynamicRefs?sanitizeDynamicRefs(c,obj):obj;
    };exports.mergeObjects=mergeObjects;

    var updateConfigFile=function updateConfigFile(update,globalConfigPath){var configContents;return _regenerator.default.async(function updateConfigFile$(_context){while(1){switch(_context.prev=_context.next){case 0:
    configContents=JSON.parse(_fs.default.readFileSync(globalConfigPath));

    if(update.androidSdk){
    configContents.sdks.ANDROID_SDK=update.androidSdk;
    }

    if(update.tizenSdk){
    configContents.sdks.TIZEN_SDK=update.tizenSdk;
    }

    if(update.webosSdk){
    configContents.sdks.WEBOS_SDK=update.webosSdk;
    }

    (0,_logger.logDebug)("Updating "+_this.globalConfigPath+" with "+JSON.stringify(update,null,3));

    _fs.default.writeFileSync(globalConfigPath,JSON.stringify(configContents,null,3));case 6:case"end":return _context.stop();}}});};exports.updateConfigFile=updateConfigFile;

    var replaceHomeFolder=function replaceHomeFolder(p){
    ifI(isRunningOnWindows)return p.replace('~',process.env.USERPROFILE);
    return p.replace('~',process.env.HOME);
    };exports.replaceHomeFolder=replaceHomeFolder;

    var getFileListSync=function getFileListSync(dir){
    var results=[];
    var list=_fs.default.readdirSync(dir);
    list.forEach(function(file){
    file=dir+"/"+file;
    var stat=_fs.default.statSync(file);
    if(stat&&stat.isDirectory()){

    results=results.concat(getFileListSync(file));
    }else{

    results.push(file);
    }
    });
    return results;
    };exports.getFileListSync=getFileListSync;var _default=

    {
    sanitizeDynamicRefs:sanitizeDynamicRefs,
    getFileListSync:getFileListSync,
    removeDirs:removeDirs,
    copyFileSync:copyFileSync,
    copyFolderRecursiveSync:copyFolderRecursiveSync,
    removeDir:removeDir,
    removeDirsSync:removeDirsSync,
    removeFilesSync:removeFilesSync,
    saveAsJs:saveAsJs,
    mkdirSync:mkdirSync,
    copyFolderContentsRecursive:copyFolderContentsRecursive,
    copyFolderContentsRecursiveSync:copyFolderContentsRecursiveSync,
    cleanFolder:cleanFolder,
    writeObjectSync:writeObjectSync,
    readObjectSync:readObjectSync,
    updateObjectSync:updateObjectSync,
    arrayMerge:arrayMerge,
    mergeObjects:mergeObjects,
    updateConfigFile:updateConfigFile,
    replaceHomeFolder:replaceHomeFolder};exports.default=_default;
    //# sourceMappingURL=fileutils.js.map