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.
- package/index.js +57 -74
- 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")))
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
console.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
👀
|
|
83
|
+
👀 FILE WATCHER
|
|
100
84
|
========================= */
|
|
101
85
|
addAutoScript();
|
|
102
|
-
runAutoAction(pkgPath); // first time run
|
|
103
86
|
|
|
104
87
|
chokidar.watch(projectRoot, {
|
|
105
|
-
ignored: [
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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();
|