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.
Files changed (2) hide show
  1. package/index.js +76 -88
  2. 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
- 🎨 NEON-PULSE UI CORE
11
- ============================================================= */
12
- const T = {
13
- reset: "\x1b[0m",
14
- bold: "\x1b[1m",
15
- italic: "\x1b[3m",
16
- underline: "\x1b[4m",
17
- cyan: "\x1b[38;5;51m",
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
- function stopLoading() {
42
- if (spinInterval) {
43
- clearInterval(spinInterval);
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
- 🧩 AUTO SCRIPT INJECTOR
25
+ ✍️ AUTO SCRIPT INJECTOR
51
26
  ========================= */
52
- const PKG = path.join(process.cwd(), "package.json");
53
- function injectScript() {
54
- if (!fs.existsSync(PKG)) return;
55
- const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
56
- pkg.scripts ||= {};
57
- if (!pkg.scripts.auto) {
58
- pkg.scripts.auto = "next-auto-build";
59
- fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
60
- console.log(`${T.lime}☕ Script added → npm run auto${T.reset}`);
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
- 🖇 FILE HASH HELPER
39
+ 🖇 HELPER: HASH FILE CONTENT
67
40
  ========================= */
68
41
  function getFileHash(filePath) {
69
- if (!fs.existsSync(filePath)) return null;
70
- return crypto.createHash("md5").update(fs.readFileSync(filePath, "utf8")).digest("hex");
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
- const currentHash = getFileHash(changedFile) || changedFile;
75
- const flagPath = path.join(process.cwd(), `.next-auto-${Buffer.from(changedFile).toString("hex")}`);
76
- const lastHash = fs.existsSync(flagPath) ? fs.readFileSync(flagPath, "utf8") : null;
77
- if (currentHash === lastHash) return;
78
- fs.writeFileSync(flagPath, currentHash);
79
- spawn("node", [__filename, "--postinstall"], { stdio: "inherit", shell: true });
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
- 📦 POSTINSTALL MODE
68
+ 📦 POSTINSTALL MODE
84
69
  ========================= */
85
70
  if (process.argv.includes("--postinstall")) {
86
- injectScript();
87
- process.exit(0);
71
+ addAutoScript();
72
+ process.exit(0);
88
73
  }
89
74
 
90
75
  /* =========================
91
- 🚀 NEXT.JS BUILD ENGINE
76
+ 🚀 AUTO BUILD ENGINE
92
77
  ========================= */
93
- const nextBin = path.join(process.cwd(), "node_modules", ".bin", "next");
78
+ const nextBin = path.join(projectRoot, "node_modules", ".bin", "next");
94
79
  let running = false;
95
80
 
96
81
  function build() {
97
- if (running) return;
98
- if (!fs.existsSync(nextBin)) {
99
- console.log(`${T.red}❌ Next.js not found.${T.reset}`);
100
- return;
101
- }
102
- running = true;
103
- console.clear();
104
- console.log(`${T.yellow}🚧 Production build running...${T.reset}`);
105
- const p = spawn(nextBin, ["build", "--webpack"], { stdio: "inherit", shell: true });
106
- p.on("close", (code) => {
107
- running = false;
108
- if (code === 0) console.log(`${T.lime}✅ Build success. Watching...${T.reset}`);
109
- else console.log(`${T.red}💥 Build failed. Fix & save.${T.reset}`);
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
- 👀 WATCHER
99
+ 👀 INIT EVERYTHING AUTOMATICALLY
115
100
  ========================= */
116
- runAutoAction(PKG);
117
- chokidar.watch(process.cwd(), {
118
- ignored: ["**/node_modules/**","**/.next/**","**/.git/**","**/*.log"],
119
- ignoreInitial: true,
120
- usePolling: true,
121
- interval: 300
122
- }).on("all", (_, filePath) => {
123
- if (!filePath) return;
124
- runAutoAction(filePath);
125
- build();
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();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-auto-build",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Auto production build watcher for Next.js (adds auto script automatically)",
5
5
  "author": "Pavel Ahmmed Hridoy",
6
6
  "license": "MIT",