react-native-integrate
Version:
Automate integration of additional code into React Native projects
76 lines (75 loc) • 2.66 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.summary = void 0;
exports.jsonTask = jsonTask;
exports.runTask = runTask;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const applyObjectModification_1 = require("../utils/applyObjectModification");
const checkCondition_1 = require("../utils/checkCondition");
const getErrMessage_1 = require("../utils/getErrMessage");
const getProjectPath_1 = require("../utils/getProjectPath");
const setState_1 = require("../utils/setState");
const variables_1 = require("../variables");
function jsonTask(args) {
let { content } = args;
const { task } = args;
for (const action of task.actions) {
variables_1.variables.set('CONTENT', content);
if (action.when && !(0, checkCondition_1.checkCondition)(action.when)) {
(0, setState_1.setState)(action.name, {
state: 'skipped',
reason: 'when',
});
continue;
}
(0, setState_1.setState)(action.name, {
state: 'progress',
});
try {
content = (0, applyObjectModification_1.applyObjectModification)(content, action);
(0, setState_1.setState)(action.name, {
state: 'done',
});
}
catch (e) {
(0, setState_1.setState)(action.name, {
state: 'error',
reason: (0, getErrMessage_1.getErrMessage)(e),
});
throw e;
}
}
return content;
}
function getJsonPath(filePath) {
const projectPath = (0, getProjectPath_1.getProjectPath)();
const jsonPath = path_1.default.join(projectPath, filePath);
// security check
if (!jsonPath.startsWith(projectPath)) {
throw new Error('invalid destination path');
}
return jsonPath;
}
function readJsonContent(filePath) {
const jsonPath = getJsonPath(filePath);
return fs_1.default.existsSync(jsonPath)
? JSON.parse(fs_1.default.readFileSync(jsonPath, 'utf-8'))
: {};
}
function writeJsonContent(content, filePath) {
const jsonPath = getJsonPath(filePath);
return fs_1.default.writeFileSync(jsonPath, JSON.stringify(content, null, 2), 'utf-8');
}
function runTask(args) {
let content = readJsonContent(args.task.path);
content = jsonTask({
...args,
content,
});
writeJsonContent(content, args.task.path);
}
exports.summary = 'Json file modification';