@focuson/cod
Version:
A command line tool to help with the code on demand
34 lines (33 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonTransformer = void 0;
const Strings_1 = require("./Strings");
const path = require('path');
class JsonTransformer {
constructor(files) { this.files = files; }
makeStringReplaceData(pathAndSha) {
let name = pathAndSha.parsedPath.name;
return ({ fromMatcher: new RegExp(`#${name}/render#`, 'gi'), to: `created/${name}/${pathAndSha.sha}` });
}
toFileName(sourceAndTargetDir, fileName) { return path.join(sourceAndTargetDir.targetDir, fileName); }
processOneJsonFile(sourceAndTargetDir, stringReplaceData) {
return file => {
let parsedPath = path.parse(path.join(sourceAndTargetDir.jsonSourceDir, file));
return this.files.copyAndChangeFile(parsedPath, Strings_1.Strings.replaceMultipleStrings(stringReplaceData), this.toFileName(sourceAndTargetDir, file));
};
}
updateJsonFiles(sourceAndTargetDir) {
this.validate(sourceAndTargetDir);
return pathAndShas => {
let stringReplaceData = pathAndShas.map(this.makeStringReplaceData);
return this.files.forEachFile(sourceAndTargetDir.jsonSourceDir)(this.processOneJsonFile(sourceAndTargetDir, stringReplaceData));
};
}
validate(sourceAndTargetDir) {
return Promise.all([
this.files.validateDirectoryExists("Json Source Directory", sourceAndTargetDir.jsonSourceDir),
this.files.validateDirectoryExists("Target Directory", sourceAndTargetDir.targetDir)
]);
}
}
exports.JsonTransformer = JsonTransformer;