@pob/pretty-pkg 7.0.0 → 7.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +12 -12
- package/bin/pretty-pkg.js +9 -9
- package/lib/index.js +13 -13
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,20 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [7.1.1](https://github.com/christophehurpeau/pob/compare/@pob/pretty-pkg@7.1.0...@pob/pretty-pkg@7.1.1) (2024-05-05)
|
|
7
|
+
|
|
8
|
+
Note: no notable changes
|
|
9
|
+
|
|
10
|
+
Version bump for dependency: sort-pkg
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## [7.1.0](https://github.com/christophehurpeau/pob/compare/@pob/pretty-pkg@7.0.0...@pob/pretty-pkg@7.1.0) (2024-04-21)
|
|
14
|
+
|
|
15
|
+
Note: no notable changes
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
6
20
|
## [7.0.0](https://github.com/christophehurpeau/pob/compare/@pob/pretty-pkg@6.3.0...@pob/pretty-pkg@7.0.0) (2023-12-29)
|
|
7
21
|
|
|
8
22
|
|
package/README.md
CHANGED
|
@@ -44,32 +44,32 @@ yarn pretty-pkg package.json
|
|
|
44
44
|
#### overrideSync
|
|
45
45
|
|
|
46
46
|
```js
|
|
47
|
-
const { overrideSync } = require(
|
|
47
|
+
const { overrideSync } = require("@pob/pretty-pkg");
|
|
48
48
|
|
|
49
|
-
overrideSync(
|
|
49
|
+
overrideSync("package.json");
|
|
50
50
|
```
|
|
51
51
|
|
|
52
52
|
#### writeSync
|
|
53
53
|
|
|
54
54
|
```js
|
|
55
|
-
const fs = require(
|
|
56
|
-
const { writeSync } = require(
|
|
55
|
+
const fs = require("fs");
|
|
56
|
+
const { writeSync } = require("@pob/pretty-pkg");
|
|
57
57
|
|
|
58
|
-
const pkg = JSON.parse(fs.readFileSync(
|
|
59
|
-
writeSync(pkg,
|
|
58
|
+
const pkg = JSON.parse(fs.readFileSync("package.json", "utf-8"));
|
|
59
|
+
writeSync(pkg, "package.json");
|
|
60
60
|
```
|
|
61
61
|
|
|
62
62
|
#### default
|
|
63
63
|
|
|
64
64
|
```js
|
|
65
|
-
const fs = require(
|
|
66
|
-
const prettyPkg = require(
|
|
65
|
+
const fs = require("fs");
|
|
66
|
+
const prettyPkg = require("@pob/pretty-pkg");
|
|
67
67
|
|
|
68
68
|
// parsing yourself
|
|
69
|
-
const pkg = JSON.parse(fs.readFileSync(
|
|
70
|
-
fs.writeFileSync(prettyPkg(pkg),
|
|
69
|
+
const pkg = JSON.parse(fs.readFileSync("package.json", "utf-8"));
|
|
70
|
+
fs.writeFileSync(prettyPkg(pkg), "package.json");
|
|
71
71
|
|
|
72
72
|
// let prettyPkg parse
|
|
73
|
-
const pkgContent = fs.readFileSync(
|
|
74
|
-
fs.writeFileSync(prettyPkg(pkgContent),
|
|
73
|
+
const pkgContent = fs.readFileSync("package.json", "utf-8");
|
|
74
|
+
fs.writeFileSync(prettyPkg(pkgContent), "package.json");
|
|
75
75
|
```
|
package/bin/pretty-pkg.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import fs from
|
|
4
|
-
import path from
|
|
5
|
-
import { glob } from
|
|
6
|
-
import { overrideSync, writeSync } from
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { glob } from "glob";
|
|
6
|
+
import { overrideSync, writeSync } from "../lib/index.js";
|
|
7
7
|
|
|
8
8
|
const packageJsonPathnames = process.argv.slice(2);
|
|
9
9
|
|
|
@@ -12,22 +12,22 @@ if (packageJsonPathnames.length > 0) {
|
|
|
12
12
|
overrideSync(packageJsonPathname);
|
|
13
13
|
});
|
|
14
14
|
} else {
|
|
15
|
-
const pkg = JSON.parse(fs.readFileSync(
|
|
16
|
-
writeSync(pkg,
|
|
15
|
+
const pkg = JSON.parse(fs.readFileSync("package.json"));
|
|
16
|
+
writeSync(pkg, "package.json");
|
|
17
17
|
|
|
18
18
|
if (pkg.workspaces && pkg.workspaces.length > 0) {
|
|
19
19
|
const patterns = pkg.workspaces;
|
|
20
20
|
|
|
21
21
|
const getGlobPattern = (pattern) =>
|
|
22
|
-
pattern.endsWith(
|
|
22
|
+
pattern.endsWith("/") ? pattern : `${pattern}/`;
|
|
23
23
|
|
|
24
24
|
for (const pattern of patterns) {
|
|
25
25
|
const matches = await glob(getGlobPattern(pattern), {
|
|
26
|
-
ignore: [
|
|
26
|
+
ignore: ["**/node_modules/**"],
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
for (const match of matches) {
|
|
30
|
-
const packageJsonPathname = path.join(match,
|
|
30
|
+
const packageJsonPathname = path.join(match, "package.json");
|
|
31
31
|
if (fs.existsSync(packageJsonPathname)) {
|
|
32
32
|
overrideSync(packageJsonPathname);
|
|
33
33
|
}
|
package/lib/index.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import fs from
|
|
2
|
-
import sortPkg from
|
|
3
|
-
import prettier from
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import sortPkg from "@pob/sort-pkg";
|
|
3
|
+
import prettier from "prettier";
|
|
4
4
|
|
|
5
5
|
export default function prettyPkg(pkg, prettierOptions) {
|
|
6
|
-
if (typeof pkg ===
|
|
6
|
+
if (typeof pkg === "string") {
|
|
7
7
|
pkg = JSON.parse(pkg);
|
|
8
|
-
if (typeof pkg !==
|
|
8
|
+
if (typeof pkg !== "object") {
|
|
9
9
|
throw new TypeError(
|
|
10
|
-
|
|
10
|
+
"Invalid package: not an object after parsing string"
|
|
11
11
|
);
|
|
12
12
|
}
|
|
13
|
-
} else if (typeof pkg !==
|
|
14
|
-
throw new TypeError(
|
|
13
|
+
} else if (typeof pkg !== "object") {
|
|
14
|
+
throw new TypeError("expected pkg to be object or string");
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
if (typeof prettierOptions ===
|
|
17
|
+
if (typeof prettierOptions === "string") {
|
|
18
18
|
throw new TypeError(
|
|
19
|
-
`Please import "${prettierOptions}" and pass it as the second argument of prettyPkg
|
|
19
|
+
`Please import "${prettierOptions}" and pass it as the second argument of prettyPkg`
|
|
20
20
|
);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
sortPkg(pkg);
|
|
24
24
|
return prettier.format(JSON.stringify(pkg, undefined, 2), {
|
|
25
|
-
filepath:
|
|
25
|
+
filepath: "package.json",
|
|
26
26
|
printWidth: 80,
|
|
27
27
|
...prettierOptions,
|
|
28
28
|
});
|
|
@@ -30,10 +30,10 @@ export default function prettyPkg(pkg, prettierOptions) {
|
|
|
30
30
|
|
|
31
31
|
export function writeSync(pkg, path, prettierOptions) {
|
|
32
32
|
const string = prettyPkg(pkg, prettierOptions);
|
|
33
|
-
fs.writeFileSync(path, string,
|
|
33
|
+
fs.writeFileSync(path, string, "utf8");
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
export function overrideSync(path, prettierOptions) {
|
|
37
|
-
const pkg = fs.readFileSync(path,
|
|
37
|
+
const pkg = fs.readFileSync(path, "utf8");
|
|
38
38
|
return writeSync(pkg, path, prettierOptions);
|
|
39
39
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pob/pretty-pkg",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.1",
|
|
4
4
|
"description": "prettier and sort package.json",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "Christophe Hurpeau <christophe@hurpeau.com> (https://christophe.hurpeau.com)",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"pob": {},
|
|
38
38
|
"prettier": "@pob/root/prettier-config",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@pob/sort-pkg": "7.
|
|
40
|
+
"@pob/sort-pkg": "7.1.1",
|
|
41
41
|
"glob": "^10.0.0",
|
|
42
42
|
"prettier": "^2.6.2"
|
|
43
43
|
}
|