Skip to content

Commit d0acb5c

Browse files
committed
switch to rescript tools for extracting embedded tags
1 parent d50bc95 commit d0acb5c

7 files changed

+130
-87
lines changed

package-lock.json

+32-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"dependencies": {
3939
"@pgtyped/parser": "^2.1.0",
4040
"@pgtyped/wire": "^2.2.0",
41+
"@rescript/tools": "^0.6.2",
4142
"camel-case": "^4.1.1",
4243
"chalk": "^4.0.0",
4344
"chokidar": "^3.3.1",

packages/cli/src/generator.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ async function generateTypedecsFromFile(
289289
const typeSource: TypeSource = (query) => getTypes(query, connection);
290290

291291
const { queries, events } =
292-
mode === 'res' ? parseRescriptFile(contents) : parseSQLFile(contents);
292+
mode === 'res'
293+
? parseRescriptFile(contents, fileName)
294+
: parseSQLFile(contents);
293295
if (events.length > 0) {
294296
prettyPrintEvents(contents, events);
295297
if (events.find((e) => 'critical' in e)) {

packages/cli/src/parseRescript.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { parseSQLFile } from '@pgtyped/parser';
22
import { SQLParseResult } from '@pgtyped/parser/lib/loader/sql';
3+
import cp from 'child_process';
4+
// @ts-ignore
5+
import { getBinaryPath } from '@rescript/tools/npm/getBinaryPath.js';
36

4-
export function parseCode(fileContent: string): SQLParseResult {
7+
export function parseCode(
8+
fileContent: string,
9+
fileName: string,
10+
): SQLParseResult {
511
if (!fileContent.includes('%sql')) {
612
return {
713
queries: [],
@@ -10,12 +16,22 @@ export function parseCode(fileContent: string): SQLParseResult {
1016
}
1117

1218
// Replace with more robust @rescript/tools CLI usage when that package ships linuxarm64 binary.
13-
const regex = /%sql(?:\.\w+)?\(`([^`]*)`\)/g;
14-
let match;
15-
const queries = [];
19+
const content: Array<{ contents: string }> = JSON.parse(
20+
cp
21+
.execFileSync(getBinaryPath(), [
22+
'extract-embedded',
23+
['sql', 'sql.one', 'sql.expectOne', 'sql.many', 'sql.execute'].join(
24+
',',
25+
),
26+
fileName,
27+
])
28+
.toString(),
29+
);
1630

17-
while ((match = regex.exec(fileContent)) !== null) {
18-
let query = match[1].trim();
31+
const queries: Array<string> = [];
32+
33+
content.forEach((v) => {
34+
let query = v.contents.trim();
1935
if (!query.endsWith(';')) {
2036
query += ';';
2137
}
@@ -40,7 +56,7 @@ export function parseCode(fileContent: string): SQLParseResult {
4056
}
4157

4258
queries.push(query);
43-
}
59+
});
4460

4561
const asSql = queries.join('\n\n');
4662

packages/example/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"pgtyped-rescript": "^2.4.0",
2929
"pgtyped-rescript-query": "^2.3.0",
3030
"rescript": "11.1.0",
31-
"rescript-embed-lang": "^0.5.0",
31+
"rescript-embed-lang": "^0.5.1",
3232
"typescript": "4.9.4"
3333
},
3434
"devDependencies": {

packages/example/src/books/BookService__sql.gen.tsx

+26-26
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export type arrayJSON_t = JSON_t[];
1515

1616
export type categoryArray = category[];
1717

18-
/** 'FindBookById' parameters type */
19-
export type findBookByIdParams = { readonly id?: number };
18+
/** 'BooksByAuthor' parameters type */
19+
export type booksByAuthorParams = { readonly authorName: string };
2020

21-
/** 'FindBookById' return type */
22-
export type findBookByIdResult = {
21+
/** 'BooksByAuthor' return type */
22+
export type booksByAuthorResult = {
2323
readonly author_id: (undefined | number);
2424
readonly big_int: (undefined | bigint);
2525
readonly categories: (undefined | categoryArray);
@@ -29,14 +29,14 @@ export type findBookByIdResult = {
2929
readonly rank: (undefined | number)
3030
};
3131

32-
/** 'FindBookById' query type */
33-
export type findBookByIdQuery = { readonly params: findBookByIdParams; readonly result: findBookByIdResult };
32+
/** 'BooksByAuthor' query type */
33+
export type booksByAuthorQuery = { readonly params: booksByAuthorParams; readonly result: booksByAuthorResult };
3434

35-
/** 'BooksByAuthor' parameters type */
36-
export type booksByAuthorParams = { readonly authorName: string };
35+
/** 'FindBookById' parameters type */
36+
export type findBookByIdParams = { readonly id?: number };
3737

38-
/** 'BooksByAuthor' return type */
39-
export type booksByAuthorResult = {
38+
/** 'FindBookById' return type */
39+
export type findBookByIdResult = {
4040
readonly author_id: (undefined | number);
4141
readonly big_int: (undefined | bigint);
4242
readonly categories: (undefined | categoryArray);
@@ -46,40 +46,40 @@ export type booksByAuthorResult = {
4646
readonly rank: (undefined | number)
4747
};
4848

49-
/** 'BooksByAuthor' query type */
50-
export type booksByAuthorQuery = { readonly params: booksByAuthorParams; readonly result: booksByAuthorResult };
49+
/** 'FindBookById' query type */
50+
export type findBookByIdQuery = { readonly params: findBookByIdParams; readonly result: findBookByIdResult };
5151

5252
/** Returns an array of all matched results. */
53-
export const FindBookById_many: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<findBookByIdResult[]> = BookService__sqlJS.FindBookById.many as any;
53+
export const BooksByAuthor_many: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<booksByAuthorResult[]> = BookService__sqlJS.BooksByAuthor.many as any;
5454

5555
/** Returns exactly 1 result. Returns `None` if more or less than exactly 1 result is returned. */
56-
export const FindBookById_one: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<(undefined | findBookByIdResult)> = BookService__sqlJS.FindBookById.one as any;
56+
export const BooksByAuthor_one: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<(undefined | booksByAuthorResult)> = BookService__sqlJS.BooksByAuthor.one as any;
5757

5858
/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
59-
export const FindBookById_expectOne: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams, errorMessage:(undefined | string)) => Promise<
60-
{ TAG: "Ok"; _0: findBookByIdResult }
61-
| { TAG: "Error"; _0: string }> = BookService__sqlJS.FindBookById.expectOne as any;
59+
export const BooksByAuthor_expectOne: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams, errorMessage:(undefined | string)) => Promise<
60+
{ TAG: "Ok"; _0: booksByAuthorResult }
61+
| { TAG: "Error"; _0: string }> = BookService__sqlJS.BooksByAuthor.expectOne as any;
6262

6363
/** Executes the query, but ignores whatever is returned by it. */
64-
export const FindBookById_execute: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<void> = BookService__sqlJS.FindBookById.execute as any;
64+
export const BooksByAuthor_execute: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<void> = BookService__sqlJS.BooksByAuthor.execute as any;
6565

66-
export const findBookById: (params:findBookByIdParams, client:PgTyped_Pg_Client_t) => Promise<findBookByIdResult[]> = BookService__sqlJS.findBookById as any;
66+
export const booksByAuthor: (params:booksByAuthorParams, client:PgTyped_Pg_Client_t) => Promise<booksByAuthorResult[]> = BookService__sqlJS.booksByAuthor as any;
6767

6868
/** Returns an array of all matched results. */
69-
export const BooksByAuthor_many: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<booksByAuthorResult[]> = BookService__sqlJS.BooksByAuthor.many as any;
69+
export const FindBookById_many: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<findBookByIdResult[]> = BookService__sqlJS.FindBookById.many as any;
7070

7171
/** Returns exactly 1 result. Returns `None` if more or less than exactly 1 result is returned. */
72-
export const BooksByAuthor_one: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<(undefined | booksByAuthorResult)> = BookService__sqlJS.BooksByAuthor.one as any;
72+
export const FindBookById_one: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<(undefined | findBookByIdResult)> = BookService__sqlJS.FindBookById.one as any;
7373

7474
/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
75-
export const BooksByAuthor_expectOne: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams, errorMessage:(undefined | string)) => Promise<
76-
{ TAG: "Ok"; _0: booksByAuthorResult }
77-
| { TAG: "Error"; _0: string }> = BookService__sqlJS.BooksByAuthor.expectOne as any;
75+
export const FindBookById_expectOne: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams, errorMessage:(undefined | string)) => Promise<
76+
{ TAG: "Ok"; _0: findBookByIdResult }
77+
| { TAG: "Error"; _0: string }> = BookService__sqlJS.FindBookById.expectOne as any;
7878

7979
/** Executes the query, but ignores whatever is returned by it. */
80-
export const BooksByAuthor_execute: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<void> = BookService__sqlJS.BooksByAuthor.execute as any;
80+
export const FindBookById_execute: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<void> = BookService__sqlJS.FindBookById.execute as any;
8181

82-
export const booksByAuthor: (params:booksByAuthorParams, client:PgTyped_Pg_Client_t) => Promise<booksByAuthorResult[]> = BookService__sqlJS.booksByAuthor as any;
82+
export const findBookById: (params:findBookByIdParams, client:PgTyped_Pg_Client_t) => Promise<findBookByIdResult[]> = BookService__sqlJS.findBookById as any;
8383

8484
export const FindBookById: {
8585
/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */

0 commit comments

Comments
 (0)