@html-validate/release-scripts 3.4.3 → 3.5.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/README.md CHANGED
@@ -9,13 +9,49 @@ Various scripts used by release toolchain.
9
9
  ```json
10
10
  {
11
11
  "scripts": {
12
- "prepack": "release-prepack package.json",
13
- "postpack": "release-postpack package.json"
14
- "prepublishOnly": "release-prepublish package.json",
15
- "postpublish": "release-postpublish package.json"
12
+ "prepack": "release-prepack",
13
+ "postpack": "release-postpack",
14
+ "prepublishOnly": "release-prepublish",
15
+ "postpublish": "release-postpublish"
16
16
  }
17
17
  }
18
18
  ```
19
19
 
20
- If the library or CLI tool bundles all dependencies use `release-prepack package.json --bundle`.
20
+ If the library or CLI tool bundles all dependencies use `release-prepack --bundle`.
21
21
  Dependencies can also be filtered by marking dependencies as external: `--bundle --external:foo --external:bar`.
22
+
23
+ ```json
24
+ {
25
+ "scripts": {
26
+ "prepack": "release-prepack --bundle --external:foo --external:bar",
27
+ "postpack": "release-postpack",
28
+ "prepublishOnly": "release-prepublish --bundle --external:foo --external:bar",
29
+ "postpublish": "release-postpublish"
30
+ }
31
+ }
32
+ ```
33
+
34
+ ### Usage
35
+
36
+ release-prepack [FILENAME] [OPTIONS..]
37
+ release-postpack [FILENAME]
38
+ release-prepublish [FILENAME] [OPTIONS..]
39
+ release-postpublish [FILENAME]
40
+
41
+ If `FILENAME` is set it is the filename to use instead of the default `package.json`.
42
+
43
+ ### Options
44
+
45
+ #### `--bundle`
46
+
47
+ By default the `dependencies` field in `package.json` is intact but if your package bundles its dependencies the `--bundle` flag can be used to strip out dependencies as well.
48
+
49
+ #### `--external:${NAME}`
50
+
51
+ When using in conjunction with `--bundle` it marks a dependency which should still be intact.
52
+ For instance, using `--external:foobar` all packages in `dependencies` except `foobar` will be stripped.
53
+ This flag can be used multiple times.
54
+
55
+ #### `--retain-scripts`
56
+
57
+ By default the `scripts` field is stripped but with this flag enabled the scripts will be intact.
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @param {string[]} argv
3
+ * @returns {[flags: string[], positionals: string[]]}
4
+ */
5
+ function getArgs(argv) {
6
+ const flags = argv.filter((it) => it.startsWith("--"));
7
+ const positionals = argv.filter((it) => !it.startsWith("--"));
8
+ return [flags, positionals];
9
+ }
10
+
11
+ module.exports = { getArgs };
package/lib/postpack.js CHANGED
@@ -9,7 +9,7 @@ if (command === "publish" && event === "postpack") {
9
9
  process.exit(0);
10
10
  }
11
11
 
12
- const filename = process.argv[2];
12
+ const filename = process.argv[2] || "package.json";
13
13
  const filepath = path.resolve(filename);
14
14
  const origfile = `${filepath}.orig`;
15
15
 
package/lib/prepack.js CHANGED
@@ -3,6 +3,8 @@
3
3
  const path = require("path");
4
4
  const fs = require("fs");
5
5
  const { filterDependencies } = require("./filter-dependencies");
6
+ const { getArgs } = require("./get-args");
7
+ const { readPackageJson } = require("./read-package-json");
6
8
  const { stripFields } = require("./strip-fields");
7
9
 
8
10
  /* ensure this script runs via the correct lifecycle event */
@@ -11,41 +13,45 @@ if (command === "publish" && event === "prepack") {
11
13
  process.exit(0);
12
14
  }
13
15
 
14
- const filename = process.argv[2];
15
- const filepath = path.resolve(filename);
16
- const tmpfile = `${filepath}.tmp`;
17
- const origfile = `${filepath}.orig`;
18
-
19
- const bundle = process.argv.includes("--bundle");
20
- const externals = process.argv
16
+ const [flags, positionals] = getArgs(process.argv.slice(2));
17
+ const bundle = flags.includes("--bundle");
18
+ const retainScripts = flags.includes("--retain-scripts");
19
+ const externals = flags
21
20
  .filter((it) => it.startsWith("--external:"))
22
21
  .map((it) => it.slice("--external:".length));
23
22
 
24
- if (!fs.existsSync(filepath)) {
25
- process.stderr.write(`release-prepack: No such file or directory: "${filepath}"\n`);
23
+ const filename = positionals[0] || "package.json";
24
+ const filePath = path.resolve(filename);
25
+ const tmpfile = `${filePath}.tmp`;
26
+ const origfile = `${filePath}.orig`;
27
+
28
+ if (!fs.existsSync(filePath)) {
29
+ process.stderr.write(`release-prepack: No such file or directory: "${filePath}"\n`);
26
30
  process.exit(1);
27
31
  }
28
32
 
29
- const pkg = JSON.parse(fs.readFileSync(filepath, "utf-8"));
33
+ const pkg = readPackageJson(filePath);
30
34
  pkg.dependencies = filterDependencies(pkg.dependencies, bundle, externals);
31
35
 
32
- stripFields(pkg);
36
+ const stripped = stripFields(pkg, {
37
+ retainScripts,
38
+ });
33
39
 
34
- const content = JSON.stringify(pkg, null, 2);
40
+ const content = JSON.stringify(stripped, null, 2);
35
41
  console.log(content);
36
42
 
37
43
  /* backup original file in case of error */
38
- fs.copyFileSync(filepath, origfile);
44
+ fs.copyFileSync(filePath, origfile);
39
45
 
40
46
  try {
41
47
  /* write to a temporary file first */
42
48
  fs.writeFileSync(tmpfile, `${content}\n`, "utf-8");
43
49
 
44
50
  /* swap tempfile for original */
45
- fs.renameSync(tmpfile, filepath);
51
+ fs.renameSync(tmpfile, filePath);
46
52
  } catch (err) {
47
53
  console.error(err);
48
54
  process.stderr.write(`\nRestoring "${filename}" after failure\n`);
49
- fs.copyFileSync(origfile, filepath);
55
+ fs.copyFileSync(origfile, filePath);
50
56
  process.exit(1);
51
57
  }
@@ -0,0 +1,11 @@
1
+ const fs = require("fs");
2
+
3
+ /**
4
+ * @param {string} filePath
5
+ * @returns {object}
6
+ */
7
+ function readPackageJson(filePath) {
8
+ return JSON.parse(fs.readFileSync(filePath, "utf-8"));
9
+ }
10
+
11
+ module.exports = { readPackageJson };
@@ -1,3 +1,8 @@
1
+ /**
2
+ * @typedef {object} StripOptions
3
+ * @property {boolean} [retainScripts]
4
+ */
5
+
1
6
  const strip = [
2
7
  "ava",
3
8
  "c8",
@@ -12,18 +17,30 @@ const strip = [
12
17
  "prettier",
13
18
  "release",
14
19
  "renovate",
15
- "scripts",
16
20
  "simple-git-hooks",
17
21
  "stylelint",
18
22
  "tsd",
19
23
  "xo",
20
24
  ];
21
25
 
22
- function stripFields(pkg) {
26
+ /**
27
+ * @template T
28
+ * @param {T} pkg
29
+ * @param {StripOptions} options
30
+ * @returns {Partial<T>}
31
+ */
32
+ function stripFields(pkg, options = {}) {
33
+ const result = { ...pkg };
23
34
  for (const key of strip) {
24
35
  /* eslint-disable-next-line security/detect-object-injection */
25
- delete pkg[key];
36
+ delete result[key];
37
+ }
38
+
39
+ if (!options.retainScripts) {
40
+ delete result.scripts;
26
41
  }
42
+
43
+ return result;
27
44
  }
28
45
 
29
46
  module.exports = { stripFields };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@html-validate/release-scripts",
3
- "version": "3.4.3",
3
+ "version": "3.5.1",
4
4
  "description": "Various script used by release toolchain",
5
5
  "keywords": [
6
6
  "release"
@@ -38,5 +38,5 @@
38
38
  "publishConfig": {
39
39
  "access": "public"
40
40
  },
41
- "gitHead": "f14f12b2be0e76be0ee4436f7a19d9ecb5039186"
41
+ "gitHead": "401cc65adde6b671a7859d28ff994e74e01f5953"
42
42
  }