# All files / dist/templateTools index.js

13.75% Statements 33/240
0% Branches 0/103
0% Functions 0/34
15.71% Lines 30/191

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
    3644x
    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
     var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.templateApply=exports.templateAdd=exports.templateList=exports.getInstalledTemplateOptions=exports.getTemplateOptions=exports.applyTemplate=exports.checkIfTemplateInstalled=exports.addTemplate=exports.listTemplates=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _chalk=_interopRequireDefault(require("chalk"));
    var _fs=_interopRequireDefault(require("fs"));
    var _path=_interopRequireDefault(require("path"));
    var _inquirer=_interopRequireDefault(require("inquirer"));

    var _constants=require("../constants");
    var _fileutils=require("../systemTools/fileutils");

    var _common=require("../common");
    var _pluginTools=require("../pluginTools");
    var _prompt=require("../systemTools/prompt");
    var _projectParser=require("../projectTools/projectParser");
    var _configParser=require("../configTools/configParser");

    var _templates=require("../../renativeTemplates/templates.json");

    var listTemplates=function listTemplates(c){return new Promise(function(resolve,reject){
    (0,_common.logTask)('listTemplates');
    var opts=getTemplateOptions(c);
    console.log(opts.asString);
    resolve();
    });};exports.listTemplates=listTemplates;

    var addTemplate=function addTemplate(c,template){
    (0,_common.logTask)('addTemplate');

    c.files.project.config.templates=c.files.project.config.templates||{};

    if(!c.files.project.config.templates[template]){
    c.files.project.config.templates[template]={
    version:'latest'};

    }

    _writeObjectSync(c,c.paths.project.config,c.files.project.config);
    };exports.addTemplate=addTemplate;

    var checkIfTemplateInstalled=function checkIfTemplateInstalled(c){return new Promise(function(resolve,reject){
    (0,_common.logTask)('checkIfTemplateInstalled');
    if(!c.buildConfig.templates){
    (0,_common.logWarning)("Your "+_chalk.default.white(c.paths.project.config)+" does not contain "+_chalk.default.white('templates')+" object. ReNative will skip template generation");
    resolve();
    return;
    }

    for(var k in c.buildConfig.templates){
    var t=k;
    var obj=c.buildConfig.templates[k];
    if(k.version&&k.version.startsWith('file:')){
    t="../"+k.version.replace('file:','');
    }
    var templateFolder=_path.default.join(c.paths.project.nodeModulesDir,k);
    if(!_fs.default.existsSync(templateFolder)){
    (0,_common.logWarning)("Your "+_chalk.default.white(templateFolder)+" template is not installed. ReNative will install it for you");

    c._requiresNpmInstall=true;
    }
    if(c.files.project.package.devDependencies){
    c.files.project.package.devDependencies[k]=obj.version;
    }
    }
    _writeObjectSync(c,c.paths.project.package,c.files.project.package);

    resolve();
    });};exports.checkIfTemplateInstalled=checkIfTemplateInstalled;

    var applyTemplate=function applyTemplate(c,selectedTemplate){var opts,_ref,template;return _regenerator.default.async(function applyTemplate$(_context){while(1){switch(_context.prev=_context.next){case 0:
    (0,_common.logTask)("applyTemplate:"+c.buildConfig.currentTemplate+"=>"+selectedTemplate+":");
    c.runtime.selectedTemplate=selectedTemplate;if(

    c.buildConfig.currentTemplate){_context.next=12;break;}
    (0,_common.logWarning)('You don\'t have any current template selected');
    opts=getInstalledTemplateOptions(c);_context.next=7;return _regenerator.default.awrap(

    _inquirer.default.prompt({
    type:'list',
    name:'template',
    message:'Pick which template to apply',
    choices:opts.keysAsArray}));case 7:_ref=_context.sent;template=_ref.template;

    c.buildConfig.currentTemplate=template;
    c.files.project.config.currentTemplate=template;
    _writeObjectSync(c,c.paths.project.config,c.files.project.config);case 12:_context.next=14;return _regenerator.default.awrap(

    _applyTemplate(c));case 14:_context.next=16;return _regenerator.default.awrap(
    _configureSrc(c));case 16:_context.next=18;return _regenerator.default.awrap(
    _configureAppConfigs(c));case 18:_context.next=20;return _regenerator.default.awrap(
    _configureProjectConfig(c));case 20:_context.next=22;return _regenerator.default.awrap(
    _configureRenativeConfig(c));case 22:_context.next=24;return _regenerator.default.awrap(
    _configureEntryPoints(c));case 24:case"end":return _context.stop();}}});};exports.applyTemplate=applyTemplate;

    var _cleanProjectTemplateSync=function _cleanProjectTemplateSync(c){
    (0,_common.logTask)('_cleanProjectTemplateSync');
    var dirsToRemove=[
    _path.default.join(c.paths.project.projectConfig.dir),
    _path.default.join(c.paths.project.srcDir),
    _path.default.join(c.paths.project.appConfigsDir)];

    var filesToRemove=c.buildConfig.defaults.supportedPlatforms.map(function(p){return _path.default.join(c.paths.project.dir,"index."+p+".js");});

    (0,_fileutils.removeDirsSync)(dirsToRemove);

    (0,_fileutils.removeFilesSync)(filesToRemove);
    };

    var _applyTemplate=function _applyTemplate(c){return new Promise(function(resolve,reject){
    (0,_common.logTask)("_applyTemplate:"+c.runtime.selectedTemplate);

    if(c.runtime.selectedTemplate){
    _cleanProjectTemplateSync(c);
    if(c.runtime.isWrapper){
    c.paths.template.dir=_path.default.join(c.paths.project.dir,'packages',c.runtime.selectedTemplate);
    }else{
    c.paths.template.dir=_path.default.join(c.paths.project.nodeModulesDir,c.runtime.selectedTemplate);
    }
    }else{
    c.paths.template.dir=_path.default.join(c.paths.project.nodeModulesDir,c.buildConfig.currentTemplate);
    }

    c.paths.template.configTemplate=_path.default.join(c.paths.template.dir,_constants.RENATIVE_CONFIG_TEMPLATE_NAME);

    if(!_fs.default.existsSync(c.paths.template.configTemplate)){
    (0,_common.logWarning)("Template file "+_chalk.default.white(c.paths.template.configTemplate)+" does not exist. check your "+_chalk.default.white(c.paths.template.dir)+". skipping");
    resolve();
    return;
    }

    (0,_common.logTask)("_applyTemplate:"+c.runtime.selectedTemplate+":"+c.paths.template.dir,_chalk.default.grey);

    c.paths.template.appConfigsDir=_path.default.join(c.paths.template.dir,'appConfigs');
    c.paths.template.projectConfigDir=_path.default.join(c.paths.template.dir,'projectConfig');
    c.runtime.currentTemplate=c.files.project.config.currentTemplate;
    if(!c.runtime.currentTemplate){
    c.runtime.currentTemplate=Object.keys(c.files.project.config.templates)[0];
    c.runtime.requiresForcedTemplateApply=true;
    }

    (0,_configParser.setAppConfig)(c,c.runtime.appId);
    (0,_configParser.generateLocalConfig)(c,!!c.runtime.selectedTemplate);

    resolve();
    });};

    var _configureSrc=function _configureSrc(c){return new Promise(function(resolve,reject){

    (0,_common.logTask)('configureProject:check src',_chalk.default.grey);
    if(!_fs.default.existsSync(c.paths.project.srcDir)){
    (0,_common.logInfo)("Looks like your src folder "+_chalk.default.white(c.paths.project.srcDir)+" is missing! Let's create one for you.");
    (0,_fileutils.copyFolderContentsRecursiveSync)(_path.default.join(c.paths.template.dir,'src'),c.paths.project.srcDir);
    }
    resolve();
    });};

    var _configureAppConfigs=function _configureAppConfigs(c){return new Promise(function(resolve,reject){

    (0,_common.logTask)('configureProject:check appConfigs',_chalk.default.grey);

    if(!_fs.default.existsSync(c.paths.project.appConfigsDir)){
    (0,_common.logInfo)("Looks like your appConfig folder "+
    _chalk.default.white(
    c.paths.project.appConfigsDir)+" is missing! Let's create sample config for you.");

    (0,_fileutils.copyFolderContentsRecursiveSync)(c.paths.template.appConfigsDir,c.paths.project.appConfigsDir);

    var appConfigIds=(0,_configParser.listAppConfigsFoldersSync)(c,true);

    try{var _c$files$project,_c$files$project$defa;
    appConfigIds.forEach(function(v){
    var appConfigPath=_path.default.join(c.paths.project.appConfigsDir,v,_constants.RENATIVE_CONFIG_NAME);
    var appConfig=(0,_fileutils.readObjectSync)(appConfigPath);
    if(appConfig){
    appConfig.common=appConfig.common||{};
    if(!c.runtime.isWrapper){var _c$files$project$conf,_c$files$project$conf2,_c$files$project$conf3,_c$files$project$conf4;
    appConfig.common.title=(_c$files$project$conf=c.files.project.config)==null?void 0:(_c$files$project$conf2=_c$files$project$conf.defaults)==null?void 0:_c$files$project$conf2.title;
    appConfig.common.id=(_c$files$project$conf3=c.files.project.config)==null?void 0:(_c$files$project$conf4=_c$files$project$conf3.defaults)==null?void 0:_c$files$project$conf4.id;
    }

    _writeObjectSync(c,appConfigPath,appConfig);
    }
    });

    var supPlats=(_c$files$project=c.files.project)==null?void 0:(_c$files$project$defa=_c$files$project.defaults)==null?void 0:_c$files$project$defa.supportedPlatforms;

    if(supPlats){
    for(var pk in appConfig.platforms){
    if(!supPlats.includes(pk)){
    delete appConfig.platforms[pk];
    }
    }
    }
    }catch(e){
    (0,_common.logError)(e);
    }
    }
    resolve();
    });};

    var _configureProjectConfig=function _configureProjectConfig(c){return new Promise(function(resolve,reject){

    (0,_common.logTask)('configureProject:check projectConfigs',_chalk.default.grey);
    if(!_fs.default.existsSync(c.paths.project.projectConfig.dir)){
    (0,_common.logInfo)("Looks like your projectConfig folder "+
    _chalk.default.white(c.paths.project.projectConfig.dir)+" is missing! Let's create one for you.");

    (0,_fileutils.copyFolderContentsRecursiveSync)(c.paths.template.projectConfigDir,c.paths.project.projectConfig.dir);
    }
    resolve();
    });};

    var _configureRenativeConfig=function _configureRenativeConfig(c){return new Promise(function(resolve,reject){

    var templateConfig=(0,_fileutils.readObjectSync)(c.paths.template.configTemplate);
    (0,_common.logTask)('configureProject:check renative.json',_chalk.default.grey);
    if(!c.runtime.isWrapper){
    if(c.runtime.selectedTemplate||c.runtime.requiresForcedTemplateApply||c.files.project.config.isNew){
    (0,_common.logWarning)("Looks like your "+
    c.paths.project.config+" need to be updated with "+c.paths.template.configTemplate);

    var mergedObj=(0,_fileutils.mergeObjects)(c,c.files.project.config,templateConfig,false,true);
    mergedObj.currentTemplate=c.runtime.currentTemplate;
    mergedObj.isNew=null;
    delete mergedObj.isNew;
    c.files.project.config=mergedObj;
    _writeObjectSync(c,c.paths.project.config,mergedObj);
    }
    }else{
    if(templateConfig.plugins.renative){
    templateConfig.plugins.renative=(0,_pluginTools.getLocalRenativePlugin)();
    }
    _writeObjectSync(c,c.paths.project.configLocal,templateConfig);
    }
    resolve();
    });};

    var _configureEntryPoints=function _configureEntryPoints(c){return new Promise(function(resolve,reject){
    (0,_common.logTask)('configureEntryPoints');

    try{var _c$buildConfig$defaul;
    if(!_fs.default.existsSync(c.paths.appConfig.config)){
    (0,_common.logWarning)("ERROR: c.paths.appConfig.config: "+c.paths.appConfig.config+" does not exist");
    resolve();
    return;
    }
    var plat;
    var p=c.buildConfig.platforms;
    var supportedPlatforms=(_c$buildConfig$defaul=c.buildConfig.defaults)==null?void 0:_c$buildConfig$defaul.supportedPlatforms;
    for(var k in p){
    if(supportedPlatforms&&supportedPlatforms.includes(k)||!supportedPlatforms){
    plat=p[k];
    var source=_path.default.join(c.paths.template.dir,plat.entryFile+".js");
    var backupSource=_path.default.join(c.paths.rnv.projectTemplate.dir,'entry',plat.entryFile+".js");
    var dest=_path.default.join(c.paths.project.dir,plat.entryFile+".js");
    if(!_fs.default.existsSync(dest)){
    if(!plat.entryFile){
    (0,_common.logError)("You missing entryFile for "+_chalk.default.white(k)+" platform in your "+_chalk.default.white(c.paths.appConfig.config)+".");
    }else if(!_fs.default.existsSync(source)){
    (0,_common.logInfo)("You missing entry file "+_chalk.default.white(source)+" in your template. ReNative Will use default backup entry from "+_chalk.default.white(backupSource)+"!");
    (0,_fileutils.copyFileSync)(backupSource,dest);
    }else{
    (0,_common.logInfo)("You missing entry file "+_chalk.default.white(plat.entryFile)+" in your project. let's create one for you!");
    (0,_fileutils.copyFileSync)(source,dest);
    }
    }
    }else{
    (0,_common.logWarning)("Extra platform "+_chalk.default.white(k)+" will be ignored because it's not configured in your "+_chalk.default.white('./renative.json: { defaults.supportedPlatforms }')+" object.");
    }
    }
    }catch(e){
    reject();
    return;
    }

    resolve();
    });};

    var _writeObjectSync=function _writeObjectSync(c,p,s){
    (0,_fileutils.writeObjectSync)(p,s);
    (0,_configParser.generateBuildConfig)(c);
    };

    var getTemplateOptions=function getTemplateOptions(c){return(0,_prompt.generateOptions)(_templates.templates,false,null,function(i,obj,mapping,defaultVal){var _c$buildConfig$templa;
    var exists=(_c$buildConfig$templa=c.buildConfig.templates)==null?void 0:_c$buildConfig$templa[defaultVal];
    var installed=exists?_chalk.default.red(' (installed)'):'';
    return"-["+_chalk.default.green(i+1)+"] "+_chalk.default.green(defaultVal)+installed+" \n";
    });};exports.getTemplateOptions=getTemplateOptions;

    var getInstalledTemplateOptions=function getInstalledTemplateOptions(c){
    if(c.buildConfig.templates){
    return(0,_prompt.generateOptions)(c.buildConfig.templates);
    }
    (0,_common.logError)('You don\'t have any local templates installed');
    return[];
    };exports.getInstalledTemplateOptions=getInstalledTemplateOptions;

    var templateList=function templateList(c){return new Promise(function(resolve,reject){
    (0,_common.logTask)('templateList');
    listTemplates(c).
    then(function(){return resolve();}).
    catch(function(e){return reject(e);});
    });};exports.templateList=templateList;

    var templateAdd=function templateAdd(c){var opts,_ref2,template;return _regenerator.default.async(function templateAdd$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
    (0,_common.logTask)('templateAdd');

    opts=getTemplateOptions(c);_context2.next=4;return _regenerator.default.awrap(

    _inquirer.default.prompt({
    type:'list',
    message:'Pick which template to install',
    name:'template',
    choices:opts.keysAsArray}));case 4:_ref2=_context2.sent;template=_ref2.template;

    addTemplate(c,template);case 7:case"end":return _context2.stop();}}});};exports.templateAdd=templateAdd;

    var templateApply=function templateApply(c){var opts,_ref3,template;return _regenerator.default.async(function templateApply$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
    (0,_common.logTask)("templateApply:"+c.program.template);if(!

    c.program.template){_context3.next=3;break;}return _context3.abrupt("return",
    applyTemplate(c,c.program.template));case 3:

    opts=getInstalledTemplateOptions(c);_context3.next=6;return _regenerator.default.awrap(

    _inquirer.default.prompt({
    type:'list',
    message:'Pick which template to install',
    name:'template',
    choices:opts.keysAsArray}));case 6:_ref3=_context3.sent;template=_ref3.template;

    applyTemplate(c,template);case 9:case"end":return _context3.stop();}}});};exports.templateApply=templateApply;
    //# sourceMappingURL=index.js.map