Skip to content

Commit

Permalink
improves config default code
Browse files Browse the repository at this point in the history
  • Loading branch information
ieedan committed Oct 21, 2024
1 parent b56aa9d commit 45683d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
"type": "git",
"url": "git+https://github.com/ieedan/ts-blocks"
},
"keywords": [
"changelog",
"date"
],
"keywords": ["changelog", "date"],
"author": "Aidan Bleser",
"license": "MIT",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const _add = async (blockNames: string[], options: Options) => {
index = project.createSourceFile(indexPath);
}

if (config.imports === undefined || config.imports === 'node') {
if (config.imports === 'node') {
index.addExportDeclaration({
moduleSpecifier: `./${blockName}`,
isTypeOnly: false,
Expand Down
40 changes: 23 additions & 17 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import fs from 'node:fs';
import color from 'chalk';
import { program } from 'commander';
import {
type InferInput,
boolean,
literal,
minLength,
object,
optional,
parse,
pipe,
string,
union,
} from 'valibot';
import { boolean, literal, minLength, object, optional, parse, pipe, string, union } from 'valibot';

const CONFIG_NAME = 'blocks.json';

Expand All @@ -25,20 +14,37 @@ const schema = object({
imports: optional(union([literal('deno'), literal('node')])),
});

type Config = InferInput<typeof schema>;
type Config = {
$schema: string;
addByCategory: boolean;
includeIndexFile: boolean;
includeTests: boolean;
path: string;
imports: 'deno' | 'node';
};

const getConfig = () => {
const getConfig = (): Config => {
if (!fs.existsSync(CONFIG_NAME)) {
program.error(
color.red(
`Could not find your configuration file! Please run ${color.bold(`'ts-blocks init'`)}.`
`Could not find your configuration file! Please run ${color.bold(
`'ts-blocks init'`
)}.`
)
);
}

return parse(schema, JSON.parse(fs.readFileSync(CONFIG_NAME).toString()), {
const config = parse(schema, JSON.parse(fs.readFileSync(CONFIG_NAME).toString()), {
message: color.red('Invalid config file!'),
});

// set defaults here

if (config.imports === undefined) {
config.imports = 'node';
}

return config as Config;
};

export { getConfig, type Config, schema, CONFIG_NAME };
export { type Config, CONFIG_NAME, getConfig, schema };

0 comments on commit 45683d3

Please sign in to comment.