-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathindex.ts
157 lines (141 loc) · 3.96 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
import fs from 'node:fs/promises';
import { Command } from 'commander';
import { spinnerError, stopSpinner } from './cli/spinner.js';
import { init } from './cli/commands/init/index.js';
import { estimate } from './cli/commands/estimate/index.js';
import { index } from './cli/commands/index/index.js';
import { query } from './cli/commands/query/index.js';
import { AutodocRepoConfig, AutodocUserConfig } from './types.js';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { user } from './cli/commands/user/index.js';
import { userConfigFilePath } from './const.js';
const program = new Command();
program.description('Autodoc CLI Tool');
program.version('0.0.9');
program
.command('init')
.description(
'Initialize repository by creating a `autodoc.config.json` file in the current directory.',
)
.action(async () => {
try {
const config: AutodocRepoConfig = JSON.parse(
await fs.readFile('./autodoc.config.json', 'utf8'),
);
init(config);
} catch (e) {
init();
}
});
program
.command('estimate')
.description('Estimate the cost of running `index` on your respository.')
.action(async () => {
try {
const config: AutodocRepoConfig = JSON.parse(
await fs.readFile('./autodoc.config.json', 'utf8'),
);
estimate(config);
} catch (e) {
console.error(
'Failed to find `autodoc.config.json` file. Did you run `doc init`?',
);
console.error(e);
process.exit(1);
}
});
program
.command('index')
.description(
'Traverse your codebase, write docs via LLM, and create a locally stored index.',
)
.action(async () => {
try {
const config: AutodocRepoConfig = JSON.parse(
await fs.readFile('./autodoc.config.json', 'utf8'),
);
await estimate(config);
const questions = [
{
type: 'confirm',
name: 'continue',
message: 'Do you want to continue with indexing?',
default: true,
},
];
const answers = await inquirer.prompt(questions);
if (answers.continue) {
console.log(chalk.green('Starting crawl...'));
index(config);
} else {
console.log('Exiting...');
process.exit(0);
}
} catch (e) {
console.error(
'Failed to find `autodoc.config.json` file. Did you run `doc init`?',
);
console.error(e);
process.exit(1);
}
});
program
.command('user')
.description('Set the Autodoc user config')
.action(async () => {
try {
const config: AutodocUserConfig = JSON.parse(
await fs.readFile(userConfigFilePath, 'utf8'),
);
user(config);
} catch (e) {
user();
}
});
program
.command('q')
.description('Query an Autodoc index')
.action(async () => {
let repoConfig: AutodocRepoConfig;
try {
repoConfig = JSON.parse(
await fs.readFile('./autodoc.config.json', 'utf8'),
);
} catch (e) {
console.error(
'Failed to find `autodoc.config.json` file. Did you run `doc init`?',
);
console.error(e);
process.exit(1);
}
try {
const userConfig: AutodocUserConfig = JSON.parse(
await fs.readFile(userConfigFilePath, 'utf8'),
);
query(repoConfig, userConfig);
} catch (e) {
try {
await user();
const userConfig: AutodocRepoConfig = JSON.parse(
await fs.readFile(userConfigFilePath, 'utf8'),
);
query(repoConfig, userConfig);
} catch (e) {
console.error('Failed to config file. Did you run `doc init`?');
console.error(e);
process.exit(1);
}
}
});
/**
* Listen for unhandled promise rejections
*/
process.on('unhandledRejection', function (err: Error) {
console.error(err.stack);
spinnerError(); // show an error spinner
stopSpinner(); // stop the spinner
program.error('', { exitCode: 1 }); // exit with error code 1
});
program.parse();