From 59a3b58805569426bc114bac30d50713a98aa38b Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Fri, 12 Nov 2021 17:25:24 +0100 Subject: [PATCH] fix(techdocs-cli): cross-platform build script Co-authored-by: Emma Indal Signed-off-by: Camila Belo --- package.json | 5 ++- packages/techdocs-cli/package.json | 1 - packages/techdocs-cli/scripts/build.sh | 45 ++++++++++--------- .../techdocs-cli/src/commands/serve/serve.ts | 4 +- packages/techdocs-cli/src/e2e.test.ts | 14 +++--- packages/techdocs-cli/src/lib/mkdocsServer.ts | 1 + scripts/techdocs-cli.js | 25 +++++++++++ 7 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 scripts/techdocs-cli.js diff --git a/package.json b/package.json index c0c68add37..d63790554f 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,8 @@ "lerna": "lerna", "storybook": "yarn workspace storybook start", "build-storybook": "yarn workspace storybook build-storybook", - "techdocs-cli": "yarn workspace @techdocs/cli build && packages/techdocs-cli/bin/techdocs-cli", - "techdocs-cli:dev": "yarn workspace @techdocs/cli build:dev && TECHDOCS_CLI_DEV_MODE=true packages/techdocs-cli/bin/techdocs-cli", + "techdocs-cli": "node scripts/techdocs-cli.js", + "techdocs-cli:dev": "cross-env TECHDOCS_CLI_DEV_MODE=true node scripts/techdocs-cli.js", "prepare": "husky install", "lock:check": "yarn-lock-check" }, @@ -63,6 +63,7 @@ "@spotify/prettier-config": "^11.0.0", "@types/webpack": "^5.28.0", "command-exists": "^1.2.9", + "cross-env": "^7.0.0", "concurrently": "^6.0.0", "eslint-plugin-notice": "^0.9.10", "fs-extra": "9.1.0", diff --git a/packages/techdocs-cli/package.json b/packages/techdocs-cli/package.json index 5367f0a151..a7ac3c3d80 100644 --- a/packages/techdocs-cli/package.json +++ b/packages/techdocs-cli/package.json @@ -22,7 +22,6 @@ "scripts": { "start": "nodemon --", "build": "./scripts/build.sh", - "build:dev": "TECHDOCS_CLI_DEV_MODE=true yarn build", "clean": "backstage-cli clean", "lint": "backstage-cli lint", "test": "backstage-cli test --testPathIgnorePatterns src/e2e.test.ts", diff --git a/packages/techdocs-cli/scripts/build.sh b/packages/techdocs-cli/scripts/build.sh index 7aec589b13..630fef42f0 100755 --- a/packages/techdocs-cli/scripts/build.sh +++ b/packages/techdocs-cli/scripts/build.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Copyright 2020 The Backstage Authors +# Copyright 2021 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. @@ -16,26 +16,29 @@ set -e -# Build the TechDocs CLI -npx backstage-cli -- build --outputs cjs +SCRIPT_DIR=$(dirname $0) +TECHDOCS_CLI_DIR="$SCRIPT_DIR"/.. +TECHDOCS_CLI_EMBEDDED_APP_DIR="$TECHDOCS_CLI_DIR"/../embedded-techdocs-app -# Make sure to do `yarn run build` in packages/embedded-techdocs before building here. +compile_and_build_cli() { + echo "📄 Compiling..." + yarn workspace @techdocs/cli tsc > /dev/null + echo "📦️ Building..." + pushd $TECHDOCS_CLI_DIR > /dev/null + npx backstage-cli build --outputs cjs > /dev/null + popd > /dev/null +} -EMBEDDED_TECHDOCS_APP_PATH=../embedded-techdocs-app -TECHDOCS_PREVIEW_SOURCE=$EMBEDDED_TECHDOCS_APP_PATH/dist -TECHDOCS_PREVIEW_DEST=dist/techdocs-preview-bundle +build_and_embed_app() { + echo "🚚 Embedding app..." + if [ "$TECHDOCS_CLI_DEV_MODE" = "true" ] ; then + yarn workspace embedded-techdocs-app build:dev > /dev/null + else + yarn workspace embedded-techdocs-app build > /dev/null + fi + cp -r "$TECHDOCS_CLI_EMBEDDED_APP_DIR"/dist "$TECHDOCS_CLI_DIR"/dist/techdocs-preview-bundle > /dev/null +} -# Build the embedded-techdocs-app -pushd $EMBEDDED_TECHDOCS_APP_PATH >/dev/null -if [[ $TECHDOCS_CLI_DEV_MODE == "true" ]]; then - yarn build:dev -else - yarn build -fi -popd >/dev/null - -cp -r $TECHDOCS_PREVIEW_SOURCE $TECHDOCS_PREVIEW_DEST - -# Write to console -echo "[techdocs-cli]: Built the dist/ folder" -echo "[techdocs-cli]: Imported @backstage/plugin-techdocs dist/ folder into techdocs-preview-bundle/" +compile_and_build_cli +build_and_embed_app +echo "🏁 Ready!" diff --git a/packages/techdocs-cli/src/commands/serve/serve.ts b/packages/techdocs-cli/src/commands/serve/serve.ts index 5aaf601c72..5fed2dd56e 100644 --- a/packages/techdocs-cli/src/commands/serve/serve.ts +++ b/packages/techdocs-cli/src/commands/serve/serve.ts @@ -77,8 +77,8 @@ export default async function serve(cmd: Command) { // Wait until mkdocs server has started so that Backstage starts with docs loaded // Takes 1-5 seconds - for (let attempt = 0; attempt < 10; attempt++) { - await new Promise(r => setTimeout(r, 1000)); + for (let attempt = 0; attempt < 30; attempt++) { + await new Promise(r => setTimeout(r, 3000)); if (mkdocsServerHasStarted) { break; } diff --git a/packages/techdocs-cli/src/e2e.test.ts b/packages/techdocs-cli/src/e2e.test.ts index 45fa112bde..9dbbf58431 100644 --- a/packages/techdocs-cli/src/e2e.test.ts +++ b/packages/techdocs-cli/src/e2e.test.ts @@ -22,7 +22,7 @@ const FIXTURE_DIR = path.resolve(PROJECT_ROOT_DIR, 'src/fixture'); describe('end-to-end', () => { it('shows help text', async () => { - jest.setTimeout(10000); + jest.setTimeout(30000); const proc = await executeTechDocsCliCommand(['--help']); expect(proc.combinedStdOutErr).toContain('Usage: techdocs-cli [options]'); @@ -30,10 +30,10 @@ describe('end-to-end', () => { }); it('can generate', async () => { - jest.setTimeout(10000); + jest.setTimeout(30000); const proc = await executeTechDocsCliCommand(['generate', '--no-docker'], { cwd: FIXTURE_DIR, - killAfter: 8000, + killAfter: 16000, }); expect(proc.combinedStdOutErr).toContain('Successfully generated docs'); @@ -41,12 +41,12 @@ describe('end-to-end', () => { }); it('can serve in mkdocs', async () => { - jest.setTimeout(10000); + jest.setTimeout(30000); const proc = await executeTechDocsCliCommand( ['serve:mkdocs', '--no-docker'], { cwd: FIXTURE_DIR, - killAfter: 8000, + killAfter: 16000, }, ); @@ -55,10 +55,10 @@ describe('end-to-end', () => { }); it('can serve in backstage', async () => { - jest.setTimeout(10000); + jest.setTimeout(30000); const proc = await executeTechDocsCliCommand(['serve', '--no-docker'], { cwd: FIXTURE_DIR, - killAfter: 8000, + killAfter: 16000, }); expect(proc.combinedStdOutErr).toContain('Starting mkdocs server'); diff --git a/packages/techdocs-cli/src/lib/mkdocsServer.ts b/packages/techdocs-cli/src/lib/mkdocsServer.ts index 30e9b46451..60efcfaa89 100644 --- a/packages/techdocs-cli/src/lib/mkdocsServer.ts +++ b/packages/techdocs-cli/src/lib/mkdocsServer.ts @@ -40,6 +40,7 @@ export const runMkdocsServer = async (options: { `${process.cwd()}:/content`, '-p', `${port}:${port}`, + '-it', dockerImage, 'serve', '--dev-addr', diff --git a/scripts/techdocs-cli.js b/scripts/techdocs-cli.js new file mode 100644 index 0000000000..90dcc40f39 --- /dev/null +++ b/scripts/techdocs-cli.js @@ -0,0 +1,25 @@ +#!/usr/bin/env node +/* + * Copyright 2021 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. + */ + +const { execSync } = require('child_process'); + +const args = process.argv.slice(2); + +execSync(`yarn workspace @techdocs/cli build`, { stdio: 'inherit' }); +execSync(`yarn workspace @techdocs/cli link`, { stdio: 'ignore' }); +execSync(`techdocs-cli ${args.join(' ')}`, { stdio: 'inherit' }); +execSync(`yarn workspace @techdocs/cli unlink`, { stdio: 'ignore' });