UNPKG

@mountainpass/hooked-cli

Version:
103 lines (102 loc) 4.89 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import defaults from '../defaults.js'; import fileUtils from './fileUtils.js'; import logger from './logger.js'; import fs from 'fs'; import path from 'path'; import { glob } from 'glob'; /** * Fetches the imports into the local cache. * @param imports * @param pull */ export const fetchImports = (imports_1, ...args_1) => __awaiter(void 0, [imports_1, ...args_1], void 0, function* (imports, pull = false) { const removeOptionalTrailingQuestion = (url) => url.replace(/\?$/, ''); if (Array.isArray(imports) && imports.length > 0) { const remotes = imports.filter(i => i.startsWith('https://')); const localGlobs = imports.filter(i => !i.startsWith('https://')).map(str => fileUtils.resolvePath(str)); let locals = []; for (const globPath of localGlobs) { const tmp = yield glob(globPath, { signal: AbortSignal.timeout(1000) }); if (tmp.length > 0) { locals = [...locals, ...tmp]; } else { logger.debug(`No files matching glob path "${globPath}", treating as file reference.`); locals = [...locals, globPath]; } } const remotesCache = remotes.map(i => defaults.getLocalImportsCachePath(removeOptionalTrailingQuestion(path.basename(i)))); const allLocal = []; // const allLocal = imports.map(i => i.startsWith('https://') // ? getLocalImportsCachePath(removeOptionalTrailingQuestion(path.basename(i))) // : i) // force-pull remotes if (pull) { yield Promise.all(remotes.map((url, i) => __awaiter(void 0, void 0, void 0, function* () { logger.debug(`Downloading remote import: ${url} -> ${remotesCache[i]}`); try { yield fileUtils.downloadFile(removeOptionalTrailingQuestion(url), remotesCache[i]); allLocal.push(remotesCache[i]); } catch (e) { if (url.endsWith('?')) { logger.debug(`Optional import file not found: ${url}`); } else { throw e; } } }))); } else { // pull remotes if not cached yield Promise.all(remotes.map((url, i) => __awaiter(void 0, void 0, void 0, function* () { if (!fs.existsSync(remotesCache[i])) { logger.debug(`Downloading remote import : ${url} -> ${remotesCache[i]}`); try { yield fileUtils.downloadFile(removeOptionalTrailingQuestion(url), remotesCache[i]); allLocal.push(remotesCache[i]); } catch (e) { if (url.endsWith('?')) { logger.debug(`Optional import file not found: ${url}`); } else { throw e; } } } else { allLocal.push(remotesCache[i]); } }))); } // report (and error) if any required files are missing const missingLocalFiles = locals.filter(i => !fs.existsSync(fileUtils.resolvePath(removeOptionalTrailingQuestion(i)))); const notMissingLocalFiles = locals.filter(i => fs.existsSync(fileUtils.resolvePath(removeOptionalTrailingQuestion(i)))) .map(i => removeOptionalTrailingQuestion(i)); const missingOptionalFiles = missingLocalFiles.filter(i => i.endsWith('?')); const missingRequiredFiles = missingLocalFiles.filter(i => !i.endsWith('?')); for (const file of missingOptionalFiles) { logger.debug(`Optional import file not found: ${file}`); } if (missingRequiredFiles.length > 0) { throw new Error(`Missing import files: ${missingLocalFiles.join(', ')}`); } allLocal.push(...notMissingLocalFiles); return allLocal; } else { logger.debug('No imports found'); return []; } });