-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Implement custom JS helper import
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const HELPER_FILE_PATH = 'helpers.mjs' | ||
|
||
export const importCustomHelpers = (withSrcDir, ifPathExists) => ( | ||
withSrcDir((prefixWithSrcDir) => ( | ||
ifPathExists( | ||
prefixWithSrcDir(HELPER_FILE_PATH), | ||
(filePath) => import(filePath), | ||
{}, | ||
) | ||
)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { describe, it } from 'node:test' | ||
import assert from 'node:assert' | ||
import { withTempDir } from '#tests/helpers' | ||
import * as fs from 'node:fs/promises' | ||
import { join } from 'node:path' | ||
import { withDir, ifPathExists } from '#src/utils/fs' | ||
import { importCustomHelpers } from '#src/domain/helpers' | ||
|
||
describe('#src/domain/helpers', () => { | ||
describe('importCustomHelpers()', () => { | ||
it('parses exports from a `helpers.js` file', async () => { | ||
await withTempDir(async (prefixWithTempDir) => { | ||
const srcDirPath = prefixWithTempDir('src') | ||
const filePath = join(srcDirPath, 'helpers.mjs') | ||
|
||
await fs.mkdir(srcDirPath) | ||
await fs.writeFile(filePath, ` | ||
export const sayHello = () => 'Hello World!' | ||
export const add2 = (num) => num + 2 | ||
`) | ||
|
||
const helpers = await importCustomHelpers(withDir(srcDirPath), ifPathExists) | ||
|
||
assert('sayHello' in helpers) | ||
assert('add2' in helpers) | ||
assert.strictEqual(helpers.sayHello(), 'Hello World!') | ||
assert.strictEqual(helpers.add2(4), 6) | ||
}) | ||
}) | ||
}) | ||
}) |