Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
kristiandupont committed Feb 18, 2024
1 parent 73a7537 commit 565be43
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 51 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion packages/kanel-seeder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"kanel": "*"
},
"dependencies": {
"@kristiandupont/mdconf": "^1.0.8",
"@kristiandupont/mdconf": "^1.1.0",
"tagged-comment-parser": "^1.3.5",
"zod": "^3.22.4"
}
Expand Down
96 changes: 50 additions & 46 deletions packages/kanel-seeder/src/makeGenerateSeeds.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { promises as fs } from "fs";
import { Dirent, promises as fs } from "fs";
import { resolve } from "path";
import { InstantiatedConfig, Output, PreRenderHook, writeFile } from "kanel";
import parse from "@kristiandupont/mdconf";
import parseMdconf from "@kristiandupont/mdconf";
import seedInput, { SeedInput } from "./seedInput";
import preprocessData from "./preprocessData";
import { RawSeedData } from "./SeedData";

const makeGenerateSeeds =
({ srcPath, dstPath }: { srcPath: string; dstPath: string }): PreRenderHook =>
Expand All @@ -21,57 +21,61 @@ const makeGenerateSeeds =
for (const file of mdconfFiles) {
const srcFilePath = resolve(srcPath, file.name);
const contents = await fs.readFile(srcFilePath, "utf-8");
const parsed = parse(contents) as Record<string, unknown>;
const parsed = parseMdconf(contents, {
keyNormalizationFunction: (s: string) => s.toLowerCase(),
validator: seedInput,
});

const config = (parsed.config || parsed.Config || {}) as Record<
string,
string
>;
processSeedInput(parsed, instantiatedConfig, srcFilePath, dstPath, file);
}

const defaults = (parsed.defaults || parsed.Defaults || {}) as Record<
string,
string
>;
// Return unchanged as we wrote the file manually
return outputAcc;
};

if (!config.schema) {
if (Object.keys(instantiatedConfig.schemas).length === 1) {
config.schema = Object.keys(instantiatedConfig.schemas)[0];
} else {
throw new Error(
`No schema specified in ${srcFilePath} and no default schema found in config`,
);
}
}
export default makeGenerateSeeds;

const inputData = (parsed.data || parsed.Data) as RawSeedData | undefined;
if (!inputData) {
throw new Error(`No data found in ${srcFilePath}`);
}
function processSeedInput(
parsed: SeedInput,
instantiatedConfig: InstantiatedConfig,
srcFilePath: string,
dstPath: string,
file: Dirent,
) {
const { config, defaults, data: inputData } = parsed;

const data = preprocessData(
inputData,
instantiatedConfig.schemas[config.schema],
defaults,
if (!config.schema) {
if (Object.keys(instantiatedConfig.schemas).length === 1) {
config.schema = Object.keys(instantiatedConfig.schemas)[0];
} else {
throw new Error(
`No schema specified in ${srcFilePath} and no default schema found in config`,
);
}
}

const fullPath = resolve(dstPath, file.name.replace(".mdconf", ".js"));
if (!inputData) {
throw new Error(`No data found in ${srcFilePath}`);
}

const lines = [
"// @generated",
"// This file is automatically generated by Kanel. Do not modify manually.",
"",
'const { makeSeeder } = require("kanel-knex");',
"",
`const data = ${JSON.stringify(data, null, 2)};`,
"",
"exports.seed = makeSeeder({ data });",
];
const data = preprocessData(
inputData,
instantiatedConfig.schemas[config.schema],
defaults,
);

writeFile({ fullPath, lines, ensureFolder: true });
}
const fullPath = resolve(dstPath, file.name.replace(".mdconf", ".js"));

// Return unchanged as we wrote the file manually
return outputAcc;
};
const lines = [
"// @generated",
"// This file is automatically generated by Kanel. Do not modify manually.",
"",
'const { makeSeeder } = require("kanel-knex");',
"",
`const data = ${JSON.stringify(data, null, 2)};`,
"",
"exports.seed = makeSeeder({ data });",
];

export default makeGenerateSeeds;
writeFile({ fullPath, lines, ensureFolder: true });
}
9 changes: 6 additions & 3 deletions packages/kanel-seeder/src/seedInput.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { z } from "zod";

const seedInput = z.object({
config: z.object({
schema: z.string(),
}),
config: z
.object({
schema: z.string(),
})
.optional(),
defaults: z.record(z.string()).optional(),
data: z.array(
z.object({
name: z.string(),
Expand Down

0 comments on commit 565be43

Please sign in to comment.