a2r
Version:
A2R Framework
39 lines (38 loc) • 1.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const typescript_1 = __importDefault(require("typescript"));
const chalk_1 = __importDefault(require("chalk"));
const telemetry_1 = require("@a2r/telemetry");
const fs_1 = require("@a2r/fs");
const colors_1 = require("../colors");
const getExportsIdentifiersFromNodes_1 = __importDefault(require("./getExportsIdentifiersFromNodes"));
const modelKeys = new Map();
const fileValidation = async (filePath) => {
const content = await (0, fs_1.readFile)(filePath, 'utf8');
if (content) {
const sourceFile = typescript_1.default.createSourceFile(filePath, content, typescript_1.default.ScriptTarget.Latest, true);
const fileNodes = sourceFile.getChildren();
const identifiers = (0, getExportsIdentifiersFromNodes_1.default)(fileNodes);
let validated = true;
for (let i = 0, l = identifiers.length; i < l && validated; i += 1) {
const identifier = identifiers[i];
const keyPath = modelKeys.get(identifier);
if (keyPath && keyPath !== filePath) {
validated = false;
telemetry_1.out.error(`There is already a type, interface or enum called ${chalk_1.default.bold(identifier)} on file ${(0, colors_1.fullPath)(keyPath)}`);
}
}
if (validated) {
identifiers.forEach((k) => {
modelKeys.set(k, filePath);
});
}
return validated;
}
telemetry_1.out.error(`File ${(0, colors_1.fullPath)(filePath)} is empty`);
return false;
};
exports.default = fileValidation;