diff --git a/packages/repo-tools/src/commands/index.ts b/packages/repo-tools/src/commands/index.ts index 06ea2fa95c..290999687c 100644 --- a/packages/repo-tools/src/commands/index.ts +++ b/packages/repo-tools/src/commands/index.ts @@ -35,9 +35,13 @@ function registerPackageCommand(program: Command) { openApiCommand .command('init') - .description('Initialize any required files to use the OpenAPI tooling.') + .description( + 'Initialize any required files to use the OpenAPI tooling for this package.', + ) .action( - lazy(() => import('./package/schema/openapi/init').then(m => m.default)), + lazy(() => + import('./package/schema/openapi/init').then(m => m.singleCommand), + ), ); const generateCommand = openApiCommand diff --git a/packages/repo-tools/src/commands/package/schema/openapi/init.ts b/packages/repo-tools/src/commands/package/schema/openapi/init.ts index ab91aabecc..00e82f65df 100644 --- a/packages/repo-tools/src/commands/package/schema/openapi/init.ts +++ b/packages/repo-tools/src/commands/package/schema/openapi/init.ts @@ -14,14 +14,14 @@ * limitations under the License. */ import fs from 'fs-extra'; -import { join } from 'path'; import { YAML_SCHEMA_PATH } from '../../../../lib/openapi/constants'; - import { paths as cliPaths } from '../../../../lib/paths'; -import { runner } from '../../../../lib/runner'; import chalk from 'chalk'; import { exec } from '../../../../lib/exec'; -import { getPathToCurrentOpenApiSpec } from '../../../../lib/openapi/helpers'; +import { + getPathToCurrentOpenApiSpec, + getRelativePathToFile, +} from '../../../../lib/openapi/helpers'; const ROUTER_TEST_PATHS = [ 'src/service/router.test.ts', @@ -37,7 +37,7 @@ async function init() { ); } - const opticConfigFilePath = await getPathTo; + const opticConfigFilePath = await getRelativePathToFile('optic.yml'); if (await fs.pathExists(opticConfigFilePath)) { throw new Error(`This directory already has an optic.yml file. Exiting.`); } @@ -48,7 +48,9 @@ async function init() { capture: ${YAML_SCHEMA_PATH}: # 🔧 Runnable example with simple get requests. - # Run with "PORT=3000 optic capture ${YAML_SCHEMA_PATH} --update interactive" in '${directoryPath}' + # Run with "PORT=3000 optic capture ${YAML_SCHEMA_PATH} --update interactive" in '${ + cliPaths.targetDir + }' # You can change the server and the 'requests' section to experiment server: # This will not be used by 'backstage-repo-tools schema openapi test', but may be useful for interactive updates. @@ -67,27 +69,13 @@ capture: } } -export default async function initCommand(paths: string[] = []) { +export async function singleCommand() { try { await init(); + console.log(chalk.green(`Successfully configured.`)); } catch (err) { console.log(chalk.red(`OpenAPI tooling initialization failed.`)); console.log(err.message); - } - const resultsList = await runner(paths, dir => init(dir), { - concurrencyLimit: 5, - }); - - let failed = false; - for (const { relativeDir, resultText } of resultsList) { - if (resultText) { - failed = true; - } - } - - if (failed) { process.exit(1); - } else { - console.log(chalk.green(`All directories have already been configured.`)); } } diff --git a/packages/repo-tools/src/lib/openapi/helpers.ts b/packages/repo-tools/src/lib/openapi/helpers.ts index dda7f110a4..f7e684a407 100644 --- a/packages/repo-tools/src/lib/openapi/helpers.ts +++ b/packages/repo-tools/src/lib/openapi/helpers.ts @@ -17,24 +17,27 @@ import { pathExists } from 'fs-extra'; import { paths } from '../paths'; import { YAML_SCHEMA_PATH } from './constants'; -import { join, resolve } from 'path'; +import { resolve } from 'path'; export const getPathToFile = async (directory: string, filename: string) => { - const path = resolve(directory, filename); - if (!(await pathExists(path))) { - throw new Error(`Could not find ${join(directory, filename)}.`); - } - return path; + return resolve(directory, filename); }; export const getRelativePathToFile = async (filename: string) => { return await getPathToFile(paths.targetDir, filename); }; +export const assertExists = async (path: string) => { + if (!(await pathExists(path))) { + throw new Error(`Could not find ${path}.`); + } + return path; +}; + export const getPathToOpenApiSpec = async (directory: string) => { - return await getPathToFile(directory, YAML_SCHEMA_PATH); + return await assertExists(await getPathToFile(directory, YAML_SCHEMA_PATH)); }; export const getPathToCurrentOpenApiSpec = async () => { - return await getRelativePathToFile(YAML_SCHEMA_PATH); + return await assertExists(await getRelativePathToFile(YAML_SCHEMA_PATH)); };