UNPKG

radashi-helper

Version:
188 lines (163 loc) 7.24 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkQJPNUX4Pcjs = require('./chunk-QJPNUX4P.cjs'); var _chunk7UCE7HEGcjs = require('./chunk-7UCE7HEG.cjs'); var _chunkDNQVKCXJcjs = require('./chunk-DNQVKCXJ.cjs'); var _chunkZP624W2Acjs = require('./chunk-ZP624W2A.cjs'); var _chunk5R2KEZLQcjs = require('./chunk-5R2KEZLQ.cjs'); // src/util/generateUmbrella.ts var _fs = require('fs'); var _path = require('path'); // src/rewired/updateRewired.ts var _promises = require('fs/promises'); async function updateRewired(env) { const prevRewired = await _chunk7UCE7HEGcjs.loadRewired.call(void 0, env); if (prevRewired.length) { _chunkZP624W2Acjs.debug.call(void 0, `Updating ${prevRewired.length} rewired files...`); await Promise.all( prevRewired.map(async (funcPath) => { await _chunk7UCE7HEGcjs.rewire.call(void 0, funcPath, env); }) ); await _promises.copyFile.call(void 0, _path.join.call(void 0, env.root, "overrides/src/tsconfig.json"), _path.join.call(void 0, env.root, "overrides/rewired/tsconfig.json") ); } } // src/util/exports.ts var import_parser = _chunk5R2KEZLQcjs.__toESM.call(void 0, _chunk7UCE7HEGcjs.require_lib.call(void 0, ), 1); function printExports(options) { const names = options.names.concat(options.types.map((name) => `type ${name}`)); return `export { ${names.join(", ")} } from '${options.from}'`; } function extractExports(file, options = {}) { const typesOption = _nullishCoalesce(options.types, () => ( true)); let fileContents; try { fileContents = _fs.readFileSync.call(void 0, file, "utf8"); } catch (e) { return []; } const parseResult = (0, import_parser.parse)(fileContents, { plugins: [["typescript", { dts: file.endsWith(".d.ts") }]], sourceType: "module", sourceFilename: file }); const names = /* @__PURE__ */ new Set(); const { program } = parseResult; function isAllowed(node) { return typesOption !== "only" && (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration" || node.type === "ExportSpecifier" && node.exportKind !== "type") || typesOption !== false && (node.type === "TSInterfaceDeclaration" || node.type === "TSTypeAliasDeclaration" || node.type === "ExportSpecifier" && node.exportKind === "type"); } _chunk5R2KEZLQcjs.traverse.call(void 0, program, (node, _key, _parent, context) => { if (_chunk7UCE7HEGcjs.isBabelNode.call(void 0, node)) { context.skip(); if (node.type === "ExportNamedDeclaration") { if (node.declaration) { if (node.declaration.type === "VariableDeclaration") { if (typesOption === "only") { return; } for (const declarator of node.declaration.declarations) { if (declarator.id.type === "Identifier") { names.add(declarator.id.name); } } } else if (isAllowed(node.declaration)) { if (node.declaration.id && node.declaration.id.type === "Identifier") { names.add(node.declaration.id.name); } } } else if (node.specifiers) { for (const specifier of node.specifiers) { if (specifier.type === "ExportSpecifier") { if (isAllowed(specifier)) { names.add(specifier.exported.name); } } } } } else if (node.type === "ExportDefaultDeclaration") { if (typesOption !== "only") { names.add("default"); } } } }); return Array.from(names).sort(); } // src/util/generateUmbrella.ts async function generateUmbrella(env) { await updateRewired(env); const typesPath = _path.join.call(void 0, env.root, "src/types.ts"); const overrideTypesPath = _path.join.call(void 0, env.root, "overrides/src/types.ts"); const pathsInside = await _chunkDNQVKCXJcjs.findSources.call(void 0, env); const namesBlocked = _chunk5R2KEZLQcjs.flat.call(void 0, Object.values(pathsInside)).concat(typesPath, overrideTypesPath).flatMap((sourcePath) => { return extractExports(sourcePath); }); const filterBlockedNames = (exportName) => !namesBlocked.includes(exportName); const upstreamPath = _path.join.call(void 0, env.root, "node_modules/radashi/dist/radashi.d.ts"); const upstreamTypes = extractExports(upstreamPath, { types: "only" }).filter( filterBlockedNames ); const upstreamExports = extractExports(upstreamPath).filter(filterBlockedNames); let code = printExports({ from: "./node_modules/radashi/dist/radashi", names: upstreamExports.filter((name) => !upstreamTypes.includes(name)), types: upstreamTypes }); const srcExports = printLocalExports(pathsInside.src, env); if (srcExports.length) { code += _chunkQJPNUX4Pcjs.dedent` \n // Our custom functions. ${srcExports} `; } const overrideExports = printLocalExports(pathsInside.overrides, env); if (overrideExports.length) { code += _chunkQJPNUX4Pcjs.dedent` \n // Our overrides. ${overrideExports} `; } const userTypeExports = _fs.existsSync.call(void 0, typesPath) ? printLocalExports([typesPath], env) : void 0; const overrideTypeExports = _fs.existsSync.call(void 0, overrideTypesPath) ? printLocalExports([overrideTypesPath], env) : void 0; if (_optionalChain([userTypeExports, 'optionalAccess', _ => _.length]) || _optionalChain([overrideTypeExports, 'optionalAccess', _2 => _2.length])) { code += _chunkQJPNUX4Pcjs.dedent` \n // Our types.\n `; if (_optionalChain([overrideTypeExports, 'optionalAccess', _3 => _3.length])) { code += overrideTypeExports; } if (_optionalChain([userTypeExports, 'optionalAccess', _4 => _4.length])) { code += userTypeExports; } } const rewiredExports = printLocalExports(pathsInside.rewired, env); if (rewiredExports.length) { code += _chunkQJPNUX4Pcjs.dedent` \n // Rewired to use our overrides. ${rewiredExports} `; } return code + "\n"; } function printLocalExports(files, env) { return _chunk5R2KEZLQcjs.select.call(void 0, files.map((file) => ({ file, exports: extractExports(file), types: extractExports(file, { types: "only" }) })), (mod) => printExports({ from: "./" + _path.relative.call(void 0, env.root, mod.file), names: mod.exports.filter((name) => !mod.types.includes(name)), types: mod.types }), (mod) => mod.exports.length > 0 ).join("\n"); } exports.generateUmbrella = generateUmbrella;