Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
"format": "prettier --write .",
"test": "vitest"
},
"dependencies": {},
"dependencies": {
"glob": "^13.0.3",
"yaml": "^2.8.2",
"zod": "^4.3.6"
},
"devDependencies": {
"@changesets/changelog-github": "^0.5.2",
"@changesets/cli": "^2.29.8",
Expand Down
3 changes: 0 additions & 3 deletions src/add.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/example/CurrencySchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import z from 'zod';

export const CurrencySchema = z.strictObject({
id: z.string(),
name: z.string(),
});
7 changes: 7 additions & 0 deletions src/example/ItemSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import z from 'zod';

export const ItemSchema = z.strictObject({
id: z.string(),
name: z.string(),
description: z.string(),
});
25 changes: 25 additions & 0 deletions src/generate/string-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const generateStringUnionType = (typeName: string, ids: string[]): string => {
if (ids.length === 0) {
return `export type ${typeName} = never;\n`;
}
return (
`export type ${typeName} =\n` +
ids
.map((id) => {
return ` | '${id}'`;
})
.join('\n') +
';'
);
};

export const generateEnumType = (typeName: string, ids: string[]): string => {
const idList = ids.map((id) => `'${id}'`).join(', ');
const idListVariable = typeName + 's';
let output = '';
output += `const ${idListVariable} = [` + idList + '] as const;\n';
output += `export const ${typeName}Schema = z.enum(${idListVariable});\n`;
output += `export type ${typeName} = z.infer<typeof ${typeName}Schema>;\n`;
output += '\n';
return output;
};
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { add } from '@/add.ts';
export { subtract } from '@/subtract.ts';
export { Louter } from '@/louter/Louter.ts';
79 changes: 79 additions & 0 deletions src/louter/Louter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { glob } from 'glob';
import { readFileSync } from 'node:fs';
import { parse } from 'yaml';
import { generateEnumType } from '@/generate/string-type.ts';
import { LouterKind } from '@/louter/LouterKind.ts';
import { LouterIndexationMap, LouterIndexationResult } from '@/louter/LouterIndexation.ts';
import { LouterErrorType } from '@/louter/LouterError.ts';

export interface LouterConfig {
root: string;
content: LouterKind[];
debug: boolean;
idKey: string;
}

export class Louter {
private readonly _config: LouterConfig;

// private _content: Record<string, Record<string, object>> = {};

constructor(config: LouterConfig) {
this._config = config;
}

public index(): LouterIndexationResult {
const indexationMap: LouterIndexationMap = {};
const errors = [];

const filePaths = glob.sync(`${this._config.root}/**/*.{yml,yaml}`);
this.debug(`Reading ${filePaths.length} files from`, this._config.root);

console.log(filePaths);
filePaths.forEach((filePath) => {
const kind = filePath.replace('.yaml', '').replace('.yml', '').split('.').pop() as string;
const fileName = filePath.replace(this._config.root, '');

indexationMap[kind] ??= {};

try {
const parsedData = parse(readFileSync(filePath, 'utf8'));
const contentId = parsedData[this._config.idKey];
indexationMap[kind][contentId] = {
id: contentId,
kind: kind,
source: fileName,
};
} catch (e) {
errors.push({
file: filePath,
type: LouterErrorType.InvalidYaml,
message: `Could not parse file '${fileName}'. Is it valid yaml?: ${e}`,
});
}
});

return {
content: indexationMap,
enumCode: this.buildKindEnum(indexationMap),
};
}

private buildKindEnum(result: LouterIndexationMap): string {
let output = "import { z } from 'zod';\n\n";

this._config.content.forEach((kind) => {
output += generateEnumType(
kind.enumSymbol,
Object.values(result[kind.id] ?? []).map((result) => result.id),
);
});
return output;
}

private debug(...args: unknown[]) {
if (this._config.debug) {
console.log(...args);
}
}
}
13 changes: 13 additions & 0 deletions src/louter/LouterError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export enum LouterErrorType {
UnrecognizedContentType = 'UnrecognizedContentType',
InvalidYaml = 'InvalidYaml',
MissingGlobalIdKey = 'MissingGlobalIdKey',
ZodValidationFailed = 'ZodValidationFailed',
DuplicateId = 'DuplicateId',
}

export interface LouterError {
file: string;
type: LouterErrorType;
message: string;
}
12 changes: 12 additions & 0 deletions src/louter/LouterIndexation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface LouterIndexation {
id: string;
kind: string;
source: string;
}

export type LouterIndexationMap = Record<string, Record<string, LouterIndexation>>;

export interface LouterIndexationResult {
enumCode: string;
content: LouterIndexationMap;
}
7 changes: 7 additions & 0 deletions src/louter/LouterKind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { ZodType } from 'zod';

export interface LouterKind {
id: string;
enumSymbol: string;
schema: ZodType;
}
3 changes: 0 additions & 3 deletions src/subtract.ts

This file was deleted.

6 changes: 0 additions & 6 deletions tests/add.test.ts

This file was deleted.

Loading