Skip to content

Commit f30878f

Browse files
committed
feat: add new package dependent-path-update
1 parent bc3c13c commit f30878f

37 files changed

+571
-19
lines changed

.eslintignore

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
*.marko.js
2-
.cache/
3-
.nyc_output/
4-
node_modules/
5-
coverage/
6-
dist/
2+
.cache
3+
.nyc_output
4+
coverage
5+
lerna.json
6+
package.json
7+
package-lock.json
8+
CHANGELOG.md
9+
packages/*/node_modules
10+
packages/*/dist
11+
packages/*/test/fixtures

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*.DS_Store
44
*.marko.js
55
*.history/
6+
.vscode
67
*actual.*
78
.cache/
89
.nyc_output/

.prettierignore

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
*.marko.js
2-
.cache/
3-
.nyc_output/
2+
.cache
3+
.nyc_output
4+
coverage
45
lerna.json
56
package.json
67
package-lock.json
78
CHANGELOG.md
8-
node_modules/
9-
coverage/
10-
dist/
9+
packages/*/node_modules
10+
packages/*/dist
11+
packages/*/test/fixtures

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
extract all valid node flags from a list of process args
3535
- [abort-group](https://github.com/marko-js/utils/blob/master/packages/abort-group/README.md) -
3636
group a set of cancelable actions to be aborted together at any time.
37+
- [dependent-path-update](https://github.com/marko-js/utils/blob/master/packages/dependent-path-update/README.md) -
38+
A tool to update dependent paths when renaming a file.
3739

3840
## Contributing
3941

package-lock.json

+107-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"lerna": "3.4.3",
2323
"lint-staged": "^7.3.0",
2424
"mocha": "^5.2.0",
25+
"mocha-autotest": "^1.0.2",
2526
"nyc": "^13.1.0",
2627
"prettier": "^1.14.3",
2728
"sinon": "^7.0.0"
@@ -32,7 +33,7 @@
3233
"git add"
3334
],
3435
"*.js": [
35-
"eslint -f visualstudio",
36+
"eslint --quiet -f visualstudio",
3637
"prettier --write",
3738
"git add"
3839
]
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# dependent-path-update
2+
3+
A tool to update dependent paths when renaming a file.
4+
5+
This uses regexps based on the folder depth to match all possible relative require paths in to file and updates all of those paths to a new path. Ultimately allowing you to rename a file and updating all dependents (not just js files!).
6+
7+
## Install
8+
9+
```bash
10+
npm install --save dependent-path-update
11+
```
12+
13+
## Usage
14+
15+
```javascript
16+
import dependentPathUpdate from "dependent-path-update";
17+
18+
dependentPathUpdate({
19+
projectDir: process.cwd(), // The root directory of the project from which all paths should be relative.
20+
from: "src/file-a.js", // The original file path.
21+
to: "src/files/a.js", // The new file path.
22+
files: [
23+
// List of glob patterns of files to check.
24+
"*.{js,json}"
25+
]
26+
}).then(output => {
27+
// Output contains an object with all of the updated sources.
28+
console.log("updated all dependents");
29+
30+
for (const file in output) {
31+
// Save all outputs to disk.
32+
fs.writeFileSync(file, output[file], "utf-8");
33+
}
34+
});
35+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "dependent-path-update",
3+
"description": "A tool to update dependent paths when renaming a file.",
4+
"version": "0.0.0",
5+
"author": "Dylan Piercey <[email protected]>",
6+
"bugs": "https://github.com/marko-js/utils/issues/new?template=Bug_report.md",
7+
"files": [
8+
"dist"
9+
],
10+
"homepage": "https://github.com/marko-js/utils/blob/master/packages/dependent-path-update/README.md",
11+
"keywords": [
12+
"dependent",
13+
"path",
14+
"update",
15+
"rename"
16+
],
17+
"dependencies": {
18+
"glob": "^7.1.3",
19+
"escape-string-regexp": "^1.0.5"
20+
},
21+
"license": "MIT",
22+
"main": "dist/index.js",
23+
"bin": "dist/cli.js",
24+
"repository": {
25+
"type": "git",
26+
"url": "https://github.com/marko-js/utils/tree/master/packages/dependent-path-update"
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { promisify } from "util";
2+
import _glob from "glob";
3+
const glob = promisify(_glob);
4+
5+
export default async function getFiles(patterns, globOptions) {
6+
return (await Promise.all(
7+
patterns.map(pattern => {
8+
if (pattern === ".") {
9+
pattern = "**/*";
10+
} else if (pattern[pattern.length - 1] === "/") {
11+
pattern += "**/*";
12+
}
13+
14+
return glob(pattern, globOptions);
15+
})
16+
)).reduce((a, b) => a.concat(b));
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import path from "path";
2+
import getRelativeRequirePath from "./get-relative-require-path";
3+
4+
export default function getPotentialPaths(projectDir, from, to) {
5+
const fromDir = path.dirname(from);
6+
const projectRelativePath = path.relative(projectDir, fromDir);
7+
const toRelativePath = getRelativeRequirePath(fromDir, to);
8+
const potentialPaths = [toRelativePath];
9+
let index = projectRelativePath.length;
10+
let backtrack = "../";
11+
12+
do {
13+
index = projectRelativePath.lastIndexOf("/", index - 2) + 1;
14+
potentialPaths.push(
15+
path.join(backtrack, projectRelativePath.slice(index), toRelativePath)
16+
);
17+
backtrack += "../";
18+
} while (index);
19+
20+
return potentialPaths;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import path from "path";
2+
3+
export default function getRelativePath(from, to) {
4+
const result = path.relative(from, to);
5+
6+
if (result === "") {
7+
return ".";
8+
}
9+
10+
if (result[0] !== ".") {
11+
return "./" + result;
12+
}
13+
14+
return result;
15+
}

0 commit comments

Comments
 (0)