-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new helper to generate glob file imports
- Loading branch information
Showing
16 changed files
with
323 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# generated/cache files | ||
**/dist | ||
**/*.g.ts | ||
**/.eslintcache | ||
|
||
# other files/folders | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Expression } from 'typescript' | ||
|
||
import fs from 'fs' | ||
|
||
import { findGlob } from './findGlob' | ||
import { nodeToStr, constFactory, objectFactory, asyncArrowFnFactory, defaultExportFactory, dynamicImportFactory } from './tsFactory.ts' | ||
|
||
const fileImportsObj = (glob: string) => { | ||
const fileImports = new Map<string, Expression>() | ||
|
||
for (const file of findGlob(glob)) fileImports.set(file, dynamicImportFactory(`./${file}`)) | ||
|
||
return objectFactory(Object.fromEntries(fileImports)) | ||
} | ||
|
||
const globImportsObj = (globImports: string[]) => { | ||
const _globImports = new Map<string, Expression>() | ||
|
||
for (const glob of globImports) _globImports.set(glob, asyncArrowFnFactory(fileImportsObj(glob))) | ||
|
||
return objectFactory(Object.fromEntries(_globImports)) | ||
} | ||
|
||
type GenGlobImportsFile = { cwd: string; globImports: string[] } | ||
export const genGlobImportsFile = ({ cwd, globImports }: GenGlobImportsFile) => { | ||
const out: string[] = [] | ||
|
||
out.push(nodeToStr(constFactory('imports', globImportsObj(globImports)))) | ||
|
||
out.push(` | ||
type Imports = { | ||
[Glob in keyof typeof imports]: () => Promise<{ | ||
[File in keyof Awaited<ReturnType<(typeof imports)[Glob]>>]: { | ||
[Export in keyof Awaited<ReturnType<(typeof imports)[Glob]>>[File]]: Awaited<ReturnType<(typeof imports)[Glob]>>[File][Export] | ||
} | ||
}> | ||
} | ||
`) | ||
|
||
out.push(nodeToStr(defaultExportFactory('imports', 'Imports'))) | ||
|
||
fs.writeFileSync(`${cwd}/imports.g.ts`, out.join('\n\n')) | ||
} | ||
|
||
if (import.meta.vitest) { | ||
const { expect, it } = import.meta.vitest | ||
|
||
it('file imports obj', () => { | ||
const code = nodeToStr(fileImportsObj('**/globImports.ts')) | ||
const expectedCode = '{ "src/lib/globImports.ts": await import("./src/lib/globImports.ts") }' | ||
expect(code.replaceAll('\n', ' ').replace(/\s+/g, ' ')).toEqual(expectedCode) | ||
}) | ||
|
||
it('glob imports obj', () => { | ||
const code = nodeToStr(globImportsObj(['**/noFiles'])) | ||
const expectedCode = '{ "**/noFiles": async () => ({}) }' | ||
expect(code.replaceAll('\n', ' ').replace(/\s+/g, ' ')).toEqual(expectedCode) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import ts from 'typescript' | ||
|
||
export const constFactory = (name: string, expression: ts.Expression) => | ||
ts.factory.createVariableStatement( | ||
undefined, | ||
ts.factory.createVariableDeclarationList( | ||
[ts.factory.createVariableDeclaration(ts.factory.createIdentifier(name), undefined, undefined, expression)], | ||
ts.NodeFlags.Const | ||
) | ||
) | ||
|
||
export const dynamicImportFactory = (path: string) => | ||
ts.factory.createAwaitExpression( | ||
ts.factory.createCallExpression( | ||
// @ts-expect-error | ||
ts.factory.createToken(ts.SyntaxKind.ImportKeyword), | ||
undefined, | ||
[ts.factory.createStringLiteral(path)] | ||
) | ||
) | ||
|
||
export const objectFactory = (obj: Record<string, ts.Expression>) => | ||
ts.factory.createObjectLiteralExpression( | ||
Object.entries(obj).map(([key, value]) => ts.factory.createPropertyAssignment(ts.factory.createStringLiteral(key), value)), | ||
true | ||
) | ||
|
||
export const asyncArrowFnFactory = (expression: ts.Expression) => | ||
ts.factory.createArrowFunction( | ||
[ts.factory.createToken(ts.SyntaxKind.AsyncKeyword)], | ||
undefined, | ||
[], | ||
undefined, | ||
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), | ||
ts.factory.createParenthesizedExpression(expression) | ||
) | ||
|
||
export const defaultExportFactory = (varName: string, typeName?: string) => | ||
ts.factory.createExportAssignment( | ||
undefined, | ||
undefined, | ||
typeName ? | ||
ts.factory.createAsExpression( | ||
ts.factory.createIdentifier(varName), | ||
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(typeName), undefined) | ||
) | ||
: ts.factory.createIdentifier(varName) | ||
) | ||
|
||
// factory.createTypeReferenceNode( | ||
// factory.createIdentifier("Z"), | ||
// undefined | ||
// ) | ||
|
||
export const nodeToStr = (node: ts.Node) => | ||
// @ts-expect-error | ||
ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }).printNode(ts.EmitHint.Unspecified, node, undefined) | ||
|
||
if (import.meta.vitest) { | ||
const { expect, it } = import.meta.vitest | ||
|
||
it('const', () => { | ||
const code = nodeToStr(constFactory('x', ts.factory.createStringLiteral('value'))) | ||
expect(code).toEqual('const x = "value";') | ||
}) | ||
|
||
it('dynamic import', () => { | ||
const code = nodeToStr(dynamicImportFactory('./file.ts')) | ||
expect(code).toEqual('await import("./file.ts")') | ||
}) | ||
|
||
it('object', () => { | ||
const code = nodeToStr(objectFactory({ key: ts.factory.createStringLiteral('value') })) | ||
expect(code.replaceAll(' ', '').replaceAll('\n', '')).toEqual('{"key":"value"}') | ||
}) | ||
|
||
it('async arrow fn', () => { | ||
const code = nodeToStr(asyncArrowFnFactory(ts.factory.createStringLiteral('value'))) | ||
expect(code).toEqual('async () => ("value")') | ||
}) | ||
|
||
it('default export', () => { | ||
const code = nodeToStr(defaultExportFactory('var1')) | ||
expect(code).toEqual('export default var1;') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import type { Config } from 'prettier' | ||
|
||
export default { | ||
semi: false, | ||
useTabs: true, | ||
printWidth: 150, | ||
singleQuote: true, | ||
trailingComma: 'none', | ||
arrowParens: 'avoid', | ||
quoteProps: 'consistent', | ||
experimentalTernaries: true, | ||
overrides: [{ files: '*.json', options: { parser: 'jsonc' } }], | ||
plugins: ['prettier-plugin-svelte', 'prettier-plugin-astro', 'prettier-plugin-tailwindcss'] | ||
plugins: ['prettier-plugin-svelte', 'prettier-plugin-astro', 'prettier-plugin-tailwindcss'], | ||
printWidth: 150, | ||
quoteProps: 'consistent', | ||
semi: false, | ||
singleQuote: true, | ||
trailingComma: 'none', | ||
useTabs: true | ||
} satisfies Config |
Oops, something went wrong.