Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add functions level to metadata object #129

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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