@bobfrankston/npmglobalize 1.0.97 → 1.0.98
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/.claude/settings.local.json +3 -1
- package/index.d.ts +2 -1
- package/lib.js +83 -5
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -3,5 +3,6 @@
|
|
|
3
3
|
* npmglobalize - Main entry point
|
|
4
4
|
*/
|
|
5
5
|
import './cli.js';
|
|
6
|
-
export { globalize, globalizeWorkspace
|
|
6
|
+
export { globalize, globalizeWorkspace } from './lib.js';
|
|
7
|
+
export type { GlobalizeOptions, WorkspaceResult } from './lib.js';
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
package/lib.js
CHANGED
|
@@ -71,6 +71,51 @@ function removeNulFiles(dir, visited = new Set()) {
|
|
|
71
71
|
}
|
|
72
72
|
return count;
|
|
73
73
|
}
|
|
74
|
+
/** Repair a corrupted git index by rebuilding it.
|
|
75
|
+
* Handles "invalid object" / "Error building trees" errors caused by
|
|
76
|
+
* stale entries in .git/index referencing missing objects. */
|
|
77
|
+
function repairGitIndex(cwd) {
|
|
78
|
+
const indexPath = path.join(cwd, '.git', 'index');
|
|
79
|
+
try {
|
|
80
|
+
console.log(colors.yellow('Detected corrupted git index — rebuilding...'));
|
|
81
|
+
// Delete the corrupted index
|
|
82
|
+
if (fs.existsSync(indexPath)) {
|
|
83
|
+
fs.unlinkSync(indexPath);
|
|
84
|
+
}
|
|
85
|
+
// Rebuild from HEAD (reset index to match last commit)
|
|
86
|
+
const resetResult = spawnSafe('git', ['reset'], {
|
|
87
|
+
encoding: 'utf-8',
|
|
88
|
+
stdio: 'pipe',
|
|
89
|
+
cwd,
|
|
90
|
+
env: process.env
|
|
91
|
+
});
|
|
92
|
+
if (resetResult.status !== 0) {
|
|
93
|
+
// If no commits yet, just proceed — git add -A will build a fresh index
|
|
94
|
+
const stderr = resetResult.stderr || '';
|
|
95
|
+
if (!stderr.includes('does not have any commits')) {
|
|
96
|
+
console.error(colors.red('Failed to reset git index:'), stderr.trim());
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Re-stage all files
|
|
101
|
+
const addResult = spawnSafe('git', ['add', '-A'], {
|
|
102
|
+
encoding: 'utf-8',
|
|
103
|
+
stdio: 'pipe',
|
|
104
|
+
cwd,
|
|
105
|
+
env: process.env
|
|
106
|
+
});
|
|
107
|
+
if (addResult.status !== 0) {
|
|
108
|
+
console.error(colors.red('Failed to re-add files after index rebuild:'), (addResult.stderr || '').trim());
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
console.log(colors.green('✓ Git index rebuilt successfully'));
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
console.error(colors.red('Failed to repair git index:'), err.message);
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
74
119
|
/** Get npm command for current platform (npm.cmd on Windows, npm elsewhere) */
|
|
75
120
|
function getNpmCommand() {
|
|
76
121
|
return process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
@@ -958,6 +1003,27 @@ export function runCommandOrThrow(cmd, args, options = {}) {
|
|
|
958
1003
|
}
|
|
959
1004
|
throw new Error('Failed to add safe.directory to git config.');
|
|
960
1005
|
}
|
|
1006
|
+
// Detect corrupted git index and auto-repair
|
|
1007
|
+
if (cmd === 'git' && (stderr.includes('invalid object') || stderr.includes('Error building trees'))) {
|
|
1008
|
+
if (options.cwd && repairGitIndex(options.cwd)) {
|
|
1009
|
+
// Retry the original command
|
|
1010
|
+
const retry = spawnSafe(cmd, args, {
|
|
1011
|
+
encoding: 'utf-8',
|
|
1012
|
+
stdio: 'pipe',
|
|
1013
|
+
cwd: options.cwd,
|
|
1014
|
+
env: process.env,
|
|
1015
|
+
shell: needsShell
|
|
1016
|
+
});
|
|
1017
|
+
if (retry.status === 0) {
|
|
1018
|
+
return (retry.stdout || '') + (retry.stderr || '');
|
|
1019
|
+
}
|
|
1020
|
+
const retryStderr = retry.stderr || '';
|
|
1021
|
+
if (retryStderr.trim()) {
|
|
1022
|
+
console.error(colors.red(retryStderr.trim()));
|
|
1023
|
+
}
|
|
1024
|
+
throw new Error(`Command failed after git index repair: ${cmd} ${args.join(' ')}`);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
961
1027
|
// Show the error output
|
|
962
1028
|
if (stderr.trim()) {
|
|
963
1029
|
console.error(colors.red(stderr.trim()));
|
|
@@ -2520,13 +2586,25 @@ export async function globalize(cwd, options = {}, configOptions = {}) {
|
|
|
2520
2586
|
}
|
|
2521
2587
|
console.log(colors.yellow('Continuing with --force...'));
|
|
2522
2588
|
}
|
|
2523
|
-
|
|
2589
|
+
let commitResult = runCommand('git', ['commit', '-m', commitMsg], { cwd });
|
|
2524
2590
|
if (!commitResult.success) {
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2591
|
+
// Check for corrupted git index ("invalid object" / "Error building trees")
|
|
2592
|
+
// Run silently to capture stderr for detection
|
|
2593
|
+
const probe = runCommand('git', ['commit', '-m', commitMsg], { cwd, silent: true });
|
|
2594
|
+
const errText = probe.stderr + probe.output;
|
|
2595
|
+
if (errText.includes('invalid object') || errText.includes('Error building trees')) {
|
|
2596
|
+
if (repairGitIndex(cwd)) {
|
|
2597
|
+
// Retry commit after repair
|
|
2598
|
+
commitResult = runCommand('git', ['commit', '-m', commitMsg], { cwd });
|
|
2599
|
+
}
|
|
2600
|
+
}
|
|
2601
|
+
if (!commitResult.success) {
|
|
2602
|
+
console.error(colors.red('ERROR: Failed to commit changes:'), commitResult.stderr);
|
|
2603
|
+
if (!force) {
|
|
2604
|
+
return false;
|
|
2605
|
+
}
|
|
2606
|
+
console.log(colors.yellow('Continuing with --force...'));
|
|
2528
2607
|
}
|
|
2529
|
-
console.log(colors.yellow('Continuing with --force...'));
|
|
2530
2608
|
}
|
|
2531
2609
|
}
|
|
2532
2610
|
else {
|