wv-gotcha
Version:
 wv-local-service-bus # Purpose To accumulate your walkthroughs to accomplish your common tasks with less effort.
99 lines • 6.39 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UTemplateTranslator = void 0;
const decova_filesystem_1 = require("decova-filesystem");
const path_1 = __importDefault(require("path"));
const temp_circuits_1 = require("temp-circuits");
const _Register_1 = require("temp-circuits/dist/Decorators/_Register");
const Shell_1 = require("../../Shell");
const UPathMan_1 = require("../../UPathMan");
const UTextTranslator_1 = require("./UTextTranslator");
const wv_filesystem_1 = require("wv-filesystem");
let UTemplateTranslator = class UTemplateTranslator extends temp_circuits_1.RootCircuit {
translateGenericTemplateFileAsync(templateDirName_1, srcFileRelPath_1, translationDictionary_1) {
return __awaiter(this, arguments, void 0, function* (templateDirName, srcFileRelPath, translationDictionary, overwrite = false) {
yield this._ensurePlaceholderValuesAsync(srcFileRelPath, translationDictionary);
let absRootSrcDir = path_1.default.join(UPathMan_1.UPathMan.$().genericGenDirTemplates.FullName, templateDirName)
.xReplaceAll('\\', '/');
const srcAbsFilePath = wv_filesystem_1.Path.join(absRootSrcDir, srcFileRelPath).xReplaceAll("\\", "/");
const dstRootDir = UPathMan_1.UPathMan.$().currentDir.FullName;
const dstFileRelPath = UTextTranslator_1.UTextTranslator.$().translateGenericTemplateFile(srcFileRelPath, translationDictionary);
const dstAbsFilePath = wv_filesystem_1.Path.join(dstRootDir, dstFileRelPath).xReplaceAll('\\', '/');
if (!overwrite && new decova_filesystem_1.FileInfo(dstAbsFilePath).exists()) {
Shell_1.Shell.warning(`Already existing file skipped: ${dstAbsFilePath}`);
return false;
}
Shell_1.Shell.info(`Creating file: ${dstAbsFilePath}`);
const contentToTranslate = new decova_filesystem_1.FileInfo(srcAbsFilePath).readAllText();
yield this._ensurePlaceholderValuesAsync(contentToTranslate, translationDictionary);
const translatedContent = UTextTranslator_1.UTextTranslator.$().translateGenericTemplateFile(contentToTranslate, translationDictionary);
const newFile = new decova_filesystem_1.FileInfo(dstAbsFilePath);
newFile.directory.Ensure();
newFile.writeAllText(translatedContent);
return true;
});
}
_ensurePlaceholderValuesAsync(text, translationDictionary) {
return __awaiter(this, void 0, void 0, function* () {
const placehoderMatches = temp_circuits_1.Regex.of(/___[0-9A-z\$_]+___/g).getMatches(text);
for (let placeholderMatch of placehoderMatches) {
const existing = translationDictionary.xFirstOrDefault(i => i.from === placeholderMatch.value);
if (!existing) {
const value = yield Shell_1.Shell.askForTextAsync(placeholderMatch.value);
translationDictionary.xAdd({ from: placeholderMatch.value, to: value });
}
}
});
}
translate(srcRootDirPath, toTranslateFileSelector, translationMap = new Map(), overwrite = false) {
let absRootSrcDir = path_1.default.join(UPathMan_1.UPathMan.$().codeTemplatesDir.FullName, srcRootDirPath)
.xReplaceAll('\\', '/');
const filesToTranslate = new decova_filesystem_1.DirectoryInfo(absRootSrcDir)
.GetDescendantFiles()
.xSelect(f => f.fullName)
.xWhere(f => toTranslateFileSelector(f))
.xSelect(f => f.xReplaceAll('\\', '/'));
let count = 0;
for (let fPath of filesToTranslate) {
const fSrcRelPath = fPath.xReplaceFirstOccurence(absRootSrcDir, '');
let fDstAbsPath = path_1.default.join(UPathMan_1.UPathMan.$().currentDir.FullName, fSrcRelPath);
fDstAbsPath = fDstAbsPath.xReplaceAll('$$$', '.');
fDstAbsPath = UTextTranslator_1.UTextTranslator.$().translate(fDstAbsPath, translationMap);
if (!overwrite && new decova_filesystem_1.FileInfo(fDstAbsPath).exists()) {
Shell_1.Shell.warning(`File skipped (It already exists): ${fDstAbsPath}`);
continue;
}
Shell_1.Shell.info(`Creating file: ${fDstAbsPath}`);
const contentToTranslate = new decova_filesystem_1.FileInfo(fPath).readAllText();
const translatedContent = UTextTranslator_1.UTextTranslator.$().translate(contentToTranslate, translationMap);
const newFile = new decova_filesystem_1.FileInfo(fDstAbsPath);
newFile.directory.Ensure();
newFile.writeAllText(translatedContent);
count++;
}
return count;
}
};
exports.UTemplateTranslator = UTemplateTranslator;
exports.UTemplateTranslator = UTemplateTranslator = __decorate([
(0, _Register_1.Register)()
], UTemplateTranslator);
//# sourceMappingURL=UTemplateTranslator.js.map