UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

105 lines (104 loc) 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const fileLookup_1 = require("./fileLookup"); const CACHED_LISTING = {}; const CACHED_PATHS = {}; function pathRegex(input) { const str = input.replace(/\*/, '(.*)').replace(/[\-\[\]\/\{\}\+\?\\\^\$\|]/g, '\\$&'); return new RegExp(`^${str}`); } /** * Compile a list of functions to easily test directories * * @param {string} homeDir * @param {{ [key: string]: Array<string> }} [paths] * @returns */ function getPathsData(props) { if (CACHED_PATHS[props.homeDir] && props.cachePaths) { return CACHED_PATHS[props.homeDir]; } const fns = []; for (const key in props.paths) { fns.push((target) => { const re = pathRegex(key); const matched = target.match(re); if (matched) { const variable = matched[1]; const directories = props.paths[key]; return directories.map(item => item.replace(/\*/, variable)); } }); } if (props.cachePaths) { CACHED_PATHS[props.homeDir] = fns; } return fns; } /** * Listing homeDir directories to simplify matching and makae it easier for the matcher * * @param {IPathsLookupProps} props * @returns {(DirectoryListing | undefined)} */ function getIndexFiles(props) { let indexFiles; if (props.baseURL) { if (CACHED_LISTING[props.baseURL]) { indexFiles = CACHED_LISTING[props.baseURL]; } else { const files = []; const listed = fs.readdirSync(props.baseURL); for (const file of listed) { if (file[0] !== '.') { const [nameWithoutExtension] = file.split('.'); files.push({ nameWithoutExtension, name: file, }); } } indexFiles = CACHED_LISTING[props.baseURL] = files; } } return indexFiles; } function pathsLookup(props) { // if baseDir is the same as homeDir we can assume aliasing directories // and files without the need in specifying "paths" // so we check if first const indexFiles = getIndexFiles(props); if (indexFiles) { for (const i in indexFiles) { const item = indexFiles[i]; // check if starts with it only const regex = new RegExp(`^${item.nameWithoutExtension}($|\\.|\\/)`); if (regex.test(props.target)) { const result = fileLookup_1.fileLookup({ fileDir: props.baseURL, target: props.target }); if (result && result.fileExists) { return result; } } } } // Performing the actual typescript paths match // "items" should be cached, so we are getting simple functions that contain // regular expressions const items = getPathsData(props); for (const i in items) { const test = items[i]; const directories = test(props.target); if (directories) { for (const j in directories) { const directory = directories[j]; const result = fileLookup_1.fileLookup({ isDev: props.isDev, fileDir: props.baseURL, target: directory }); if (result && result.fileExists) { return result; } } } } } exports.pathsLookup = pathsLookup;