Skip to content

Commit ab0efb3

Browse files
committed
refactor: stricter typing for config parsing
1 parent 30499ea commit ab0efb3

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

src/commands/generate.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import { load } from 'js-yaml';
99
import { readFile } from 'node:fs/promises';
1010
import { basename, join } from 'node:path';
1111
import { setTimeout as sleep } from 'node:timers/promises';
12+
import type { Config } from 'src/lib/types';
1213

13-
async function createComponent(component: string, name: string, config: any, configLoc: string) {
14+
async function createComponent(component: string, name: string, config: Config, configLoc: string) {
1415
const { projectLanguage } = config;
1516

1617
if (!projectLanguage) {
@@ -34,10 +35,10 @@ async function createComponent(component: string, name: string, config: any, con
3435
}
3536

3637
async function fetchConfig() {
37-
const a = await Promise.race([findUp('.sapphirerc.json', { cwd: '.' }), sleep(5000)]);
38+
const configFileAsJson = await Promise.race([findUp('.sapphirerc.json', { cwd: '.' }), sleep(5000)]);
3839

39-
if (a) {
40-
return a;
40+
if (configFileAsJson) {
41+
return configFileAsJson;
4142
}
4243

4344
return Promise.race([findUp('.sapphirerc.yml', { cwd: '.' }), sleep(5000)]);
@@ -71,7 +72,7 @@ export default async (component: string, name: string) => {
7172
return fail("Can't find the Sapphire CLI config.");
7273
}
7374

74-
const config = configLoc.endsWith('json') ? JSON.parse(await readFile(configLoc, 'utf8')) : load(await readFile(configLoc, 'utf8'));
75+
const config: Config = configLoc.endsWith('json') ? JSON.parse(await readFile(configLoc, 'utf8')) : load(await readFile(configLoc, 'utf8'));
7576

7677
if (!config) {
7778
return fail("Can't parse the Sapphire CLI config.");

src/functions/CreateFileFromTemplate.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { templatesFolder } from '#constants';
22
import { fileExists } from '#functions/FileExists';
33
import { mkdir, readFile, writeFile } from 'node:fs/promises';
44
import { dirname, resolve } from 'node:path';
5+
import type { Config } from 'src/lib/types';
56

67
export async function CreateFileFromTemplate(
78
template: string,
89
target: string,
9-
config: any,
10+
config: Config | null,
1011
params?: Record<string, string>,
1112
custom = false,
1213
component = false
@@ -25,7 +26,7 @@ export async function CreateFileFromTemplate(
2526
output.templateContent ??= await readFile(location, 'utf8');
2627

2728
if (!output.templateContent) {
28-
throw new Error(`Couldn't read the template file. Are you sure it exists, the name is correct, and the content is valid?`);
29+
throw new Error("Couldn't read the template file. Are you sure it exists, the name is correct, and the content is valid?");
2930
}
3031

3132
if (params) {
@@ -38,14 +39,14 @@ export async function CreateFileFromTemplate(
3839
throw new Error('Invalid template.');
3940
}
4041

41-
const dir = component ? config.locations[output.config!.category] : null;
42-
const ta = component ? target.replace('%L%', dir) : target;
42+
const directoryForOutput = component ? config?.locations[output.config!.category] : null;
43+
const targetPath = component ? target.replace('%L%', directoryForOutput) : target;
4344

44-
if (await fileExists(ta)) {
45+
if (await fileExists(targetPath)) {
4546
throw new Error('Component already exists');
4647
}
4748

48-
await writeFileRecursive(ta, output.templateContent);
49+
await writeFileRecursive(targetPath, output.templateContent);
4950

5051
return true;
5152
}

src/lib/types.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Scheme for Sapphire CLI Config (@sapphire/cli)
3+
*/
4+
export interface Config {
5+
/**
6+
* Settings about custom component (piece) templates
7+
*/
8+
customFileTemplates: CustomFileTemplates;
9+
/**
10+
* Categories and their locations
11+
*/
12+
locations: Locations;
13+
/**
14+
* Project language (ts | js)
15+
*/
16+
projectLanguage: string;
17+
[property: string]: any;
18+
}
19+
20+
/**
21+
* Settings about custom component (piece) templates
22+
*/
23+
interface CustomFileTemplates {
24+
/**
25+
* Enable custom file templates
26+
*/
27+
enabled: boolean;
28+
/**
29+
* Location of your custom file templates
30+
*/
31+
location: string;
32+
[property: string]: any;
33+
}
34+
35+
/**
36+
* Categories and their locations
37+
*/
38+
interface Locations {
39+
arguments: string;
40+
base: string;
41+
commands: string;
42+
listeners: string;
43+
preconditions: string;
44+
routes?: string;
45+
[property: string]: any;
46+
}

0 commit comments

Comments
 (0)