UNPKG

@vendure/cli

Version:

A modern, headless ecommerce framework

65 lines 3.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.VendureConfigRef = void 0; const node_path_1 = __importDefault(require("node:path")); const ts_morph_1 = require("ts-morph"); class VendureConfigRef { constructor(project, configFilePath) { var _a; this.project = project; if (configFilePath) { const sourceFile = project.getSourceFile(sf => sf.getFilePath().endsWith(configFilePath)); if (!sourceFile) { throw new Error(`Could not find a config file at "${configFilePath}"`); } this.sourceFile = sourceFile; } else { const sourceFiles = project .getSourceFiles() .filter(sf => this.isVendureConfigFile(sf, { checkFileName: false })); if (sourceFiles.length > 1) { throw new Error(`Multiple Vendure config files found. Please specify which one to use with the --config flag:\n` + sourceFiles.map(sf => ` - ${sf.getFilePath()}`).join('\n')); } if (sourceFiles.length === 0) { throw new Error('Could not find the VendureConfig declaration in your project.'); } this.sourceFile = sourceFiles[0]; } this.configObject = (_a = this.sourceFile .getVariableDeclarations() .find(v => this.isVendureConfigVariableDeclaration(v))) === null || _a === void 0 ? void 0 : _a.getChildren().find(ts_morph_1.Node.isObjectLiteralExpression); } getPathRelativeToProjectRoot() { var _a, _b; return node_path_1.default.relative((_b = (_a = this.project.getRootDirectories()[0]) === null || _a === void 0 ? void 0 : _a.getPath()) !== null && _b !== void 0 ? _b : '', this.sourceFile.getFilePath()); } getConfigObjectVariableName() { var _a, _b; return (_b = (_a = this.sourceFile) === null || _a === void 0 ? void 0 : _a.getVariableDeclarations().find(v => this.isVendureConfigVariableDeclaration(v))) === null || _b === void 0 ? void 0 : _b.getName(); } getPluginsArray() { var _a; return (_a = this.configObject .getProperty('plugins')) === null || _a === void 0 ? void 0 : _a.getFirstChildByKind(ts_morph_1.SyntaxKind.ArrayLiteralExpression); } addToPluginsArray(text) { var _a; (_a = this.getPluginsArray()) === null || _a === void 0 ? void 0 : _a.addElement(text).formatText(); } isVendureConfigFile(sourceFile, options = {}) { var _a; const checkFileName = (_a = options.checkFileName) !== null && _a !== void 0 ? _a : true; return ((checkFileName ? sourceFile.getFilePath().endsWith('vendure-config.ts') : true) && !!sourceFile.getVariableDeclarations().find(v => this.isVendureConfigVariableDeclaration(v))); } isVendureConfigVariableDeclaration(v) { return v.getType().getText(v) === 'VendureConfig'; } } exports.VendureConfigRef = VendureConfigRef; //# sourceMappingURL=vendure-config-ref.js.map