Skip to content

Commit

Permalink
refactor: adds json config file support
Browse files Browse the repository at this point in the history
refactor: adds json config file support
  • Loading branch information
eglavin committed Apr 19, 2024
2 parents c996839 + a96e4a5 commit a09b74e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#!/usr/bin/env bun
#!/usr/bin/env node
// @ts-check

import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { readFileSync, writeFileSync } from "node:fs";
import { zodToJsonSchema } from "zod-to-json-schema";
import { ForkConfigSchema } from "../src/config/schema.js";
import { ForkConfigSchema } from "../dist/index.js";

const projectRoot = join(dirname(fileURLToPath(import.meta.url)), "..");
const { name, version } = JSON.parse(readFileSync(join(projectRoot, "package.json"), "utf-8"));
const outputLocation = join(projectRoot, "schema", `latest.json`);

const packageJson = JSON.parse(readFileSync(join(projectRoot, "package.json"), "utf-8"));
const outputLocation = join(projectRoot, "schema", `${packageJson.version}.json`);

console.log(`Generating JSON schema for ${packageJson.name} ${packageJson.version}`);

console.log(`Generating JSON schema for ${name} ${version}`);
const jsonSchema = zodToJsonSchema(ForkConfigSchema, "type");

writeFileSync(outputLocation, JSON.stringify(jsonSchema, null, 2));
67 changes: 42 additions & 25 deletions src/config/user-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFileSync } from "node:fs";
import { parse, resolve } from "node:path";
import JoyCon from "joycon";
import { bundleRequire } from "bundle-require";
Expand All @@ -13,45 +14,61 @@ export async function getUserConfig(): Promise<ForkConfig> {
const cwd = cliArguments.flags.path ? resolve(cliArguments.flags.path) : process.cwd();
const joycon = new JoyCon();
const configFilePath = await joycon.resolve({
files: ["fork.config.ts", "fork.config.js", "fork.config.cjs", "fork.config.mjs"],
files: [
"fork.config.ts",
"fork.config.js",
"fork.config.cjs",
"fork.config.mjs",
"fork.config.json",
],
cwd,
stopDir: parse(cwd).root,
});

if (!configFilePath) {
return {
...DEFAULT_CONFIG,
...cliArguments.flags,
path: cwd,
files: getFiles([], cliArguments.flags?.files),
changelogPresetConfig: getChangelogPresetConfig(),
} as ForkConfig;
}

const foundConfig = await bundleRequire({ filepath: configFilePath });
const parsedConfig = ForkConfigSchema.partial().safeParse(
foundConfig.mod.default || foundConfig.mod,
);

if (!parsedConfig.success) {
throw parsedConfig.error;
}
const configFile = await loadConfigFile(configFilePath);

const usersConfig = {
const mergedConfig = {
...DEFAULT_CONFIG,
...parsedConfig.data,
...configFile,
...cliArguments.flags,
} as ForkConfig;

return {
...usersConfig,
...mergedConfig,

path: cwd,
files: getFiles(parsedConfig.data?.files, cliArguments.flags?.files),
changelogPresetConfig: getChangelogPresetConfig(usersConfig?.changelogPresetConfig),
files: getFilesList(configFile?.files, cliArguments.flags?.files),
changelogPresetConfig: getChangelogPresetConfig(mergedConfig?.changelogPresetConfig),
};
}

function getFiles(configFiles: string[] | undefined, cliFiles: string[] | undefined): string[] {
async function loadConfigFile(configFilePath: string | null) {
if (!configFilePath) {
return {};
}

// Handle json config file.
if (configFilePath.endsWith("json")) {
const fileContent = JSON.parse(readFileSync(configFilePath).toString());

const parsed = ForkConfigSchema.partial().safeParse(fileContent);
if (!parsed.success) {
throw parsed.error;
}
return parsed.data;
}

// Otherwise expect config file to use js or ts.
const fileContent = await bundleRequire({ filepath: configFilePath });

const parsed = ForkConfigSchema.partial().safeParse(fileContent.mod.default || fileContent.mod);
if (!parsed.success) {
throw parsed.error;
}
return parsed.data;
}

function getFilesList(configFiles: string[] | undefined, cliFiles: string[] | undefined): string[] {
const listOfFiles = new Set<string>();

// Add files from the users config file
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"include": [
"@types/*",
"src/**/*",
"tests/**/*",
"scripts/**/*",
"tests/**/*"
]
}

0 comments on commit a09b74e

Please sign in to comment.