@opentf/create-afw 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/versions.js CHANGED
@@ -1,20 +1,58 @@
1
- // The AFW package versions a new project depends on: the ones this scaffolder was
2
- // released with (`versions.json`, written by `npm run build`), or — in the AFW
3
- // repository itself — the workspace packages' current versions.
1
+ // The AFW package versions a new project depends on, resolved so the scaffolder works
2
+ // however it was installed:
3
+ //
4
+ // 1. `versions.json` beside this file, when a release build wrote one;
5
+ // 2. the sibling workspace packages, inside the AFW repository;
6
+ // 3. the npm registry's `latest` for each package (`npm create` has just reached it);
7
+ // 4. the `latest` dist-tag, so scaffolding never fails on a lookup.
4
8
 
5
9
  import { readFile } from "node:fs/promises";
6
10
 
7
- export const AFW_PACKAGES = { "@opentf/afw": "afw", "@opentf/afw-test": "afw-test", "@opentf/vite-plugin-afw": "vite-plugin-afw" };
11
+ import { timeoutRun } from "@opentf/std";
8
12
 
9
- export async function afwVersions() {
10
- try {
11
- return JSON.parse(await readFile(new URL("./versions.json", import.meta.url), "utf8"));
12
- } catch {
13
- const versions = {};
14
- for (const [name, folder] of Object.entries(AFW_PACKAGES)) {
15
- const manifest = JSON.parse(await readFile(new URL(`../${folder}/package.json`, import.meta.url), "utf8"));
16
- versions[name] = `^${manifest.version}`;
13
+ export const AFW_PACKAGES = {
14
+ "@opentf/afw": "afw",
15
+ "@opentf/afw-docs": "afw-docs",
16
+ "@opentf/afw-test": "afw-test",
17
+ "@opentf/vite-plugin-afw": "vite-plugin-afw",
18
+ };
19
+
20
+ const REGISTRY_TIMEOUT_MS = 5000;
21
+
22
+ async function fromFile() {
23
+ return JSON.parse(await readFile(new URL("./versions.json", import.meta.url), "utf8"));
24
+ }
25
+
26
+ async function fromWorkspace() {
27
+ const versions = {};
28
+ for (const [name, folder] of Object.entries(AFW_PACKAGES)) {
29
+ const manifest = JSON.parse(await readFile(new URL(`../${folder}/package.json`, import.meta.url), "utf8"));
30
+ if (manifest.name !== name) throw new Error(`not the ${name} workspace package`);
31
+ versions[name] = `^${manifest.version}`;
32
+ }
33
+ return versions;
34
+ }
35
+
36
+ /** `^<latest>` for each package from the npm registry (`npm_config_registry` honoured). */
37
+ export async function fromRegistry({ registry = process.env.npm_config_registry || "https://registry.npmjs.org/", fetch = globalThis.fetch } = {}) {
38
+ const base = registry.endsWith("/") ? registry : `${registry}/`;
39
+ const entries = await Promise.all(Object.keys(AFW_PACKAGES).map(async (name) => {
40
+ const res = await timeoutRun(() => fetch(`${base}${name}/latest`, { headers: { accept: "application/json" } }), REGISTRY_TIMEOUT_MS);
41
+ if (!res.ok) throw new Error(`${name}: registry answered ${res.status}`);
42
+ const { version } = await res.json();
43
+ if (typeof version !== "string") throw new Error(`${name}: no version in the registry answer`);
44
+ return [name, `^${version}`];
45
+ }));
46
+ return Object.fromEntries(entries);
47
+ }
48
+
49
+ export async function afwVersions(options = {}) {
50
+ for (const source of [fromFile, fromWorkspace, () => fromRegistry(options)]) {
51
+ try {
52
+ return await source();
53
+ } catch {
54
+ // try the next source
17
55
  }
18
- return versions;
19
56
  }
57
+ return Object.fromEntries(Object.keys(AFW_PACKAGES).map((name) => [name, "latest"]));
20
58
  }
package/versions.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "@opentf/afw": "^0.35.0",
3
+ "@opentf/afw-docs": "^0.33.0",
4
+ "@opentf/afw-test": "^1.30.0",
5
+ "@opentf/vite-plugin-afw": "^0.3.0"
6
+ }