Skip to content

Commit

Permalink
feat(deps): p-finally -> Promise.prototype.finally()
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Nov 17, 2020
1 parent d966e8b commit 028db04
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 113 deletions.
8 changes: 2 additions & 6 deletions commands/bootstrap/index.js
Expand Up @@ -4,7 +4,6 @@ const dedent = require("dedent");
const getPort = require("get-port");
const npa = require("npm-package-arg");
const path = require("path");
const pFinally = require("p-finally");
const pMap = require("p-map");
const pMapSeries = require("p-map-series");
const pWaterfall = require("p-waterfall");
Expand Down Expand Up @@ -270,7 +269,7 @@ class BootstrapCommand extends Command {
})
: pMap(this.filteredPackages, mapPackageWithScript, { concurrency: this.concurrency });

return pFinally(runner, () => tracker.finish());
return runner.finally(() => tracker.finish());
}

hoistedDirectory(dependency) {
Expand Down Expand Up @@ -574,10 +573,7 @@ class BootstrapCommand extends Command {
tracker.addWork(actions.length);
}

return pFinally(
pMap(actions, (act) => act(), { concurrency: this.concurrency }),
() => tracker.finish()
);
return pMap(actions, (act) => act(), { concurrency: this.concurrency }).finally(() => tracker.finish());
}

/**
Expand Down
1 change: 0 additions & 1 deletion commands/bootstrap/package.json
Expand Up @@ -50,7 +50,6 @@
"multimatch": "^5.0.0",
"npm-package-arg": "^8.1.0",
"npmlog": "^4.1.2",
"p-finally": "^2.0.1",
"p-map": "^4.0.0",
"p-map-series": "^2.1.0",
"p-waterfall": "^2.1.0",
Expand Down
7 changes: 3 additions & 4 deletions commands/publish/index.js
Expand Up @@ -3,7 +3,6 @@
const os = require("os");
const path = require("path");
const crypto = require("crypto");
const pFinally = require("p-finally");
const pMap = require("p-map");
const pPipe = require("p-pipe");
const semver = require("semver");
Expand Down Expand Up @@ -680,7 +679,7 @@ class PublishCommand extends Command {
chain = chain.then(() => this.runPackageLifecycle(this.project.manifest, "postpack"));
}

return pFinally(chain, () => tracker.finish());
return chain.finally(() => tracker.finish());
}

publishPacked() {
Expand Down Expand Up @@ -730,7 +729,7 @@ class PublishCommand extends Command {
chain = chain.then(() => this.runRootLifecycle("postpublish"));
}

return pFinally(chain, () => tracker.finish());
return chain.finally(() => tracker.finish());
}

npmUpdateAsLatest() {
Expand Down Expand Up @@ -767,7 +766,7 @@ class PublishCommand extends Command {

chain = chain.then(() => this.topoMapPackages(mapper));

return pFinally(chain, () => tracker.finish());
return chain.finally(() => tracker.finish());
}

getDistTag() {
Expand Down
1 change: 0 additions & 1 deletion commands/publish/package.json
Expand Up @@ -60,7 +60,6 @@
"fs-extra": "^9.0.1",
"npm-package-arg": "^8.1.0",
"npmlog": "^4.1.2",
"p-finally": "^2.0.1",
"p-map": "^4.0.0",
"p-pipe": "^3.1.0",
"semver": "^7.3.2"
Expand Down
51 changes: 7 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -68,7 +68,6 @@
"npm-package-arg": "^8.1.0",
"npm-packlist": "^2.1.4",
"npmlog": "^4.1.2",
"p-finally": "^2.0.1",
"p-map": "^4.0.0",
"p-map-series": "^2.1.0",
"p-pipe": "^3.1.0",
Expand Down
1 change: 0 additions & 1 deletion utils/symlink-dependencies/package.json
Expand Up @@ -35,7 +35,6 @@
"@lerna/resolve-symlink": "file:../resolve-symlink",
"@lerna/symlink-binary": "file:../symlink-binary",
"fs-extra": "^9.0.1",
"p-finally": "^2.0.1",
"p-map": "^4.0.0",
"p-map-series": "^2.1.0"
}
Expand Down
106 changes: 51 additions & 55 deletions utils/symlink-dependencies/symlink-dependencies.js
@@ -1,7 +1,6 @@
"use strict";

const fs = require("fs-extra");
const pFinally = require("p-finally");
const pMap = require("p-map");
const pMapSeries = require("p-map-series");
const path = require("path");
Expand Down Expand Up @@ -29,68 +28,65 @@ function symlinkDependencies(packages, packageGraph, tracker) {
? packageGraph.values()
: new Set(packages.map(({ name }) => packageGraph.get(name)));

return pFinally(
pMapSeries(nodes, (currentNode) => {
const currentName = currentNode.name;
const currentNodeModules = currentNode.pkg.nodeModulesLocation;
return pMapSeries(nodes, (currentNode) => {
const currentName = currentNode.name;
const currentNodeModules = currentNode.pkg.nodeModulesLocation;

return pMap(currentNode.localDependencies, ([dependencyName, resolved]) => {
if (resolved.type === "directory") {
// a local file: specifier is already a symlink
return;
}
return pMap(currentNode.localDependencies, ([dependencyName, resolved]) => {
if (resolved.type === "directory") {
// a local file: specifier is already a symlink
return;
}

// get PackageGraphNode of dependency
// const dependencyName = resolved.name;
const dependencyNode = packageGraph.get(dependencyName);
const targetDirectory = path.join(currentNodeModules, dependencyName);
// get PackageGraphNode of dependency
// const dependencyName = resolved.name;
const dependencyNode = packageGraph.get(dependencyName);
const targetDirectory = path.join(currentNodeModules, dependencyName);

let chain = Promise.resolve();
let chain = Promise.resolve();

// check if dependency is already installed
chain = chain.then(() => fs.pathExists(targetDirectory));
chain = chain.then((dirExists) => {
if (dirExists) {
const isDepSymlink = resolveSymlink(targetDirectory);
// check if dependency is already installed
chain = chain.then(() => fs.pathExists(targetDirectory));
chain = chain.then((dirExists) => {
if (dirExists) {
const isDepSymlink = resolveSymlink(targetDirectory);

if (isDepSymlink !== false && isDepSymlink !== dependencyNode.location) {
// installed dependency is a symlink pointing to a different location
tracker.warn(
"EREPLACE_OTHER",
`Symlink already exists for ${dependencyName} dependency of ${currentName}, ` +
"but links to different location. Replacing with updated symlink..."
);
} else if (isDepSymlink === false) {
// installed dependency is not a symlink
tracker.warn(
"EREPLACE_EXIST",
`${dependencyName} is already installed for ${currentName}. Replacing with symlink...`
);
if (isDepSymlink !== false && isDepSymlink !== dependencyNode.location) {
// installed dependency is a symlink pointing to a different location
tracker.warn(
"EREPLACE_OTHER",
`Symlink already exists for ${dependencyName} dependency of ${currentName}, ` +
"but links to different location. Replacing with updated symlink..."
);
} else if (isDepSymlink === false) {
// installed dependency is not a symlink
tracker.warn(
"EREPLACE_EXIST",
`${dependencyName} is already installed for ${currentName}. Replacing with symlink...`
);

// remove installed dependency
return fs.remove(targetDirectory);
}
} else {
// ensure destination directory exists (dealing with scoped subdirs)
return fs.ensureDir(path.dirname(targetDirectory));
// remove installed dependency
return fs.remove(targetDirectory);
}
});
} else {
// ensure destination directory exists (dealing with scoped subdirs)
return fs.ensureDir(path.dirname(targetDirectory));
}
});

// create package symlink
const dependencyLocation = dependencyNode.pkg.contents
? path.resolve(dependencyNode.location, dependencyNode.pkg.contents)
: dependencyNode.location;
chain = chain.then(() => createSymlink(dependencyLocation, targetDirectory, "junction"));
// create package symlink
const dependencyLocation = dependencyNode.pkg.contents
? path.resolve(dependencyNode.location, dependencyNode.pkg.contents)
: dependencyNode.location;
chain = chain.then(() => createSymlink(dependencyLocation, targetDirectory, "junction"));

// TODO: pass PackageGraphNodes directly instead of Packages
chain = chain.then(() => symlinkBinary(dependencyNode.pkg, currentNode.pkg));
// TODO: pass PackageGraphNodes directly instead of Packages
chain = chain.then(() => symlinkBinary(dependencyNode.pkg, currentNode.pkg));

return chain;
}).then(() => {
tracker.silly("actions", "finished", currentName);
tracker.completeWork(1);
});
}),
() => tracker.finish()
);
return chain;
}).then(() => {
tracker.silly("actions", "finished", currentName);
tracker.completeWork(1);
});
}).finally(() => tracker.finish());
}

0 comments on commit 028db04

Please sign in to comment.