UNPKG

@typescript-tools/depender-graph

Version:
94 lines 5.43 kB
"use strict"; /** * depender-graph * Generate depender graph of internal packages */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.dependerGraph = void 0; const path = __importStar(require("path")); const io_ts_1 = require("@typescript-tools/io-ts"); const lerna_packages_1 = require("@typescript-tools/lerna-packages"); const lerna_utils_1 = require("@typescript-tools/lerna-utils"); const A = __importStar(require("fp-ts/Array")); const E = __importStar(require("fp-ts/Either")); const O = __importStar(require("fp-ts/Option")); const R = __importStar(require("fp-ts/Record")); const TE = __importStar(require("fp-ts/TaskEither")); const function_1 = require("fp-ts/function"); const PathReporter = __importStar(require("io-ts/lib/PathReporter")); // Widens the type of a particular DependerGraphError into a DependerGraphError const err = (error) => error; const lernaPackages = (0, function_1.flow)(lerna_packages_1.lernaPackages, TE.mapLeft(err)); const readFile = (filename) => (0, function_1.pipe)((0, lerna_utils_1.readFile)(filename), TE.mapLeft((error) => err({ type: 'unable to read file', filename, error }))); const decode = (codec) => (filename) => (value) => // eslint-disable-next-line @typescript-eslint/no-unsafe-return (0, function_1.pipe)(codec.decode(value), E.mapLeft((errors) => PathReporter.failure(errors).join('\n')), E.mapLeft((error) => err({ type: 'unexpected file contents', filename, error })), TE.fromEither); /** * Generate a DAG of internal dependers. */ function dependerGraph({ root, recursive, } = { recursive: true, }) { return (0, function_1.pipe)(lernaPackages(root), TE.chain((packages) => (0, function_1.pipe)(packages, // REFACTOR: use These to report all errors instead of just the first A.map((pkg) => (0, function_1.pipe)(path.resolve(pkg.location, 'package.json'), readFile, TE.chain(decode((0, io_ts_1.StringifiedJSON)(io_ts_1.PackageJsonDependencies))(pkg.location)), TE.map((manifest) => (Object.assign(Object.assign({}, pkg), manifest))))), TE.sequenceArray)), TE.map((manifests) => { // map of a package name to its metadata const internalPackages = manifests.reduce((acc, pkg) => Object.assign(acc, { [pkg.name]: pkg }), {}); // map of a package name to its (direct) internal dependers const internalDependers = manifests.reduce((acc, manifest) => { var _a, _b, _c, _d; return (0, function_1.pipe)([ ...Object.keys((_a = manifest.dependencies) !== null && _a !== void 0 ? _a : {}), ...Object.keys((_b = manifest.devDependencies) !== null && _b !== void 0 ? _b : {}), ...Object.keys((_c = manifest.optionalDependencies) !== null && _c !== void 0 ? _c : {}), ...Object.keys((_d = manifest.peerDependencies) !== null && _d !== void 0 ? _d : {}), ], A.chain((dependency) => (0, function_1.pipe)(R.lookup(dependency)(internalPackages), O.map(A.of), O.getOrElseW((0, function_1.constant)(A.empty)))), // this is direct dependencies (dependencies) => { for (const dependency of dependencies) { acc[dependency.name] = (0, function_1.pipe)(O.fromNullable(acc[dependency.name]), O.map((dependers) => (dependers.push(manifest), dependers)), O.getOrElse(() => [manifest])); } }, () => acc); }, {}); // NOTE: the types in this file may be kinda janky, it's // copy-pasta from dependency-graph.ts with the exception of // the implementation of `internalDependencies` const allInternalDependers = (pkg) => { var _a; const processed = new Set(); const deps = []; let next = (_a = internalDependers[pkg]) !== null && _a !== void 0 ? _a : []; do { next.forEach((dependency) => { processed.add(dependency.name); deps.push(dependency); }); next = (0, function_1.pipe)(next, A.chain((dependency) => { var _a; return (_a = (recursive === true ? internalDependers[dependency.name] : [])) !== null && _a !== void 0 ? _a : []; }), A.filter((dependency) => !processed.has(dependency.name))); } while (!A.isEmpty(next)); return deps; }; return manifests.reduce((acc, { name }) => (acc.set(name, allInternalDependers(name)), acc), new Map()); })); } exports.dependerGraph = dependerGraph; // LocalWords: dependers //# sourceMappingURL=index.js.map