Skip to content

Commit

Permalink
format: ran the formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
fhur committed Apr 17, 2024
1 parent 970803e commit c61c76b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
4 changes: 2 additions & 2 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"cache": true
},
"compile-executable-examples": {
"dependsOn": ["test:ci","format"],
"dependsOn": ["test:ci", "format"],
"cache": true
},
"build:typedoc": {
"dependsOn": ["^build","^compile-executable-examples"],
"dependsOn": ["^build", "^compile-executable-examples"],
"cache": true,
"outputs": ["{projectRoot}/static/reference"]
},
Expand Down
15 changes: 7 additions & 8 deletions packages/docs/docs/350-examples.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# Examples

## Find all users with ids in the list
## Find all users with ids in the list

Finds all records in the `users` table where the `id` is in the list of ids.


```ts
const q = from('users')
.columns('id', 'name')
.where({ id: { in: ids } })
.many();

const result: UseQueryResult<
Array<{ id: string; name: string }>
> = useSynthql<DB, 'users', typeof q>(q);


```
const result: UseQueryResult<Array<{ id: string; name: string }>> = useSynthql<
DB,
'users',
typeof q
>(q);
```
2 changes: 1 addition & 1 deletion packages/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"outDir": "./build/types",
"emitDeclarationOnly": true,
"jsx": "preserve"
"jsx": "preserve",
},
"include": ["src"],
"exclude": ["./build/*", "./node_modules/*"],
Expand Down
59 changes: 37 additions & 22 deletions scripts/compile-executable-examples.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path');

// Command line arguments
const [,, testFilePath] = process.argv;
const [, , testFilePath] = process.argv;

// Check if the test file path is provided
if (!testFilePath) {
Expand All @@ -12,8 +12,8 @@ if (!testFilePath) {

// Function to parse the test file and extract examples
/**
*
* @param {string} filePath
*
* @param {string} filePath
* @returns {Array<{ title: string, description: string, code: string }>
*/
function parseTestFile(filePath) {
Expand All @@ -25,14 +25,19 @@ function parseTestFile(filePath) {

for (const line of lines) {
if (line.includes('@@start-example@@')) {
currentExample = { title: line.split('@@start-example@@')[1].trim(), description: '', code: '' };
currentExample = {
title: line.split('@@start-example@@')[1].trim(),
description: '',
code: '',
};
collectingDescription = true;
} else if (line.includes('@@end-example@@')) {
examples.push(currentExample);
currentExample = null;
collectingDescription = false;
} else if (collectingDescription && line.includes('@@desc@@')) {
currentExample.description += line.split('@@desc@@')[1].trim() + '\n';
currentExample.description +=
line.split('@@desc@@')[1].trim() + '\n';
} else if (currentExample) {
currentExample.code += line + '\n';
}
Expand All @@ -47,52 +52,62 @@ function parseTestFile(filePath) {

/**
* Takes a string of code and fixes the indentation by removing the common leading whitespace
*
* @param {string} code
*
* @param {string} code
* @returns {string}
*/
function fixIdentation(code) {
const lines = code.split('\n');
const leadingWhitespace = lines
.filter(line => line.trim().length > 0)
.map(line => line.match(/^\s*/)[0])
.filter((line) => line.trim().length > 0)
.map((line) => line.match(/^\s*/)[0])
.reduce((acc, whitespace) => {
if (acc === null) {
return whitespace;
}
let i = 0;
while (i < acc.length && i < whitespace.length && acc[i] === whitespace[i]) {
while (
i < acc.length &&
i < whitespace.length &&
acc[i] === whitespace[i]
) {
i++;
}
return acc.slice(0, i);
}, null);
return lines.map(line => line.replace(leadingWhitespace, '')).join('\n');
return lines.map((line) => line.replace(leadingWhitespace, '')).join('\n');
}

/**
*
*
* @param {Array<{ title: string, description: string, code: string }>} examples
* @returns {string}
*/
function generateMarkdown(examples) {
return examples.map(example => {
return [
`## ${example.title}`,
`${example.description}`,
"```ts\n"+example.code+"\n```",
]
.filter(x => x.length>0)
return examples
.map((example) => {
return [
`## ${example.title}`,
`${example.description}`,
'```ts\n' + example.code + '\n```',
]
.filter((x) => x.length > 0)
.join('\n\n');
})
.join('\n\n');
}).join('\n\n');
}

// Main function to process the test file and generate markdown
function main() {
const examples = parseTestFile(testFilePath);
const markdown = `# Examples\n\n ${generateMarkdown(examples)}`;
const outputFilePath = path.join(__dirname,'../packages/docs/docs', '350-examples.md');
const outputFilePath = path.join(
__dirname,
'../packages/docs/docs',
'350-examples.md',
);
fs.writeFileSync(outputFilePath, markdown);
console.log(`Markdown generated at ${outputFilePath}`);
}

main();
main();

0 comments on commit c61c76b

Please sign in to comment.