next-auto-build 1.0.9 â 1.0.10
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 +11 -69
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,19 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
|
-
const { spawn } = require('cross-spawn');
|
|
6
|
-
const chokidar = require('chokidar');
|
|
7
5
|
|
|
8
6
|
/* ===============================
|
|
9
7
|
đ Detect Target Project Root
|
|
10
8
|
================================= */
|
|
11
9
|
function getProjectRoot() {
|
|
12
|
-
// INIT_CWD is
|
|
10
|
+
// INIT_CWD is set by npm/pnpm/yarn during install in the host project
|
|
13
11
|
const initCwd = process.env.INIT_CWD;
|
|
14
12
|
if (initCwd && fs.existsSync(path.join(initCwd, 'package.json'))) {
|
|
15
13
|
return initCwd;
|
|
16
14
|
}
|
|
17
|
-
|
|
15
|
+
// fallback just in case
|
|
16
|
+
return process.cwd();
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
/* ===============================
|
|
@@ -29,84 +28,27 @@ function addAutoScript() {
|
|
|
29
28
|
try {
|
|
30
29
|
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
31
30
|
if (!pkg.scripts) pkg.scripts = {};
|
|
31
|
+
|
|
32
32
|
if (!pkg.scripts.auto) {
|
|
33
|
-
pkg.scripts.auto = "next-auto"; // inject script
|
|
33
|
+
pkg.scripts.auto = "next-auto-build"; // inject script
|
|
34
34
|
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
|
|
35
|
-
console.log('\x1b[32m%s\x1b[0m', 'đ¤ [next-auto-build]
|
|
35
|
+
console.log('\x1b[32m%s\x1b[0m', 'đ¤ [next-auto-build] "auto" script injected into host project package.json');
|
|
36
|
+
} else {
|
|
37
|
+
console.log('\x1b[36m%s\x1b[0m', 'âšī¸ [next-auto-build] "auto" script already exists, skipping...');
|
|
36
38
|
}
|
|
37
39
|
} catch (err) {
|
|
38
|
-
console.error('đž Failed to update package.json:', err.message);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/* ===============================
|
|
43
|
-
âī¸ Production Build Function
|
|
44
|
-
================================= */
|
|
45
|
-
let isBuilding = false;
|
|
46
|
-
const nextBin = path.resolve(getProjectRoot(), 'node_modules', '.bin', 'next');
|
|
47
|
-
|
|
48
|
-
function log(message, type = 'info') {
|
|
49
|
-
const icons = { info: 'đ¤', success: 'đ', error: 'đž', build: 'đ§' };
|
|
50
|
-
console.log(`${icons[type] || 'âĸ'} [${new Date().toLocaleTimeString()}] ${message}`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function runProductionBuild() {
|
|
54
|
-
if (isBuilding) return;
|
|
55
|
-
if (!fs.existsSync(nextBin)) {
|
|
56
|
-
log('Next.js binary not found. Run install first.', 'error');
|
|
57
|
-
return;
|
|
40
|
+
console.error('đž Failed to update host package.json:', err.message);
|
|
58
41
|
}
|
|
59
|
-
|
|
60
|
-
isBuilding = true;
|
|
61
|
-
console.clear();
|
|
62
|
-
log('Starting Production Build (Global Monitor Mode)...', 'build');
|
|
63
|
-
|
|
64
|
-
const build = spawn(nextBin, ['build', '--webpack'], {
|
|
65
|
-
stdio: 'inherit',
|
|
66
|
-
shell: true
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
build.on('close', (code) => {
|
|
70
|
-
isBuilding = false;
|
|
71
|
-
if (code === 0) log('Production Build Completed Successfully!', 'success');
|
|
72
|
-
else log('Build Failed! Fix errors to retry.', 'error');
|
|
73
|
-
log('Waiting for any file change to rebuild...', 'info');
|
|
74
|
-
});
|
|
75
42
|
}
|
|
76
43
|
|
|
77
44
|
/* ===============================
|
|
78
45
|
đ Main Execution
|
|
79
46
|
================================= */
|
|
80
47
|
const args = process.argv.slice(2);
|
|
81
|
-
|
|
82
|
-
// Postinstall hook: inject script automatically
|
|
83
48
|
if (args.includes('--postinstall')) {
|
|
84
49
|
addAutoScript();
|
|
85
50
|
process.exit(0);
|
|
86
51
|
}
|
|
87
52
|
|
|
88
|
-
//
|
|
89
|
-
addAutoScript();
|
|
90
|
-
|
|
91
|
-
const watcher = chokidar.watch('.', {
|
|
92
|
-
ignored: ['**/node_modules/**', '**/.next/**', '**/.git/**', '**/*.log'],
|
|
93
|
-
persistent: true,
|
|
94
|
-
ignoreInitial: true,
|
|
95
|
-
usePolling: true,
|
|
96
|
-
interval: 300
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
log('Next-Auto-Builder Active (Production Mode Only)', 'info');
|
|
100
|
-
|
|
101
|
-
watcher.on('all', (event, filePath) => {
|
|
102
|
-
if (filePath.includes('.next')) return;
|
|
103
|
-
log(`File Event [${event}]: ${filePath}`, 'info');
|
|
104
|
-
runProductionBuild();
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
runProductionBuild();
|
|
108
|
-
|
|
109
|
-
process.on('uncaughtException', (err) => {
|
|
110
|
-
log(`System Error: ${err.message}`, 'error');
|
|
111
|
-
isBuilding = false;
|
|
112
|
-
});
|
|
53
|
+
// optional: if user runs manually
|
|
54
|
+
addAutoScript();
|