Skip to content

Commit

Permalink
deprecate: mode (#325)
Browse files Browse the repository at this point in the history
* test: add tests for context

* deprecate: mode

* revert docs for deprecated option
  • Loading branch information
jacoobes authored Aug 19, 2023
1 parent e59e0b9 commit c9f2d75
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 53 deletions.
71 changes: 30 additions & 41 deletions src/core/module-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export async function importModule<T>(absPath: string) {
.wrap(() => module.getInstance())
.unwrapOr(module) as T;
}

export async function defaultModuleLoader<T extends Module>(absPath: string): ModuleResult<T> {
let module = await importModule<T>(absPath);
assert(module, `Found an undefined module: ${absPath}`);
Expand All @@ -53,7 +54,7 @@ export function buildModuleStream<T extends Module>(
return from(input).pipe(mergeMap(defaultModuleLoader<T>));
}

export const getFullPathTree = (dir: string, mode: boolean) => readPaths(resolve(dir), mode);
export const getFullPathTree = (dir: string) => readPaths(resolve(dir));

export const filename = (path: string) => fmtFileName(basename(path));

Expand All @@ -70,22 +71,18 @@ async function deriveFileInfo(dir: string, file: string) {
base: basename(file),
};
}
async function* readPaths(dir: string, shouldDebug: boolean): AsyncGenerator<string> {
async function* readPaths(dir: string): AsyncGenerator<string> {
try {
const files = await readdir(dir);
for (const file of files) {
const { fullPath, fileStats, base } = await deriveFileInfo(dir, file);
if (fileStats.isDirectory()) {
//Todo: refactor so that i dont repeat myself for files (line 71)
if (isSkippable(base)) {
if (shouldDebug) console.info(`ignored directory: ${fullPath}`);
} else {
yield* readPaths(fullPath, shouldDebug);
if (!isSkippable(base)) {
yield* readPaths(fullPath);
}
} else {
if (isSkippable(base)) {
if (shouldDebug) console.info(`ignored: ${fullPath}`);
} else {
if (!isSkippable(base)) {
yield 'file:///' + fullPath;
}
}
Expand All @@ -98,38 +95,30 @@ async function* readPaths(dir: string, shouldDebug: boolean): AsyncGenerator<str
const requir = createRequire(import.meta.url);

export function loadConfig(wrapper: Wrapper | 'file'): Wrapper {
if (wrapper === 'file') {
console.log('Experimental loading of sern.config.json');
const config = requir(resolve('sern.config.json')) as {
language: string;
defaultPrefix?: string;
mode?: 'PROD' | 'DEV';
paths: {
base: string;
commands: string;
events?: string;
};
};
const makePath = (dir: keyof typeof config.paths) =>
config.language === 'typescript'
? join('dist', config.paths[dir]!)
: join(config.paths[dir]!);

console.log('Loading config: ', config);
const commandsPath = makePath('commands');
if (wrapper !== 'file') {
return wrapper;
}
console.log('Experimental loading of sern.config.json');
const config = requir(resolve('sern.config.json'));

console.log('Commands path is set to', commandsPath);
let eventsPath: string | undefined;
if (config.paths.events) {
eventsPath = makePath('events');
console.log('Events path is set to', eventsPath);
}
return {
defaultPrefix: config.defaultPrefix,
commands: commandsPath,
events: eventsPath,
mode: config.mode,
};
const makePath = (dir: PropertyKey) =>
config.language === 'typescript'
? join('dist', config.paths[dir]!)
: join(config.paths[dir]!);

console.log('Loading config: ', config);
const commandsPath = makePath('commands');

console.log('Commands path is set to', commandsPath);
let eventsPath: string | undefined;
if (config.paths.events) {
eventsPath = makePath('events');
console.log('Events path is set to', eventsPath);
}
return wrapper;
return {
defaultPrefix: config.defaultPrefix,
commands: commandsPath,
events: eventsPath,
};

}
13 changes: 2 additions & 11 deletions src/sern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ export function init(maybeWrapper: Wrapper | 'file') {
const dependencies = useDependencies();
const logger = dependencies[2],
errorHandler = dependencies[1];
const mode = isDevMode(wrapper.mode ?? process.env.MODE);

if (wrapper.events !== undefined) {
eventsHandler(dependencies, Files.getFullPathTree(wrapper.events, mode));
eventsHandler(dependencies, Files.getFullPathTree(wrapper.events));
}
//Ready event: load all modules and when finished, time should be taken and logged
startReadyEvent(dependencies, Files.getFullPathTree(wrapper.commands, mode)).add(() => {
startReadyEvent(dependencies, Files.getFullPathTree(wrapper.commands)).add(() => {
const time = ((performance.now() - startTime) / 1000).toFixed(2);
dependencies[0].emit('modulesLoaded');
logger?.info({
Expand All @@ -47,14 +46,6 @@ export function init(maybeWrapper: Wrapper | 'file') {
merge(messages$, interactions$).pipe(handleCrash(errorHandler, logger)).subscribe();
}

function isDevMode(mode: string | undefined) {
console.info(`Detected mode: "${mode}"`);
if (mode === undefined) {
console.info('No mode found in process.env, assuming DEV');
}
return mode === 'DEV' || mode == undefined;
}

function useDependencies() {
return Services(
'@sern/emitter',
Expand Down
3 changes: 2 additions & 1 deletion src/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export interface Wrapper {
events?: string;
/**
* Overload to enable mode in case developer does not use a .env file.
* @deprecated - https://github.com/sern-handler/handler/pull/325
*/
mode?: 'DEV' | 'PROD';
mode?: string
/*
* @deprecated
*/
Expand Down

0 comments on commit c9f2d75

Please sign in to comment.