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.
- package/index.js +64 -65
- 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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
console.
|
|
61
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
shell: true,
|
|
67
|
-
cwd: projectRoot
|
|
68
|
-
});
|
|
63
|
+
running = true;
|
|
64
|
+
console.clear();
|
|
65
|
+
console.log("🚧 Production build running...");
|
|
69
66
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
|
79
|
-
|
|
83
|
+
👀 FILE WATCHER
|
|
84
|
+
========================= */
|
|
80
85
|
addAutoScript();
|
|
81
86
|
|
|
82
87
|
chokidar.watch(projectRoot, {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
100
|
-
build();
|
|
99
|
+
build();
|