Skip to content

Commit

Permalink
fix: make the export-dynamic-plugin command more generic for backen…
Browse files Browse the repository at this point in the history
…d plugins. (#967)

* fix: make the `export-dynamic-plugin` command more generic

for backend plugins.

Signed-off-by: David Festal <[email protected]>

* fix: other fixes to make CLI more robust.

Signed-off-by: David Festal <[email protected]>

---------

Signed-off-by: David Festal <[email protected]>
  • Loading branch information
davidfestal authored Nov 22, 2023
1 parent 30cc4b3 commit b40c661
Showing 1 changed file with 54 additions and 42 deletions.
96 changes: 54 additions & 42 deletions packages/cli/src/commands/export-dynamic-plugin/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@
*/

import { BackstagePackageJson, PackageRoleInfo } from '@backstage/cli-node';
import { ConfigReader } from '@backstage/config';
import { loadConfig } from '@backstage/config-loader';

import { getPackages } from '@manypkg/get-packages';
import { OptionValues } from 'commander';
import fs from 'fs-extra';
import { rollup } from 'rollup';
import * as semver from 'semver';

import { execSync } from 'child_process';
import path, { basename } from 'path';

import { Output } from '../../lib/builder';
import { makeRollupConfigs } from '../../lib/builder/config';
import { embedModules } from '../../lib/builder/embedPlugin';
import { buildPackage, formatErrorMessage } from '../../lib/builder/packager';
import { loadCliConfig } from '../../lib/config';
import { readEntryPoints } from '../../lib/entryPoints';
import { productionPack } from '../../lib/packager/productionPack';
import { paths } from '../../lib/paths';
import { Task } from '../../lib/tasks';
Expand All @@ -38,8 +41,8 @@ export async function backend(
opts: OptionValues,
): Promise<void> {
if (!fs.existsSync(paths.resolveTarget('src', 'dynamic'))) {
throw new Error(
`Package doesn't seem to support dynamic loading. It should have a src/dynamic folder, containing the dynamic loading entrypoints.`,
console.warn(
`Package doesn't seem to provide dynamic loading entrypoints. You might want to add dynamic loading entrypoints in a src/dynamic folder.`,
);
}

Expand All @@ -65,13 +68,18 @@ export async function backend(

const target = path.join(paths.targetDir, 'dist-dynamic');

if (
!pkg.files?.includes('dist-dynamic/*.*') ||
!pkg.files?.includes('dist-dynamic/dist/**') ||
!pkg.files?.includes('dist-dynamic/alpha/*')
) {
throw new Error(
`Package doesn't seem to support dynamic loading: its "files" property should include the following entries: ["dist-dynamic/*.*", "dist-dynamic/dist/**", "dist-dynamic/alpha/*"].`,
const requiredFiles = ['dist-dynamic/*.*', 'dist-dynamic/dist/**'];

const entryPoints = readEntryPoints(pkg);
if (entryPoints.find(e => e.mount === './alpha')) {
requiredFiles.push('dist-dynamic/alpha/*');
}

if (requiredFiles.some(f => !pkg.files?.includes(f))) {
console.warn(
`Package doesn't seem to fully support dynamic loading: its "files" property should include the following entries: [${requiredFiles
.map(f => `"${f}"`)
.join(', ')}].`,
);
}

Expand Down Expand Up @@ -332,9 +340,10 @@ export async function backend(
}

if (opts.install) {
const yarnInstall = `yarn install --production${
yarnLockExists ? ' --frozen-lockfile' : ''
}`;
const version = execSync('yarn --version').toString().trim();
const yarnInstall = version.startsWith('1.')
? `yarn install --production${yarnLockExists ? ' --frozen-lockfile' : ''}`
: `yarn install${yarnLockExists ? ' --immutable' : ''}`;

await Task.forCommand(yarnInstall, { cwd: target, optional: false });
await fs.remove(paths.resolveTarget('dist-dynamic', '.yarn'));
Expand All @@ -349,35 +358,38 @@ export async function backend(
});

if (opts.dev) {
if (opts.dev) {
const { fullConfig } = await loadCliConfig({ args: [] });
const dynamicPlugins = fullConfig.getOptional('dynamicPlugins');
if (
typeof dynamicPlugins === 'object' &&
dynamicPlugins !== null &&
'rootDirectory' in dynamicPlugins &&
typeof dynamicPlugins.rootDirectory === 'string'
) {
await fs.ensureSymlink(
paths.resolveTarget('src'),
path.resolve(target, 'src'),
'dir',
);
const dynamicPluginsRootPath = path.isAbsolute(
dynamicPlugins.rootDirectory,
)
? dynamicPlugins.rootDirectory
: paths.resolveTargetRoot(dynamicPlugins.rootDirectory);
await fs.ensureSymlink(
target,
path.resolve(dynamicPluginsRootPath, basename(paths.targetDir)),
'dir',
);
} else {
throw new Error(
`'dynamicPlugins.rootDirectory' should be configured in the app config in order to use the --dev option.`,
);
}
const appConfigs = await loadConfig({
configRoot: paths.targetRoot,
configTargets: [],
});
const fullConfig = ConfigReader.fromConfigs(appConfigs.appConfigs);

const dynamicPlugins = fullConfig.getOptional('dynamicPlugins');
if (
typeof dynamicPlugins === 'object' &&
dynamicPlugins !== null &&
'rootDirectory' in dynamicPlugins &&
typeof dynamicPlugins.rootDirectory === 'string'
) {
await fs.ensureSymlink(
paths.resolveTarget('src'),
path.resolve(target, 'src'),
'dir',
);
const dynamicPluginsRootPath = path.isAbsolute(
dynamicPlugins.rootDirectory,
)
? dynamicPlugins.rootDirectory
: paths.resolveTargetRoot(dynamicPlugins.rootDirectory);
await fs.ensureSymlink(
target,
path.resolve(dynamicPluginsRootPath, basename(paths.targetDir)),
'dir',
);
} else {
throw new Error(
`'dynamicPlugins.rootDirectory' should be configured in the app config in order to use the --dev option.`,
);
}
}
}

0 comments on commit b40c661

Please sign in to comment.