@trydecember/cli 0.2.6 → 0.2.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/LICENSE +202 -0
- package/dist/december.js +100239 -57666
- package/package.json +12 -2
- package/src/auth/browser.ts +0 -134
- package/src/auth/device.ts +0 -70
- package/src/auth/index.ts +0 -2
- package/src/index.ts +0 -145
- package/src/local-operations.ts +0 -110
- package/tsconfig.json +0 -6
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"module": "src/index.ts",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
|
-
"version": "0.2.
|
|
6
|
+
"version": "0.2.8",
|
|
7
7
|
"description": "Turn ideas into reality.",
|
|
8
8
|
"author": "Chaitanya Sonawane",
|
|
9
9
|
"license": "Apache-2.0",
|
|
@@ -14,11 +14,18 @@
|
|
|
14
14
|
"engines": {
|
|
15
15
|
"node": ">=18.0.0"
|
|
16
16
|
},
|
|
17
|
+
"main": "./dist/december.js",
|
|
17
18
|
"bin": {
|
|
18
19
|
"december": "./dist/december.js"
|
|
19
20
|
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE"
|
|
25
|
+
],
|
|
20
26
|
"scripts": {
|
|
21
|
-
"build": "bun build ./src/index.ts --target=node --outfile=./dist/december.js"
|
|
27
|
+
"build": "bun build ./src/index.ts --target=node --outfile=./dist/december.js",
|
|
28
|
+
"prepublishOnly": "bun run build"
|
|
22
29
|
},
|
|
23
30
|
"devDependencies": {
|
|
24
31
|
"@december/agent": "workspace:*",
|
|
@@ -35,6 +42,9 @@
|
|
|
35
42
|
"typescript": "^5"
|
|
36
43
|
},
|
|
37
44
|
"dependencies": {
|
|
45
|
+
"clipboardy": "5.3.1",
|
|
46
|
+
"ink-select-input": "6.2.0",
|
|
47
|
+
"ink-text-input": "6.0.0",
|
|
38
48
|
"open": "11.0.0"
|
|
39
49
|
}
|
|
40
50
|
}
|
package/src/auth/browser.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import http from 'node:http'
|
|
2
|
-
|
|
3
|
-
import open from 'open'
|
|
4
|
-
|
|
5
|
-
export async function loginViaBrowser(
|
|
6
|
-
baseUrl: string = 'http://localhost:3000/cli/login'
|
|
7
|
-
): Promise<{ token: string; email: string | null }> {
|
|
8
|
-
return new Promise((resolve, reject) => {
|
|
9
|
-
const server = http.createServer((req, res) => {
|
|
10
|
-
const url = new URL(req.url || '', `http://${req.headers.host}`)
|
|
11
|
-
|
|
12
|
-
if (url.pathname === '/callback') {
|
|
13
|
-
const token = url.searchParams.get('token')
|
|
14
|
-
const email = url.searchParams.get('email')
|
|
15
|
-
|
|
16
|
-
if (token) {
|
|
17
|
-
res.writeHead(200, { 'Content-Type': 'text/html' })
|
|
18
|
-
res.end(`
|
|
19
|
-
<html>
|
|
20
|
-
<head>
|
|
21
|
-
<title>Authorized</title>
|
|
22
|
-
<style>
|
|
23
|
-
body {
|
|
24
|
-
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
|
25
|
-
display: flex;
|
|
26
|
-
flex-direction: column;
|
|
27
|
-
justify-content: center;
|
|
28
|
-
align-items: center;
|
|
29
|
-
height: 100vh;
|
|
30
|
-
margin: 0;
|
|
31
|
-
background-color: #141414;
|
|
32
|
-
color: #fff;
|
|
33
|
-
text-align: center;
|
|
34
|
-
}
|
|
35
|
-
.container {
|
|
36
|
-
display: flex;
|
|
37
|
-
flex-direction: column;
|
|
38
|
-
align-items: center;
|
|
39
|
-
max-width: 380px;
|
|
40
|
-
width: 100%;
|
|
41
|
-
}
|
|
42
|
-
svg {
|
|
43
|
-
width: 42px;
|
|
44
|
-
height: 42px;
|
|
45
|
-
margin-bottom: 24px;
|
|
46
|
-
}
|
|
47
|
-
h1 {
|
|
48
|
-
font-size: 22px;
|
|
49
|
-
font-weight: 400;
|
|
50
|
-
margin: 0 0 4px 0;
|
|
51
|
-
letter-spacing: -0.025em;
|
|
52
|
-
}
|
|
53
|
-
p {
|
|
54
|
-
font-size: 13px;
|
|
55
|
-
color: #A3A3A3;
|
|
56
|
-
margin: 0;
|
|
57
|
-
}
|
|
58
|
-
</style>
|
|
59
|
-
</head>
|
|
60
|
-
<body>
|
|
61
|
-
<div class="container">
|
|
62
|
-
<svg viewBox="5 4 14 16" fill="none" stroke="white" stroke-width="1" stroke-linecap="round" stroke-linejoin="round">
|
|
63
|
-
<line x1="12" y1="12" x2="12" y2="5.3" />
|
|
64
|
-
<line x1="12" y1="7.5" x2="13.9" y2="6.4" />
|
|
65
|
-
<line x1="12" y1="7.5" x2="10.1" y2="6.4" />
|
|
66
|
-
<line x1="12" y1="12" x2="17.8" y2="8.65" />
|
|
67
|
-
<line x1="15.9" y1="9.75" x2="17.8" y2="10.85" />
|
|
68
|
-
<line x1="15.9" y1="9.75" x2="15.9" y2="7.55" />
|
|
69
|
-
<line x1="12" y1="12" x2="17.8" y2="15.35" />
|
|
70
|
-
<line x1="15.9" y1="14.25" x2="15.9" y2="16.45" />
|
|
71
|
-
<line x1="15.9" y1="14.25" x2="17.8" y2="13.15" />
|
|
72
|
-
<line x1="12" y1="12" x2="12" y2="18.7" />
|
|
73
|
-
<line x1="12" y1="16.5" x2="10.1" y2="17.6" />
|
|
74
|
-
<line x1="12" y1="16.5" x2="13.9" y2="17.6" />
|
|
75
|
-
<line x1="12" y1="12" x2="6.2" y2="15.35" />
|
|
76
|
-
<line x1="8.1" y1="14.25" x2="6.2" y2="13.15" />
|
|
77
|
-
<line x1="8.1" y1="14.25" x2="8.1" y2="16.45" />
|
|
78
|
-
<line x1="12" y1="12" x2="6.2" y2="8.65" />
|
|
79
|
-
<line x1="8.1" y1="9.75" x2="8.1" y2="7.55" />
|
|
80
|
-
<line x1="8.1" y1="9.75" x2="6.2" y2="10.85" />
|
|
81
|
-
</svg>
|
|
82
|
-
<h1>Authorized</h1>
|
|
83
|
-
<p>You can safely close this tab and return to the terminal.</p>
|
|
84
|
-
</div>
|
|
85
|
-
<script>
|
|
86
|
-
setTimeout(() => window.close(), 3000);
|
|
87
|
-
</script>
|
|
88
|
-
</body>
|
|
89
|
-
</html>
|
|
90
|
-
`)
|
|
91
|
-
|
|
92
|
-
server.close(() => {
|
|
93
|
-
resolve({ token, email })
|
|
94
|
-
})
|
|
95
|
-
} else {
|
|
96
|
-
res.writeHead(400, { 'Content-Type': 'text/plain' })
|
|
97
|
-
res.end('Missing token in callback.')
|
|
98
|
-
server.close(() => {
|
|
99
|
-
reject(new Error('Missing token in callback'))
|
|
100
|
-
})
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
res.writeHead(404, { 'Content-Type': 'text/plain' })
|
|
104
|
-
res.end('Not found')
|
|
105
|
-
}
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
server.listen(0, '127.0.0.1', () => {
|
|
109
|
-
const address = server.address()
|
|
110
|
-
if (address && typeof address === 'object') {
|
|
111
|
-
const port = address.port
|
|
112
|
-
const redirectUri = `http://127.0.0.1:${port}/callback`
|
|
113
|
-
const loginUrl = `${baseUrl}?redirect_uri=${encodeURIComponent(redirectUri)}`
|
|
114
|
-
|
|
115
|
-
open(loginUrl).catch((err) => {
|
|
116
|
-
server.close()
|
|
117
|
-
reject(err)
|
|
118
|
-
})
|
|
119
|
-
} else {
|
|
120
|
-
server.close()
|
|
121
|
-
reject(new Error('Failed to start local server'))
|
|
122
|
-
}
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
// Timeout after 5 minutes
|
|
126
|
-
setTimeout(
|
|
127
|
-
() => {
|
|
128
|
-
server.close()
|
|
129
|
-
reject(new Error('Login timed out'))
|
|
130
|
-
},
|
|
131
|
-
5 * 60 * 1000
|
|
132
|
-
)
|
|
133
|
-
})
|
|
134
|
-
}
|
package/src/auth/device.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
export async function loginViaDeviceCode(
|
|
2
|
-
baseUrl: string = 'http://localhost:4000',
|
|
3
|
-
onCodeGenerated: (userCode: string, verificationUri: string) => void
|
|
4
|
-
): Promise<{ token: string; email: string | null }> {
|
|
5
|
-
return new Promise(async (resolve, reject) => {
|
|
6
|
-
try {
|
|
7
|
-
// 1. Generate Code
|
|
8
|
-
const genRes = await fetch(`${baseUrl}/api/v1/auth/device/code`, {
|
|
9
|
-
method: 'POST',
|
|
10
|
-
headers: { 'Content-Type': 'application/json' },
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
if (!genRes.ok) {
|
|
14
|
-
throw new Error('Failed to generate device code')
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const genData = await genRes.json()
|
|
18
|
-
if (!genData.success) {
|
|
19
|
-
throw new Error(genData.message || 'Failed to generate device code')
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const { deviceCode, userCode, verificationUri, expiresIn, interval } = genData.data
|
|
23
|
-
|
|
24
|
-
onCodeGenerated(userCode, verificationUri)
|
|
25
|
-
|
|
26
|
-
// 2. Poll for token
|
|
27
|
-
const startTime = Date.now()
|
|
28
|
-
const pollInterval = interval * 1000 || 5000
|
|
29
|
-
|
|
30
|
-
const poll = async () => {
|
|
31
|
-
if (Date.now() - startTime > expiresIn * 1000) {
|
|
32
|
-
reject(new Error('Device code expired'))
|
|
33
|
-
return
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
try {
|
|
37
|
-
const tokenRes = await fetch(`${baseUrl}/api/v1/auth/device/token`, {
|
|
38
|
-
method: 'POST',
|
|
39
|
-
headers: { 'Content-Type': 'application/json' },
|
|
40
|
-
body: JSON.stringify({ deviceCode }),
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
const tokenData = await tokenRes.json()
|
|
44
|
-
|
|
45
|
-
if (tokenRes.ok && tokenData.success) {
|
|
46
|
-
resolve({
|
|
47
|
-
token: tokenData.data.token,
|
|
48
|
-
email: tokenData.data.email,
|
|
49
|
-
})
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// If not ok, check if it's authorization_pending
|
|
54
|
-
if (tokenData.message === 'authorization_pending') {
|
|
55
|
-
setTimeout(poll, pollInterval)
|
|
56
|
-
} else {
|
|
57
|
-
reject(new Error(tokenData.message || 'Polling failed'))
|
|
58
|
-
}
|
|
59
|
-
} catch (err) {
|
|
60
|
-
// Network errors during polling, just keep trying
|
|
61
|
-
setTimeout(poll, pollInterval)
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
setTimeout(poll, pollInterval)
|
|
66
|
-
} catch (err) {
|
|
67
|
-
reject(err)
|
|
68
|
-
}
|
|
69
|
-
})
|
|
70
|
-
}
|
package/src/auth/index.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { AgentHarness, FileSessionRepository, getProviderConfig, loadConfig } from '@december/agent'
|
|
3
|
-
import {
|
|
4
|
-
OpenAIProvider,
|
|
5
|
-
AnthropicProvider,
|
|
6
|
-
GeminiProvider,
|
|
7
|
-
OpenRouterProvider,
|
|
8
|
-
} from '@december/providers'
|
|
9
|
-
import {
|
|
10
|
-
BashTool,
|
|
11
|
-
ReadFileTool,
|
|
12
|
-
WriteFileTool,
|
|
13
|
-
LsTool,
|
|
14
|
-
EditFileTool,
|
|
15
|
-
EditDiffTool,
|
|
16
|
-
FindFilesTool,
|
|
17
|
-
GrepSearchTool,
|
|
18
|
-
SubagentTool,
|
|
19
|
-
} from '@december/tools'
|
|
20
|
-
import { App } from '@december/tui'
|
|
21
|
-
import { render } from 'ink'
|
|
22
|
-
import React from 'react'
|
|
23
|
-
|
|
24
|
-
import pkg from '../package.json' with { type: 'json' }
|
|
25
|
-
|
|
26
|
-
import { loginViaBrowser, loginViaDeviceCode } from './auth'
|
|
27
|
-
import { localOperations } from './local-operations'
|
|
28
|
-
|
|
29
|
-
const originalConsoleError = console.error
|
|
30
|
-
|
|
31
|
-
async function main() {
|
|
32
|
-
process.title = 'december'
|
|
33
|
-
process.stdout.write('\x1b]0;december\x07')
|
|
34
|
-
|
|
35
|
-
// Suppress noisy SDK console logs that corrupt the Ink TUI layout
|
|
36
|
-
console.warn = () => {}
|
|
37
|
-
console.error = () => {}
|
|
38
|
-
console.log = () => {}
|
|
39
|
-
console.info = () => {}
|
|
40
|
-
|
|
41
|
-
const providerConfig = await getProviderConfig()
|
|
42
|
-
|
|
43
|
-
// If not authenticated, we pass a dummy provider so the Agent can boot.
|
|
44
|
-
// The TUI will intercept prompts and force them to /login
|
|
45
|
-
let llm: any
|
|
46
|
-
if (providerConfig) {
|
|
47
|
-
switch (providerConfig.provider) {
|
|
48
|
-
case 'openai':
|
|
49
|
-
llm = new OpenAIProvider(undefined, providerConfig.apiKey)
|
|
50
|
-
break
|
|
51
|
-
case 'anthropic':
|
|
52
|
-
llm = new AnthropicProvider(providerConfig.apiKey)
|
|
53
|
-
break
|
|
54
|
-
case 'google':
|
|
55
|
-
case 'gemini':
|
|
56
|
-
llm = new GeminiProvider(providerConfig.apiKey)
|
|
57
|
-
break
|
|
58
|
-
case 'openrouter':
|
|
59
|
-
llm = new OpenRouterProvider(providerConfig.apiKey)
|
|
60
|
-
break
|
|
61
|
-
case 'deepseek':
|
|
62
|
-
llm = new OpenAIProvider('https://api.deepseek.com', providerConfig.apiKey)
|
|
63
|
-
break
|
|
64
|
-
case 'groq':
|
|
65
|
-
llm = new OpenAIProvider('https://api.groq.com/openai/v1', providerConfig.apiKey)
|
|
66
|
-
break
|
|
67
|
-
case 'huggingface':
|
|
68
|
-
llm = new OpenAIProvider(
|
|
69
|
-
'https://api-inference.huggingface.co/v1',
|
|
70
|
-
providerConfig.apiKey
|
|
71
|
-
)
|
|
72
|
-
break
|
|
73
|
-
case 'kimi':
|
|
74
|
-
case 'moonshoot':
|
|
75
|
-
llm = new OpenAIProvider('https://api.moonshot.cn/v1', providerConfig.apiKey)
|
|
76
|
-
break
|
|
77
|
-
case 'mistral':
|
|
78
|
-
llm = new OpenAIProvider('https://api.mistral.ai/v1', providerConfig.apiKey)
|
|
79
|
-
break
|
|
80
|
-
case 'xai':
|
|
81
|
-
llm = new OpenAIProvider('https://api.x.ai/v1', providerConfig.apiKey)
|
|
82
|
-
break
|
|
83
|
-
case 'zai':
|
|
84
|
-
llm = new OpenAIProvider('https://api.zai.ai/v1', providerConfig.apiKey)
|
|
85
|
-
break
|
|
86
|
-
}
|
|
87
|
-
} else {
|
|
88
|
-
llm = new OpenAIProvider(undefined, 'dummy-key')
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const isAuthenticated = !!providerConfig
|
|
92
|
-
|
|
93
|
-
const sessionRepository = new FileSessionRepository()
|
|
94
|
-
const sessionId = `session-${Date.now()}`
|
|
95
|
-
|
|
96
|
-
const harness = new AgentHarness({
|
|
97
|
-
baseSystemPrompt:
|
|
98
|
-
'You are December, an autonomous software engineer. You have access to tools. When executing code, please use JSON schemas for tool inputs. Before using a tool, you MUST enclose your thought process inside <thought>...</thought> tags. At the end of your work, provide a summary of what you did, highlighting important keywords.',
|
|
99
|
-
llm: llm,
|
|
100
|
-
tools: [
|
|
101
|
-
BashTool,
|
|
102
|
-
ReadFileTool,
|
|
103
|
-
WriteFileTool,
|
|
104
|
-
LsTool,
|
|
105
|
-
EditFileTool,
|
|
106
|
-
EditDiffTool,
|
|
107
|
-
FindFilesTool,
|
|
108
|
-
GrepSearchTool,
|
|
109
|
-
SubagentTool,
|
|
110
|
-
],
|
|
111
|
-
operations: localOperations,
|
|
112
|
-
modelOptions: { model: providerConfig?.model || 'gemini-3.5-flash' },
|
|
113
|
-
sessionRepository,
|
|
114
|
-
sessionId,
|
|
115
|
-
workspaceDir: process.cwd(),
|
|
116
|
-
hooks: {
|
|
117
|
-
beforeToolCall: async (toolCall) => {
|
|
118
|
-
// Future integration: Hook into the TUI to request user approval for destructive bash commands
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
const agent = harness.getAgent()
|
|
124
|
-
|
|
125
|
-
await agent.loadContext()
|
|
126
|
-
|
|
127
|
-
const config = await loadConfig()
|
|
128
|
-
const userEmail = config.decemberToken ? config.email : undefined
|
|
129
|
-
|
|
130
|
-
render(
|
|
131
|
-
React.createElement(App, {
|
|
132
|
-
agent,
|
|
133
|
-
isAuthenticated,
|
|
134
|
-
cliVersion: pkg.version,
|
|
135
|
-
userEmail,
|
|
136
|
-
sessionRepository,
|
|
137
|
-
onLogin: loginViaBrowser,
|
|
138
|
-
onLoginHeadless: (onCode: (code: string, uri: string) => void) =>
|
|
139
|
-
loginViaDeviceCode('http://localhost:4000', onCode), // Replace with prod URL later
|
|
140
|
-
}),
|
|
141
|
-
{ exitOnCtrlC: false }
|
|
142
|
-
)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
main().catch(originalConsoleError)
|
package/src/local-operations.ts
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { spawn, exec } from 'node:child_process'
|
|
2
|
-
import fs from 'node:fs/promises'
|
|
3
|
-
import path from 'node:path'
|
|
4
|
-
import { promisify } from 'node:util'
|
|
5
|
-
|
|
6
|
-
import { AgentOperations, taskManager } from '@december/agent'
|
|
7
|
-
import fg from 'fast-glob'
|
|
8
|
-
|
|
9
|
-
const execAsync = promisify(exec)
|
|
10
|
-
|
|
11
|
-
export const localOperations: AgentOperations = {
|
|
12
|
-
bash: {
|
|
13
|
-
exec: async (command, onData) => {
|
|
14
|
-
return new Promise((resolve) => {
|
|
15
|
-
let stdout = ''
|
|
16
|
-
let stderr = ''
|
|
17
|
-
|
|
18
|
-
const child = spawn(command, {
|
|
19
|
-
shell: true,
|
|
20
|
-
detached: true,
|
|
21
|
-
stdio: 'pipe',
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
const task = taskManager.addTask(command, child)
|
|
25
|
-
|
|
26
|
-
child.stdout?.on('data', (data) => {
|
|
27
|
-
const chunk = data.toString()
|
|
28
|
-
stdout += chunk
|
|
29
|
-
taskManager.appendOutput(task.id, chunk)
|
|
30
|
-
onData(chunk)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
child.stderr?.on('data', (data) => {
|
|
34
|
-
const chunk = data.toString()
|
|
35
|
-
stderr += chunk
|
|
36
|
-
taskManager.appendOutput(task.id, chunk)
|
|
37
|
-
onData(chunk)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
let isFinished = false
|
|
41
|
-
|
|
42
|
-
child.on('close', (code) => {
|
|
43
|
-
taskManager.markCompleted(task.id, code)
|
|
44
|
-
if (isFinished) return
|
|
45
|
-
isFinished = true
|
|
46
|
-
taskManager.removeTask(task.id)
|
|
47
|
-
resolve({ exitCode: code, output: stdout + '\n' + stderr })
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
child.on('error', (err) => {
|
|
51
|
-
taskManager.markCompleted(task.id, 1)
|
|
52
|
-
if (isFinished) return
|
|
53
|
-
isFinished = true
|
|
54
|
-
taskManager.removeTask(task.id)
|
|
55
|
-
resolve({ exitCode: 1, output: `Failed to start command: ${err.message}` })
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
// Auto-detach logic
|
|
59
|
-
setTimeout(() => {
|
|
60
|
-
if (isFinished) return
|
|
61
|
-
isFinished = true
|
|
62
|
-
|
|
63
|
-
child.unref()
|
|
64
|
-
child.stdout?.unref()
|
|
65
|
-
child.stderr?.unref()
|
|
66
|
-
|
|
67
|
-
resolve({ exitCode: null, output: stdout + '\n' + stderr })
|
|
68
|
-
}, 3000)
|
|
69
|
-
})
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
fs: {
|
|
73
|
-
readFile: async (filepath) => {
|
|
74
|
-
return fs.readFile(path.resolve(process.cwd(), filepath), 'utf8')
|
|
75
|
-
},
|
|
76
|
-
writeFile: async (filepath, content) => {
|
|
77
|
-
const absolutePath = path.resolve(process.cwd(), filepath)
|
|
78
|
-
await fs.mkdir(path.dirname(absolutePath), { recursive: true })
|
|
79
|
-
await fs.writeFile(absolutePath, content, 'utf8')
|
|
80
|
-
},
|
|
81
|
-
readdir: async (dirPath) => {
|
|
82
|
-
const absolutePath = path.resolve(process.cwd(), dirPath)
|
|
83
|
-
const entries = await fs.readdir(absolutePath, { withFileTypes: true })
|
|
84
|
-
return entries.map((entry) => {
|
|
85
|
-
const type = entry.isDirectory() ? 'DIR ' : 'FILE'
|
|
86
|
-
return `[${type}] ${entry.name}`
|
|
87
|
-
})
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
search: {
|
|
91
|
-
find: async (dirPath, query) => {
|
|
92
|
-
const files = await fg([query], {
|
|
93
|
-
cwd: dirPath,
|
|
94
|
-
ignore: ['**/node_modules/**', '**/.git/**'],
|
|
95
|
-
dot: true,
|
|
96
|
-
})
|
|
97
|
-
return files.join('\n')
|
|
98
|
-
},
|
|
99
|
-
grep: async (dirPath, query) => {
|
|
100
|
-
try {
|
|
101
|
-
const cmd = `git grep -nI "${query}" ${dirPath} || grep -rnI --exclude-dir=node_modules --exclude-dir=.git "${query}" ${dirPath}`
|
|
102
|
-
const { stdout } = await execAsync(cmd)
|
|
103
|
-
return stdout
|
|
104
|
-
} catch (error: any) {
|
|
105
|
-
if (error.code === 1) return '' // grep returns 1 when no matches
|
|
106
|
-
throw error
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
}
|