next-auto-build 1.1.2 → 1.1.4

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.
Files changed (2) hide show
  1. package/index.js +57 -74
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -4,113 +4,96 @@ const fs = require("fs");
4
4
  const path = require("path");
5
5
  const chokidar = require("chokidar");
6
6
  const spawn = require("cross-spawn");
7
- const crypto = require("crypto");
8
7
 
9
8
  /* =========================
10
- 🧠 PROJECT ROOT DETECTOR
9
+ 🧠 PROJECT ROOT DETECTOR
11
10
  ========================= */
12
11
  function getProjectRoot() {
13
- const init = process.env.INIT_CWD;
14
- if (init && fs.existsSync(path.join(init, "package.json"))) return init;
15
- let cwd = process.cwd();
16
- if (cwd.includes("node_modules")) cwd = cwd.split(node_modules${path.sep})[0];
17
- return cwd;
12
+ const init = process.env.INIT_CWD;
13
+ if (init && fs.existsSync(path.join(init, "package.json"))) {
14
+ return init;
15
+ }
16
+ let cwd = process.cwd();
17
+ if (cwd.includes("node_modules")) {
18
+ cwd = cwd.split(`node_modules${path.sep}`)[0];
19
+ }
20
+ return cwd;
18
21
  }
19
22
 
20
23
  const projectRoot = getProjectRoot();
21
24
  const pkgPath = path.join(projectRoot, "package.json");
22
- const POST_FLAG = path.join(projectRoot, ".next-auto-postinstalled");
23
25
 
24
26
  /* =========================
25
- ✍️ AUTO SCRIPT INJECTOR
27
+ ✍️ AUTO SCRIPT INJECTOR
26
28
  ========================= */
27
29
  function addAutoScript() {
28
- if (!fs.existsSync(pkgPath)) return;
29
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
30
- pkg.scripts ||= {};
31
- if (!pkg.scripts.auto) {
32
- pkg.scripts.auto = "next-auto-build";
33
- fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
34
- console.log("⚡ auto script injected → npm run auto");
35
- }
36
- }
37
-
38
- /* =========================
39
- 🖇 HELPER: HASH FILE CONTENT
40
- ========================= */
41
- function getFileHash(filePath) {
42
- if (!fs.existsSync(filePath)) return null;
43
- const content = fs.readFileSync(filePath, "utf8");
44
- return crypto.createHash("md5").update(content).digest("hex");
45
- }
30
+ if (!fs.existsSync(pkgPath)) return;
46
31
 
47
- /* =========================
48
- 🤖 AUTO POSTINSTALL / AUTO ACTION
49
- ========================= */
50
- function runAutoAction(changedFile) {
51
- const currentHash = getFileHash(changedFile) || changedFile;
52
- const flagPath = path.join(projectRoot, .next-auto-${Buffer.from(changedFile).toString('hex')});
32
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
33
+ pkg.scripts ||= {};
53
34
 
54
- const lastHash = fs.existsSync(flagPath) ? fs.readFileSync(flagPath, "utf8") : null;
55
- if (currentHash === lastHash) return; // already processed
56
-
57
- fs.writeFileSync(flagPath, currentHash); // save new hash
58
-
59
- // Run self postinstall / auto logic
60
- spawn(
61
- "node",
62
- [__filename, "--postinstall"],
63
- { stdio: "inherit", shell: true, cwd: projectRoot }
64
- );
35
+ if (!pkg.scripts.auto) {
36
+ pkg.scripts.auto = "next-auto-build";
37
+ fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
38
+ console.log("⚡ auto script injected npm run auto");
39
+ }
65
40
  }
66
41
 
67
42
  /* =========================
68
- 📦 POSTINSTALL MODE
43
+ 📦 POSTINSTALL MODE
69
44
  ========================= */
70
45
  if (process.argv.includes("--postinstall")) {
71
- addAutoScript();
72
- process.exit(0);
46
+ addAutoScript();
47
+ process.exit(0);
73
48
  }
74
49
 
75
50
  /* =========================
76
- 🚀 AUTO BUILD ENGINE
51
+ 🚀 AUTO BUILD ENGINE
77
52
  ========================= */
78
53
  const nextBin = path.join(projectRoot, "node_modules", ".bin", "next");
79
54
  let running = false;
80
55
 
81
56
  function build() {
82
- if (running) return;
83
- if (!fs.existsSync(nextBin)) {
84
- console.log("❌ Next.js not found. Install next first.");
85
- return;
86
- }
87
- running = true;
88
- console.clear();
89
- console.log("🚧 Production build running...");
90
- const p = spawn(nextBin, ["build", "--webpack"], { stdio: "inherit", shell: true });
91
- p.on("close", (code) => {
92
- running = false;
93
- if (code === 0) console.log("✅ Build success. Watching...");
94
- else console.log("💥 Build failed. Fix & save.");
95
- });
57
+ if (running) return;
58
+ if (!fs.existsSync(nextBin)) {
59
+ console.log("❌ Next.js not found. Install next first.");
60
+ return;
61
+ }
62
+
63
+ running = true;
64
+ console.clear();
65
+ console.log("🚧 Production build running...");
66
+
67
+ const p = spawn(nextBin, ["build", "--webpack"], {
68
+ stdio: "inherit",
69
+ shell: true
70
+ });
71
+
72
+ p.on("close", (code) => {
73
+ running = false;
74
+ if (code === 0) {
75
+ console.log("✅ Build success. Watching...");
76
+ } else {
77
+ console.log("💥 Build failed. Fix & save.");
78
+ }
79
+ });
96
80
  }
97
81
 
98
82
  /* =========================
99
- 👀 INIT EVERYTHING AUTOMATICALLY
83
+ 👀 FILE WATCHER
100
84
  ========================= */
101
85
  addAutoScript();
102
- runAutoAction(pkgPath); // first time run
103
86
 
104
87
  chokidar.watch(projectRoot, {
105
- ignored: ["/node_modules/", "/.next/", "/.git/", "**/*.log"],
106
- ignoreInitial: true,
107
- usePolling: true,
108
- interval: 300
109
- }).on("all", (event, filePath) => {
110
- if (!filePath) return;
111
- runAutoAction(filePath); // react to ANY file change
112
- build();
113
- });
88
+ ignored: [
89
+ "**/node_modules/**",
90
+ "**/.next/**",
91
+ "**/.git/**",
92
+ "**/*.log"
93
+ ],
94
+ ignoreInitial: true,
95
+ usePolling: true,
96
+ interval: 300
97
+ }).on("all", build);
114
98
 
115
- // start first build
116
99
  build();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-auto-build",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Auto production build watcher for Next.js (adds auto script automatically)",
5
5
  "author": "Pavel Ahmmed Hridoy",
6
6
  "license": "MIT",