next-auto-build 1.0.7 → 1.0.9

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 +78 -81
  2. package/package.json +5 -1
package/index.js CHANGED
@@ -1,115 +1,112 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const chokidar = require('chokidar');
4
- const spawn = require('cross-spawn');
5
- const path = require('path');
6
3
  const fs = require('fs');
4
+ const path = require('path');
5
+ const { spawn } = require('cross-spawn');
6
+ const chokidar = require('chokidar');
7
7
 
8
- // প্রোজেক্ট রুট খুঁজে বের করার উন্নত লজিক (pnpm ও Termux ফ্রেন্ডলি)
8
+ /* ===============================
9
+ 🌍 Detect Target Project Root
10
+ ================================= */
9
11
  function getProjectRoot() {
10
- const initCwd = process.env.INIT_CWD;
11
-
12
- if (
13
- initCwd &&
14
- fs.existsSync(path.join(initCwd, 'package.json'))
15
- ) {
16
- return initCwd;
17
- }
18
-
19
- // fallback (rare case)
20
- let root = process.cwd();
21
- if (root.includes('node_modules')) {
22
- root = root.split(`${path.sep}node_modules`)[0];
23
- }
24
- return root;
12
+ // INIT_CWD is automatically set by npm/pnpm/yarn during install
13
+ const initCwd = process.env.INIT_CWD;
14
+ if (initCwd && fs.existsSync(path.join(initCwd, 'package.json'))) {
15
+ return initCwd;
16
+ }
17
+ return process.cwd(); // fallback
25
18
  }
26
19
 
27
- const projectRoot = getProjectRoot();
28
- const nextBin = path.resolve(projectRoot, 'node_modules', '.bin', 'next');
29
-
30
- // ১. অটো-স্ক্রিপ্ট ইনজেক্টর (FIXED PATH)
20
+ /* ===============================
21
+ 🤖 Auto Script Injector
22
+ ================================= */
31
23
  function addAutoScript() {
32
- const packageJsonPath = path.join(projectRoot, 'package.json');
33
-
34
- if (fs.existsSync(packageJsonPath)) {
35
- try {
36
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
37
- if (!packageJson.scripts) packageJson.scripts = {};
38
-
39
- if (!packageJson.scripts.auto) {
40
- packageJson.scripts.auto = "next-auto";
41
- fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
42
- console.log('\x1b[32m%s\x1b[0m', '🤖 [next-auto-build] Success: "auto" script added to your package.json');
43
- }
44
- } catch (err) { /* সাইলেন্ট এরর */ }
24
+ const projectRoot = getProjectRoot();
25
+ const pkgPath = path.join(projectRoot, 'package.json');
26
+
27
+ if (!fs.existsSync(pkgPath)) return;
28
+
29
+ try {
30
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
31
+ if (!pkg.scripts) pkg.scripts = {};
32
+ if (!pkg.scripts.auto) {
33
+ pkg.scripts.auto = "next-auto"; // inject script
34
+ fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
35
+ console.log('\x1b[32m%s\x1b[0m', '🤖 [next-auto-build] Success: "auto" script added to package.json');
45
36
  }
37
+ } catch (err) {
38
+ console.error('👾 Failed to update package.json:', err.message);
39
+ }
46
40
  }
47
41
 
48
- // ২. কাস্টম লগ সিস্টেম
42
+ /* ===============================
43
+ ⚙️ Production Build Function
44
+ ================================= */
45
+ let isBuilding = false;
46
+ const nextBin = path.resolve(getProjectRoot(), 'node_modules', '.bin', 'next');
47
+
49
48
  function log(message, type = 'info') {
50
- const icons = { info: '🤖', success: '🎉', error: '👾', build: '🚧' };
51
- console.log(`${icons[type] || '•'} [${new Date().toLocaleTimeString()}] ${message}`);
49
+ const icons = { info: '🤖', success: '🎉', error: '👾', build: '🚧' };
50
+ console.log(`${icons[type] || '•'} [${new Date().toLocaleTimeString()}] ${message}`);
52
51
  }
53
52
 
54
- // ৩. প্রোডাকশন বিল্ড লজিক
55
- let isBuilding = false;
56
53
  function runProductionBuild() {
57
- if (isBuilding) return;
58
- if (!fs.existsSync(nextBin)) {
59
- log('Next.js binary not found. Please run install first.', 'error');
60
- return;
61
- }
62
-
63
- isBuilding = true;
64
- console.clear();
65
- log('Starting Production Build (Global Monitor Mode)...', 'build');
66
-
67
- const build = spawn(nextBin, ['build', '--webpack'], {
68
- stdio: 'inherit',
69
- shell: true
70
- });
71
-
72
- build.on('close', (code) => {
73
- isBuilding = false;
74
- if (code === 0) {
75
- log('Production Build Completed Successfully!', 'success');
76
- } else {
77
- log('Build Failed! Fix errors to retry.', 'error');
78
- }
79
- log('Waiting for ANY file change to rebuild...', 'info');
80
- });
54
+ if (isBuilding) return;
55
+ if (!fs.existsSync(nextBin)) {
56
+ log('Next.js binary not found. Run install first.', 'error');
57
+ return;
58
+ }
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
+ });
81
75
  }
82
76
 
83
- // ৪. মেইন এক্সিকিউশন
77
+ /* ===============================
78
+ 🏁 Main Execution
79
+ ================================= */
84
80
  const args = process.argv.slice(2);
85
81
 
82
+ // Postinstall hook: inject script automatically
86
83
  if (args.includes('--postinstall')) {
87
- addAutoScript();
88
- process.exit(0);
84
+ addAutoScript();
85
+ process.exit(0);
89
86
  }
90
87
 
91
- // অটো-স্ক্রিপ্ট চেক এবং রান
88
+ // Run manually: add script + watcher
92
89
  addAutoScript();
93
90
 
94
- const watcher = chokidar.watch('.', {
95
- ignored: ['**/node_modules/**', '**/.next/**', '**/.git/**', '**/*.log'],
96
- persistent: true,
97
- ignoreInitial: true,
98
- usePolling: true,
99
- interval: 300
91
+ const watcher = chokidar.watch('.', {
92
+ ignored: ['**/node_modules/**', '**/.next/**', '**/.git/**', '**/*.log'],
93
+ persistent: true,
94
+ ignoreInitial: true,
95
+ usePolling: true,
96
+ interval: 300
100
97
  });
101
98
 
102
99
  log('Next-Auto-Builder Active (Production Mode Only)', 'info');
103
100
 
104
101
  watcher.on('all', (event, filePath) => {
105
- if (filePath.includes('.next')) return;
106
- log(`File Event [${event}]: ${filePath}`, 'info');
107
- runProductionBuild();
102
+ if (filePath.includes('.next')) return;
103
+ log(`File Event [${event}]: ${filePath}`, 'info');
104
+ runProductionBuild();
108
105
  });
109
106
 
110
107
  runProductionBuild();
111
108
 
112
109
  process.on('uncaughtException', (err) => {
113
- log(`System Error: ${err.message}`, 'error');
114
- isBuilding = false;
115
- });
110
+ log(`System Error: ${err.message}`, 'error');
111
+ isBuilding = false;
112
+ });
package/package.json CHANGED
@@ -1,6 +1,10 @@
1
1
  {
2
2
  "name": "next-auto-build",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
+ "description": "Auto-injects 'auto' script for Next.js projects and manages builds",
5
+ "license": "MIT",
6
+ "author": "Pavel Ahmmed Hridoy",
7
+ "type": "commonjs",
4
8
  "main": "index.js",
5
9
  "bin": {
6
10
  "next-auto": "./index.js"