UNPKG

svelte-preprocess

Version:

A Svelte preprocessor wrapper with baked-in support for commonly used preprocessors

81 lines (80 loc) 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.concat = concat; exports.getIncludePaths = getIncludePaths; exports.hasDepInstalled = hasDepInstalled; exports.isValidLocalPath = isValidLocalPath; exports.findUp = findUp; exports.setProp = setProp; const fs_1 = require("fs"); const path_1 = require("path"); function concat(...arrs) { return arrs.reduce((acc, a) => { if (a) return acc.concat(a); return acc; }, []); } /** Paths used by preprocessors to resolve @imports */ function getIncludePaths(fromFilename, base = []) { if (fromFilename == null) return []; return [ ...new Set([...base, 'node_modules', process.cwd(), (0, path_1.dirname)(fromFilename)]), ]; } const depCheckCache = {}; /** * Checks if a package is installed. * * @export * @param {string} dep * @returns boolean */ async function hasDepInstalled(dep) { if (depCheckCache[dep] != null) { return depCheckCache[dep]; } let result = false; try { await import(dep); result = true; } catch (e) { result = false; } return (depCheckCache[dep] = result); } function isValidLocalPath(path) { return path.startsWith('.'); } // finds a existing path up the tree function findUp({ what, from }) { const { root, dir } = (0, path_1.parse)(from); let cur = dir; try { while (cur !== root) { const possiblePath = (0, path_1.join)(cur, what); if ((0, fs_1.existsSync)(possiblePath)) { return possiblePath; } cur = (0, path_1.dirname)(cur); } } catch (e) { console.error(e); } return null; } // set deep property in object function setProp(obj, keyList, value) { let i = 0; for (; i < keyList.length - 1; i++) { const key = keyList[i]; if (typeof obj[key] !== 'object') { obj[key] = {}; } obj = obj[key]; } obj[keyList[i]] = value; }