e1c-repo-tools
Version:
Tools for 1C enterprise repository
144 lines (143 loc) • 7.34 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_1 = require("fs");
const util_1 = require("util");
const process_1 = require("process");
const console_operations_1 = require("./console-operations");
const utils_1 = require("./utils");
const git_utils_1 = require("./git-utils");
const getE1cRepoConfig = async () => {
const pjson = JSON.parse(await fs_1.promises.readFile('./package.json', {
'encoding': 'utf8',
}));
if (pjson.e1cRepoConfig) {
return pjson.e1cRepoConfig;
}
throw new Error('Repo config not found in "package.json"');
};
const externalBinFileTypesInfo = {
'ExternalReport': { 'name': 'ExternalReport', 'extension': 'epf' },
'ExternalDataProcessor': { 'name': 'ExternalDataProcessor', 'extension': 'epf' },
};
const getTypeInfoOfDumpedBinFile = async (pathToRootSrcFile) => {
const rootSrcFileData = await fs_1.promises.readFile(pathToRootSrcFile, { 'encoding': 'utf8' });
const entries = Object.entries(externalBinFileTypesInfo);
for (let i = 0; i < entries.length; i += 1) {
if (rootSrcFileData.indexOf(entries[i][1].name) >= 0) {
return entries[i][1];
}
}
return undefined;
};
class E1cDispatcher {
constructor(e1cRepoConfig) {
this.pathToExecutable = e1cRepoConfig.pathToExecutable;
this.pathToSrcDir = e1cRepoConfig.pathToSrcDir || './src';
this.pathToDistDir = e1cRepoConfig.pathToDistDir || './dist';
this.filesExtensions = [...e1cRepoConfig.filesExtensions || ['erf', 'epf']];
this.pathToLogsDir = e1cRepoConfig.pathToLogsDir || './logs';
}
static async initWithLocalConfig() {
const config = await getE1cRepoConfig();
if (!config.pathToExecutable) {
throw new Error('Path to 1C executable undefined');
}
return new E1cDispatcher(config);
}
async DumpExternalBinFile(pathToBinFile) {
const basename = path_1.default.basename(pathToBinFile);
const basenameWithoutExt = basename.indexOf('.') < 0 ? basename : basename.split('.').filter((str) => str.length > 0).slice(0, -1).join('.');
const pathToSrcFiles = path_1.default.join(path_1.default.resolve(this.pathToSrcDir), basename);
const pathToLogFile = path_1.default.join(this.pathToLogsDir, `${utils_1.dateToLogString(new Date())}_${basename}.log`);
if (fs_1.existsSync(path_1.default.resolve(pathToSrcFiles))) {
const changes = await git_utils_1.getDirStatus(pathToSrcFiles);
if (changes.length > 0) {
const hash = await git_utils_1.revParse();
const backUpName = `${pathToSrcFiles}.${hash.length > 0 ? `${hash.slice(0, 8)}.` : ''}bak`;
let newBackupName = backUpName;
let index = 0;
while (fs_1.existsSync(newBackupName)) {
index += 1;
newBackupName = `${backUpName}${index}`;
}
await fs_1.promises.rename(pathToSrcFiles, newBackupName);
}
else {
await utils_1.removeDir(pathToSrcFiles);
}
}
if (!fs_1.existsSync(path_1.default.resolve(this.pathToLogsDir))) {
await fs_1.promises.mkdir(path_1.default.resolve(this.pathToLogsDir));
}
await console_operations_1.performOsTask(this.pathToExecutable, [
'DESIGNER',
'/DumpExternalDataProcessorOrReportToFiles',
path_1.default.join(pathToSrcFiles, basenameWithoutExt),
path_1.default.resolve(pathToBinFile),
'-Format', 'Hierarchical',
'/Out', pathToLogFile,
], `Dumping ${pathToBinFile}`, undefined, async () => {
const regexp = /Выгрузка завершена[^\d]+(?<time>\d+)/gmiu;
const textDecoder = new util_1.TextDecoder('windows-1251');
const logText = textDecoder.decode(await fs_1.promises.readFile(pathToLogFile));
const match = regexp.exec(logText);
if (match && match.groups) {
return `${match.groups.time}ms`;
}
return Error('fail');
});
return { pathToBinFile, pathToSrcFiles };
}
async BuildExternalBinFile(pathToRootSrcFile) {
const basename = path_1.default.basename(pathToRootSrcFile);
const basenameWithoutExt = basename.indexOf('.') < 0 ? basename : basename.split('.').filter((str) => str.length > 0).slice(0, -1).join('.');
const pathToSrcFiles = path_1.default.dirname(pathToRootSrcFile);
const pathToLogFile = path_1.default.join(this.pathToLogsDir, `${utils_1.dateToLogString(new Date())}_${basename}.log`);
const fileTypeInfo = await getTypeInfoOfDumpedBinFile(pathToRootSrcFile);
if (!fileTypeInfo) {
console_operations_1.error('Unknown file type', path_1.default.relative(process_1.cwd(), pathToRootSrcFile));
return { 'pathToBinFile': '', pathToSrcFiles };
}
const pathToBinFile = path_1.default.join(this.pathToDistDir, `${basenameWithoutExt}.${fileTypeInfo.extension}`);
// TODO: code duplication with DumpExternalBinFile()
if (fs_1.existsSync(path_1.default.resolve(pathToBinFile))) {
const changes = await git_utils_1.getDirStatus(pathToBinFile);
if (changes.length > 0) {
const hash = await git_utils_1.revParse();
const backUpName = `${pathToBinFile}.${hash.length > 0 ? `${hash.slice(0, 8)}.` : ''}bak`;
let newBackupName = backUpName;
let index = 0;
while (fs_1.existsSync(newBackupName)) {
index += 1;
newBackupName = `${backUpName}${index}`;
}
await fs_1.promises.rename(pathToBinFile, newBackupName);
}
}
if (!fs_1.existsSync(path_1.default.resolve(this.pathToLogsDir))) {
await fs_1.promises.mkdir(path_1.default.resolve(this.pathToLogsDir));
}
await console_operations_1.performOsTask(this.pathToExecutable, [
'DESIGNER',
'/LoadExternalDataProcessorOrReportFromFiles',
pathToRootSrcFile,
path_1.default.resolve(pathToBinFile),
'/Out', pathToLogFile,
], `Building ${path_1.default.relative(process_1.cwd(), pathToSrcFiles)}`, undefined, async () => {
const regexp = /Загрузка завершена[^\d]+(?<time>\d+)/gmiu;
const textDecoder = new util_1.TextDecoder('windows-1251');
const logText = textDecoder.decode(await fs_1.promises.readFile(pathToLogFile));
const match = regexp.exec(logText);
if (match && match.groups) {
return `${match.groups.time}ms`;
}
return Error('fail');
});
return { pathToBinFile, pathToSrcFiles };
}
}
exports.default = E1cDispatcher;