UNPKG

p5-analysis

Version:

API to find, create, and analyze p5.js sketch files.

97 lines (96 loc) 4.57 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.findMinimizedImportPathAlternatives = exports.checkLibraryImportPaths = exports.checkLibraryHomepagePaths = exports.checkLibraries = void 0; const __1 = require(".."); const check_library_collisions_1 = __importDefault(require("../commands/check-library-collisions")); const ts_extras_1 = require("../helpers/ts-extras"); const cachedFetch_1 = require("./cachedFetch"); async function checkLibraries({ parseScripts = false }) { await checkLibraryHomepagePaths(); await checkLibraryImportPaths({ parseScripts }); await findMinimizedImportPathAlternatives(); await (0, check_library_collisions_1.default)(); } exports.checkLibraries = checkLibraries; async function checkLibraryHomepagePaths() { const homepages = await Promise.all(__1.Library.all.map(library => (0, cachedFetch_1.cachedFetch)(library.homepage))); const invalid = __1.Library.all.filter((_library, i) => { const homepage = homepages[i]; return homepage.status !== 200; }); if (invalid.length) { console.log(`${invalid.length} invalid library homepage paths:`); invalid.forEach(library => console.log(library.homepage)); console.log(); process.exitCode = 1; } } exports.checkLibraryHomepagePaths = checkLibraryHomepagePaths; async function checkLibraryImportPaths({ parseScripts = false }) { const missingImportPaths = __1.Library.all.filter(library => !library.importPath); if (missingImportPaths.length) { console.log(`These libraries are missing import paths:`); missingImportPaths.forEach(library => console.log(' ', `${library.name} (${library.homepage})`)); console.log(); process.exitCode = 1; } const librariesWithPaths = __1.Library.all.filter(library => library.importPath); const responses = await Promise.all(librariesWithPaths.map(async (library) => { const res = await (0, cachedFetch_1.cachedFetch)(library.importPath); return { library, ok: res.ok, text: res.ok ? await res.text() : undefined }; })); const invalidImportPaths = responses.filter(res => !res.ok); if (invalidImportPaths.length) { console.log(`These library import paths are invalid:`); invalidImportPaths.forEach(({ library }) => console.log(` ${library.name} (${library.homepage}) – ${library.importPath}`)); console.log(); process.exitCode = 1; } if (parseScripts) { const libraryScripts = responses .filter(res => res.ok) .map(({ library, text }) => [ library, __1.Script.fromSource(text) ]); const scriptErrors = libraryScripts.filter(([, script]) => script.getErrors().length > 0); for (const [library, script] of scriptErrors) { console.log(`${library.name}:`, library.importPath); for (const err of script.getErrors()) { console.log(' ', err.message); } } if (scriptErrors.length) { console.log(); process.exitCode = 1; } } // for (const [library, script] of libraryScripts.filter(([, script]) => !script.getErrors().length)) { // const globals = Array.from(script.globals.keys()); // if (globals.length > 0) { // console.log(library.name + ':', globals.join(', ')); // } else { // console.log(library.name + ':', 'none'); // } // } } exports.checkLibraryImportPaths = checkLibraryImportPaths; async function findMinimizedImportPathAlternatives() { const candidates = __1.Library.all.filter(library => library.importPath && library.importPath.endsWith('.js') && !library.importPath.endsWith('.min.js')); const found = (await Promise.all(candidates.map(async function (library) { const url = library.importPath.replace(/\.js$/, '.min.js'); const res = await (0, cachedFetch_1.cachedFetch)(url); return res.ok ? [library, url] : null; }))).filter(ts_extras_1.isDefined); if (found.length) { console.log('These libraries have minimized alternatives:'); found.forEach(([library, replacement]) => console.log(`${library.name}\n ${library.importPath} -> ${replacement}`)); console.log(); } } exports.findMinimizedImportPathAlternatives = findMinimizedImportPathAlternatives;