From 95c266467835887b7d9fdf7160e6fceab213443f Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Thu, 1 Jun 2023 19:52:41 +0100 Subject: [PATCH] Allow reading remote config from file URLs --- src/common/remote-config.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/common/remote-config.ts b/src/common/remote-config.ts index 53a619f..8097b15 100644 --- a/src/common/remote-config.ts +++ b/src/common/remote-config.ts @@ -1,5 +1,6 @@ import * as path from 'node:path'; import * as fs from 'node:fs/promises'; +import { fileURLToPath } from 'node:url'; import fetch from 'node-fetch'; import mkdirp from 'mkdirp'; import { ErrorResponse, ResponseSymbol } from '../api/util.js'; @@ -32,6 +33,34 @@ async function loadRemoteConfig() { const config_cache_path = path.resolve(paths.cache, 'config.json'); const url = process.env.NXAPI_CONFIG_URL ?? CONFIG_URL; + const url_parsed = new URL(url); + + if (url_parsed.protocol === 'file:') { + const file = fileURLToPath(url_parsed); + + const stats = await fs.stat(file); + + const config: NxapiRemoteConfig = { + ...JSON.parse(await fs.readFile(file, 'utf-8')), + [SourceSymbol]: url_parsed.toString(), + }; + + const cache: RemoteConfigCacheData = { + created_at: stats.ctimeMs, + updated_at: stats.mtimeMs, + etag: null, + revalidated_at: Date.now(), + stale_at: null, + expires_at: Date.now(), + version, + revision: git?.revision ?? null, + url: url_parsed.toString(), + headers: {}, + data: config, + }; + + return cache; + } let data: RemoteConfigCacheData | undefined = undefined; let must_revalidate = true;