next-auto-build 1.1.3 → 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 +64 -65
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -4,97 +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
11
- ========================= */
9
+ 🧠 PROJECT ROOT DETECTOR
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")) {
17
- cwd = cwd.split(`node_modules${path.sep}`)[0];
18
- }
19
- 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;
20
21
  }
21
22
 
22
23
  const projectRoot = getProjectRoot();
23
24
  const pkgPath = path.join(projectRoot, "package.json");
24
25
 
25
26
  /* =========================
26
- AUTO SCRIPT INJECTOR
27
- ========================= */
27
+ ✍️ AUTO SCRIPT INJECTOR
28
+ ========================= */
28
29
  function addAutoScript() {
29
- if (!fs.existsSync(pkgPath)) return;
30
- try {
31
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
32
- pkg.scripts ||= {};
33
- if (!pkg.scripts.auto) {
34
- pkg.scripts.auto = "node next-auto-build.js";
35
- fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
36
- console.log("⚡ auto script injected → npm run auto");
37
- }
38
- } catch (e) { /* ignore parse errors */ }
30
+ if (!fs.existsSync(pkgPath)) return;
31
+
32
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
33
+ pkg.scripts ||= {};
34
+
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
+ }
39
40
  }
40
41
 
41
42
  /* =========================
42
- HELPER: HASH FILE CONTENT
43
- ========================= */
44
- function getFileHash(filePath) {
45
- if (!fs.existsSync(filePath)) return null;
46
- try {
47
- const content = fs.readFileSync(filePath, "utf8");
48
- return crypto.createHash("md5").update(content).digest("hex");
49
- } catch (e) { return null; }
43
+ 📦 POSTINSTALL MODE
44
+ ========================= */
45
+ if (process.argv.includes("--postinstall")) {
46
+ addAutoScript();
47
+ process.exit(0);
50
48
  }
51
49
 
52
50
  /* =========================
53
51
  🚀 AUTO BUILD ENGINE
54
- ========================= */
52
+ ========================= */
53
+ const nextBin = path.join(projectRoot, "node_modules", ".bin", "next");
55
54
  let running = false;
56
55
 
57
56
  function build() {
58
- if (running) return;
59
- running = true;
60
- console.clear();
61
- console.log("🚧 Production build running...");
57
+ if (running) return;
58
+ if (!fs.existsSync(nextBin)) {
59
+ console.log("❌ Next.js not found. Install next first.");
60
+ return;
61
+ }
62
62
 
63
- // Using npx ensures it works even if next isn't in global PATH
64
- const p = spawn("npx", ["next", "build"], {
65
- stdio: "inherit",
66
- shell: true,
67
- cwd: projectRoot
68
- });
63
+ running = true;
64
+ console.clear();
65
+ console.log("🚧 Production build running...");
69
66
 
70
- p.on("close", (code) => {
71
- running = false;
72
- if (code === 0) console.log("✅ Build success. Watching for changes...");
73
- else console.log("💥 Build failed. Fix errors to retry.");
74
- });
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
+ });
75
80
  }
76
81
 
77
82
  /* =========================
78
- 👀 WATCHER INIT
79
- ========================= */
83
+ 👀 FILE WATCHER
84
+ ========================= */
80
85
  addAutoScript();
81
86
 
82
87
  chokidar.watch(projectRoot, {
83
- ignored: [
84
- /(^|[\/\\])\../, // ignore dotfiles
85
- "**/node_modules/**",
86
- "**/.next/**",
87
- "**/.git/**",
88
- "**/*.log"
89
- ],
90
- ignoreInitial: true,
91
- usePolling: true, // Required for Termux/Mobile reliability
92
- interval: 400
93
- }).on("all", (event, filePath) => {
94
- if (filePath.endsWith(".json") || filePath.endsWith(".js") || filePath.endsWith(".ts") || filePath.endsWith(".tsx")) {
95
- build();
96
- }
97
- });
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);
98
98
 
99
- // Start initial build
100
- build();
99
+ build();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-auto-build",
3
- "version": "1.1.3",
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",