Skip to content

Commit

Permalink
fix(import): Use file paths as URL (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktfth committed Jan 28, 2024
1 parent b426650 commit d1a8e8a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/pinion/src/cli.ts
@@ -1,8 +1,8 @@
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
import { existsSync, readFileSync } from 'fs'
import { Command } from 'commander'

import { Command } from 'commander'
import { fileURLToPath } from 'url'
import { getContext } from './core.js'
import { loadModule } from './utils.js'

Expand Down
9 changes: 5 additions & 4 deletions packages/pinion/src/utils.ts
@@ -1,6 +1,7 @@
import { existsSync } from 'fs'
import { readdir } from 'fs/promises'
import path from 'path'
import { pathToFileURL } from 'url'
import { readdir } from 'fs/promises'

let tsModule: any

Expand All @@ -19,10 +20,10 @@ const getFileUrl = (file: string) => {
}

if (!url.startsWith('file://')) {
url = `file://${url}`
url = pathToFileURL(url).href
}

return file
return url
}

export const loadModule = async (file: string) => {
Expand All @@ -32,7 +33,7 @@ export const loadModule = async (file: string) => {
await tsRegister()
}

return import(file)
return import(fileName)
}

export const listFiles = async (folder: string, extension?: string): Promise<string[]> => {
Expand Down
15 changes: 11 additions & 4 deletions packages/pinion/test/index.test.ts
@@ -1,9 +1,11 @@
import { PinionContext, getContext, runModule } from '../lib/index.js'
import { describe, it } from 'node:test'
import path from 'path'

import assert from 'assert'
import { fileURLToPath } from 'url'
import path from 'path'
import { platform } from 'os'
import { readFile } from 'fs/promises'
import assert from 'assert'
import { getContext, PinionContext, runModule } from '../lib/index.js'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
Expand Down Expand Up @@ -54,7 +56,12 @@ describe('@featherscloud/pinion', () => {
const written = await readFile(path.join(__dirname, 'tmp', 'hello.md'))
const writtenJSON = JSON.parse((await readFile(path.join(__dirname, 'tmp', 'testing.json'))).toString())

assert.strictEqual(written.toString(), expectedFileContent)
if (platform() === 'win32') {
assert.strictEqual(written.toString().replace(/\r/g, ''), expectedFileContent.replace(/\r/g, ''))
} else if (platform() !== 'win32') {
assert.strictEqual(written.toString(), expectedFileContent)
}

assert.deepStrictEqual(writtenJSON, {
written: true,
merged: true,
Expand Down

0 comments on commit d1a8e8a

Please sign in to comment.