ai-cmg 0.0.6 → 0.0.8
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/dist/index.js +55 -14
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { execSync, spawnSync } from 'child_process';
|
|
3
|
-
import { readFileSync } from 'fs';
|
|
4
|
-
import { dirname, join } from 'path';
|
|
3
|
+
import { readFileSync, existsSync } from 'fs';
|
|
4
|
+
import { dirname, join, resolve } from 'path';
|
|
5
5
|
import { fileURLToPath } from 'url';
|
|
6
6
|
import prompts from 'prompts';
|
|
7
7
|
import clipboardy from 'clipboardy';
|
|
@@ -96,13 +96,46 @@ async function runConfigCommand(args) {
|
|
|
96
96
|
console.log('No changes made.');
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
-
function
|
|
99
|
+
function getGitEnv() {
|
|
100
|
+
const env = { ...process.env };
|
|
101
|
+
delete env.GIT_DIR;
|
|
102
|
+
delete env.GIT_WORK_TREE;
|
|
103
|
+
delete env.GIT_CEILING_DIRECTORIES;
|
|
104
|
+
return env;
|
|
105
|
+
}
|
|
106
|
+
function isGitAvailable() {
|
|
100
107
|
try {
|
|
101
|
-
|
|
108
|
+
execSync('git --version', { env: getGitEnv(), stdio: 'ignore' });
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function findRepoRootByFs(startDir) {
|
|
116
|
+
let current = resolve(startDir);
|
|
117
|
+
while (true) {
|
|
118
|
+
const gitPath = join(current, '.git');
|
|
119
|
+
if (existsSync(gitPath))
|
|
120
|
+
return current;
|
|
121
|
+
const parent = dirname(current);
|
|
122
|
+
if (parent === current)
|
|
123
|
+
return null;
|
|
124
|
+
current = parent;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function resolveRepoRoot(baseDir) {
|
|
128
|
+
try {
|
|
129
|
+
const root = execSync('git rev-parse --show-toplevel', {
|
|
130
|
+
env: getGitEnv(),
|
|
131
|
+
cwd: baseDir
|
|
132
|
+
}).toString().trim();
|
|
102
133
|
return root || null;
|
|
103
134
|
}
|
|
104
135
|
catch {
|
|
105
|
-
|
|
136
|
+
if (!baseDir)
|
|
137
|
+
return null;
|
|
138
|
+
return findRepoRootByFs(baseDir);
|
|
106
139
|
}
|
|
107
140
|
}
|
|
108
141
|
function parseMessageArgs(rawArgs) {
|
|
@@ -141,7 +174,9 @@ function parseMessageArgs(rawArgs) {
|
|
|
141
174
|
function getNameStatus(repoRoot) {
|
|
142
175
|
const map = new Map();
|
|
143
176
|
try {
|
|
144
|
-
const output = execSync(
|
|
177
|
+
const output = execSync(`git -C "${repoRoot}" diff --cached --name-status`, {
|
|
178
|
+
env: getGitEnv()
|
|
179
|
+
}).toString();
|
|
145
180
|
output.split('\n').forEach((line) => {
|
|
146
181
|
if (!line.trim())
|
|
147
182
|
return;
|
|
@@ -158,7 +193,9 @@ function getNameStatus(repoRoot) {
|
|
|
158
193
|
}
|
|
159
194
|
function getChangedFiles(repoRoot) {
|
|
160
195
|
try {
|
|
161
|
-
const output = execSync(
|
|
196
|
+
const output = execSync(`git -C "${repoRoot}" status --porcelain -z`, {
|
|
197
|
+
env: getGitEnv()
|
|
198
|
+
}).toString();
|
|
162
199
|
if (!output)
|
|
163
200
|
return [];
|
|
164
201
|
const parts = output.split('\0').filter(Boolean);
|
|
@@ -216,7 +253,7 @@ async function promptStageFiles(repoRoot) {
|
|
|
216
253
|
if (action === 'cancel')
|
|
217
254
|
return false;
|
|
218
255
|
if (action === 'all') {
|
|
219
|
-
spawnSync('git', ['add', '.'], { stdio: 'inherit',
|
|
256
|
+
spawnSync('git', ['-C', repoRoot, 'add', '.'], { stdio: 'inherit', env: getGitEnv() });
|
|
220
257
|
return true;
|
|
221
258
|
}
|
|
222
259
|
const fileChoices = candidates.map((entry) => ({
|
|
@@ -233,7 +270,7 @@ async function promptStageFiles(repoRoot) {
|
|
|
233
270
|
const files = picked.files;
|
|
234
271
|
if (!files || files.length === 0)
|
|
235
272
|
return false;
|
|
236
|
-
spawnSync('git', ['add', '--', ...files], { stdio: 'inherit',
|
|
273
|
+
spawnSync('git', ['-C', repoRoot, 'add', '--', ...files], { stdio: 'inherit', env: getGitEnv() });
|
|
237
274
|
return true;
|
|
238
275
|
}
|
|
239
276
|
function getExtension(filePath) {
|
|
@@ -348,7 +385,11 @@ async function main() {
|
|
|
348
385
|
return;
|
|
349
386
|
console.log('Analyzing staged changes...');
|
|
350
387
|
try {
|
|
351
|
-
|
|
388
|
+
if (!isGitAvailable()) {
|
|
389
|
+
console.log('Error: git is not available in PATH.');
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
const repoRoot = resolveRepoRoot(process.cwd());
|
|
352
393
|
if (!repoRoot) {
|
|
353
394
|
console.log('Error: not a git repository.');
|
|
354
395
|
return;
|
|
@@ -356,7 +397,7 @@ async function main() {
|
|
|
356
397
|
// 3. Git Diff 가져오기
|
|
357
398
|
let diff;
|
|
358
399
|
try {
|
|
359
|
-
diff = execSync(
|
|
400
|
+
diff = execSync(`git -C "${repoRoot}" diff --cached`, { env: getGitEnv() }).toString();
|
|
360
401
|
}
|
|
361
402
|
catch (e) {
|
|
362
403
|
console.log('Error: not a git repository or git is unavailable.');
|
|
@@ -366,7 +407,7 @@ async function main() {
|
|
|
366
407
|
const staged = await promptStageFiles(repoRoot);
|
|
367
408
|
if (!staged)
|
|
368
409
|
return;
|
|
369
|
-
diff = execSync(
|
|
410
|
+
diff = execSync(`git -C "${repoRoot}" diff --cached`, { env: getGitEnv() }).toString();
|
|
370
411
|
if (!diff.trim()) {
|
|
371
412
|
console.log('No staged changes. Nothing to commit.');
|
|
372
413
|
return;
|
|
@@ -425,11 +466,11 @@ async function main() {
|
|
|
425
466
|
const action = actions[choice - 1].value;
|
|
426
467
|
switch (action) {
|
|
427
468
|
case 'commit':
|
|
428
|
-
spawnSync('git', ['commit', '-m', message], { stdio: 'inherit',
|
|
469
|
+
spawnSync('git', ['-C', repoRoot, 'commit', '-m', message], { stdio: 'inherit', env: getGitEnv() });
|
|
429
470
|
console.log('Commit complete.');
|
|
430
471
|
break;
|
|
431
472
|
case 'edit':
|
|
432
|
-
spawnSync('git', ['commit', '-e', '-m', message], { stdio: 'inherit',
|
|
473
|
+
spawnSync('git', ['-C', repoRoot, 'commit', '-e', '-m', message], { stdio: 'inherit', env: getGitEnv() });
|
|
433
474
|
console.log('Commit complete.');
|
|
434
475
|
break;
|
|
435
476
|
case 'copy':
|