Skip to content

Commit

Permalink
fix: Ensure all module names are proper URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Jan 27, 2024
1 parent a472c4a commit e2bb6a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/pinion/src/core.ts
Expand Up @@ -162,7 +162,7 @@ export const getContext = <T extends PinionContext>(
export const generator = async <T extends PinionContext>(initialContext: T) => initialContext

export const runModule = async (file: string, ctx: PinionContext) => {
const { generate } = await loadModule(`file://${file}`)
const { generate } = await loadModule(file)

return generate(ctx)
}
22 changes: 12 additions & 10 deletions packages/pinion/src/utils.ts
Expand Up @@ -7,24 +7,26 @@ let tsModule: any
const tsRegister = async (mod = 'tsx') => (tsModule = tsModule || import(mod))

const extensionCheck = /(\.ts|\.js)$/
const getFileName = (file: string) => {
if (extensionCheck.test(file)) {
return file
}

if (existsSync(`${file}.js`)) {
return `${file}.js`
const getFileUrl = (file: string) => {
let url = file

if (!extensionCheck.test(file)) {
if (existsSync(`${file}.js`)) {
url = `${file}.js`
} else if (existsSync(`${file}.ts`)) {
url = `${file}.ts`
}
}

if (existsSync(`${file}.ts`)) {
return `${file}.ts`
if (!url.startsWith('file://')) {
url = `file://${url}`
}

return file
}

export const loadModule = async (file: string) => {
const fileName = getFileName(file)
const fileName = getFileUrl(file)

if (fileName.endsWith('.ts')) {
await tsRegister()
Expand Down

0 comments on commit e2bb6a9

Please sign in to comment.