@afuteam-nx/plugin-pick-keyinfo-from-file-base-repos
Version:
批量统计团队代码仓库中配置文件是否有指定的配置,如 `package.json` 中是否有 依赖 `@afuteam/eslint-plugin-fe` 安装等 支持批量。
115 lines • 4.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path = require("path");
const fs = require("fs");
const Toml = require("toml");
const index_js_1 = require("./utils/index.js");
function runExecutor(options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
console.log('\npick-keyinfo-from-file-base-repos 插件执行的入参:\n', options);
const { fileType, fileName, targetKey, localFileRepoListPath, localAllReposCodePath, resultPath, } = options;
function loadFileList() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const data = yield fs.promises.readFile(localFileRepoListPath, 'utf-8');
return JSON.parse(data);
});
}
function findFilePath(startPath, name = '') {
const filePath = path.join(startPath, name);
if (fs.existsSync(filePath)) {
return filePath;
}
return null;
}
// 读取 json 的 fileName 内容
function readJsonFile(directoryPath) {
const packageJsonPath = findFilePath(directoryPath, fileName);
const packageJson = {
dependencies: {},
devDependencies: {},
};
if (!packageJsonPath) {
return packageJson;
}
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf-8');
try {
const { dependencies, devDependencies } = JSON.parse(packageJsonContent);
packageJson.dependencies = dependencies || {};
packageJson.devDependencies = devDependencies || {};
}
catch (error) {
console.error(`\n读取目录 ${directoryPath} 下的 ${fileName} 文件内容异常: `, error);
}
return packageJson;
}
// 读取 toml 的文件内容
function readTomlFile(directoryPath) {
const packageTomlPath = findFilePath(directoryPath, fileName);
let packageToml = {};
if (!packageTomlPath) {
return packageToml;
}
packageToml = Toml.parse(fs.readFileSync(packageTomlPath, 'utf-8'));
return packageToml;
}
function getTargetKeyInfo(directoryPath) {
var _a, _b;
const result = {
key: targetKey,
value: '',
};
const rootHasFile = fs.existsSync(path.join(directoryPath, fileName));
if (rootHasFile) {
let fileJson = {};
if (fileType === 'json') {
fileJson = Object.assign(Object.assign({}, (_a = readJsonFile(directoryPath)) === null || _a === void 0 ? void 0 : _a.dependencies), (_b = readJsonFile(directoryPath)) === null || _b === void 0 ? void 0 : _b.devDependencies);
}
if (fileType === 'toml') {
fileJson = readTomlFile(directoryPath);
}
try {
const hasValue = fileJson[targetKey];
if (hasValue) {
result.value = hasValue;
}
}
catch (error) {
console.error(`\n读取目录 ${directoryPath} 下的 ${fileName} 文件内容异常: `, error);
}
}
return result;
}
function updateProjectLint(repositories) {
const result = [];
for (const repositoryInfo of repositories) {
const { name, web_url } = repositoryInfo;
const repositoryPath = path.join(localAllReposCodePath, name);
if (fs.existsSync(repositoryPath)) {
const { key, value } = getTargetKeyInfo(repositoryPath);
result.push({
name,
key,
value,
web_url,
});
}
}
return result;
}
function main() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const allProjects = yield loadFileList();
console.log(`\n项目共有 ${allProjects.length} 个\n`);
const result = updateProjectLint(allProjects);
index_js_1.default.writeRes2SomePath('fileKeyInfos.json', result, resultPath);
});
}
yield main();
return {
success: true,
};
});
}
exports.default = runExecutor;
//# sourceMappingURL=executor.js.map