@rnv/engine-core
Version:
ReNative Engine Core
230 lines • 12.1 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var path_1 = tslib_1.__importDefault(require("path"));
var core_1 = require("@rnv/core");
var _loadAppConfigIDfromDir = function (dirName, appConfigsDir) {
(0, core_1.logDebug)("_loadAppConfigIDfromDir:".concat(dirName, ":").concat(appConfigsDir), (0, core_1.chalk)().grey);
var filePath = path_1.default.join(appConfigsDir, 'renative.json');
if ((0, core_1.fsExistsSync)(filePath)) {
try {
var renativeConf = JSON.parse((0, core_1.fsReadFileSync)(filePath).toString());
return { dir: dirName, id: renativeConf.id };
}
catch (e) {
(0, core_1.logError)("File ".concat(filePath, " is MALFORMED:\n").concat(e));
}
}
return { dir: dirName, id: null };
};
var _askUserAboutConfigs = function (ctx, dir, id, basePath) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var choice, conf, filePath, fileContents;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
(0, core_1.logTask)('_askUserAboutConfigs');
(0, core_1.logWarning)("AppConfig error - It seems you have a mismatch between appConfig folder name (".concat(dir, ") and the id defined in renative.json (").concat(id, "). They must match."));
if (ctx.program.opts().ci === true) {
throw new Error('You cannot continue if you set --ci flag. please fix above error first');
}
return [4 /*yield*/, (0, core_1.inquirerPrompt)({
type: 'list',
name: 'choice',
message: 'You must choose what you want to keep',
choices: [
// {
// name: `Keep ID from renative.json (${id}) and rename the folder (${dir} -> ${id})`,
// value: 'keepID'
// },
{
name: "Keep folder name (".concat(dir, ") and rename the ID from renative.json (").concat(id, " -> ").concat(dir, ")"),
value: 'keepFolder',
},
(0, core_1.inquirerSeparator)(),
{
name: "I'll do it manually",
value: 'manually',
},
],
})];
case 1:
choice = (_a.sent()).choice;
if (choice === 'manually') {
throw new Error('Please do the changes and rerun the command');
}
conf = { id: id, dir: dir };
if (choice === 'keepID') {
conf.dir = path_1.default.join(basePath, id);
(0, core_1.fsRenameSync)(path_1.default.join(basePath, dir), conf.dir);
}
if (choice === 'keepFolder') {
filePath = path_1.default.join(basePath, dir, 'renative.json');
fileContents = JSON.parse((0, core_1.fsReadFileSync)(filePath).toString());
fileContents.id = dir;
conf.id = dir;
(0, core_1.writeFileSync)(filePath, fileContents);
}
return [2 /*return*/, conf];
}
});
}); };
/* eslint-disable no-await-in-loop */
var matchAppConfigID = function (ctx, appConfigID) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var _a, appConfigsDirs, appConfigsDirNames, acIndex, conf, dir, id;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
(0, core_1.logTask)('matchAppConfigID', "appId:".concat(appConfigID));
if (!appConfigID)
return [2 /*return*/, false];
_a = ctx.paths.project, appConfigsDirs = _a.appConfigsDirs, appConfigsDirNames = _a.appConfigsDirNames;
acIndex = appConfigsDirNames.indexOf(appConfigID);
if (!(acIndex !== -1)) return [3 /*break*/, 3];
conf = _loadAppConfigIDfromDir(appConfigID, appConfigsDirs[acIndex]);
dir = conf.dir, id = conf.id;
if (!(id !== dir)) return [3 /*break*/, 2];
return [4 /*yield*/, _askUserAboutConfigs(ctx, conf.dir, conf.id, path_1.default.join(appConfigsDirs[acIndex], '..'))];
case 1:
conf = _b.sent();
_b.label = 2;
case 2: return [2 /*return*/, conf.id];
case 3: return [2 /*return*/, false];
}
});
}); };
var _findAndSwitchAppConfigDir = function (ctx) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var appConfigsDirNames, conf;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
(0, core_1.logTask)('_findAndSwitchAppConfigDir');
appConfigsDirNames = ctx.paths.project.appConfigsDirNames;
if (!appConfigsDirNames.length) return [3 /*break*/, 2];
if (appConfigsDirNames.length === 1) {
// we have only one, skip the question
(0, core_1.logInfo)("Found only one app config available. Will use ".concat((0, core_1.chalk)().bold.white(appConfigsDirNames[0])));
_setAppId(ctx, appConfigsDirNames[0]);
return [2 /*return*/, true];
}
return [4 /*yield*/, (0, core_1.inquirerPrompt)({
name: 'conf',
type: 'list',
message: 'Which one would you like to pick?',
choices: appConfigsDirNames,
pageSize: 50,
logMessage: 'ReNative found multiple existing appConfigs',
})];
case 1:
conf = (_a.sent()).conf;
if (conf) {
_setAppId(ctx, conf);
return [2 /*return*/, true];
}
_a.label = 2;
case 2: return [2 /*return*/, false];
}
});
}); };
var _setAppId = function (ctx, appId) {
var _a, _b, _c;
var currentAppConfigId = (_c = (_b = (_a = ctx.files.project) === null || _a === void 0 ? void 0 : _a.configLocal) === null || _b === void 0 ? void 0 : _b._meta) === null || _c === void 0 ? void 0 : _c.currentAppConfigId;
(0, core_1.logTask)('_setAppId', "appId:".concat(appId, " runtime.appId:").concat(ctx.runtime.appId, " _meta.appId:").concat(currentAppConfigId));
ctx.runtime.appId = appId || ctx.runtime.appId || currentAppConfigId;
ctx.runtime.appDir = path_1.default.join(ctx.paths.project.builds.dir, "".concat(ctx.runtime.appId, "_").concat(ctx.platform));
};
var appConfigure = function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var ctx, appConfigsDirsExt, hasAppConfig, aid, hasAppConfig;
var _a, _b;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
ctx = (0, core_1.getContext)();
ctx.paths.project.appConfigsDirNames = (0, core_1.listAppConfigsFoldersSync)(true);
ctx.paths.project.appConfigsDirNames.forEach(function (dirName) {
ctx.paths.project.appConfigsDirs.push(path_1.default.join(ctx.paths.project.appConfigsDir, dirName));
});
appConfigsDirsExt = (_b = (_a = ctx.buildConfig) === null || _a === void 0 ? void 0 : _a.paths) === null || _b === void 0 ? void 0 : _b.appConfigsDirs;
if (appConfigsDirsExt) {
appConfigsDirsExt.forEach(function (apePath) {
var appConfigsExt = (0, core_1.listAppConfigsFoldersSync)(true, apePath);
appConfigsExt.forEach(function (appExtName) {
ctx.paths.project.appConfigsDirNames.push(appExtName);
ctx.paths.project.appConfigsDirs.push(path_1.default.join(apePath, appExtName));
});
});
}
// Reset appId if appConfig no longer exists but renative.local.json still has reference to it
if (ctx.runtime.appId && !ctx.paths.project.appConfigsDirNames.includes(ctx.runtime.appId)) {
ctx.runtime.appId = undefined;
}
if (!(ctx.program.opts().appConfigID === true || (!ctx.program.opts().appConfigID && !ctx.runtime.appId))) return [3 /*break*/, 2];
return [4 /*yield*/, _findAndSwitchAppConfigDir(ctx)];
case 1:
hasAppConfig = _c.sent();
if (!hasAppConfig) {
// await executeTask(c, RnvTaskName.appCreate, RnvTaskName.appConfigure);
// return Promise.reject('No app configs found for this project');
(0, core_1.logWarning)('No app configs found for this project');
return [2 /*return*/, true];
}
return [3 /*break*/, 6];
case 2:
if (!ctx.program.opts().appConfigID) return [3 /*break*/, 6];
return [4 /*yield*/, matchAppConfigID(ctx, ctx.program.opts().appConfigID)];
case 3:
aid = _c.sent();
if (!!aid) return [3 /*break*/, 5];
(0, core_1.logWarning)("Cannot find app config ".concat((0, core_1.chalk)().bold.white(ctx.program.opts().appConfigID)));
return [4 /*yield*/, _findAndSwitchAppConfigDir(ctx)];
case 4:
hasAppConfig = _c.sent();
if (!hasAppConfig) {
// await executeTask(c, RnvTaskName.appCreate, RnvTaskName.appConfigure);
// return Promise.reject('No app configs found for this project');
(0, core_1.logWarning)('No app configs found for this project');
return [2 /*return*/, true];
}
_c.label = 5;
case 5:
_setAppId(ctx, aid);
_c.label = 6;
case 6:
// Generate true path to appConfig (ensure external appConfigsDirs are included)
if (ctx.runtime.appId) {
ctx.runtime.appConfigDir =
ctx.paths.project.appConfigsDirs[ctx.paths.project.appConfigsDirNames.indexOf(ctx.runtime.appId)];
}
return [2 /*return*/, true];
}
});
}); };
exports.default = (0, core_1.createTask)({
description: 'Configure project with specific appConfig',
fn: function (_a) {
var ctx = _a.ctx;
return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
if (ctx.runtime.isAppConfigured)
return [2 /*return*/, true];
return [4 /*yield*/, appConfigure()];
case 1:
_b.sent();
return [4 /*yield*/, (0, core_1.updateRenativeConfigs)()];
case 2:
_b.sent();
(0, core_1.logAppInfo)(ctx);
ctx.runtime.isAppConfigured = true;
return [4 /*yield*/, (0, core_1.checkAndInstallPackageDependenciesIfRequired)()];
case 3:
_b.sent();
return [2 /*return*/, true];
}
});
});
},
task: core_1.RnvTaskName.appConfigure,
options: core_1.RnvTaskOptionPresets.withConfigure(),
});
//# sourceMappingURL=taskAppConfigure.js.map
;