next-auto-build 1.1.0 → 1.1.2
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 +76 -88
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -6,123 +6,111 @@ const chokidar = require("chokidar");
|
|
|
6
6
|
const spawn = require("cross-spawn");
|
|
7
7
|
const crypto = require("crypto");
|
|
8
8
|
|
|
9
|
-
/*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
pink: "\x1b[38;5;201m",
|
|
19
|
-
lime: "\x1b[38;5;118m",
|
|
20
|
-
yellow: "\x1b[38;5;226m",
|
|
21
|
-
red: "\x1b[38;5;196m",
|
|
22
|
-
gray: "\x1b[38;5;244m",
|
|
23
|
-
magenta: "\x1b[38;5;201m",
|
|
24
|
-
bg_dark: "\x1b[48;5;234m"
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/* =============================================================
|
|
28
|
-
⏳ SPINNER & LOADING
|
|
29
|
-
============================================================= */
|
|
30
|
-
const frames = ["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"];
|
|
31
|
-
let spinIdx = 0;
|
|
32
|
-
let spinInterval;
|
|
33
|
-
|
|
34
|
-
function startLoading(msg) {
|
|
35
|
-
stopLoading();
|
|
36
|
-
spinInterval = setInterval(() => {
|
|
37
|
-
process.stdout.write(`\r${T.cyan}${frames[spinIdx++ % frames.length]}${T.reset} ${T.bold}${msg}${T.reset} `);
|
|
38
|
-
}, 80);
|
|
9
|
+
/* =========================
|
|
10
|
+
🧠 PROJECT ROOT DETECTOR
|
|
11
|
+
========================= */
|
|
12
|
+
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;
|
|
39
18
|
}
|
|
40
19
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
spinInterval = null;
|
|
45
|
-
process.stdout.write("\r\x1b[K");
|
|
46
|
-
}
|
|
47
|
-
}
|
|
20
|
+
const projectRoot = getProjectRoot();
|
|
21
|
+
const pkgPath = path.join(projectRoot, "package.json");
|
|
22
|
+
const POST_FLAG = path.join(projectRoot, ".next-auto-postinstalled");
|
|
48
23
|
|
|
49
24
|
/* =========================
|
|
50
|
-
|
|
25
|
+
✍️ AUTO SCRIPT INJECTOR
|
|
51
26
|
========================= */
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
27
|
+
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
|
+
}
|
|
62
36
|
}
|
|
63
|
-
injectScript();
|
|
64
37
|
|
|
65
38
|
/* =========================
|
|
66
|
-
|
|
39
|
+
🖇 HELPER: HASH FILE CONTENT
|
|
67
40
|
========================= */
|
|
68
41
|
function getFileHash(filePath) {
|
|
69
|
-
|
|
70
|
-
|
|
42
|
+
if (!fs.existsSync(filePath)) return null;
|
|
43
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
44
|
+
return crypto.createHash("md5").update(content).digest("hex");
|
|
71
45
|
}
|
|
72
46
|
|
|
47
|
+
/* =========================
|
|
48
|
+
🤖 AUTO POSTINSTALL / AUTO ACTION
|
|
49
|
+
========================= */
|
|
73
50
|
function runAutoAction(changedFile) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
51
|
+
const currentHash = getFileHash(changedFile) || changedFile;
|
|
52
|
+
const flagPath = path.join(projectRoot, .next-auto-${Buffer.from(changedFile).toString('hex')});
|
|
53
|
+
|
|
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
|
+
);
|
|
80
65
|
}
|
|
81
66
|
|
|
82
67
|
/* =========================
|
|
83
|
-
|
|
68
|
+
📦 POSTINSTALL MODE
|
|
84
69
|
========================= */
|
|
85
70
|
if (process.argv.includes("--postinstall")) {
|
|
86
|
-
|
|
87
|
-
|
|
71
|
+
addAutoScript();
|
|
72
|
+
process.exit(0);
|
|
88
73
|
}
|
|
89
74
|
|
|
90
75
|
/* =========================
|
|
91
|
-
|
|
76
|
+
🚀 AUTO BUILD ENGINE
|
|
92
77
|
========================= */
|
|
93
|
-
const nextBin = path.join(
|
|
78
|
+
const nextBin = path.join(projectRoot, "node_modules", ".bin", "next");
|
|
94
79
|
let running = false;
|
|
95
80
|
|
|
96
81
|
function build() {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
+
});
|
|
111
96
|
}
|
|
112
97
|
|
|
113
98
|
/* =========================
|
|
114
|
-
|
|
99
|
+
👀 INIT EVERYTHING AUTOMATICALLY
|
|
115
100
|
========================= */
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
101
|
+
addAutoScript();
|
|
102
|
+
runAutoAction(pkgPath); // first time run
|
|
103
|
+
|
|
104
|
+
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();
|
|
126
113
|
});
|
|
127
114
|
|
|
115
|
+
// start first build
|
|
128
116
|
build();
|