Skip to content

Commit

Permalink
feat: add functions level to metadata object (#129)
Browse files Browse the repository at this point in the history
* feat: add `functions` level to `metadata` object

* chore: use file URL in test
  • Loading branch information
eduardoboucas committed Sep 19, 2022
1 parent ed7503a commit 45cf3b2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
33 changes: 25 additions & 8 deletions deno/lib/stage2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,30 @@ import type { InputFunction, WriteStage2Options } from '../../shared/stage2.ts'
import { PUBLIC_SPECIFIER, STAGE2_SPECIFIER, virtualRoot } from './consts.ts'
import { inlineModule, loadFromVirtualRoot, loadWithRetry } from './common.ts'

const getFunctionReference = (basePath: string, func: InputFunction, index: number) => {
interface FunctionReference {
exportLine: string
importLine: string
metadata: {
url: URL
}
name: string
}

const getMetadata = (references: FunctionReference[]) => {
const functions = references.reduce(
(acc, { metadata, name }) => ({
...acc,
[name]: metadata,
}),
{},
)

return {
functions,
}
}

const getFunctionReference = (basePath: string, func: InputFunction, index: number): FunctionReference => {
const importName = `func${index}`
const exportLine = `"${func.name}": ${importName}`
const url = getVirtualPath(basePath, func.path)
Expand All @@ -25,13 +48,7 @@ export const getStage2Entry = (basePath: string, functions: InputFunction[]) =>
const lines = functions.map((func, index) => getFunctionReference(basePath, func, index))
const importLines = lines.map(({ importLine }) => importLine).join('\n')
const exportLines = lines.map(({ exportLine }) => exportLine).join(', ')
const metadata = lines.reduce(
(acc, { metadata, name }) => ({
...acc,
[name]: metadata,
}),
{},
)
const metadata = getMetadata(lines)
const functionsExport = `export const functions = {${exportLines}};`
const metadataExport = `export const metadata = ${JSON.stringify(metadata)};`

Expand Down
4 changes: 2 additions & 2 deletions node/formats/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const getLocalEntryPoint = (
}: GetLocalEntryPointOptions,
) => {
const bootImport = `import { boot } from "${getBootstrapURL()}";`
const declaration = `const functions = {}; const metadata = {};`
const declaration = `const functions = {}; const metadata = { functions: {} };`
const imports = functions.map((func) => {
const url = pathToFileURL(func.path)
const metadata = {
Expand All @@ -121,7 +121,7 @@ const getLocalEntryPoint = (
if (typeof func === "function") {
functions["${func.name}"] = func;
metadata["${func.name}"] = ${JSON.stringify(metadata)}
metadata.functions["${func.name}"] = ${JSON.stringify(metadata)}
} else {
console.log(${JSON.stringify(formatExportTypeError(func.name))});
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"test:ci": "run-s build test:ci:*",
"test:dev:ava": "ava",
"test:dev:deno": "deno test --allow-all test/deno",
"test:ci:ava": "nyc -r lcovonly -r text -r json ava"
"test:ci:ava": "nyc -r lcovonly -r text -r json ava",
"test:ci:deno": "deno test --allow-all test/deno"
},
"config": {
"eslint": "--ignore-path .gitignore --cache --format=codeframe --max-warnings=0 \"{node,scripts,.github}/**/*.{js,ts,md,html}\" \"*.{js,ts,md,html}\"",
Expand Down
5 changes: 3 additions & 2 deletions test/deno/stage2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ Deno.test('`getStage2Entry` returns a valid stage 2 file', async () => {
const normalizedStage2 = stage2.replaceAll(virtualRoot, `${baseURL.href}/`)

const stage2Path = join(directory, 'stage2.ts')
const stage2URL = pathToFileURL(stage2Path)

await Deno.writeTextFile(stage2Path, normalizedStage2)

const mod = await import(stage2Path)
const mod = await import(stage2URL.href)

await Deno.remove(directory, { recursive: true })

for (const func of functions) {
const result = await mod.functions[func.name]()

assertEquals(await result.text(), func.response)
assertEquals(mod.metadata[func.name].url, pathToFileURL(func.path).toString())
assertEquals(mod.metadata.functions[func.name].url, pathToFileURL(func.path).toString())
}
})
2 changes: 1 addition & 1 deletion test/node/stage_2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test('`getLocalEntryPoint` returns a valid stage 2 file for local development',

for (const func of functions) {
t.is(responses[func.name], func.response)
t.is(metadata[func.name].url, pathToFileURL(func.path).toString())
t.is(metadata.functions[func.name].url, pathToFileURL(func.path).toString())
}

await del(tmpDir, { force: true })
Expand Down

0 comments on commit 45cf3b2

Please sign in to comment.