command modules do not depend on anything but itself

This commit is contained in:
Jacob Nguyen
2024-04-28 15:50:29 -05:00
parent 6228f53244
commit 599a02c9df
7 changed files with 88 additions and 45 deletions

View File

@@ -1,6 +1,6 @@
import { type Observable, from, mergeMap, ObservableInput } from 'rxjs';
import { readdir, stat } from 'fs/promises';
import { basename, extname, join, resolve, parse, dirname } from 'path';
import path from 'node:path';
import assert from 'assert';
import { createRequire } from 'node:module';
import type { ImportPayload, Wrapper } from '../types/core';
@@ -10,11 +10,18 @@ import type { Logging } from './contracts/logging';
export const parseCallsite = (fpath: string) => {
return parse(fpath.replace(/file:\\?/, "")).name;
const pathobj =
path.parse(fpath.replace(/file:\\?/, "")
.split(path.sep)
.join(path.posix.sep))
return {
name: pathobj.name,
absPath : path.posix.format(pathobj)
}
}
export const shouldHandle = (path: string, fpath: string) => {
const file_name = fpath+extname(path);
let newPath = join(dirname(path), file_name)
export const shouldHandle = (pth: string, fpath: string) => {
const file_name = fpath+path.extname(pth);
let newPath = path.join(path.dirname(pth), file_name)
.replace(/file:\\?/, "");
return { exists: existsSync(newPath),
path: 'file:///'+newPath };
@@ -41,7 +48,7 @@ export async function importModule<T>(absPath: string) {
let commandModule = fileModule.default;
assert(commandModule , `Found no export @ ${absPath}. Forgot to ignore with "!"? (!${basename(absPath)})?`);
assert(commandModule , `Found no export @ ${absPath}. Forgot to ignore with "!"? (!${path.basename(absPath)})?`);
if ('default' in commandModule ) {
commandModule = commandModule.default;
}
@@ -54,7 +61,7 @@ export async function defaultModuleLoader<T extends Module>(absPath: string): Mo
return { module, absPath };
}
export const fmtFileName = (fileName: string) => parse(fileName).name;
export const fmtFileName = (fileName: string) => path.parse(fileName).name;
/**
* a directory string is converted into a stream of modules.
@@ -69,21 +76,21 @@ export function buildModuleStream<T extends Module>(
.pipe(mergeMap(defaultModuleLoader<T>));
}
export const getFullPathTree = (dir: string) => readPaths(resolve(dir));
export const getFullPathTree = (dir: string) => readPaths(path.resolve(dir));
export const filename = (path: string) => fmtFileName(basename(path));
export const filename = (p: string) => fmtFileName(path.basename(p));
const validExtensions = ['.js', '.cjs', '.mts', '.mjs', '.cts', '.ts', ''];
const isSkippable = (filename: string) => {
//empty string is for non extension files (directories)
const validExtensions = ['.js', '.cjs', '.mts', '.mjs', '.cts', '.ts', ''];
return filename[0] === '!' || !validExtensions.includes(extname(filename));
return filename[0] === '!' || !validExtensions.includes(path.extname(filename));
};
async function deriveFileInfo(dir: string, file: string) {
const fullPath = join(dir, file);
const fullPath = path.join(dir, file);
return { fullPath,
fileStats: await stat(fullPath),
base: basename(file) };
base: path.basename(file) };
}
async function* readPaths(dir: string): AsyncGenerator<string> {
@@ -114,12 +121,12 @@ export function loadConfig(wrapper: Wrapper | 'file', log: Logging | undefined):
return wrapper;
}
log?.info({ message: 'Experimental loading of sern.config.json'});
const config = requir(resolve('sern.config.json'));
const config = requir(path.resolve('sern.config.json'));
const makePath = (dir: PropertyKey) =>
config.language === 'typescript'
? join('dist', config.paths[dir]!)
: join(config.paths[dir]!);
? path.join('dist', config.paths[dir]!)
: path.join(config.paths[dir]!);
log?.info({ message: 'Loading config: ' + JSON.stringify(config, null, 4) });
const commandsPath = makePath('commands');