diff --git a/.changeset/fancy-frogs-like.md b/.changeset/fancy-frogs-like.md new file mode 100644 index 0000000000..ed3a9fbf81 --- /dev/null +++ b/.changeset/fancy-frogs-like.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Internal code cleanup diff --git a/packages/cli/src/commands/start/command.ts b/packages/cli/src/commands/start/command.ts deleted file mode 100644 index addae78fb1..0000000000 --- a/packages/cli/src/commands/start/command.ts +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2020 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 fs from 'fs-extra'; -import { OptionValues } from 'commander'; -import { resolve as resolvePath } from 'node:path'; -import { PackageRole } from '@backstage/cli-node'; -import { findRoleFromCommand } from '../../lib/role'; -import { startBackend, startBackendPlugin } from './startBackend'; -import { startFrontend } from './startFrontend'; -import { ForwardedError } from '@backstage/errors'; - -export async function command(opts: OptionValues): Promise { - const role = await findRoleFromCommand(opts); - - if (opts.link) { - const dir = resolvePath(opts.link); - if (!fs.pathExistsSync(dir)) { - throw new Error( - `Invalid workspace link, directory does not exist: ${dir}`, - ); - } - const pkgJson = await fs - .readJson(resolvePath(dir, 'package.json')) - .catch(error => { - throw new ForwardedError( - 'Failed to read package.json in linked workspace', - error, - ); - }); - - if (!pkgJson.workspaces) { - throw new Error( - `Invalid workspace link, directory is not a workspace: ${dir}`, - ); - } - } - - const options = { - configPaths: opts.config as string[], - checksEnabled: Boolean(opts.check), - linkedWorkspace: opts.link, - inspectEnabled: opts.inspect, - inspectBrkEnabled: opts.inspectBrk, - require: opts.require, - }; - - switch (role) { - case 'backend': - return startBackend(options); - case 'backend-plugin': - case 'backend-plugin-module': - case 'node-library': - return startBackendPlugin(options); - case 'frontend': - return startFrontend({ - ...options, - entry: 'src/index', - verifyVersions: true, - }); - case 'web-library': - case 'frontend-plugin': - case 'frontend-plugin-module': - return startFrontend({ entry: 'dev/index', ...options }); - case 'frontend-dynamic-container' as PackageRole: // experimental - return startFrontend({ - entry: 'src/index', - ...options, - skipOpenBrowser: true, - isModuleFederationRemote: true, - }); - default: - throw new Error( - `Start command is not supported for package role '${role}'`, - ); - } -} diff --git a/packages/cli/src/commands/start/index.ts b/packages/cli/src/commands/start/index.ts deleted file mode 100644 index 680fe9e11d..0000000000 --- a/packages/cli/src/commands/start/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright 2022 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. - */ - -export { command } from './command'; diff --git a/packages/cli/src/commands/start/startBackend.ts b/packages/cli/src/commands/start/startBackend.ts deleted file mode 100644 index 84f9eb0890..0000000000 --- a/packages/cli/src/commands/start/startBackend.ts +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2020 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 fs from 'fs-extra'; -import { paths } from '../../lib/paths'; -import { runBackend } from '../../lib/runner'; - -interface StartBackendOptions { - checksEnabled: boolean; - inspectEnabled: boolean; - inspectBrkEnabled: boolean; - linkedWorkspace?: string; - require?: string; -} - -export async function startBackend(options: StartBackendOptions) { - const waitForExit = await runBackend({ - entry: 'src/index', - inspectEnabled: options.inspectEnabled, - inspectBrkEnabled: options.inspectBrkEnabled, - linkedWorkspace: options.linkedWorkspace, - require: options.require, - }); - - await waitForExit(); -} - -export async function startBackendPlugin(options: StartBackendOptions) { - const hasDevIndexEntry = await fs.pathExists( - paths.resolveTarget('dev', 'index.ts'), - ); - if (!hasDevIndexEntry) { - console.warn( - `The 'dev' directory is missing. Please create a proper dev/index.ts in order to start the plugin.`, - ); - return; - } - - const waitForExit = await runBackend({ - entry: 'dev/index', - inspectEnabled: options.inspectEnabled, - inspectBrkEnabled: options.inspectBrkEnabled, - require: options.require, - linkedWorkspace: options.linkedWorkspace, - }); - - await waitForExit(); -} diff --git a/packages/cli/src/commands/start/startFrontend.ts b/packages/cli/src/commands/start/startFrontend.ts deleted file mode 100644 index e5b3b0dfbc..0000000000 --- a/packages/cli/src/commands/start/startFrontend.ts +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2020 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 { readJson } from 'fs-extra'; -import { resolve as resolvePath } from 'path'; -import { - getModuleFederationOptions, - serveBundle, -} from '../../modules/build/lib/bundler'; -import { paths } from '../../lib/paths'; -import { BackstagePackageJson } from '@backstage/cli-node'; - -interface StartAppOptions { - verifyVersions?: boolean; - entry: string; - - checksEnabled: boolean; - configPaths: string[]; - skipOpenBrowser?: boolean; - isModuleFederationRemote?: boolean; - linkedWorkspace?: string; -} - -export async function startFrontend(options: StartAppOptions) { - const packageJson = (await readJson( - paths.resolveTarget('package.json'), - )) as BackstagePackageJson; - - const waitForExit = await serveBundle({ - entry: options.entry, - checksEnabled: options.checksEnabled, - configPaths: options.configPaths, - verifyVersions: options.verifyVersions, - skipOpenBrowser: options.skipOpenBrowser, - linkedWorkspace: options.linkedWorkspace, - moduleFederation: await getModuleFederationOptions( - packageJson, - resolvePath(paths.targetDir), - options.isModuleFederationRemote, - ), - }); - - await waitForExit(); -} diff --git a/packages/cli/src/commands/repo/optionsParser.ts b/packages/cli/src/lib/optionsParser.ts similarity index 100% rename from packages/cli/src/commands/repo/optionsParser.ts rename to packages/cli/src/lib/optionsParser.ts diff --git a/packages/cli/src/modules/build/commands/repo/build.ts b/packages/cli/src/modules/build/commands/repo/build.ts index 44fbc13887..d0b9de8867 100644 --- a/packages/cli/src/modules/build/commands/repo/build.ts +++ b/packages/cli/src/modules/build/commands/repo/build.ts @@ -27,7 +27,7 @@ import { import { runParallelWorkers } from '../../../../lib/parallel'; import { buildFrontend } from '../../lib/buildFrontend'; import { buildBackend } from '../../lib/buildBackend'; -import { createScriptOptionsParser } from '../../../../commands/repo/optionsParser'; +import { createScriptOptionsParser } from '../../../../lib/optionsParser'; export async function command(opts: OptionValues, cmd: Command): Promise { let packages = await PackageGraph.listTargetPackages(); diff --git a/packages/cli/src/modules/lint/commands/repo/lint.ts b/packages/cli/src/modules/lint/commands/repo/lint.ts index ab6597c70a..72bb02a693 100644 --- a/packages/cli/src/modules/lint/commands/repo/lint.ts +++ b/packages/cli/src/modules/lint/commands/repo/lint.ts @@ -26,7 +26,7 @@ import { } from '@backstage/cli-node'; import { paths } from '../../../../lib/paths'; import { runWorkerQueueThreads } from '../../../../lib/parallel'; -import { createScriptOptionsParser } from '../../../../commands/repo/optionsParser'; +import { createScriptOptionsParser } from '../../../../lib/optionsParser'; import { SuccessCache } from '../../../../lib/cache/SuccessCache'; function depCount(pkg: BackstagePackageJson) {