Skip to content

Commit 19c1f7b

Browse files
committed
publish runtime packages in JSON format for AI
1 parent d01c0a5 commit 19c1f7b

File tree

6 files changed

+77
-33
lines changed

6 files changed

+77
-33
lines changed

apps/outreach/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@astrojs/starlight": "^0.34.3",
2828
"@astrojs/starlight-tailwind": "4.0.1",
2929
"@opendatacapture/runtime-core": "workspace:*",
30+
"@opendatacapture/runtime-meta": "workspace:*",
3031
"@tailwindcss/typography": "^0.5.16",
3132
"@tailwindcss/vite": "^4.1.5",
3233
"astro": "^5.15.9",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as url from 'node:url';
2+
3+
import { generateMetadata, RUNTIME_VERSIONS } from '@opendatacapture/runtime-meta';
4+
import type { APIRoute, GetStaticPaths, InferGetStaticParamsType, InferGetStaticPropsType } from 'astro';
5+
import * as Astro from 'astro:config/server';
6+
7+
type Params = InferGetStaticParamsType<typeof getStaticPaths>;
8+
9+
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
10+
11+
export const getStaticPaths = (async () => {
12+
const metadata = await generateMetadata({ rootDir: url.fileURLToPath(Astro.root) });
13+
return RUNTIME_VERSIONS.map((version) => {
14+
const entry = metadata.get(version);
15+
return {
16+
params: {
17+
version
18+
},
19+
props: {
20+
packages: entry!.packages
21+
}
22+
};
23+
});
24+
}) satisfies GetStaticPaths;
25+
26+
export const GET: APIRoute<Props, Params> = ({ props }) => {
27+
return new Response(JSON.stringify(props, null, 2));
28+
};

packages/runtime-meta/src/index.d.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export declare const RUNTIME_VERSIONS: string[];
2+
13
/** Contains the relative paths of assets for a given runtime version. */
24
export type RuntimeManifest = {
35
/** Relative paths to TypeScript declaration files (.d.ts). */
@@ -10,16 +12,22 @@ export type RuntimeManifest = {
1012
styles: string[];
1113
};
1214

15+
export type RuntimePackageMetadata = {
16+
exports: {
17+
css: string[];
18+
html: string[];
19+
js: string[];
20+
};
21+
name: string;
22+
version: string;
23+
};
24+
1325
/**
1426
* Metadata for a given runtime version.
1527
*
1628
* @example
1729
* {
1830
* baseDir: "/root/OpenDataCapture/runtime/v1/dist",
19-
* importPaths: [
20-
* "/runtime/v1/@opendatacapture/runtime-core/index.js",
21-
* ...
22-
* ],
2331
* manifest: {
2432
* declarations: [
2533
* "@opendatacapture/runtime-core/index.d.ts",
@@ -40,10 +48,10 @@ export type RuntimeManifest = {
4048
export type RuntimeVersionMetadata = {
4149
/** Absolute path to the root directory where runtime files are located. */
4250
baseDir: string;
43-
/** List of fully-qualified import paths available at runtime. */
44-
importPaths: string[];
4551
/** Manifest containing relative paths to declarations, html, sources, and styles. */
4652
manifest: RuntimeManifest;
53+
/** A list of packages available */
54+
packages: RuntimePackageMetadata[];
4755
};
4856

4957
export type RuntimeMetadataMap = Map<string, RuntimeVersionMetadata>;

packages/runtime-meta/src/index.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const isDirectory = async (path) => fs.existsSync(path) && fs.lstatSync(path).is
77

88
const RUNTIME_DIST_DIRNAME = 'dist';
99

10-
const RUNTIME_VERSIONS = [1];
10+
export const RUNTIME_VERSIONS = ['v1'];
1111

1212
/** @type {import('.').MANIFEST_FILENAME} */
1313
export const MANIFEST_FILENAME = 'runtime.json';
@@ -37,43 +37,50 @@ export async function generateManifest(baseDir) {
3737
return results;
3838
}
3939

40-
// /** @type {import('.').generateMetadataForVersion} */
41-
// export async function generateMetadataForVersion(version) {
42-
// const baseDir = path.resolve(RUNTIME_DIR, version, RUNTIME_DIST_DIRNAME);
43-
// if (!(await isDirectory(baseDir))) {
44-
// throw new Error(`Not a directory: ${baseDir}`);
45-
// }
46-
// const { declarations, html, sources, styles } = await generateManifest(baseDir);
47-
// return {
48-
// baseDir,
49-
// importPaths: sources.map((filename) => `/runtime/${version}/${filename}`),
50-
// manifest: {
51-
// declarations,
52-
// html,
53-
// sources,
54-
// styles
55-
// }
56-
// };
57-
// }
58-
5940
/** @type {import('.').generateMetadata} */
6041
export async function generateMetadata(options) {
6142
const require = module.createRequire(options.rootDir);
43+
44+
/** @type {import('.').RuntimeMetadataMap} */
6245
const metadata = new Map();
63-
for (const version of RUNTIME_VERSIONS) {
64-
const packageDir = path.dirname(require.resolve(`@opendatacapture/runtime-v${version}/package.json`));
46+
47+
for (const v of RUNTIME_VERSIONS) {
48+
const packageDir = path.dirname(require.resolve(`@opendatacapture/runtime-${v}/package.json`));
6549
const baseDir = path.resolve(packageDir, RUNTIME_DIST_DIRNAME);
6650
const { declarations, html, sources, styles } = await generateManifest(baseDir);
6751

68-
metadata.set(`v${version}`, {
52+
const importPathPattern = /^(@?[^@/]+(?:\/[^@/]+)?)(?:@([^/]+))?/;
53+
54+
/** @type {Map<string, import('.').RuntimePackageMetadata>} */
55+
const packages = new Map();
56+
57+
/** @param {string} filename @param {'css' | 'html' | 'js'} kind */
58+
const addToPackage = (filename, kind) => {
59+
const match = filename.match(importPathPattern);
60+
if (!match) {
61+
throw new Error(`Unexpected import path pattern: ${filename}`);
62+
}
63+
const [name, version] = /** @type {[string, string]} */ (match.slice(1, 3));
64+
const key = name + '$' + version;
65+
if (!packages.has(key)) {
66+
packages.set(key, { exports: { css: [], html: [], js: [] }, name, version });
67+
}
68+
packages.get(key)?.exports[kind].push(`/runtime/${v}/${filename}`);
69+
};
70+
71+
sources.forEach((source) => addToPackage(source, 'js'));
72+
html.forEach((file) => addToPackage(file, 'html'));
73+
styles.forEach((style) => addToPackage(style, 'css'));
74+
75+
metadata.set(v, {
6976
baseDir,
70-
importPaths: sources.map((filename) => `/runtime/${version}/${filename}`),
7177
manifest: {
7278
declarations,
7379
html,
7480
sources,
7581
styles
76-
}
82+
},
83+
packages: [...packages.values()]
7784
});
7885
}
7986
return metadata;

packages/serve-instrument/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@opendatacapture/serve-instrument",
33
"type": "module",
4-
"version": "0.0.8",
4+
"version": "0.0.9",
55
"license": "Apache-2.0",
66
"bin": {
77
"serve-instrument": "./dist/cli.js"

packages/vite-plugin-runtime/src/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function plugin(options) {
2323
},
2424
config: () => ({
2525
optimizeDeps: {
26-
exclude: Array.from(metadata.values().flatMap((pkg) => pkg.importPaths))
26+
exclude: Array.from(metadata.values().flatMap(({ packages }) => packages.flatMap((pkg) => pkg.exports.js)))
2727
}
2828
}),
2929
configureServer: (server) => {

0 commit comments

Comments
 (0)