@backstage/config-loader
Advanced tools
Comparing version 0.1.1-alpha.16 to 0.1.1-alpha.17
@@ -12,31 +12,4 @@ 'use strict'; | ||
function findRootPath(topPath) { | ||
let path2 = topPath; | ||
for (let i = 0; i < 1e3; i++) { | ||
const packagePath = path.resolve(path2, "package.json"); | ||
const exists = fs.pathExistsSync(packagePath); | ||
if (exists) { | ||
try { | ||
const data = fs.readJsonSync(packagePath); | ||
if (data.name === "root" || data.name.includes("backstage-e2e")) { | ||
return path2; | ||
} | ||
} catch (error) { | ||
throw new Error(`Failed to parse package.json file while searching for root, ${error}`); | ||
} | ||
} | ||
const newPath = path.dirname(path2); | ||
if (newPath === path2) { | ||
throw new Error(`No package.json with name "root" found as a parent of ${topPath}`); | ||
} | ||
path2 = newPath; | ||
} | ||
throw new Error(`Iteration limit reached when searching for root package.json at ${topPath}`); | ||
} | ||
async function resolveStaticConfig(options) { | ||
let {configPath} = options; | ||
if (!configPath) { | ||
configPath = path.resolve(findRootPath(fs.realpathSync(process.cwd())), "app-config.yaml"); | ||
} | ||
const configPath = path.resolve(options.rootPath, "app-config.yaml"); | ||
return [configPath]; | ||
@@ -128,3 +101,3 @@ } | ||
try { | ||
const parsedValue = JSON.parse(value); | ||
const [, parsedValue] = safeJsonParse(value); | ||
if (parsedValue === null) { | ||
@@ -143,2 +116,9 @@ throw new Error("value may not be null"); | ||
} | ||
function safeJsonParse(str) { | ||
try { | ||
return [null, JSON.parse(str)]; | ||
} catch (err) { | ||
return [err, str]; | ||
} | ||
} | ||
@@ -226,3 +206,3 @@ const secretLoaderSchemas = { | ||
} | ||
async function loadConfig(options = {}) { | ||
async function loadConfig(options) { | ||
const configs = []; | ||
@@ -229,0 +209,0 @@ configs.push(...readEnv(process.env)); |
import { AppConfig } from '@backstage/config'; | ||
declare type LoadConfigOptions = { | ||
configPath?: string; | ||
rootPath: string; | ||
shouldReadSecrets?: boolean; | ||
}; | ||
declare function loadConfig(options?: LoadConfigOptions): Promise<AppConfig[]>; | ||
declare function loadConfig(options: LoadConfigOptions): Promise<AppConfig[]>; | ||
export { LoadConfigOptions, loadConfig }; |
import fs from 'fs-extra'; | ||
import { resolve, dirname, basename, extname } from 'path'; | ||
import { resolve, basename, extname, dirname } from 'path'; | ||
import yaml2 from 'yaml'; | ||
import { object, string, lazy, array, ValidationError } from 'yup'; | ||
function findRootPath(topPath) { | ||
let path2 = topPath; | ||
for (let i = 0; i < 1e3; i++) { | ||
const packagePath = resolve(path2, "package.json"); | ||
const exists = fs.pathExistsSync(packagePath); | ||
if (exists) { | ||
try { | ||
const data = fs.readJsonSync(packagePath); | ||
if (data.name === "root" || data.name.includes("backstage-e2e")) { | ||
return path2; | ||
} | ||
} catch (error) { | ||
throw new Error(`Failed to parse package.json file while searching for root, ${error}`); | ||
} | ||
} | ||
const newPath = dirname(path2); | ||
if (newPath === path2) { | ||
throw new Error(`No package.json with name "root" found as a parent of ${topPath}`); | ||
} | ||
path2 = newPath; | ||
} | ||
throw new Error(`Iteration limit reached when searching for root package.json at ${topPath}`); | ||
} | ||
async function resolveStaticConfig(options) { | ||
let {configPath} = options; | ||
if (!configPath) { | ||
configPath = resolve(findRootPath(fs.realpathSync(process.cwd())), "app-config.yaml"); | ||
} | ||
const configPath = resolve(options.rootPath, "app-config.yaml"); | ||
return [configPath]; | ||
@@ -121,3 +94,3 @@ } | ||
try { | ||
const parsedValue = JSON.parse(value); | ||
const [, parsedValue] = safeJsonParse(value); | ||
if (parsedValue === null) { | ||
@@ -136,2 +109,9 @@ throw new Error("value may not be null"); | ||
} | ||
function safeJsonParse(str) { | ||
try { | ||
return [null, JSON.parse(str)]; | ||
} catch (err) { | ||
return [err, str]; | ||
} | ||
} | ||
@@ -219,3 +199,3 @@ const secretLoaderSchemas = { | ||
} | ||
async function loadConfig(options = {}) { | ||
async function loadConfig(options) { | ||
const configs = []; | ||
@@ -222,0 +202,0 @@ configs.push(...readEnv(process.env)); |
{ | ||
"name": "@backstage/config-loader", | ||
"description": "Config loading functionality used by Backstage backend, and CLI", | ||
"version": "0.1.1-alpha.16", | ||
"version": "0.1.1-alpha.17", | ||
"private": false, | ||
@@ -33,3 +33,3 @@ "publishConfig": { | ||
"dependencies": { | ||
"@backstage/config": "^0.1.1-alpha.16", | ||
"@backstage/config": "^0.1.1-alpha.17", | ||
"fs-extra": "^9.0.0", | ||
@@ -40,3 +40,3 @@ "yaml": "^1.9.2", | ||
"devDependencies": { | ||
"@types/jest": "^25.2.2", | ||
"@types/jest": "^26.0.7", | ||
"@types/node": "^12.0.0", | ||
@@ -48,4 +48,4 @@ "@types/yup": "^0.28.2" | ||
], | ||
"gitHead": "8d58618eb2ff068f47509029216a2990e567f7fe", | ||
"gitHead": "24ed336579226fc628f7f6ba38c0bb27f430b257", | ||
"module": "dist/index.esm.js" | ||
} |
26074
428