lambdaorm-base
Version:
81 lines • 3.58 kB
JavaScript
;
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.SchemaFileHelper = void 0;
const path_1 = __importDefault(require("path"));
class SchemaFileHelper {
// eslint-disable-next-line no-useless-constructor
constructor(helper) {
this.helper = helper;
}
getConfigPath(source) {
return __awaiter(this, void 0, void 0, function* () {
let workspace;
let configFile;
workspace = process.cwd();
if (source === undefined) {
configFile = yield this.getConfigFileName(workspace);
}
else if (typeof source === 'string') {
if (source.startsWith('http')) {
return source;
}
else if (yield this.helper.fs.exists(source)) {
const lstat = yield this.helper.fs.lstat(source);
if (lstat.isFile()) {
configFile = path_1.default.basename(source);
workspace = path_1.default.dirname(source);
}
else {
workspace = source;
configFile = yield this.getConfigFileName(workspace);
}
}
else {
yield this.helper.logger.log(`Not exists path ${source}`);
}
}
else {
yield this.helper.logger.log('Schema: not supported:');
yield this.helper.logger.log(source);
}
if (configFile) {
return path_1.default.join(workspace, configFile);
}
else {
return undefined;
}
});
}
getConfigFileName(workspace) {
return __awaiter(this, void 0, void 0, function* () {
const entries = yield this.helper.fs.readdir(workspace);
const candidates = entries.filter(entry => {
const ext = path_1.default.extname(entry).toLowerCase();
const name = path_1.default.basename(entry, ext).toLowerCase();
return (['.json', '.yaml', '.yml'].includes(ext) &&
(name.includes('lambdaorm') || name.includes('orm') || name.includes('schema')));
});
const priority = ['lambdaorm', 'orm', 'schema'];
for (const p of priority) {
const match = candidates.find(c => path_1.default.basename(c).toLowerCase().includes(p));
if (match)
return match;
}
return undefined;
});
}
}
exports.SchemaFileHelper = SchemaFileHelper;
//# sourceMappingURL=schemaFileHelper.js.map