@vendure/cli
Version:
A modern, headless ecommerce framework
147 lines • 6.58 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadVendureConfigFile = loadVendureConfigFile;
const node_fs_1 = require("node:fs");
const node_path_1 = __importDefault(require("node:path"));
const ts_node_1 = require("ts-node");
const ast_utils_1 = require("../utilities/ast-utils");
const utils_1 = require("../utilities/utils");
function findNearestTsConfig(startPath) {
let currentDir = node_path_1.default.dirname(startPath);
const root = node_path_1.default.parse(currentDir).root;
while (currentDir !== root) {
const tsConfigPath = node_path_1.default.join(currentDir, 'tsconfig.json');
if ((0, node_fs_1.existsSync)(tsConfigPath)) {
return tsConfigPath;
}
const parentDir = node_path_1.default.dirname(currentDir);
if (parentDir === currentDir) {
break;
}
currentDir = parentDir;
}
return null;
}
async function resolveTsConfigWithExtends(tsConfigPath) {
let tsConfigFileContent;
let tsConfigJson;
try {
tsConfigFileContent = (0, node_fs_1.readFileSync)(tsConfigPath, 'utf-8');
}
catch (error) {
throw new Error(`Failed to read TypeScript config file at ${tsConfigPath}: ${getErrorMessage(error)}`);
}
try {
const { default: stripJsonComments } = await Promise.resolve().then(() => __importStar(require('strip-json-comments')));
tsConfigJson = JSON.parse(stripJsonComments(tsConfigFileContent));
}
catch (error) {
throw new Error(`Failed to parse TypeScript config file at ${tsConfigPath}: ${getErrorMessage(error)}`);
}
if (tsConfigJson.extends) {
const extendsArray = Array.isArray(tsConfigJson.extends)
? tsConfigJson.extends
: [tsConfigJson.extends];
let mergedBaseConfig = {};
for (const extendsPath of extendsArray) {
const baseConfigPath = node_path_1.default.resolve(node_path_1.default.dirname(tsConfigPath), extendsPath);
const baseConfig = await resolveTsConfigWithExtends(baseConfigPath);
mergedBaseConfig = deepMergeConfigs(mergedBaseConfig, baseConfig);
}
return deepMergeConfigs(mergedBaseConfig, tsConfigJson);
}
return tsConfigJson;
}
function deepMergeConfigs(base, current) {
var _a, _b, _c, _d;
const result = Object.assign({}, base);
for (const key of Object.keys(current)) {
if (key === 'compilerOptions' && base.compilerOptions) {
result.compilerOptions = Object.assign(Object.assign({}, base.compilerOptions), current.compilerOptions);
if (((_a = base.compilerOptions) === null || _a === void 0 ? void 0 : _a.paths) || ((_b = current.compilerOptions) === null || _b === void 0 ? void 0 : _b.paths)) {
result.compilerOptions.paths = Object.assign(Object.assign({}, (_c = base.compilerOptions) === null || _c === void 0 ? void 0 : _c.paths), (_d = current.compilerOptions) === null || _d === void 0 ? void 0 : _d.paths);
}
}
else if (key !== 'extends') {
result[key] = current[key];
}
}
return result;
}
async function loadVendureConfigFile(vendureConfig, providedTsConfigPath) {
await Promise.resolve().then(() => __importStar(require('dotenv/config')));
if (!(0, utils_1.isRunningInTsNode)()) {
const configFilePath = vendureConfig.sourceFile.getFilePath();
const nearestTsConfig = findNearestTsConfig(configFilePath);
let tsConfigPath;
if (nearestTsConfig) {
tsConfigPath = nearestTsConfig;
}
else if (providedTsConfigPath) {
tsConfigPath = providedTsConfigPath;
}
else {
const tsConfigFile = (0, ast_utils_1.selectTsConfigFile)();
tsConfigPath = node_path_1.default.join(process.cwd(), tsConfigFile);
}
const tsConfigJson = await resolveTsConfigWithExtends(tsConfigPath);
const compilerOptions = tsConfigJson.compilerOptions;
(0, ts_node_1.register)({
compilerOptions: Object.assign(Object.assign({}, compilerOptions), { moduleResolution: 'NodeNext', module: 'NodeNext' }),
transpileOnly: true,
});
if (compilerOptions.paths) {
const tsConfigPaths = await Promise.resolve().then(() => __importStar(require('tsconfig-paths')));
tsConfigPaths.register({
baseUrl: './',
paths: compilerOptions.paths,
});
}
}
const exportedVarName = vendureConfig.getConfigObjectVariableName();
if (!exportedVarName) {
throw new Error('Could not find the exported variable name in the VendureConfig file');
}
const configModule = await Promise.resolve(`${vendureConfig.sourceFile.getFilePath()}`).then(s => __importStar(require(s)));
return configModule[exportedVarName];
}
function getErrorMessage(error) {
return error instanceof Error ? error.message : String(error);
}
//# sourceMappingURL=load-vendure-config-file.js.map