cli: refactor pacakge detection

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: Johan Haals <johan.haals@gmail.com>
Co-authored-by: Camila Belo <camilaibs@gmail.com>
Co-authored-by: Philipp Hugenroth <philipph@spotify.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-09-05 15:04:29 +02:00
parent 9fa2fae3c2
commit 24cf05cb36
3 changed files with 31 additions and 25 deletions
+3 -4
View File
@@ -27,7 +27,7 @@ import { createConfig, resolveBaseUrl } from './config';
import { BuildOptions } from './types';
import { resolveBundlingPaths } from './paths';
import chalk from 'chalk';
import { writeDetectedPluginsModule } from './discover';
import { buildDetectedPlugins } from './discover';
// TODO(Rugvip): Limits from CRA, we might want to tweak these though.
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
@@ -50,10 +50,9 @@ export async function buildBundle(options: BuildOptions) {
getFrontendAppConfigs: () => options.frontendAppConfigs,
});
await writeDetectedPluginsModule({
await buildDetectedPlugins({
config: options.fullConfig,
entry: options.entry,
targetDir: options.targetDir,
targetPath: paths.targetPath,
});
const isCi = yn(process.env.CI, { default: false });
+27 -19
View File
@@ -16,38 +16,39 @@
import { BackstagePackageJson } from '@backstage/cli-node';
import { Config } from '@backstage/config';
import fs from 'fs-extra';
import path from 'path';
import chokidar from 'chokidar';
import fs from 'fs-extra';
import { join as joinPath, resolve as resolvePath } from 'path';
import { paths as cliPaths } from '../../lib/paths';
import { BundlingPathsOptions, resolveBundlingPaths } from './paths';
type Options = { config: Config; watch: () => void } & BundlingPathsOptions;
export async function buildDetectedPlugins(options: Options) {
const { entry, targetDir } = options;
const { targetPackageJson } = resolveBundlingPaths({ entry, targetDir });
if (!!options.watch) {
const watcher = chokidar.watch(targetPackageJson);
export async function buildDetectedPlugins(options: {
config: Config;
targetPath: string;
watch?: () => void;
}) {
const { watch, targetPath } = options;
if (watch) {
const watcher = chokidar.watch(resolvePath(targetPath, 'package.json'));
watcher.on('change', async () => {
await writeDetectedPluginsModule(options);
options.watch();
watch();
});
}
await writeDetectedPluginsModule(options);
}
async function writeDetectedPluginsModule(options: Options) {
async function writeDetectedPluginsModule(options: {
config: Config;
targetPath: string;
}) {
const requirePackageScript = (await detectPlugins(options))
?.map(pkg => `{name: '${pkg}', module: require('${pkg}')}`)
.join(',');
await fs.writeFile(
path.join(
joinPath(
cliPaths.targetRoot,
'node_modules',
'__backstage-autodetected-plugins__.js',
@@ -56,9 +57,16 @@ async function writeDetectedPluginsModule(options: Options) {
);
}
async function detectPlugins({ config, entry, targetDir }: Options) {
const paths = resolveBundlingPaths({ entry, targetDir });
const pkg: BackstagePackageJson = await fs.readJson(paths.targetPackageJson);
async function detectPlugins({
config,
targetPath,
}: {
config: Config;
targetPath: string;
}) {
const pkg: BackstagePackageJson = await fs.readJson(
resolvePath(targetPath, 'package.json'),
);
// TODO: proper
// Assumption for config string based on https://github.com/backstage/backstage/issues/18372 ^
@@ -74,7 +82,7 @@ async function detectPlugins({ config, entry, targetDir }: Options) {
.map(depName => {
const depPackageJson: BackstagePackageJson = require(require.resolve(
`${depName}/package.json`,
{ paths: [paths.targetPath] },
{ paths: [targetPath] },
));
if (
['frontend-plugin', 'frontend-plugin-module'].includes(
+1 -2
View File
@@ -133,8 +133,7 @@ export async function serveBundle(options: ServeOptions) {
await buildDetectedPlugins({
config: fullConfig,
entry: options.entry,
targetDir: options.targetDir,
targetPath: paths.targetPath,
watch() {
server?.invalidate();
},