@equinor/fusion-framework-cli
Version:
--- title: Fusion Framework CLI ---
84 lines • 3.88 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 __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { readFile } from 'node:fs/promises';
import { extname } from 'node:path';
import { findUpMultiple } from 'find-up';
import { assert } from 'node:console';
import { AssertionError } from 'node:assert';
import { pathToFileURL } from 'node:url';
import { transpile } from './ts-transpile.js';
import { fileExists } from './file-exists.js';
export const supportedExt = ['.ts', '.mjs', '.js', '.json'];
/**
* @param filename name if config file without extension
*/
export const findConfigs = (filename, options) => __awaiter(void 0, void 0, void 0, function* () {
const _a = options !== null && options !== void 0 ? options : {}, { extensions } = _a, findOptions = __rest(_a, ["extensions"]);
extensions && assertConfigFileType(extensions);
return findUpMultiple((extensions !== null && extensions !== void 0 ? extensions : supportedExt).map((ext) => [filename, ext].join('')), Object.assign(Object.assign({}, findOptions), { type: 'file' }));
});
export const resolveConfig = (filename, options) => __awaiter(void 0, void 0, void 0, function* () {
const [file] = yield findConfigs(filename, options === null || options === void 0 ? void 0 : options.find);
if (file) {
return {
path: file,
config: yield loadConfig(file),
};
}
});
function assertConfigFileType(value, message) {
const values = typeof value === 'string' ? [value] : value;
for (const ext of values) {
assert(supportedExt.includes(ext), new AssertionError({
message: message !== null && message !== void 0 ? message : 'unsupported file type',
actual: value,
expected: supportedExt.join('|'),
}));
}
}
const configExtname = (file) => {
const ext = extname(file);
assertConfigFileType(ext);
return ext;
};
export const loadConfig = (file) => __awaiter(void 0, void 0, void 0, function* () {
assert(yield fileExists(file, { assert: true }), `failed to access file ${file}`);
switch (configExtname(file)) {
case '.ts': {
return loadConfig(yield transpile(file));
}
case '.mjs':
case '.js': {
const result = (yield import(String(pathToFileURL(file)))).default;
return typeof result === 'function' ? result : () => result;
}
case '.json': {
return () => __awaiter(void 0, void 0, void 0, function* () { return JSON.parse(yield readFile(file, 'utf-8')); });
}
default:
throw Error('unsupported file type');
}
});
export function initiateConfig(config, ...args) {
return Promise.resolve(config(...args));
}
export default loadConfig;
//# sourceMappingURL=config.js.map