UNPKG

@anatine/esbuildnx

Version:
97 lines 4.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.copyPackages = exports.getPackagesToCopy = void 0; const tslib_1 = require("tslib"); const fs_extra_1 = require("fs-extra"); const recursive_copy_1 = tslib_1.__importDefault(require("recursive-copy")); function getPackagesToCopy(rootDirectory, external = []) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const modules = new Map(); const cwd = `${rootDirectory}/node_modules`; const dirs = (yield (0, fs_extra_1.readdir)(cwd).catch((err) => console.error(err))) || []; // Break out the @ package directories as well for (let i = 0; i < dirs.length; i++) { const name = dirs[i]; if (name.startsWith('@')) { const subDirs = (yield (0, fs_extra_1.readdir)(`${cwd}/${name}`).catch((err) => console.error(err))) || []; dirs.splice(i, 1, ...subDirs.map((dir) => `${name}/${dir}`)); i += subDirs.length - 1; } } const processPackage = (path, external = false, top = false) => tslib_1.__awaiter(this, void 0, void 0, function* () { const packagePath = `${path}/package.json`; if (!(0, fs_extra_1.existsSync)(packagePath)) return; const pkg = (yield (0, fs_extra_1.readJson)(packagePath).catch((err) => console.error(err))) || {}; const allDeps = Object.keys(Object.assign({}, pkg.dependencies || {}, pkg.optionalDependencies || {})); for (let i = 0; i < allDeps.length; i++) { const name = allDeps[i]; const module = modules.get(name); let alreadyExternal = false; if (module) { alreadyExternal = module.external; module.external = alreadyExternal || external; module.path = [...module.path, packagePath]; modules.set(name, module); } else { modules.set(name, { external, top, path: [packagePath] }); } // Will only process a module in the package.json if it is at the top // and there is no instance in a sub directory of node_modules // Also, only runs if a package hasn't already been flagged for external but it should be const subModule = rootDirectory + '/node_modules/' + name; const embeddedModule = path + '/node_modules/' + name; if (!(0, fs_extra_1.existsSync)(embeddedModule) && (0, fs_extra_1.existsSync)(subModule) && !alreadyExternal && external) { yield processPackage(`${subModule}`, external); } } }); for (let i = 0; i < dirs.length; i++) { const item = dirs[i]; const data = { external: Boolean(external.find((search) => search === item)), top: true, path: [], }; const current = modules.get(item); if (current) { modules.set(item, Object.assign(Object.assign({}, current), { external: current.external || data.external, top: true })); } else { modules.set(item, data); } yield processPackage(`${cwd}/${item}`, data.external); } const final = []; modules.forEach((value, key) => { if (value.external) { final.push(key); } }); return final; }); } exports.getPackagesToCopy = getPackagesToCopy; function copyPackages(projectRoot, destPath, modules = []) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!(0, fs_extra_1.existsSync)(`${projectRoot}/node_modules`)) { console.error(`Missing Directory: Unable to copy ${projectRoot}`); return; } if (modules.length === 0) return modules; yield (0, fs_extra_1.ensureDir)(`${destPath}/node_modules`); for (let i = 0; i < modules.length; i++) { const name = modules[i]; yield (0, recursive_copy_1.default)(`${projectRoot}/node_modules/${name}`, `${destPath}/node_modules/${name}`, { overwrite: true }); } return modules; }); } exports.copyPackages = copyPackages; //# sourceMappingURL=walk-packages.js.map