adr
Version:
轻量级架构记录工具 - Command-line tools for working with Architecture Decision Records
54 lines • 4.87 kB
JavaScript
///<reference path="generate.ts"/>
let fs = require('fs');
import Utils from './utils';
import { generate } from './generate';
import Config from './Config';
import getAdrFiles from './helpers/getAdrFiles';
let savePath = Config.getSavePath();
function generateNewFileName(newIndex, title) {
let fileExt = Config.getDocExtension();
let indexString = Utils.createIndexByNumber(newIndex);
let decisionInfile = Utils.generateFileName(title);
return indexString + '-' + decisionInfile + '.' + fileExt;
}
function updateNameByTitle() {
let files = getAdrFiles();
files.forEach(function (file) {
let fileName = file.relativePath;
let fileExt = Config.getDocExtension();
let startChar = fileExt === 'adoc' ? '=' : '#';
let fileData = fs.readFileSync(savePath + fileName, 'utf8');
let firstLine = fileData.split('\n')[0];
let indexRegexValue = new RegExp(String.raw `${startChar}\s(\d+)\.\s`, '');
let title = firstLine.replace(indexRegexValue, '');
let indexRegex = indexRegexValue.exec(firstLine);
let oldIndex;
if (!indexRegex) {
oldIndex = Utils.getIndexByString(fileName);
if (!oldIndex) {
return;
}
}
else {
oldIndex = indexRegex[1];
}
let newIndex = parseInt(oldIndex, 10);
let newFileName = generateNewFileName(newIndex, title);
if (fileName !== newFileName) {
console.log(fileName + ' -> ' + newFileName);
fs.renameSync(savePath + fileName, savePath + newFileName);
}
});
}
function updateToc() {
let fileExt = Config.getDocExtension();
let toc = generate('toc', { output: false });
fs.writeFileSync(savePath + 'README.' + fileExt, toc + '\n');
}
export function update() {
console.log('update decisions ...');
updateNameByTitle();
console.log('update adr toc ...');
updateToc();
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXBkYXRlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2xpYi91cGRhdGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsa0NBQWtDO0FBQ2xDLElBQUksRUFBRSxHQUFHLE9BQU8sQ0FBQyxJQUFJL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