Skip to content

Commit 0d8a718

Browse files
authored
Tsconfig improvements (#1391)
* various tsconfig imporments * ✨ Add JSON module support and remove debug console log Added JSON import capability with type inference for .mjs/.mts files. Removed unused debug console log statement in bundleprompts.js. * move file * pre-filter by diff * tiny change * m,ore logging * ✨ improve: enhance diff function documentation Updated and clarified JSDoc comments for better code readability.
1 parent d0732fd commit 0d8a718

14 files changed

Lines changed: 82 additions & 24 deletions

File tree

docs/genaisrc/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"allowJs": true,
1212
"skipLibCheck": true,
1313
"noEmit": true,
14-
"allowImportingTsExtensions": true
14+
"allowImportingTsExtensions": true,
15+
"verbatimModuleSyntax": true,
16+
"resolveJsonModule": true
1517
},
1618
"include": [
1719
"*.mjs",

docs/src/content/docs/reference/scripts/imports.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ export function summarize(_, files) {
9292
}
9393
```
9494

95+
## JSON Modules
96+
97+
You can import JSON files using the `import` statement and get automatic type inference.
98+
99+
```js title="data.json"
100+
{
101+
"name": "GenAIScript"
102+
}
103+
```
104+
105+
Use the `with { type: "json" }` syntax to import JSON files in `.mjs` or `.mts` files.
106+
The file path is relative to the genaiscript source file.
107+
108+
```js title="script.genai.mts"
109+
import data from "./data.json" with { type: "json" }
110+
111+
console.log(data.name) // GenAIScript
112+
```
113+
95114
## Default function export
96115

97116
If you set a function as the default export, GenAIScript will call it.

eval/extrism/genaisrc/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"allowJs": true,
1212
"skipLibCheck": true,
1313
"noEmit": true,
14-
"allowImportingTsExtensions": true
14+
"allowImportingTsExtensions": true,
15+
"verbatimModuleSyntax": true,
16+
"resolveJsonModule": true
1517
},
1618
"include": [
1719
"*.mjs",
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ script({
3636
},
3737
},
3838
})
39-
const { files, output, dbg, vars } = env
39+
const { output, dbg, vars } = env
40+
let { files } = env
4041
const { applyEdits, diff, pretty, missing, update } = vars
4142

43+
dbg({ applyEdits, diff, pretty, missing, update })
44+
4245
if (!missing && !update) cancel(`not generating or updating docs, exiting...`)
4346

4447
if (!applyEdits)
@@ -48,6 +51,15 @@ if (!applyEdits)
4851

4952
// filter by diff
5053
const gitDiff = diff ? await git.diff({ base: "main" }) : undefined
54+
console.debug(gitDiff)
55+
const diffFiles = gitDiff ? DIFF.parse(gitDiff) : undefined
56+
if (diffFiles?.length) {
57+
dbg(`diff files: ${diffFiles.map((f) => f.to)}`)
58+
files = files.filter(({ filename }) =>
59+
diffFiles.some((f) => path.resolve(f.to) === path.resolve(filename))
60+
)
61+
dbg(`diff filtered files: ${files.length}`)
62+
}
5163
const sg = await host.astGrep()
5264
const stats = []
5365
for (const file of files) {
@@ -208,7 +220,7 @@ async function updateDocs(file: WorkspaceFile, fileStats: any) {
208220
},
209221
{ diff: gitDiff }
210222
)
211-
223+
dbg(`found ${matches.length} docs to update`)
212224
const edits = sg.changeset()
213225
// for each match, generate a docstring for functions not documented
214226
for (const match of matches) {

genaisrc/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"allowJs": true,
1212
"skipLibCheck": true,
1313
"noEmit": true,
14-
"allowImportingTsExtensions": true
14+
"allowImportingTsExtensions": true,
15+
"verbatimModuleSyntax": true,
16+
"resolveJsonModule": true
1517
},
1618
"include": [
1719
"*.mjs",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"genai": "node packages/cli/built/genaiscript.cjs run",
9090
"genai:convert": "node packages/cli/built/genaiscript.cjs convert",
9191
"genai:debug": "yarn compile-debug && node packages/cli/built/genaiscript.cjs run",
92-
"genai:docs": "yarn genai ast-docs \"packages/cli/src/**.ts\" \"packages/core/src/**.ts\" --vars diff=true --vars applyEdits=true",
92+
"genai:docs": "yarn genai docs \"packages/cli/src/**.ts\" \"packages/core/src/**.ts\" --vars diff=true --vars applyEdits=true",
9393
"upgrade:deps": "zx scripts/upgrade-deps.mjs",
9494
"cli": "node packages/cli/built/genaiscript.cjs",
9595
"ffmpeg:install": "sudo apt-get update && sudo apt-get install ffmpeg -y",

packages/auto/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"allowJs": true,
1212
"skipLibCheck": true,
1313
"noEmit": true,
14-
"allowImportingTsExtensions": true
14+
"allowImportingTsExtensions": true,
15+
"verbatimModuleSyntax": true,
16+
"resolveJsonModule": true
1517
},
1618
"include": [
1719
"*.mjs",

packages/core/bundleprompts.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ async function main() {
3838
)
3939
.toString("utf8")
4040
.matchAll(/debug\("genaiscript:(?<category>[^"]+)"\)/g)
41-
).sort().map((m) => m.groups.category),
41+
)
42+
.sort()
43+
.map((m) => m.groups.category),
4244
])
43-
console.log({ logCategories })
4445
writeFileSync(
4546
"./src/dbg.ts",
4647
dedent`// auto-generated: do not edit
@@ -91,6 +92,8 @@ async function main() {
9192
skipLibCheck: true,
9293
noEmit: true,
9394
allowImportingTsExtensions: true,
95+
verbatimModuleSyntax: true,
96+
resolveJsonModule: true,
9497
},
9598
include: ["*.mjs", "*.mts", "src/*.mts", "./genaiscript.d.ts"],
9699
},

packages/core/src/astgrep.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,13 @@ export async function astGrepFindFiles(
167167
// apply diff
168168
if (diffFiles?.length) {
169169
matches = matches.filter((m) => {
170-
const chunk = diffFindChunk(
171-
m.getRoot().filename(),
172-
[m.range().start.line, m.range().end.line],
173-
diffFiles
174-
)
170+
const range = [m.range().start.line, m.range().end.line]
171+
const { chunk } =
172+
diffFindChunk(m.getRoot().filename(), range, diffFiles) || {}
173+
if (chunk)
174+
dbg(
175+
`diff overlap at (${range[0]},${range[1]}) x (${chunk.newStart},${chunk.newStart + chunk.newLines})`
176+
)
175177
return chunk
176178
})
177179
dbg(`matches filtered by diff: ${matches.length}`)

packages/core/src/diff.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ export function diffResolve(
3333

3434
/**
3535
* Attempts to parse a diff string into a structured format.
36-
* If parsing fails, logs the error and returns an empty array.
36+
* If parsing fails, logs the error message and returns an empty array.
3737
*
3838
* @param diff - The diff string to parse.
39-
* @returns An array of parsed file objects if successful, or an empty array if parsing fails.
39+
* @returns An array of parsed file objects if successful, or an empty array if parsing fails. Logs an error message if parsing fails.
4040
*/
4141
export function tryDiffParse(diff: string) {
4242
try {
@@ -49,10 +49,13 @@ export function tryDiffParse(diff: string) {
4949

5050
/**
5151
* Creates a unified diff between two workspace files.
52-
* @param left - The original workspace file or its content. If a string, it is wrapped in a WorkspaceFile object with a default filename.
53-
* @param right - The modified workspace file or its content. If a string, it is wrapped in a WorkspaceFile object with a default filename.
54-
* @param options - Optional parameters, such as the number of context lines, case sensitivity, and whitespace handling.
55-
* @returns The diff as a string, with redundant headers removed.
52+
* If the input is a string, it is wrapped in a WorkspaceFile object with a default filename.
53+
* If the input is an object, it should contain a filename and content.
54+
*
55+
* @param left - The original workspace file or its content. If a string, it is wrapped in a WorkspaceFile object with the filename "left".
56+
* @param right - The modified workspace file or its content. If a string, it is wrapped in a WorkspaceFile object with the filename "right".
57+
* @param options - Optional parameters, such as the number of context lines, case sensitivity, and whitespace handling. Defaults to ignoring case and whitespace. Additional options can be provided.
58+
* @returns The diff as a string, with redundant headers removed. The diff is generated using createTwoFilesPatch.
5659
*/
5760
export function diffCreatePatch(
5861
left: string | WorkspaceFile,
@@ -94,7 +97,7 @@ export function diffFindChunk(
9497
line: ElementOrArray<number>,
9598
diff: ElementOrArray<DiffFile>
9699
): { file?: DiffFile; chunk?: DiffChunk } | undefined {
97-
// line is zero-based
100+
// line is zero-based!
98101
const fn = file ? resolve(file) : undefined
99102
const df = arrayify(diff).find(
100103
(f) => (!file && !f.to) || resolve(f.to) === fn

0 commit comments

Comments
 (0)