@arthurgubaidullin/nx-embed-dependencies
Version:
This plugin for [Nx](https://nx.dev) helps injecting local dependencies into a package.
35 lines • 2.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.injectDependencies = void 0;
const tslib_1 = require("tslib");
const package_json_program_1 = require("@embed-dependencies/package-json-program");
const A = require("fp-ts/Array");
const E = require("fp-ts/Either");
const function_1 = require("fp-ts/function");
const T = require("fp-ts/Task");
const TE = require("fp-ts/TaskEither");
const node_child_process_1 = require("node:child_process");
const fs = require("node:fs/promises");
const path = require("node:path");
const node_util_1 = require("node:util");
const _exec = (0, node_util_1.promisify)(node_child_process_1.exec);
function injectDependencies(targetPackagePath, dependencyPaths) {
return () => (0, function_1.pipe)(TE.of(dependencyPaths), TE.chain(TE.traverseArray((sourcePackagePath) => pack(sourcePackagePath, targetPackagePath))), TE.chainFirst((0, function_1.flow)((0, package_json_program_1.insertDependencies)(targetPackagePath), T.fromIO)), TE.map(function_1.constVoid), TE.fold((e) => () => tslib_1.__awaiter(this, void 0, void 0, function* () {
console.error(e);
}), T.of));
}
exports.injectDependencies = injectDependencies;
function pack(sourcePackagePath, targetPath) {
return (0, function_1.pipe)(TE.of({ sourcePackagePath, targetPath }), TE.bind('packageName', ({ sourcePackagePath }) => (0, function_1.pipe)(sourcePackagePath, package_json_program_1.getPackageName, T.fromIO)), TE.let('targetPackagePath', ({ packageName }) => path.join(targetPath, 'libs', packageName)), TE.chainFirst(({ targetPackagePath }) => TE.fromTask(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
yield fs.mkdir(targetPackagePath, { recursive: true });
}))), TE.chainFirst(({ targetPackagePath, sourcePackagePath }) => (0, function_1.pipe)(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
yield _exec(`npm pack --pack-destination ${targetPackagePath}`, {
cwd: sourcePackagePath,
});
}), (f) => TE.tryCatch(f, E.toError))), TE.bind('filename', ({ targetPackagePath }) => (0, function_1.pipe)(() => tslib_1.__awaiter(this, void 0, void 0, function* () { return yield fs.readdir(targetPackagePath); }), TE.fromTask, TE.chain(TE.fromPredicate(A.isNonEmpty, () => new Error('Not found package project file.'))), TE.map((a) => a[0]))), TE.let('relativePackagePath', ({ targetPath, targetPackagePath, filename }) => 'file:' +
path.join(path.relative(targetPath, targetPackagePath), filename)), TE.map((a) => ({
name: a.packageName,
path: a.relativePackagePath,
})));
}
//# sourceMappingURL=NpmPackInjector.js.map