cli: experimental support for using embedded-postgres as dev DB
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com> Made-with: Cursor
This commit is contained in:
@@ -65,6 +65,7 @@
|
||||
"cross-spawn": "^7.0.3",
|
||||
"css-loader": "^6.5.1",
|
||||
"ctrlc-windows": "^2.1.0",
|
||||
"embedded-postgres": "^17.2.0-beta.15",
|
||||
"esbuild-loader": "^4.0.0",
|
||||
"eslint-rspack-plugin": "^4.2.1",
|
||||
"eslint-webpack-plugin": "^4.2.0",
|
||||
@@ -77,6 +78,7 @@
|
||||
"node-stdlib-browser": "^1.3.1",
|
||||
"npm-packlist": "^5.0.0",
|
||||
"p-queue": "^6.6.2",
|
||||
"portfinder": "^1.0.32",
|
||||
"postcss": "^8.1.0",
|
||||
"postcss-import": "^16.1.0",
|
||||
"process": "^0.11.10",
|
||||
|
||||
@@ -24,6 +24,7 @@ import { isAbsolute as isAbsolutePath } from 'node:path';
|
||||
import { targetPaths } from '@backstage/cli-common';
|
||||
|
||||
import spawn from 'cross-spawn';
|
||||
import { startEmbeddedDb } from './startEmbeddedDb';
|
||||
|
||||
const loaderArgs = [
|
||||
'--enable-source-maps',
|
||||
@@ -57,6 +58,16 @@ export async function runBackend(options: RunBackendOptions) {
|
||||
const server = new IpcServer();
|
||||
ServerDataStore.bind(server);
|
||||
|
||||
const extraEnv: Record<string, string> = {};
|
||||
|
||||
if (process.env.EXPERIMENTAL_DEV_DB) {
|
||||
const db = await startEmbeddedDb();
|
||||
extraEnv.APP_CONFIG_backend_database = JSON.stringify({
|
||||
client: 'pg',
|
||||
connection: db.connection,
|
||||
});
|
||||
}
|
||||
|
||||
let exiting = false;
|
||||
let firstStart = true;
|
||||
let child: ChildProcess | undefined;
|
||||
@@ -134,6 +145,7 @@ export async function runBackend(options: RunBackendOptions) {
|
||||
cwd: options.targetDir,
|
||||
env: {
|
||||
...process.env,
|
||||
...extraEnv,
|
||||
BACKSTAGE_CLI_LINKED_WORKSPACE: options.linkedWorkspace,
|
||||
BACKSTAGE_CLI_CHANNEL: '1',
|
||||
ESBK_TSCONFIG_PATH: targetPaths.resolveRoot('tsconfig.json'),
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2024 The Backstage Authors
|
||||
*
|
||||
* 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 os from 'node:os';
|
||||
import fs from 'fs-extra';
|
||||
import { resolve as resolvePath } from 'path';
|
||||
import { getPortPromise } from 'portfinder';
|
||||
|
||||
export async function startEmbeddedDb() {
|
||||
const { default: EmbeddedPostgres } = await import('embedded-postgres').catch(
|
||||
error => {
|
||||
throw new Error(
|
||||
`Failed to load peer dependency 'embedded-postgres' for generating SQL reports. ` +
|
||||
`It must be installed as an explicit dependency in your project. Caused by; ${error}`,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
const host = 'localhost';
|
||||
const user = 'postgres';
|
||||
const password = 'password';
|
||||
const port = await getPortPromise();
|
||||
const tmpDir = await fs.mkdtemp(
|
||||
resolvePath(os.tmpdir(), 'backstage-dev-db-'),
|
||||
);
|
||||
const pg = new EmbeddedPostgres({
|
||||
databaseDir: tmpDir,
|
||||
user,
|
||||
password,
|
||||
port,
|
||||
persistent: false,
|
||||
onError(_messageOrError) {},
|
||||
onLog(_message) {},
|
||||
});
|
||||
|
||||
// Create the cluster config files
|
||||
await pg.initialise();
|
||||
|
||||
// Start the server
|
||||
await pg.start();
|
||||
|
||||
return {
|
||||
connection: {
|
||||
host,
|
||||
user,
|
||||
password,
|
||||
port,
|
||||
},
|
||||
async close() {
|
||||
await pg.stop();
|
||||
await fs.rmdir(tmpDir, { recursive: true, maxRetries: 3 });
|
||||
},
|
||||
};
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2024 The Backstage Authors
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// It's missing a types entry point, but has types in dist
|
||||
declare module 'embedded-postgres' {
|
||||
export { default } from 'embedded-postgres/dist/index';
|
||||
}
|
||||
Reference in New Issue
Block a user