From 9306dc035c7283473830f24e5e17e5c1ab1e6932 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 3 Aug 2020 17:29:46 +0200 Subject: [PATCH 1/7] packages: add cli-common --- packages/cli-common/.eslintrc.js | 3 + packages/cli-common/README.md | 12 ++ packages/cli-common/package.json | 39 +++++++ packages/cli-common/src/index.ts | 18 +++ packages/cli-common/src/paths.test.ts | 92 +++++++++++++++ packages/cli-common/src/paths.ts | 155 ++++++++++++++++++++++++++ 6 files changed, 319 insertions(+) create mode 100644 packages/cli-common/.eslintrc.js create mode 100644 packages/cli-common/README.md create mode 100644 packages/cli-common/package.json create mode 100644 packages/cli-common/src/index.ts create mode 100644 packages/cli-common/src/paths.test.ts create mode 100644 packages/cli-common/src/paths.ts diff --git a/packages/cli-common/.eslintrc.js b/packages/cli-common/.eslintrc.js new file mode 100644 index 0000000000..16a033dbc6 --- /dev/null +++ b/packages/cli-common/.eslintrc.js @@ -0,0 +1,3 @@ +module.exports = { + extends: [require.resolve('@backstage/cli/config/eslint.backend')], +}; diff --git a/packages/cli-common/README.md b/packages/cli-common/README.md new file mode 100644 index 0000000000..3488003a76 --- /dev/null +++ b/packages/cli-common/README.md @@ -0,0 +1,12 @@ +# @backstage/cli-common + +This package provides config loading functionality used by the backend, and CLI. + +## Installation + +Do not install this package directly, it is an internal package used by [@backstage/cli](https://www.npmjs.com/package/@backstage/cli), and [@backstage/create-app](https://www.npmjs.com/package/@backstage/create-app). Depend on either of those instead. + +## Documentation + +- [Backstage Readme](https://github.com/spotify/backstage/blob/master/README.md) +- [Backstage Documentation](https://github.com/spotify/backstage/blob/master/docs/README.md) diff --git a/packages/cli-common/package.json b/packages/cli-common/package.json new file mode 100644 index 0000000000..1d2dc35cd9 --- /dev/null +++ b/packages/cli-common/package.json @@ -0,0 +1,39 @@ +{ + "name": "@backstage/cli-common", + "description": "Common functionality used by cli, backend, and create-app", + "version": "0.1.1-alpha.16", + "private": false, + "main": "src/index.ts", + "types": "src/index.ts", + "publishConfig": { + "access": "public", + "main": "dist/index.cjs.js", + "types": "dist/index.d.ts" + }, + "homepage": "https://backstage.io", + "repository": { + "type": "git", + "url": "https://github.com/spotify/backstage", + "directory": "packages/cli-common" + }, + "keywords": [ + "backstage" + ], + "license": "Apache-2.0", + "scripts": { + "build": "backstage-cli build --outputs cjs,types", + "lint": "backstage-cli lint", + "test": "backstage-cli test", + "prepack": "backstage-cli prepack", + "postpack": "backstage-cli postpack", + "clean": "backstage-cli clean" + }, + "devDependencies": { + "@backstage/cli": "^0.1.1-alpha.16", + "@types/jest": "^26.0.7", + "@types/node": "^12.0.0" + }, + "files": [ + "dist" + ] +} diff --git a/packages/cli-common/src/index.ts b/packages/cli-common/src/index.ts new file mode 100644 index 0000000000..a080f49b6b --- /dev/null +++ b/packages/cli-common/src/index.ts @@ -0,0 +1,18 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export { findPaths } from './paths'; +export type { Paths } from './paths'; diff --git a/packages/cli-common/src/paths.test.ts b/packages/cli-common/src/paths.test.ts new file mode 100644 index 0000000000..087f95af38 --- /dev/null +++ b/packages/cli-common/src/paths.test.ts @@ -0,0 +1,92 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { resolve as resolvePath } from 'path'; +import { findPaths, findRootPath, findOwnDir, findOwnRootDir } from './paths'; + +describe('paths', () => { + it('findOwnDir and findOwnRootDir should find owns paths', () => { + const dir = findOwnDir(__dirname); + const root = findOwnRootDir(dir); + + expect(dir).toBe(resolvePath(__dirname, '..')); + expect(root).toBe(resolvePath(__dirname, '../../..')); + }); + + it('findRootPath should find a root path', () => { + expect(findRootPath(__dirname, () => true)).toBe( + resolvePath(__dirname, '..'), + ); + + expect(findRootPath(__dirname, () => false)).toBeUndefined(); + + expect( + findRootPath( + __dirname, + path => path !== resolvePath(__dirname, '../package.json'), + ), + ).toBe(resolvePath(__dirname, '../../..')); + }); + + it('findPaths should find package paths', () => { + const dir = resolvePath(__dirname, '..'); + const root = resolvePath(__dirname, '../../..'); + + const paths = findPaths(__dirname); + + expect(paths.ownDir).toBe(dir); + expect(paths.ownRoot).toBe(root); + expect(paths.resolveOwn('./derp.txt')).toBe(resolvePath(dir, 'derp.txt')); + expect(paths.resolveOwnRoot('./derp.txt')).toBe( + resolvePath(root, 'derp.txt'), + ); + expect(paths.targetDir).toBe(dir); + expect(paths.targetRoot).toBe(root); + expect(paths.resolveTarget('./derp.txt')).toBe( + resolvePath(dir, 'derp.txt'), + ); + expect(paths.resolveTargetRoot('./derp.txt')).toBe( + resolvePath(root, 'derp.txt'), + ); + }); + + it('findPaths should find mocked package paths', () => { + const mockCwd = resolvePath(__dirname, '../../core'); + const mockDir = resolvePath(__dirname, '../../cli'); + const root = resolvePath(__dirname, '../../..'); + + jest.spyOn(process, 'cwd').mockReturnValue(mockCwd); + + const paths = findPaths(resolvePath(mockDir, 'src/lib')); + + expect(paths.ownDir).toBe(mockDir); + expect(paths.ownRoot).toBe(root); + expect(paths.resolveOwn('./derp.txt')).toBe( + resolvePath(mockDir, 'derp.txt'), + ); + expect(paths.resolveOwnRoot('./derp.txt')).toBe( + resolvePath(root, 'derp.txt'), + ); + expect(paths.targetDir).toBe(mockCwd); + expect(paths.targetRoot).toBe(root); + expect(paths.resolveTarget('./derp.txt')).toBe( + resolvePath(mockCwd, 'derp.txt'), + ); + expect(paths.resolveTargetRoot('./derp.txt')).toBe( + resolvePath(root, 'derp.txt'), + ); + }); +}); diff --git a/packages/cli-common/src/paths.ts b/packages/cli-common/src/paths.ts new file mode 100644 index 0000000000..70761ad2d7 --- /dev/null +++ b/packages/cli-common/src/paths.ts @@ -0,0 +1,155 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import fs from 'fs'; +import { dirname, resolve as resolvePath } from 'path'; + +export type ResolveFunc = (...paths: string[]) => string; + +// Common paths and resolve functions used by the cli. +// Currently assumes it is being executed within a monorepo. +export type Paths = { + // Root dir of the cli itself, containing package.json + ownDir: string; + + // Monorepo root dir of the cli itself. Only accessible when running inside Backstage repo. + ownRoot: string; + + // The location of the app that the cli is being executed in + targetDir: string; + + // The monorepo root package of the app that the cli is being executed in. + targetRoot: string; + + // Resolve a path relative to own repo + resolveOwn: ResolveFunc; + + // Resolve a path relative to own monorepo root. Only accessible when running inside Backstage repo. + resolveOwnRoot: ResolveFunc; + + // Resolve a path relative to the app + resolveTarget: ResolveFunc; + + // Resolve a path relative to the app repo root + resolveTargetRoot: ResolveFunc; +}; + +// Looks for a package.json with a workspace config to identify the root of the monorepo +export function findRootPath( + searchDir: string, + filterFunc: (pkgJsonPath: string) => boolean, +): string | undefined { + let path = searchDir; + + // Some sanity check to avoid infinite loop + for (let i = 0; i < 1000; i++) { + const packagePath = resolvePath(path, 'package.json'); + const exists = fs.existsSync(packagePath); + if (exists && filterFunc(packagePath)) { + return path; + } + + const newPath = dirname(path); + if (newPath === path) { + return undefined; + } + path = newPath; + } + + throw new Error( + `Iteration limit reached when searching for root package.json at ${searchDir}`, + ); +} + +// Finds the root of a given package +export function findOwnDir(searchDir: string) { + const path = findRootPath(searchDir, () => true); + if (!path) { + throw new Error( + `No package.json found while searching for package root of ${searchDir}`, + ); + } + return path; +} + +// Finds the root of the monorepo that the package exists in. Only accessible when running inside Backstage repo. +export function findOwnRootDir(ownDir: string) { + const isLocal = fs.existsSync(resolvePath(ownDir, 'src')); + if (!isLocal) { + throw new Error( + 'Tried to access monorepo package root dir outside of Backstage repository', + ); + } + + return resolvePath(ownDir, '../..'); +} + +/** + * Find paths related to a package and its execution context. + * + * @example + * + * const paths = findPaths(__dirname) + */ +export function findPaths(searchDir: string): Paths { + const ownDir = findOwnDir(searchDir); + const targetDir = fs.realpathSync(process.cwd()); + + // Lazy load this as it will throw an error if we're not inside the Backstage repo. + let ownRoot = ''; + const getOwnRoot = () => { + if (!ownRoot) { + ownRoot = findOwnRootDir(ownDir); + } + return ownRoot; + }; + + // We're not always running in a monorepo, so we lazy init this to only crash commands + // that require a monorepo when we're not in one. + let targetRoot = ''; + const getTargetRoot = () => { + if (!targetRoot) { + targetRoot = + findRootPath(targetDir, path => { + try { + const content = fs.readFileSync(path, 'utf8'); + const data = JSON.parse(content); + return Boolean(data.workspaces?.packages); + } catch (error) { + throw new Error( + `Failed to parse package.json file while searching for root, ${error}`, + ); + } + }) ?? targetDir; // We didn't find any root package.json, assume we're not in a monorepo + } + return targetRoot; + }; + + return { + ownDir, + get ownRoot() { + return getOwnRoot(); + }, + targetDir, + get targetRoot() { + return getTargetRoot(); + }, + resolveOwn: (...paths) => resolvePath(ownDir, ...paths), + resolveOwnRoot: (...paths) => resolvePath(getOwnRoot(), ...paths), + resolveTarget: (...paths) => resolvePath(targetDir, ...paths), + resolveTargetRoot: (...paths) => resolvePath(getTargetRoot(), ...paths), + }; +} From 2f76581b6097fe928d57694c4d23e8b440bd78b8 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 3 Aug 2020 18:17:58 +0200 Subject: [PATCH 2/7] cli: use cli-common to discover paths --- packages/cli/package.json | 1 + packages/cli/src/lib/paths.ts | 133 +--------------------------------- 2 files changed, 3 insertions(+), 131 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index 250aa5f796..f6fdaf8e5d 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -29,6 +29,7 @@ "backstage-cli": "bin/backstage-cli" }, "dependencies": { + "@backstage/cli-common": "^0.1.1-alpha.16", "@backstage/config": "^0.1.1-alpha.16", "@backstage/config-loader": "^0.1.1-alpha.16", "@hot-loader/react-dom": "^16.13.0", diff --git a/packages/cli/src/lib/paths.ts b/packages/cli/src/lib/paths.ts index ab3578ca1c..06c8f2ab38 100644 --- a/packages/cli/src/lib/paths.ts +++ b/packages/cli/src/lib/paths.ts @@ -14,135 +14,6 @@ * limitations under the License. */ -import fs from 'fs-extra'; -import { dirname, resolve as resolvePath } from 'path'; +import { findPaths } from '@backstage/cli-common'; -export type ResolveFunc = (...paths: string[]) => string; - -// Common paths and resolve functions used by the cli. -// Currently assumes it is being executed within a monorepo. -export type Paths = { - // Root dir of the cli itself, containing package.json - ownDir: string; - - // Monorepo root dir of the cli itself. Only accessible when running inside Backstage repo. - ownRoot: string; - - // The location of the app that the cli is being executed in - targetDir: string; - - // The monorepo root package of the app that the cli is being executed in. - targetRoot: string; - - // Resolve a path relative to own repo - resolveOwn: ResolveFunc; - - // Resolve a path relative to own monorepo root. Only accessible when running inside Backstage repo. - resolveOwnRoot: ResolveFunc; - - // Resolve a path relative to the app - resolveTarget: ResolveFunc; - - // Resolve a path relative to the app repo root - resolveTargetRoot: ResolveFunc; -}; - -// Looks for a package.json with a workspace config to identify the root of the monorepo -export function findRootPath(topPath: string): string { - let path = topPath; - - // Some sanity check to avoid infinite loop - for (let i = 0; i < 1000; i++) { - const packagePath = resolvePath(path, 'package.json'); - const exists = fs.pathExistsSync(packagePath); - if (exists) { - try { - const data = fs.readJsonSync(packagePath); - if (data.workspaces?.packages) { - return path; - } - } catch (error) { - throw new Error( - `Failed to parse package.json file while searching for root, ${error}`, - ); - } - } - - const newPath = dirname(path); - if (newPath === path) { - return topPath; // We didn't find any root package.json, assume we're not in a monorepo - } - path = newPath; - } - - throw new Error( - `Iteration limit reached when searching for root package.json at ${topPath}`, - ); -} - -// Finds the root of the cli package itself -export function findOwnDir() { - // Known relative locations of package in dist/dev - const pathDist = '..'; - const pathDev = '../..'; - - // Check the closest dir first - const pkgInDist = resolvePath(__dirname, pathDist, 'package.json'); - const isDist = fs.pathExistsSync(pkgInDist); - - const path = isDist ? pathDist : pathDev; - return resolvePath(__dirname, path); -} - -// Finds the root of the monorepo that the cli exists in. Only accessible when running inside Backstage repo. -export function findOwnRootPath(ownDir: string) { - const isLocal = fs.pathExistsSync(resolvePath(ownDir, 'src')); - if (!isLocal) { - throw new Error( - 'Tried to access monorepo package root dir outside of Backstage repository', - ); - } - - return resolvePath(ownDir, '../..'); -} - -export function findPaths(): Paths { - const ownDir = findOwnDir(); - const targetDir = fs.realpathSync(process.cwd()); - - // Lazy load this as it will throw an error if we're not inside the Backstage repo. - let ownRoot = ''; - const getOwnRoot = () => { - if (!ownRoot) { - ownRoot = findOwnRootPath(ownDir); - } - return ownRoot; - }; - - // We're not always running in a monorepo, so we lazy init this to only crash commands - // that require a monorepo when we're not in one. - let targetRoot = ''; - const getTargetRoot = () => { - if (!targetRoot) { - targetRoot = findRootPath(targetDir); - } - return targetRoot; - }; - - return { - ownDir, - get ownRoot() { - return getOwnRoot(); - }, - targetDir, - get targetRoot() { - return getTargetRoot(); - }, - resolveOwn: (...paths) => resolvePath(ownDir, ...paths), - resolveOwnRoot: (...paths) => resolvePath(getOwnRoot(), ...paths), - resolveTarget: (...paths) => resolvePath(targetDir, ...paths), - resolveTargetRoot: (...paths) => resolvePath(getTargetRoot(), ...paths), - }; -} - -export const paths = findPaths(); +export const paths = findPaths(__dirname); From 0a09976548df83050d7890122bc3e7600281bc05 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 3 Aug 2020 18:18:51 +0200 Subject: [PATCH 3/7] create-app: use cli-common to discover paths and import version directly --- packages/create-app/package.json | 1 + packages/create-app/src/createApp.ts | 6 +- packages/create-app/src/index.ts | 4 +- packages/create-app/src/lib/paths.ts | 150 ------------------------- packages/create-app/src/lib/version.ts | 26 ----- 5 files changed, 7 insertions(+), 180 deletions(-) delete mode 100644 packages/create-app/src/lib/paths.ts delete mode 100644 packages/create-app/src/lib/version.ts diff --git a/packages/create-app/package.json b/packages/create-app/package.json index 79b14fc379..9cd9ee7920 100644 --- a/packages/create-app/package.json +++ b/packages/create-app/package.json @@ -25,6 +25,7 @@ "lint": "backstage-cli lint" }, "dependencies": { + "@backstage/cli-common": "^0.1.1-alpha.16", "commander": "^4.1.1", "chalk": "^4.0.0", "fs-extra": "^9.0.0", diff --git a/packages/create-app/src/createApp.ts b/packages/create-app/src/createApp.ts index b4564b1ff8..28e3805ce8 100644 --- a/packages/create-app/src/createApp.ts +++ b/packages/create-app/src/createApp.ts @@ -21,10 +21,10 @@ import { Command } from 'commander'; import inquirer, { Answers, Question } from 'inquirer'; import { exec as execCb } from 'child_process'; import { resolve as resolvePath } from 'path'; +import { findPaths } from '@backstage/cli-common'; +import { version } from '../package.json'; import os from 'os'; import { Task, templatingTask } from './lib/tasks'; -import { paths } from './lib/paths'; -import { version } from './lib/version'; const exec = promisify(execCb); @@ -88,6 +88,8 @@ async function moveApp(tempDir: string, destination: string, id: string) { } export default async (cmd: Command): Promise => { + const paths = findPaths(__dirname); + const questions: Question[] = [ { type: 'input', diff --git a/packages/create-app/src/index.ts b/packages/create-app/src/index.ts index 93c10c8c99..d8c0f55572 100644 --- a/packages/create-app/src/index.ts +++ b/packages/create-app/src/index.ts @@ -17,7 +17,7 @@ import program from 'commander'; import chalk from 'chalk'; import { exitWithError } from './lib/errors'; -import { version } from './lib/version'; +import { version } from '../package.json'; import createApp from './createApp'; const main = (argv: string[]) => { @@ -29,7 +29,7 @@ const main = (argv: string[]) => { '--skip-install', 'Skip the install and builds steps after creating the app', ) - .action(createApp) + .action(createApp); if (!process.argv.slice(2).length) { program.outputHelp(chalk.yellow); diff --git a/packages/create-app/src/lib/paths.ts b/packages/create-app/src/lib/paths.ts deleted file mode 100644 index be3f3dcee8..0000000000 --- a/packages/create-app/src/lib/paths.ts +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import fs from 'fs-extra'; -import { dirname, resolve as resolvePath } from 'path'; - -export type ResolveFunc = (...paths: string[]) => string; - -// Common paths and resolve functions used by the cli. -// Currently assumes it is being executed within a monorepo. -export type Paths = { - // Root dir of the cli itself, containing package.json - ownDir: string; - - // Monorepo root dir of the cli itself. Only accessible when running inside Backstage repo. - ownRoot: string; - - // The location of the app that the cli is being executed in - targetDir: string; - - // The monorepo root package of the app that the cli is being executed in. - targetRoot: string; - - // Resolve a path relative to own repo - resolveOwn: ResolveFunc; - - // Resolve a path relative to own monorepo root. Only accessible when running inside Backstage repo. - resolveOwnRoot: ResolveFunc; - - // Resolve a path relative to the app - resolveTarget: ResolveFunc; - - // Resolve a path relative to the app repo root - resolveTargetRoot: ResolveFunc; -}; - -// Looks for a package.json that has name: "root" to identify the root of the monorepo -export function findRootPath(topPath: string): string { - let path = topPath; - - // Some sanity check to avoid infinite loop - for (let i = 0; i < 1000; i++) { - const packagePath = resolvePath(path, '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 path; - } - } catch (error) { - throw new Error( - `Failed to parse package.json file while searching for root, ${error}`, - ); - } - } - - const newPath = dirname(path); - if (newPath === path) { - throw new Error( - `No package.json with name "root" found as a parent of ${topPath}`, - ); - } - path = newPath; - } - - throw new Error( - `Iteration limit reached when searching for root package.json at ${topPath}`, - ); -} - -// Finds the root of the cli package itself -export function findOwnDir() { - // Known relative locations of package in dist/dev - const pathDist = '..'; - const pathDev = '../..'; - - // Check the closest dir first - const pkgInDist = resolvePath(__dirname, pathDist, 'package.json'); - const isDist = fs.pathExistsSync(pkgInDist); - - const path = isDist ? pathDist : pathDev; - return resolvePath(__dirname, path); -} - -// Finds the root of the monorepo that the cli exists in. Only accessible when running inside Backstage repo. -export function findOwnRootPath(ownDir: string) { - const isLocal = fs.pathExistsSync(resolvePath(ownDir, 'src')); - if (!isLocal) { - throw new Error( - 'Tried to access monorepo package root dir outside of Backstage repository', - ); - } - - return resolvePath(ownDir, '../..'); -} - -export function findPaths(): Paths { - const ownDir = findOwnDir(); - const targetDir = fs.realpathSync(process.cwd()); - - // Lazy load this as it will throw an error if we're not inside the Backstage repo. - let ownRoot = ''; - const getOwnRoot = () => { - if (!ownRoot) { - ownRoot = findOwnRootPath(ownDir); - } - return ownRoot; - }; - - // We're not always running in a monorepo, so we lazy init this to only crash commands - // that require a monorepo when we're not in one. - let targetRoot = ''; - const getTargetRoot = () => { - if (!targetRoot) { - targetRoot = findRootPath(targetDir); - } - return targetRoot; - }; - - return { - ownDir, - get ownRoot() { - return getOwnRoot(); - }, - targetDir, - get targetRoot() { - return getTargetRoot(); - }, - resolveOwn: (...paths) => resolvePath(ownDir, ...paths), - resolveOwnRoot: (...paths) => resolvePath(getOwnRoot(), ...paths), - resolveTarget: (...paths) => resolvePath(targetDir, ...paths), - resolveTargetRoot: (...paths) => resolvePath(getTargetRoot(), ...paths), - }; -} - -export const paths = findPaths(); diff --git a/packages/create-app/src/lib/version.ts b/packages/create-app/src/lib/version.ts deleted file mode 100644 index 24734b87cc..0000000000 --- a/packages/create-app/src/lib/version.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import fs from 'fs-extra'; -import { paths } from './paths'; - -export function findVersion() { - const pkgContent = fs.readFileSync(paths.resolveOwn('package.json'), 'utf8'); - return JSON.parse(pkgContent).version; -} - -export const version = findVersion(); -export const isDev = fs.pathExistsSync(paths.resolveOwn('src')); From aa265769d2c364ee45c094cfca3f9d36fa22c8e5 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 3 Aug 2020 18:19:19 +0200 Subject: [PATCH 4/7] techdocs-cli: import version from techdocs-cli directly --- packages/techdocs-cli/src/index.ts | 2 +- packages/techdocs-cli/src/lib/paths.ts | 150 ----------------------- packages/techdocs-cli/src/lib/version.ts | 26 ---- 3 files changed, 1 insertion(+), 177 deletions(-) delete mode 100644 packages/techdocs-cli/src/lib/paths.ts delete mode 100644 packages/techdocs-cli/src/lib/version.ts diff --git a/packages/techdocs-cli/src/index.ts b/packages/techdocs-cli/src/index.ts index 1469089d52..b48f03d35b 100644 --- a/packages/techdocs-cli/src/index.ts +++ b/packages/techdocs-cli/src/index.ts @@ -15,7 +15,7 @@ */ import { spawn, ChildProcess } from 'child_process'; import program from 'commander'; -import { version } from './lib/version'; +import { version } from '../package.json'; import path from 'path'; import HTTPServer from './lib/httpServer'; import openBrowser from 'react-dev-utils/openBrowser'; diff --git a/packages/techdocs-cli/src/lib/paths.ts b/packages/techdocs-cli/src/lib/paths.ts deleted file mode 100644 index be3f3dcee8..0000000000 --- a/packages/techdocs-cli/src/lib/paths.ts +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import fs from 'fs-extra'; -import { dirname, resolve as resolvePath } from 'path'; - -export type ResolveFunc = (...paths: string[]) => string; - -// Common paths and resolve functions used by the cli. -// Currently assumes it is being executed within a monorepo. -export type Paths = { - // Root dir of the cli itself, containing package.json - ownDir: string; - - // Monorepo root dir of the cli itself. Only accessible when running inside Backstage repo. - ownRoot: string; - - // The location of the app that the cli is being executed in - targetDir: string; - - // The monorepo root package of the app that the cli is being executed in. - targetRoot: string; - - // Resolve a path relative to own repo - resolveOwn: ResolveFunc; - - // Resolve a path relative to own monorepo root. Only accessible when running inside Backstage repo. - resolveOwnRoot: ResolveFunc; - - // Resolve a path relative to the app - resolveTarget: ResolveFunc; - - // Resolve a path relative to the app repo root - resolveTargetRoot: ResolveFunc; -}; - -// Looks for a package.json that has name: "root" to identify the root of the monorepo -export function findRootPath(topPath: string): string { - let path = topPath; - - // Some sanity check to avoid infinite loop - for (let i = 0; i < 1000; i++) { - const packagePath = resolvePath(path, '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 path; - } - } catch (error) { - throw new Error( - `Failed to parse package.json file while searching for root, ${error}`, - ); - } - } - - const newPath = dirname(path); - if (newPath === path) { - throw new Error( - `No package.json with name "root" found as a parent of ${topPath}`, - ); - } - path = newPath; - } - - throw new Error( - `Iteration limit reached when searching for root package.json at ${topPath}`, - ); -} - -// Finds the root of the cli package itself -export function findOwnDir() { - // Known relative locations of package in dist/dev - const pathDist = '..'; - const pathDev = '../..'; - - // Check the closest dir first - const pkgInDist = resolvePath(__dirname, pathDist, 'package.json'); - const isDist = fs.pathExistsSync(pkgInDist); - - const path = isDist ? pathDist : pathDev; - return resolvePath(__dirname, path); -} - -// Finds the root of the monorepo that the cli exists in. Only accessible when running inside Backstage repo. -export function findOwnRootPath(ownDir: string) { - const isLocal = fs.pathExistsSync(resolvePath(ownDir, 'src')); - if (!isLocal) { - throw new Error( - 'Tried to access monorepo package root dir outside of Backstage repository', - ); - } - - return resolvePath(ownDir, '../..'); -} - -export function findPaths(): Paths { - const ownDir = findOwnDir(); - const targetDir = fs.realpathSync(process.cwd()); - - // Lazy load this as it will throw an error if we're not inside the Backstage repo. - let ownRoot = ''; - const getOwnRoot = () => { - if (!ownRoot) { - ownRoot = findOwnRootPath(ownDir); - } - return ownRoot; - }; - - // We're not always running in a monorepo, so we lazy init this to only crash commands - // that require a monorepo when we're not in one. - let targetRoot = ''; - const getTargetRoot = () => { - if (!targetRoot) { - targetRoot = findRootPath(targetDir); - } - return targetRoot; - }; - - return { - ownDir, - get ownRoot() { - return getOwnRoot(); - }, - targetDir, - get targetRoot() { - return getTargetRoot(); - }, - resolveOwn: (...paths) => resolvePath(ownDir, ...paths), - resolveOwnRoot: (...paths) => resolvePath(getOwnRoot(), ...paths), - resolveTarget: (...paths) => resolvePath(targetDir, ...paths), - resolveTargetRoot: (...paths) => resolvePath(getTargetRoot(), ...paths), - }; -} - -export const paths = findPaths(); diff --git a/packages/techdocs-cli/src/lib/version.ts b/packages/techdocs-cli/src/lib/version.ts deleted file mode 100644 index 24734b87cc..0000000000 --- a/packages/techdocs-cli/src/lib/version.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import fs from 'fs-extra'; -import { paths } from './paths'; - -export function findVersion() { - const pkgContent = fs.readFileSync(paths.resolveOwn('package.json'), 'utf8'); - return JSON.parse(pkgContent).version; -} - -export const version = findVersion(); -export const isDev = fs.pathExistsSync(paths.resolveOwn('src')); From 62222b543cf0ee9bd870cce5ba54ff7f3ab7b9dc Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 3 Aug 2020 18:21:03 +0200 Subject: [PATCH 5/7] config-loader,backend-common: refactor config loading to pass in path and use common util for backend --- packages/backend-common/package.json | 2 + .../src/config.ts} | 18 ++++-- packages/backend-common/src/index.ts | 1 + packages/backend/package.json | 1 - packages/backend/src/index.ts | 4 +- packages/cli/src/commands/app/build.ts | 3 +- packages/cli/src/commands/app/serve.ts | 3 +- packages/cli/src/commands/backend/dev.ts | 3 +- packages/cli/src/commands/plugin/export.ts | 3 +- packages/cli/src/commands/plugin/serve.ts | 3 +- packages/config-loader/src/lib/paths.ts | 57 ------------------- packages/config-loader/src/lib/resolver.ts | 14 +---- packages/config-loader/src/loader.ts | 6 +- .../packages/backend/package.json.hbs | 5 +- .../packages/backend/src/index.ts.hbs | 4 +- plugins/auth-backend/package.json | 1 - .../src/service/standaloneServer.ts | 9 ++- plugins/proxy-backend/package.json | 1 - .../proxy-backend/src/service/router.test.ts | 4 +- .../src/service/standaloneServer.ts | 8 ++- 20 files changed, 50 insertions(+), 100 deletions(-) rename packages/{config-loader/src/lib/paths.test.ts => backend-common/src/config.ts} (63%) delete mode 100644 packages/config-loader/src/lib/paths.ts diff --git a/packages/backend-common/package.json b/packages/backend-common/package.json index 18131236df..b8a4e372ab 100644 --- a/packages/backend-common/package.json +++ b/packages/backend-common/package.json @@ -29,7 +29,9 @@ "clean": "backstage-cli clean" }, "dependencies": { + "@backstage/cli-common": "^0.1.1-alpha.16", "@backstage/config": "^0.1.1-alpha.16", + "@backstage/config-loader": "^0.1.1-alpha.16", "@types/cors": "^2.8.6", "@types/express": "^4.17.6", "compression": "^1.7.4", diff --git a/packages/config-loader/src/lib/paths.test.ts b/packages/backend-common/src/config.ts similarity index 63% rename from packages/config-loader/src/lib/paths.test.ts rename to packages/backend-common/src/config.ts index dd5ed14748..8391ed4104 100644 --- a/packages/config-loader/src/lib/paths.test.ts +++ b/packages/backend-common/src/config.ts @@ -14,11 +14,17 @@ * limitations under the License. */ -import { findRootPath } from './paths'; +import { findPaths } from '@backstage/cli-common'; +import { loadConfig } from '@backstage/config-loader'; -describe('findRootPath', () => { - it('should find root path', () => { - const rootPath = findRootPath(process.cwd()); - expect(typeof rootPath).toBe('string'); +/** + * Load configuration for a Backend + */ +export async function loadBackendConfig() { + const paths = findPaths(__dirname); + const configs = await loadConfig({ + rootPath: paths.targetRoot, + shouldReadSecrets: true, }); -}); + return configs; +} diff --git a/packages/backend-common/src/index.ts b/packages/backend-common/src/index.ts index 0e4e5b2c6b..a3f57ed9df 100644 --- a/packages/backend-common/src/index.ts +++ b/packages/backend-common/src/index.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +export * from './config'; export * from './errors'; export * from './logging'; export * from './middleware'; diff --git a/packages/backend/package.json b/packages/backend/package.json index 7ca11fc636..1de51948d8 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -21,7 +21,6 @@ "@backstage/backend-common": "^0.1.1-alpha.16", "@backstage/catalog-model": "^0.1.1-alpha.16", "@backstage/config": "^0.1.1-alpha.16", - "@backstage/config-loader": "^0.1.1-alpha.16", "@backstage/plugin-auth-backend": "^0.1.1-alpha.16", "@backstage/plugin-catalog-backend": "^0.1.1-alpha.16", "@backstage/plugin-identity-backend": "^0.1.1-alpha.16", diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index 3fccfe9c59..261a4d2894 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -24,11 +24,11 @@ import { createServiceBuilder, + loadBackendConfig, getRootLogger, useHotMemoize, } from '@backstage/backend-common'; import { ConfigReader, AppConfig } from '@backstage/config'; -import { loadConfig } from '@backstage/config-loader'; import knex, { PgConnectionConfig } from 'knex'; import healthcheck from './plugins/healthcheck'; import auth from './plugins/auth'; @@ -85,7 +85,7 @@ function makeCreateEnv(loadedConfigs: AppConfig[]) { } async function main() { - const configs = await loadConfig({ shouldReadSecrets: true }); + const configs = await loadBackendConfig(); const configReader = ConfigReader.fromConfigs(configs); const createEnv = makeCreateEnv(configs); diff --git a/packages/cli/src/commands/app/build.ts b/packages/cli/src/commands/app/build.ts index 4ecf54f532..b3ec5ad2bf 100644 --- a/packages/cli/src/commands/app/build.ts +++ b/packages/cli/src/commands/app/build.ts @@ -17,10 +17,11 @@ import { Command } from 'commander'; import { loadConfig } from '@backstage/config-loader'; import { ConfigReader } from '@backstage/config'; +import { paths } from '../../lib/paths'; import { buildBundle } from '../../lib/bundler'; export default async (cmd: Command) => { - const appConfigs = await loadConfig(); + const appConfigs = await loadConfig({ rootPath: paths.targetRoot }); await buildBundle({ entry: 'src/index', statsJsonEnabled: cmd.stats, diff --git a/packages/cli/src/commands/app/serve.ts b/packages/cli/src/commands/app/serve.ts index 023a3a4663..732370ef3b 100644 --- a/packages/cli/src/commands/app/serve.ts +++ b/packages/cli/src/commands/app/serve.ts @@ -17,10 +17,11 @@ import { Command } from 'commander'; import { loadConfig } from '@backstage/config-loader'; import { ConfigReader } from '@backstage/config'; +import { paths } from '../../lib/paths'; import { serveBundle } from '../../lib/bundler'; export default async (cmd: Command) => { - const appConfigs = await loadConfig(); + const appConfigs = await loadConfig({ rootPath: paths.targetRoot }); const waitForExit = await serveBundle({ entry: 'src/index', checksEnabled: cmd.check, diff --git a/packages/cli/src/commands/backend/dev.ts b/packages/cli/src/commands/backend/dev.ts index f04f904e09..03d9464722 100644 --- a/packages/cli/src/commands/backend/dev.ts +++ b/packages/cli/src/commands/backend/dev.ts @@ -17,10 +17,11 @@ import { ConfigReader } from '@backstage/config'; import { loadConfig } from '@backstage/config-loader'; import { Command } from 'commander'; +import { paths } from '../../lib/paths'; import { serveBackend } from '../../lib/bundler/backend'; export default async (cmd: Command) => { - const appConfigs = await loadConfig(); + const appConfigs = await loadConfig({ rootPath: paths.targetRoot }); const waitForExit = await serveBackend({ entry: 'src/index', checksEnabled: cmd.check, diff --git a/packages/cli/src/commands/plugin/export.ts b/packages/cli/src/commands/plugin/export.ts index b6db0d8ddd..be6df707f3 100644 --- a/packages/cli/src/commands/plugin/export.ts +++ b/packages/cli/src/commands/plugin/export.ts @@ -17,10 +17,11 @@ import { Command } from 'commander'; import { loadConfig } from '@backstage/config-loader'; import { ConfigReader } from '@backstage/config'; +import { paths } from '../../lib/paths'; import { buildBundle } from '../../lib/bundler'; export default async (cmd: Command) => { - const appConfigs = await loadConfig(); + const appConfigs = await loadConfig({ rootPath: paths.targetRoot }); await buildBundle({ entry: 'dev/index', statsJsonEnabled: cmd.stats, diff --git a/packages/cli/src/commands/plugin/serve.ts b/packages/cli/src/commands/plugin/serve.ts index 57b939fa63..5ee4b70a1e 100644 --- a/packages/cli/src/commands/plugin/serve.ts +++ b/packages/cli/src/commands/plugin/serve.ts @@ -17,10 +17,11 @@ import { Command } from 'commander'; import { loadConfig } from '@backstage/config-loader'; import { ConfigReader } from '@backstage/config'; +import { paths } from '../../lib/paths'; import { serveBundle } from '../../lib/bundler'; export default async (cmd: Command) => { - const appConfigs = await loadConfig(); + const appConfigs = await loadConfig({ rootPath: paths.targetRoot }); const waitForExit = await serveBundle({ entry: 'dev/index', checksEnabled: cmd.check, diff --git a/packages/config-loader/src/lib/paths.ts b/packages/config-loader/src/lib/paths.ts deleted file mode 100644 index d0e7e96e36..0000000000 --- a/packages/config-loader/src/lib/paths.ts +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import fs from 'fs-extra'; -import { dirname, resolve as resolvePath } from 'path'; - -/** - * Looks for a package.json that has name: "root" to identify the root of the monorepo - * - * This is a copy of the same function in the CLI - */ -export function findRootPath(topPath: string): string { - let path = topPath; - - // Some sanity check to avoid infinite loop - for (let i = 0; i < 1000; i++) { - const packagePath = resolvePath(path, '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 path; - } - } catch (error) { - throw new Error( - `Failed to parse package.json file while searching for root, ${error}`, - ); - } - } - - const newPath = dirname(path); - if (newPath === path) { - throw new Error( - `No package.json with name "root" found as a parent of ${topPath}`, - ); - } - path = newPath; - } - - throw new Error( - `Iteration limit reached when searching for root package.json at ${topPath}`, - ); -} diff --git a/packages/config-loader/src/lib/resolver.ts b/packages/config-loader/src/lib/resolver.ts index 4a06f3ebca..a3921f07aa 100644 --- a/packages/config-loader/src/lib/resolver.ts +++ b/packages/config-loader/src/lib/resolver.ts @@ -14,13 +14,11 @@ * limitations under the License. */ -import fs from 'fs-extra'; import { resolve as resolvePath } from 'path'; -import { findRootPath } from './paths'; type ResolveOptions = { - // Same as configPath in LoadConfigOptions - configPath?: string; + // Root path for search for app-config.yaml + rootPath: string; }; /** @@ -31,13 +29,7 @@ export async function resolveStaticConfig( ): Promise { // TODO: We'll want this to be a bit more elaborate, probably adding configs for // specific env, and maybe local config for plugins. - let { configPath } = options; - if (!configPath) { - configPath = resolvePath( - findRootPath(fs.realpathSync(process.cwd())), - 'app-config.yaml', - ); - } + const configPath = resolvePath(options.rootPath, 'app-config.yaml'); return [configPath]; } diff --git a/packages/config-loader/src/loader.ts b/packages/config-loader/src/loader.ts index 1b6c8f34da..ce0e09e815 100644 --- a/packages/config-loader/src/loader.ts +++ b/packages/config-loader/src/loader.ts @@ -25,8 +25,8 @@ import { } from './lib'; export type LoadConfigOptions = { - // Config path, defaults to app-config.yaml in project root - configPath?: string; + // Root path for search for app-config.yaml + rootPath: string; // Whether to read secrets or omit them, defaults to false. shouldReadSecrets?: boolean; @@ -59,7 +59,7 @@ class Context { } export async function loadConfig( - options: LoadConfigOptions = {}, + options: LoadConfigOptions, ): Promise { const configs = []; diff --git a/packages/create-app/templates/default-app/packages/backend/package.json.hbs b/packages/create-app/templates/default-app/packages/backend/package.json.hbs index 93c076a967..a978349b02 100644 --- a/packages/create-app/templates/default-app/packages/backend/package.json.hbs +++ b/packages/create-app/templates/default-app/packages/backend/package.json.hbs @@ -19,8 +19,7 @@ "dependencies": { "@backstage/backend-common": "^{{version}}", "@backstage/catalog-model": "^{{version}}", - "@backstage/config": "^0.1.1-alpha.13", - "@backstage/config-loader": "^0.1.1-alpha.13", + "@backstage/config": "^{{version}}", "@backstage/plugin-auth-backend": "^{{version}}", "@backstage/plugin-catalog-backend": "^{{version}}", "@backstage/plugin-identity-backend": "^{{version}}", @@ -42,7 +41,7 @@ "winston": "^3.2.1" }, "devDependencies": { - "@backstage/cli": "^0.1.1-alpha.15", + "@backstage/cli": "^{{version}}", "@types/dockerode": "^2.5.32", "@types/express": "^4.17.6", "@types/express-serve-static-core": "^4.17.5", diff --git a/packages/create-app/templates/default-app/packages/backend/src/index.ts.hbs b/packages/create-app/templates/default-app/packages/backend/src/index.ts.hbs index 591fa1a28f..ec050970d7 100644 --- a/packages/create-app/templates/default-app/packages/backend/src/index.ts.hbs +++ b/packages/create-app/templates/default-app/packages/backend/src/index.ts.hbs @@ -24,11 +24,11 @@ import { createServiceBuilder, + loadBackendConfig, getRootLogger, useHotMemoize, } from '@backstage/backend-common'; import { ConfigReader, AppConfig } from '@backstage/config'; -import { loadConfig } from '@backstage/config-loader'; {{#if dbTypePG}} import knex, { PgConnectionConfig } from 'knex'; {{/if}} @@ -78,7 +78,7 @@ function makeCreateEnv(loadedConfigs: AppConfig[]) { } async function main() { - const configs = await loadConfig(); + const configs = await loadBackendConfig(); const configReader = ConfigReader.fromConfigs(configs); const createEnv = makeCreateEnv(configs); diff --git a/plugins/auth-backend/package.json b/plugins/auth-backend/package.json index b16c1e5a4f..a0a863618f 100644 --- a/plugins/auth-backend/package.json +++ b/plugins/auth-backend/package.json @@ -22,7 +22,6 @@ "dependencies": { "@backstage/backend-common": "^0.1.1-alpha.16", "@backstage/config": "^0.1.1-alpha.16", - "@backstage/config-loader": "^0.1.1-alpha.16", "@types/express": "^4.17.6", "body-parser": "^1.19.0", "compression": "^1.7.4", diff --git a/plugins/auth-backend/src/service/standaloneServer.ts b/plugins/auth-backend/src/service/standaloneServer.ts index d2372a7e91..dc91c92631 100644 --- a/plugins/auth-backend/src/service/standaloneServer.ts +++ b/plugins/auth-backend/src/service/standaloneServer.ts @@ -18,9 +18,12 @@ import Knex from 'knex'; import { Server } from 'http'; import { Logger } from 'winston'; import { ConfigReader } from '@backstage/config'; -import { loadConfig } from '@backstage/config-loader'; import { createRouter } from './router'; -import { createServiceBuilder, useHotMemoize } from '@backstage/backend-common'; +import { + createServiceBuilder, + useHotMemoize, + loadBackendConfig, +} from '@backstage/backend-common'; export interface ServerOptions { logger: Logger; @@ -30,7 +33,7 @@ export async function startStandaloneServer( options: ServerOptions, ): Promise { const logger = options.logger.child({ service: 'auth-backend' }); - const config = ConfigReader.fromConfigs(await loadConfig()); + const config = ConfigReader.fromConfigs(await loadBackendConfig()); const database = useHotMemoize(module, () => { const knex = Knex({ diff --git a/plugins/proxy-backend/package.json b/plugins/proxy-backend/package.json index e80bf0e00f..f65e3a7298 100644 --- a/plugins/proxy-backend/package.json +++ b/plugins/proxy-backend/package.json @@ -21,7 +21,6 @@ "dependencies": { "@backstage/backend-common": "^0.1.1-alpha.16", "@backstage/config": "^0.1.1-alpha.16", - "@backstage/config-loader": "^0.1.1-alpha.16", "@types/express": "^4.17.6", "express": "^4.17.1", "express-promise-router": "^3.0.3", diff --git a/plugins/proxy-backend/src/service/router.test.ts b/plugins/proxy-backend/src/service/router.test.ts index 699ed9b99d..10690dfbdd 100644 --- a/plugins/proxy-backend/src/service/router.test.ts +++ b/plugins/proxy-backend/src/service/router.test.ts @@ -17,12 +17,12 @@ import { createRouter } from './router'; import winston from 'winston'; import { ConfigReader } from '@backstage/config'; -import { loadConfig } from '@backstage/config-loader'; +import { loadBackendConfig } from '@backstage/backend-common'; describe('createRouter', () => { it('works', async () => { const logger = winston.createLogger(); - const config = ConfigReader.fromConfigs(await loadConfig()); + const config = ConfigReader.fromConfigs(await loadBackendConfig()); const router = await createRouter({ config, logger, diff --git a/plugins/proxy-backend/src/service/standaloneServer.ts b/plugins/proxy-backend/src/service/standaloneServer.ts index 84c803eeac..f75c044f0f 100644 --- a/plugins/proxy-backend/src/service/standaloneServer.ts +++ b/plugins/proxy-backend/src/service/standaloneServer.ts @@ -14,12 +14,14 @@ * limitations under the License. */ -import { createServiceBuilder } from '@backstage/backend-common'; +import { + createServiceBuilder, + loadBackendConfig, +} from '@backstage/backend-common'; import { Server } from 'http'; import { Logger } from 'winston'; import { createRouter } from './router'; import { ConfigReader } from '@backstage/config'; -import { loadConfig } from '@backstage/config-loader'; export interface ServerOptions { port: number; @@ -34,7 +36,7 @@ export async function startStandaloneServer( logger.debug('Creating application...'); - const config = ConfigReader.fromConfigs(await loadConfig()); + const config = ConfigReader.fromConfigs(await loadBackendConfig()); const router = await createRouter({ config, logger, From 843c6bf3a5f88cbff320623d96f372c745b1c4b8 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 4 Aug 2020 01:03:05 +0200 Subject: [PATCH 6/7] packages/cli: fix packaging of packages without deps --- packages/cli/src/lib/packager/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/lib/packager/index.ts b/packages/cli/src/lib/packager/index.ts index f4a2181b40..d65d1af304 100644 --- a/packages/cli/src/lib/packager/index.ts +++ b/packages/cli/src/lib/packager/index.ts @@ -136,7 +136,7 @@ async function findTargetPackages(pkgNames: string[]): Promise { // Don't include dependencies of packages that are marked as bundled if (!node.pkg.get('bundled')) { - const pkgDeps = Object.keys(node.pkg.dependencies); + const pkgDeps = Object.keys(node.pkg.dependencies ?? {}); const localDeps: string[] = Array.from(node.localDependencies.keys()); const filteredDeps = localDeps.filter(dep => pkgDeps.includes(dep)); From cd6febaf2dfdf2d219b4112b2ef5d97e31eb7da4 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 5 Aug 2020 11:34:17 +0200 Subject: [PATCH 7/7] cli: update e2e-test to include dist versions of backend packages --- packages/cli/e2e-test/cli-e2e-test.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/cli/e2e-test/cli-e2e-test.js b/packages/cli/e2e-test/cli-e2e-test.js index e9635de6bb..c90433db1d 100644 --- a/packages/cli/e2e-test/cli-e2e-test.js +++ b/packages/cli/e2e-test/cli-e2e-test.js @@ -73,6 +73,8 @@ async function buildDistWorkspace(workspaceName, rootDir) { '@backstage/core', '@backstage/dev-utils', '@backstage/test-utils', + // We don't use the backend itself, but want all of its dependencies + 'example-backend', ]); print('Pinning yarn version in workspace'); @@ -180,13 +182,15 @@ async function overrideModuleResolutions(appDir, workspaceDir) { pkgJson.resolutions = pkgJson.resolutions || {}; pkgJson.dependencies = pkgJson.dependencies || {}; - const packageNames = await fs.readdir(resolvePath(workspaceDir, 'packages')); - for (const name of packageNames) { - const pkgPath = joinPath('..', 'workspace', 'packages', name); + for (const dir of ['packages', 'plugins']) { + const packageNames = await fs.readdir(resolvePath(workspaceDir, dir)); + for (const name of packageNames) { + const pkgPath = joinPath('..', 'workspace', dir, name); - pkgJson.dependencies[`@backstage/${name}`] = `file:${pkgPath}`; - pkgJson.resolutions[`@backstage/${name}`] = `file:${pkgPath}`; - delete pkgJson.devDependencies[`@backstage/${name}`]; + pkgJson.dependencies[`@backstage/${name}`] = `file:${pkgPath}`; + pkgJson.resolutions[`@backstage/${name}`] = `file:${pkgPath}`; + delete pkgJson.devDependencies[`@backstage/${name}`]; + } } fs.writeJson(pkgJsonPath, pkgJson, { spaces: 2 }); }