|
"use strict"; |
|
var __importDefault = (this && this.__importDefault) || function (mod) { |
|
return (mod && mod.__esModule) ? mod : { "default": mod }; |
|
}; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.resolveSvelteConfigFromOption = resolveSvelteConfigFromOption; |
|
const path_1 = __importDefault(require("path")); |
|
const fs_1 = __importDefault(require("fs")); |
|
const parser_1 = require("./parser"); |
|
const caches = new Map(); |
|
|
|
|
|
|
|
function resolveSvelteConfigFromOption(options) { |
|
if (options === null || options === void 0 ? void 0 : options.svelteConfig) { |
|
return options.svelteConfig; |
|
} |
|
return resolveSvelteConfig(options === null || options === void 0 ? void 0 : options.filePath); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function resolveSvelteConfig(filePath) { |
|
let cwd = filePath && fs_1.default.existsSync(filePath) ? path_1.default.dirname(filePath) : null; |
|
if (cwd == null) { |
|
if (typeof process === "undefined") |
|
return null; |
|
cwd = process.cwd(); |
|
} |
|
const configFilePath = findConfigFilePath(cwd); |
|
if (!configFilePath) |
|
return null; |
|
if (caches.has(configFilePath)) { |
|
return caches.get(configFilePath) || null; |
|
} |
|
const code = fs_1.default.readFileSync(configFilePath, "utf8"); |
|
const config = (0, parser_1.parseConfig)(code); |
|
caches.set(configFilePath, config); |
|
return config; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function findConfigFilePath(cwd) { |
|
let directory = path_1.default.resolve(cwd); |
|
const { root } = path_1.default.parse(directory); |
|
const stopAt = path_1.default.resolve(directory, root); |
|
while (directory !== stopAt) { |
|
const target = path_1.default.resolve(directory, "svelte.config.js"); |
|
const stat = fs_1.default.existsSync(target) |
|
? fs_1.default.statSync(target, { |
|
throwIfNoEntry: false, |
|
}) |
|
: null; |
|
if (stat === null || stat === void 0 ? void 0 : stat.isFile()) { |
|
return target; |
|
} |
|
const next = path_1.default.dirname(directory); |
|
if (next === directory) |
|
break; |
|
directory = next; |
|
} |
|
return null; |
|
} |
|
|