@typescript-tools/packages-to-rebuild-on-changes
Version:
Calculate packages required to rebuild when a given package changes
104 lines • 6.47 kB
JavaScript
;
/**
* packages-to-rebuild-on-changes
* Calculate packages required to rebuild when a given package changes.
*/
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.packagesToRebuildOnChanges = 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"));
const readFile = (filename) => (0, function_1.pipe)((0, lerna_utils_1.readFile)(filename), TE.mapLeft((error) => ({ 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((0, function_1.flow)((errors) => PathReporter.failure(errors).join('\n'), (error) => ({ type: 'unexpected file contents', filename, error }))), TE.fromEither);
/**
* Calculate packages required to rebuild when a given package changes.
* This is calculated by gathering the set of packages that depend
* on the changed packages, and adding to it the required dependencies
* to build that set of packages.
*/
function packagesToRebuildOnChanges(root) {
return (0, function_1.pipe)((0, lerna_packages_1.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);
}, {});
// map of a package name to its (direct) internal dependencies
const internalDependencies = manifests.reduce((acc, manifest) => {
var _a, _b, _c, _d;
return Object.assign(acc, {
[manifest.name]: (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))))),
});
}, {});
// 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 allRequiredToRebuild = (pkg) => {
var _a, _b;
const processed = new Set();
const neighbors = [];
let next = [(_a = internalPackages[pkg]) !== null && _a !== void 0 ? _a : [], ...((_b = internalDependers[pkg]) !== null && _b !== void 0 ? _b : [])];
do {
next.forEach((neighbor) => {
processed.add(neighbor.name);
neighbors.push(neighbor);
});
next = (0, function_1.pipe)(next, A.chain((dependency) => { var _a; return (_a = internalDependencies[dependency.name]) !== null && _a !== void 0 ? _a : []; }), A.filter((neighbor) => !processed.has(neighbor.name)));
} while (!A.isEmpty(next));
return neighbors;
};
return manifests.reduce((acc, { name }) => (acc.set(name, allRequiredToRebuild(name)), acc), new Map());
}));
}
exports.packagesToRebuildOnChanges = packagesToRebuildOnChanges;
// LocalWords: depender dependers
//# sourceMappingURL=index.js.map