Skip to content

Commit 52fe9cf

Browse files
committed
Add work on Configurator and add messageLexer todos
1 parent 2262ff8 commit 52fe9cf

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

src/lib/configurator.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,28 @@ import path from "path";
88
import fs from "fs";
99
import consola from "consola";
1010

11-
import Constants from "./util/constants";
11+
import Constants, { type ConfigPathResolvable } from "./util/constants";
1212

13-
export interface MessageConfiguration {
14-
emoji: string;
13+
export type MessageConfiguration = {
1514
message: string;
15+
emoji?: string;
1616
insertAtIndex?: number;
1717
replaceAtIndex?: number;
1818
}
1919

20+
export type DefaultMessageOptions = {
21+
emoji?: string;
22+
fileName?: string;
23+
};
24+
2025
class Configurator {
2126
static getNearestConfigurationFilePath(
2227
currentDirectory: string,
2328
level: number = 0
2429
): string {
2530
const currentDir = fs.readdirSync(currentDirectory);
2631
const query = currentDir.find((item) =>
27-
Constants.ACCEPTED_CONFIG_FILENAMES.includes(item)
32+
([...Constants.ACCEPTED_CONFIG_FILENAMES] as string[]).includes(item)
2833
);
2934
if (query) return path.join(currentDirectory, query);
3035
else if (level <= Constants.CONFIG_FILE_MAX_DEPTH) {
@@ -41,12 +46,31 @@ class Configurator {
4146
}
4247
}
4348

49+
static getGitRootPath(currentDirectory: string, level: number = 0) {
50+
// TODO: Recursive find for the nearest directory with a .git path
51+
// Perhaps make this cacheable? Update at every change?
52+
// Maybe ENV Vars?
53+
currentDirectory; level;
54+
}
55+
4456
static addMessageConfiguration(
45-
filePath: string,
57+
configFilePath: ConfigPathResolvable,
4658
message: MessageConfiguration
4759
): boolean {
4860
if (message.insertAtIndex && message.replaceAtIndex)
4961
throw new Error("You cannot insert and replace at the same time.");
62+
63+
loadFile(configFilePath)
64+
65+
return true;
66+
}
67+
68+
static addDefaultMessageConfiguration(message: string, options: DefaultMessageOptions): boolean {
69+
const newMessage = {
70+
message,
71+
...options,
72+
}
73+
newMessage
5074
return true;
5175
}
5276
}

src/lib/messageLexer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// TODO: Implement a lexer to replace the template strings with actual values.
2+
// Needs a context object to say what files and thus which folders the commit cares about, then needs to make choices about what to put where.

src/lib/util/constants.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@
22
* Here, we will put important constant values for the project.
33
*/
44

5+
// Put not any leading dot modifiers (.labrc -> labrc)
6+
const ACCEPTED_CONFIG_FILENAMES = [
7+
"labrc.mjs", // default
8+
"labrc",
9+
"labrc.json", // if we want to support a JSON config file.
10+
] as const;
11+
512
interface CONSTANTS {
6-
ACCEPTED_CONFIG_FILENAMES: string[];
13+
ACCEPTED_CONFIG_FILENAMES: typeof ACCEPTED_CONFIG_FILENAMES;
714
CONFIG_FILE_MAX_DEPTH: number;
815
}
916

1017
const constants: CONSTANTS = {
11-
ACCEPTED_CONFIG_FILENAMES: [
12-
".labrc.mjs", // default
13-
".labrc",
14-
".labrc.json", // if we want to support a JSON config file.
15-
],
18+
ACCEPTED_CONFIG_FILENAMES,
1619
CONFIG_FILE_MAX_DEPTH: 5,
1720
};
1821

1922
export default constants;
23+
24+
export type FileNameResolvable = (typeof ACCEPTED_CONFIG_FILENAMES)[number];
25+
export type DotOption = "" | ".";
26+
export type ConfigPathResolvable = `${string}${DotOption}${FileNameResolvable}`;
27+

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
2626

2727
/* Modules */
28-
"module": "nodenext" /* Specify what module code is generated. */,
28+
"module": "commonjs" /* Specify what module code is generated. */,
2929
"rootDir": "./src" /* Specify the root folder within your source files. */,
3030
// "moduleResolution": "nodenext", /* Specify how TypeScript looks up a file from a given module specifier. */
3131
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */

0 commit comments

Comments
 (0)