├── .changeset ├── README.md └── config.json ├── .editorconfig ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── custom.md │ └── feature_request.md ├── dependabot.yml └── workflows │ ├── ci.yml │ ├── main.yml │ └── project-board.yml ├── .gitignore ├── .husky └── pre-commit ├── .prettierignore ├── LICENSE ├── README.md ├── app-config.dev.yaml ├── app-config.yaml ├── dist-types └── packages │ └── techdocs-cli │ └── src │ ├── commands │ ├── generate │ │ └── generate.d.ts │ ├── index.d.ts │ ├── publish │ │ └── publish.d.ts │ └── serve │ │ └── serve.d.ts │ ├── e2e.test.d.ts │ └── lib │ ├── mkdocsServer.d.ts │ └── mkdocsServer.test.d.ts ├── docs ├── README.md └── assets │ └── techdocs-cli-serve-preview.png ├── lerna.json ├── mkdocs.yml ├── package.json ├── packages ├── embedded-techdocs-app │ ├── .eslintrc.js │ ├── cypress.json │ ├── cypress │ │ ├── .eslintrc.json │ │ └── integration │ │ │ └── app.js │ ├── package.json │ ├── public │ │ ├── android-chrome-192x192.png │ │ ├── apple-touch-icon.png │ │ ├── favicon-16x16.png │ │ ├── favicon-32x32.png │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── manifest.json │ │ ├── robots.txt │ │ └── safari-pinned-tab.svg │ └── src │ │ ├── App.test.tsx │ │ ├── App.tsx │ │ ├── apis.ts │ │ ├── components │ │ ├── Root │ │ │ ├── LogoFull.tsx │ │ │ ├── LogoIcon.tsx │ │ │ ├── Root.tsx │ │ │ └── index.ts │ │ └── TechDocsPage │ │ │ ├── TechDocsPage.tsx │ │ │ └── index.ts │ │ ├── index.tsx │ │ ├── plugins.ts │ │ └── setupTests.ts └── techdocs-cli │ ├── .eslintrc.js │ ├── CHANGELOG.md │ ├── README.md │ ├── bin │ └── techdocs-cli │ ├── package.json │ ├── scripts │ └── build.sh │ └── src │ ├── commands │ ├── generate │ │ └── generate.ts │ ├── index.ts │ ├── migrate │ │ └── migrate.ts │ ├── publish │ │ └── publish.ts │ └── serve │ │ ├── mkdocs.ts │ │ └── serve.ts │ ├── e2e.test.ts │ ├── index.ts │ └── lib │ ├── PublisherConfig.test.ts │ ├── PublisherConfig.ts │ ├── httpServer.ts │ ├── mkdocsServer.test.ts │ ├── mkdocsServer.ts │ ├── run.ts │ └── utility.ts ├── tsconfig.json └── yarn.lock /.changeset/README.md: -------------------------------------------------------------------------------- 1 | # Changesets 2 | 3 | Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works 4 | with multi-package repos, or single-package repos to help you version and publish your code. You can 5 | find the full documentation for it [in our repository](https://github.com/changesets/changesets) 6 | 7 | We have a quick list of common questions to get you started engaging with this project in 8 | [our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) 9 | -------------------------------------------------------------------------------- /.changeset/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://unpkg.com/@changesets/config@1.6.0/schema.json", 3 | "changelog": "@changesets/cli/changelog", 4 | "commit": false, 5 | "linked": [], 6 | "access": "restricted", 7 | "baseBranch": "main", 8 | "updateInternalDependencies": "patch", 9 | "ignore": [] 10 | } 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | charset = utf-8 11 | indent_style = space 12 | 13 | [*.html] 14 | indent_style = space 15 | indent_size = 2 16 | 17 | [*.{ts,json,js,tsx,jsx}] 18 | indent_style = space 19 | indent_size = 2 20 | 21 | [*.md] 22 | indent_size = 2 23 | indent_style = space 24 | 25 | [Dockerfile] 26 | indent_style = space 27 | indent_size = 2 28 | 29 | [*.{yml,yaml}] 30 | indent_size = 2 31 | indent_style = space 32 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # This file registers ownership for certain parts of the backstage code. 2 | # Review from a member of the corresponding code owner is required to merge pull requests. 3 | # 4 | # The last matching pattern takes precedence. 5 | # https://help.github.com/articles/about-codeowners/ 6 | 7 | * @backstage/techdocs-core -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 'Bug Report' 3 | about: 'Create Bug Report' 4 | labels: bug 5 | --- 6 | 7 | 8 | 9 | ## Expected Behavior 10 | 11 | 12 | 13 | ## Current Behavior 14 | 15 | 16 | 17 | ## Possible Solution 18 | 19 | 20 | 21 | 22 | ## Steps to Reproduce 23 | 24 | 25 | 26 | 27 | 1. Step 1 28 | 2. Step 2 29 | 3. ... 30 | 31 | ## Context 32 | 33 | 34 | 35 | 36 | 37 | ## Your Environment 38 | 39 | 40 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/custom.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Custom issue template 3 | about: Describe this issue template's purpose here. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 'Feature Request' 3 | about: 'Suggest new features and changes' 4 | labels: enhancement 5 | --- 6 | 7 | 8 | 9 | ## Feature Suggestion 10 | 11 | 12 | 13 | ## Possible Implementation 14 | 15 | 16 | 17 | ## Context 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # Dependabot version updates configuration. 2 | # See the documentation for all configuration options: 3 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 4 | 5 | version: 2 6 | updates: 7 | - package-ecosystem: "npm" 8 | directory: "/" 9 | schedule: 10 | interval: "weekly" 11 | day: "sunday" 12 | 13 | - package-ecosystem: "npm" 14 | directory: "/packages/embedded-techdocs-app" 15 | schedule: 16 | interval: "weekly" 17 | day: "sunday" 18 | 19 | - package-ecosystem: "npm" 20 | directory: "/packages/techdocs-cli" 21 | schedule: 22 | interval: "weekly" 23 | day: "sunday" 24 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: pull_request 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | matrix: 11 | node-version: [12.x, 14.x] 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - uses: actions/setup-python@v2 16 | 17 | - name: yarn install 18 | run: yarn install --frozen-lockfile 19 | 20 | - name: yarn lint 21 | run: yarn lint 22 | 23 | - name: yarn tsc 24 | run: yarn tsc 25 | 26 | - name: yarn build 27 | run: yarn build 28 | 29 | - name: Install mkdocs 30 | run: python -m pip install mkdocs 31 | 32 | - name: Install mkdocs-techdocs-core 33 | run: python -m pip install mkdocs-techdocs-core 34 | 35 | - name: yarn test 36 | run: yarn test 37 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Main Build 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | 7 | jobs: 8 | release: 9 | name: Release 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout Repo 13 | uses: actions/checkout@master 14 | with: 15 | # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits 16 | fetch-depth: 0 17 | 18 | - name: Setup Node.js 12.x 19 | uses: actions/setup-node@master 20 | with: 21 | node-version: 12.x 22 | registry-url: https://registry.npmjs.org/ # Needed for auth 23 | 24 | - name: Install Dependencies 25 | run: yarn install --frozen-lockfile 26 | 27 | - name: Compile Files 28 | run: yarn tsc 29 | 30 | - name: Build Artifacts 31 | run: yarn build 32 | 33 | - name: Create Release Pull Request or Publish to npm 34 | id: changesets 35 | uses: changesets/action@master 36 | with: 37 | publish: yarn release 38 | env: 39 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 40 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 41 | -------------------------------------------------------------------------------- /.github/workflows/project-board.yml: -------------------------------------------------------------------------------- 1 | name: Automatically add new TechDocs Issues and PRs to the GitHub project board 2 | # Development of TechDocs in Backstage is managed by this Kanban board - https://github.com/orgs/backstage/projects/1 3 | # New issues and PRs in this repository will be added to the board. 4 | # Caveat: New PRs created from forks will not be added since GitHub actions don't share credentials with forks. 5 | # Inspired by https://github.com/backstage/backstage/blob/master/.github/workflows/techdocs-project-board.yml 6 | 7 | on: 8 | issues: 9 | types: [opened, reopened] 10 | pull_request: 11 | types: [opened, reopened] 12 | branches-ignore: ['dependabot/*'] 13 | 14 | env: 15 | MY_GITHUB_TOKEN: ${{ secrets.GH_SERVICE_ACCOUNT_TOKEN }} 16 | 17 | jobs: 18 | assign_issue_or_pr_to_project: 19 | runs-on: ubuntu-latest 20 | name: Triage 21 | steps: 22 | - name: Assign new issue or PR to Incoming 23 | uses: srggrs/assign-one-project-github-action@1.2.0 24 | with: 25 | project: 'https://github.com/orgs/backstage/projects/1' 26 | column_name: 'Incoming' 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | dist-types 85 | 86 | # Gatsby files 87 | .cache/ 88 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 89 | # https://nextjs.org/blog/next-9-1#public-directory-support 90 | # public 91 | 92 | # vuepress build output 93 | .vuepress/dist 94 | 95 | # Serverless directories 96 | .serverless/ 97 | 98 | # FuseBox cache 99 | .fusebox/ 100 | 101 | # DynamoDB Local files 102 | .dynamodb/ 103 | 104 | # TernJS port file 105 | .tern-port 106 | 107 | .DS_Store 108 | 109 | # TechDocs-specific stuff 110 | site/ 111 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | yarn lint-staged 5 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | .yarn 2 | dist 3 | coverage 4 | *.hbs 5 | templates 6 | .vscode 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # techdocs-cli 2 | 3 | Development of the TechDocs CLI has moved back to the [backstage/backstage](https://github.com/backstage/backstage) monorepo. 4 | 5 | See you there, friends! 6 | -------------------------------------------------------------------------------- /app-config.dev.yaml: -------------------------------------------------------------------------------- 1 | # Usages inherit from app-config.yaml 2 | 3 | backend: 4 | baseUrl: http://localhost:7000 5 | 6 | techdocs: 7 | requestUrl: http://localhost:7000/api 8 | -------------------------------------------------------------------------------- /app-config.yaml: -------------------------------------------------------------------------------- 1 | app: 2 | title: Techdocs Preview App 3 | baseUrl: http://localhost:3000 4 | 5 | backend: 6 | baseUrl: http://localhost:3000 7 | 8 | techdocs: 9 | builder: 'external' 10 | requestUrl: http://localhost:3000/api 11 | -------------------------------------------------------------------------------- /dist-types/packages/techdocs-cli/src/commands/generate/generate.d.ts: -------------------------------------------------------------------------------- 1 | import { Command } from "commander"; 2 | export default function generate(cmd: Command): Promise; 3 | -------------------------------------------------------------------------------- /dist-types/packages/techdocs-cli/src/commands/index.d.ts: -------------------------------------------------------------------------------- 1 | import { CommanderStatic } from "commander"; 2 | export declare function registerCommands(program: CommanderStatic): void; 3 | -------------------------------------------------------------------------------- /dist-types/packages/techdocs-cli/src/commands/publish/publish.d.ts: -------------------------------------------------------------------------------- 1 | import { Command } from "commander"; 2 | export default function publish(cmd: Command): Promise; 3 | -------------------------------------------------------------------------------- /dist-types/packages/techdocs-cli/src/commands/serve/serve.d.ts: -------------------------------------------------------------------------------- 1 | import { Command } from "commander"; 2 | export default function serve(cmd: Command): Promise; 3 | -------------------------------------------------------------------------------- /dist-types/packages/techdocs-cli/src/e2e.test.d.ts: -------------------------------------------------------------------------------- 1 | export {}; 2 | -------------------------------------------------------------------------------- /dist-types/packages/techdocs-cli/src/lib/mkdocsServer.d.ts: -------------------------------------------------------------------------------- 1 | import { ChildProcess } from "child_process"; 2 | import { LogFunc } from "./run"; 3 | export declare const runMkdocsServer: (options: { 4 | port?: string; 5 | useDocker?: boolean; 6 | dockerImage?: string; 7 | stdoutLogFunc?: LogFunc; 8 | stderrLogFunc?: LogFunc; 9 | }) => Promise; 10 | -------------------------------------------------------------------------------- /dist-types/packages/techdocs-cli/src/lib/mkdocsServer.test.d.ts: -------------------------------------------------------------------------------- 1 | export {}; 2 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # TechDocs CLI 2 | 3 | Utility command line interface for managing TechDocs sites in [Backstage](https://github.com/backstage/backstage). 4 | 5 | https://backstage.io/docs/features/techdocs/techdocs-overview 6 | 7 | ## Features 8 | 9 | - Supports local development/preview of a TechDocs site in a Backstage app. 10 | - Supports generation and publishing of a documentation site in a CI/CD workflow. 11 | 12 | ```bash 13 | techdocs-cli --help 14 | Usage: techdocs-cli [options] [command] 15 | 16 | Options: 17 | -V, --version output the version number 18 | -h, --help display help for command 19 | 20 | Commands: 21 | generate|build [options] Generate TechDocs documentation site using mkdocs. 22 | publish [options] Publish generated TechDocs site to an external storage AWS S3, 23 | Google GCS, etc. 24 | serve:mkdocs [options] Serve a documentation project locally using mkdocs serve. 25 | serve [options] Serve a documentation project locally in a Backstage app-like 26 | environment 27 | help [command] display help for command 28 | ``` 29 | 30 | ## Installation 31 | 32 | You can always use [`npx`](https://github.com/npm/npx) to run the latest version of `techdocs-cli` - 33 | 34 | ```bash 35 | npx @techdocs/cli [command] 36 | ``` 37 | 38 | Or you can install it using [npm](https://www.npmjs.com/package/@techdocs/cli) - 39 | 40 | ```bash 41 | npm install -g @techdocs/cli 42 | techdocs-cli [command] 43 | ``` 44 | 45 | ## Usage 46 | 47 | ### Preview TechDocs site locally in a Backstage like environment 48 | 49 | ```bash 50 | techdocs-cli serve 51 | ``` 52 | 53 | ![A preview of techdocs-cli serve command](assets/techdocs-cli-serve-preview.png) 54 | 55 | By default, Docker and [techdocs-container](https://github.com/backstage/techdocs-container) is used to make sure all the dependencies are installed. However, Docker can be disabled with `--no-docker` flag. 56 | 57 | The command starts two local servers - an MkDocs preview server on port 8000 and a Backstage app server on port 3000. The Backstage app has a custom TechDocs API implementation, which uses the MkDocs preview server as a proxy to fetch the generated documentation files and assets. 58 | 59 | NOTE: When using a custom `techdocs` docker image, make sure the entry point is also `ENTRYPOINT ["mkdocs"]`. 60 | 61 | Command reference: 62 | 63 | ```bash 64 | Usage: techdocs-cli serve [options] 65 | 66 | Serve a documentation project locally in a Backstage app-like environment 67 | 68 | Options: 69 | -i, --docker-image The mkdocs docker container to use (default: "spotify/techdocs") 70 | --no-docker Do not use Docker, use MkDocs executable in current user environment. 71 | --mkdocs-port Port for MkDocs server to use (default: "8000") 72 | -v --verbose Enable verbose output. (default: false) 73 | -h, --help display help for command 74 | ``` 75 | 76 | ### Generate TechDocs site from a documentation project 77 | 78 | ```bash 79 | techdocs-cli generate 80 | ``` 81 | 82 | Alias: `techdocs-cli build` 83 | 84 | The generate command uses the [`@backstage/techdocs-common`](https://github.com/backstage/backstage/tree/master/packages/techdocs-common) package from Backstage for consistency. A Backstage app can also generate and publish TechDocs sites if `techdocs.builder` is set to `'local'` in `app-config.yaml`. See [configuration reference](https://backstage.io/docs/features/techdocs/configuration). 85 | 86 | By default, this command uses Docker and [techdocs-container](https://github.com/backstage/techdocs-container) to make sure all the dependencies are installed. But it can be disabled using `--no-docker` flag. 87 | 88 | Command reference: 89 | 90 | ```bash 91 | techdocs-cli generate --help 92 | Usage: techdocs-cli generate|build [options] 93 | 94 | Generate TechDocs documentation site using MkDocs. 95 | 96 | Options: 97 | --source-dir Source directory containing mkdocs.yml and docs/ directory. (default: ".") 98 | --output-dir Output directory containing generated TechDocs site. (default: "./site/") 99 | --docker-image The mkdocs docker container to use (default: "spotify/techdocs") 100 | --no-pull Do not pull the latest docker image 101 | --no-docker Do not use Docker, use MkDocs executable and plugins in current user environment. 102 | --techdocs-ref The repository hosting documentation source files e.g. github:https://ghe.mycompany.net.com/org/repo. 103 | This value is same as the backstage.io/techdocs-ref annotation of the corresponding Backstage entity. 104 | It is completely fine to skip this as it is only being used to set repo_url in mkdocs.yml if not found. 105 | --etag A unique identifier for the prepared tree e.g. commit SHA. If provided it will be stored in 106 | techdocs_metadata.json. 107 | -v --verbose Enable verbose output. (default: false) 108 | -h, --help display help for command 109 | ``` 110 | 111 | ### Publish generated TechDocs sites 112 | 113 | ```bash 114 | techdocs-cli publish --publisher-type --storage-name --entity 115 | ``` 116 | 117 | After generating a TechDocs site using `techdocs-cli generate`, use the publish command to upload the static generated files on a cloud storage 118 | (AWS/GCS) bucket or (Azure) container which your Backstage app can read from. 119 | 120 | The value for `--entity` must be the Backstage entity which the generated TechDocs site belongs to. You can find the values in your Entity's `catalog-info.yaml` file. If namespace is missing in the `catalog-info.yaml`, use `default`. 121 | The directory structure used in the storage bucket is `namespace/kind/name/`. 122 | 123 | Note that the values are case-sensitive. An example for `--entity` is `default/Component/`. 124 | 125 | Command reference: 126 | 127 | ```bash 128 | Usage: techdocs-cli publish [options] 129 | 130 | Publish generated TechDocs site to an external storage AWS S3, Google GCS, etc. 131 | 132 | Options: 133 | --publisher-type (Required always) awsS3 | googleGcs | azureBlobStorage 134 | - same as techdocs.publisher.type in Backstage 135 | app-config.yaml 136 | --storage-name (Required always) In case of AWS/GCS, use the bucket 137 | name. In case of Azure, use container name. Same as 138 | techdocs.publisher.[TYPE].bucketName 139 | --entity (Required always) Entity uid separated by / in 140 | namespace/kind/name order (case-sensitive). Example: 141 | default/Component/myEntity 142 | --legacyUseCaseSensitiveTripletPaths Publishes objects with cased entity triplet prefix when set (e.g. namespace/Kind/name). 143 | Only use if your TechDocs backend is configured the same way 144 | --azureAccountName (Required for Azure) specify when --publisher-type 145 | azureBlobStorage 146 | --azureAccountKey Azure Storage Account key to use for authentication. 147 | If not specified, you must set AZURE_TENANT_ID, 148 | AZURE_CLIENT_ID & AZURE_CLIENT_SECRET as environment 149 | variables. 150 | --awsRoleArn Optional AWS ARN of role to be assumed. 151 | --awsEndpoint Optional AWS endpoint to send requests to. 152 | --awsS3ForcePathStyle Optional AWS S3 option to force path style. 153 | --directory Path of the directory containing generated files to 154 | publish (default: "./site/") 155 | -h, --help display help for command 156 | ``` 157 | 158 | ### Migrate content for case-insensitive access 159 | 160 | Prior to the beta version of TechDocs (`v[0.11.0]`), TechDocs were stored in 161 | object storage using a case-sensitive entity triplet (e.g. 162 | `default/API/name/index.html`). This resulted in a limitation where that exact 163 | case was required in the Backstage URL in order to read/render TechDocs 164 | content. As of `v[0.11.0]` of the TechDocs plugin, any case is allowed in the 165 | URL (e.g. `default/api/name`), matching the behavior of the Catalog plugin. 166 | 167 | Backstage instances created with TechDocs `v[0.11.0]` or later do not need this 168 | command. However, when upgrading to this version from an older version of 169 | TechDocs, the `migrate` command can be used prior to deployment to ensure docs 170 | remain accessible without having to rebuild all docs. 171 | 172 | Prior to upgrading to `v[0.11.0]` or greater, run this command to copy all 173 | assets to their lower-case triplet equivalents like this: 174 | 175 | ```bash 176 | techdocs-cli migrate --publisher-type --storage-name --verbose 177 | ``` 178 | 179 | Once migrated and the upgraded version of the Backstage plugin has been 180 | deployed, you can clean up the legacy, case-sensitive triplet files by 181 | re-running the command with the `--removeOriginal` flag passed, which _moves_ 182 | (rather than copies) the files. Note: this deletes files and is therefore a 183 | destructive operation that should performed with caution. 184 | 185 | ```bash 186 | techdocs-cli migrate --publisher-type --storage-name --removeOriginal --verbose 187 | ``` 188 | 189 | Afterward, update your TechDocs CLI to `v[0.7.0]` to ensure further publishing 190 | happens using a lower-case entity triplet. 191 | 192 | Note: arguments for this command largely match those of the `publish` command, 193 | depending on your chosen storage provider. Run `techdocs-cli migrate --help` 194 | for details. 195 | 196 | #### Authentication 197 | 198 | You need to make sure that your environment is able to authenticate with the target cloud provider. `techdocs-cli` uses the official Node.js clients provided by AWS (v2), Google Cloud and Azure. You can authenticate using 199 | environment variables and/or by other means (`~/.aws/credentials`, `~/.config/gcloud` etc.) 200 | 201 | Refer to the Authentication section of the following documentation depending upon your cloud storage provider - 202 | 203 | - [Google Cloud Storage](https://backstage.io/docs/features/techdocs/using-cloud-storage#configuring-google-gcs-bucket-with-techdocs) 204 | - [AWS S3](https://backstage.io/docs/features/techdocs/using-cloud-storage#configuring-aws-s3-bucket-with-techdocs) 205 | - [Azure Blob Storage](https://backstage.io/docs/features/techdocs/using-cloud-storage#configuring-azure-blob-storage-container-with-techdocs) 206 | 207 | ## Development 208 | 209 | You are welcome to contribute to TechDocs CLI to improve it and support new 210 | features! See the project [README](https://github.com/backstage/techdocs-cli/blob/main/README.md) 211 | for more information. 212 | -------------------------------------------------------------------------------- /docs/assets/techdocs-cli-serve-preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/backstage/techdocs-cli/0ff64625175f5f548ac25387c9d8792fd4603585/docs/assets/techdocs-cli-serve-preview.png -------------------------------------------------------------------------------- /lerna.json: -------------------------------------------------------------------------------- 1 | { 2 | "packages": ["packages/*"], 3 | "npmClient": "yarn", 4 | "useWorkspaces": true, 5 | "version": "0.0.0" 6 | } 7 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: techdocs-cli 2 | site_description: Documentation for the TechDocs CLI. 3 | 4 | nav: 5 | - TechDocs CLI: README.md 6 | 7 | plugins: 8 | - techdocs-core 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@techdocs/monorepo", 3 | "description": "See packages/techdocs-cli", 4 | "version": "0.0.0", 5 | "private": true, 6 | "publishConfig": { 7 | "access": "public" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/backstage/techdocs-cli", 12 | "directory": "." 13 | }, 14 | "scripts": { 15 | "start": "yarn workspace @techdocs/cli run start", 16 | "start:app": "yarn workspace embedded-techdocs-app run start", 17 | "cli": "packages/techdocs-cli/bin/techdocs-cli", 18 | "cli:dev": "TECHDOCS_CLI_DEV_MODE=true packages/techdocs-cli/bin/techdocs-cli", 19 | "build": "lerna run build", 20 | "clean": "lerna run clean", 21 | "lint": "lerna run lint", 22 | "test": "lerna run test -- --coverage", 23 | "tsc": "tsc", 24 | "changeset": "changeset add", 25 | "release": "changeset publish", 26 | "prepare": "husky install" 27 | }, 28 | "keywords": [ 29 | "backstage", 30 | "techdocs" 31 | ], 32 | "license": "Apache-2.0", 33 | "workspaces": { 34 | "packages": [ 35 | "packages/*", 36 | "plugins/*" 37 | ] 38 | }, 39 | "devDependencies": { 40 | "@backstage/cli": "^0.7.8", 41 | "@changesets/cli": "^2.16.0", 42 | "@spotify/prettier-config": "^10.0.0", 43 | "husky": "^7.0.1", 44 | "lerna": "^3.20.2", 45 | "lint-staged": "^11.1.1", 46 | "prettier": "^2.3.2" 47 | }, 48 | "prettier": "@spotify/prettier-config", 49 | "lint-staged": { 50 | "*.{js,jsx,ts,tsx}": [ 51 | "eslint --fix", 52 | "prettier --write" 53 | ], 54 | "*.{json,md}": [ 55 | "prettier --write" 56 | ] 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [require.resolve('@backstage/cli/config/eslint')], 3 | }; 4 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/cypress.json: -------------------------------------------------------------------------------- 1 | { 2 | "baseUrl": "http://localhost:3001", 3 | "fixturesFolder": false, 4 | "pluginsFile": false 5 | } 6 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/cypress/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["cypress"], 3 | "extends": ["plugin:cypress/recommended"], 4 | "rules": { 5 | "jest/expect-expect": [ 6 | "error", 7 | { 8 | "assertFunctionNames": ["expect", "cy.contains"] 9 | } 10 | ], 11 | "import/no-extraneous-dependencies": [ 12 | "error", 13 | { 14 | "devDependencies": true, 15 | "optionalDependencies": true, 16 | "peerDependencies": true, 17 | "bundledDependencies": true 18 | } 19 | ] 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/cypress/integration/app.js: -------------------------------------------------------------------------------- 1 | describe('App', () => { 2 | it('should render the catalog', () => { 3 | cy.visit('/'); 4 | cy.contains('My Company Service Catalog'); 5 | }); 6 | }); 7 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "embedded-techdocs-app", 3 | "version": "0.0.0", 4 | "private": true, 5 | "bundled": true, 6 | "dependencies": { 7 | "@backstage/catalog-model": "^0.9.0", 8 | "@backstage/cli": "^0.7.8", 9 | "@backstage/config": "^0.1.6", 10 | "@backstage/core": "^0.7.14", 11 | "@backstage/core-components": "^0.7.0", 12 | "@backstage/integration-react": "^0.1.7", 13 | "@backstage/plugin-catalog": "^0.7.1", 14 | "@backstage/plugin-techdocs": "^0.12.2", 15 | "@backstage/test-utils": "^0.1.17", 16 | "@backstage/theme": "^0.2.9", 17 | "@material-ui/core": "^4.11.0", 18 | "@material-ui/icons": "^4.9.1", 19 | "history": "^5.0.0", 20 | "react": "^16.13.1", 21 | "react-dom": "^16.13.1", 22 | "react-router": "6.0.0-beta.0", 23 | "react-router-dom": "6.0.0-beta.0", 24 | "react-use": "^17.2.4" 25 | }, 26 | "devDependencies": { 27 | "@backstage/cli": "^0.7.8", 28 | "@testing-library/jest-dom": "^5.10.1", 29 | "@testing-library/react": "^12.0.0", 30 | "@testing-library/user-event": "^13.2.1", 31 | "@types/jest": "^27.0.2", 32 | "@types/node": "^16.7.1", 33 | "@types/react-dom": "^17.0.9", 34 | "cross-env": "^7.0.0", 35 | "cypress": "^8.3.1", 36 | "eslint-plugin-cypress": "^2.10.3", 37 | "start-server-and-test": "^1.10.11" 38 | }, 39 | "scripts": { 40 | "start": "backstage-cli app:serve --config ../../app-config.yaml --config ../../app-config.dev.yaml", 41 | "build": "backstage-cli app:build --config ../../app-config.yaml", 42 | "clean": "backstage-cli clean", 43 | "test": "backstage-cli test", 44 | "lint": "backstage-cli lint", 45 | "test:e2e": "cross-env PORT=3001 start-server-and-test start http://localhost:3001 cy:dev", 46 | "test:e2e:ci": "cross-env PORT=3001 start-server-and-test start http://localhost:3001 cy:run", 47 | "cy:dev": "cypress open", 48 | "cy:run": "cypress run" 49 | }, 50 | "prettier": "@spotify/prettier-config", 51 | "browserslist": { 52 | "production": [ 53 | ">0.2%", 54 | "not dead", 55 | "not op_mini all" 56 | ], 57 | "development": [ 58 | "last 1 chrome version", 59 | "last 1 firefox version", 60 | "last 1 safari version" 61 | ] 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/public/android-chrome-192x192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/backstage/techdocs-cli/0ff64625175f5f548ac25387c9d8792fd4603585/packages/embedded-techdocs-app/public/android-chrome-192x192.png -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/public/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/backstage/techdocs-cli/0ff64625175f5f548ac25387c9d8792fd4603585/packages/embedded-techdocs-app/public/apple-touch-icon.png -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/public/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/backstage/techdocs-cli/0ff64625175f5f548ac25387c9d8792fd4603585/packages/embedded-techdocs-app/public/favicon-16x16.png -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/public/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/backstage/techdocs-cli/0ff64625175f5f548ac25387c9d8792fd4603585/packages/embedded-techdocs-app/public/favicon-32x32.png -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/backstage/techdocs-cli/0ff64625175f5f548ac25387c9d8792fd4603585/packages/embedded-techdocs-app/public/favicon.ico -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 16 | 21 | 22 | 23 | 28 | 34 | 40 | 45 | 50 | <%= app.title %> 51 | 52 | 53 | 54 |
55 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "Backstage", 3 | "name": "Backstage", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "48x48", 8 | "type": "image/png" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/public/safari-pinned-tab.svg: -------------------------------------------------------------------------------- 1 | Created by potrace 1.11, written by Peter Selinger 2001-2013 -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/App.test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { renderWithEffects } from '@backstage/test-utils'; 3 | import App from './App'; 4 | 5 | describe('App', () => { 6 | it('should render', async () => { 7 | process.env = { 8 | NODE_ENV: 'test', 9 | APP_CONFIG: [ 10 | { 11 | data: { 12 | app: { title: 'Test' }, 13 | backend: { baseUrl: 'http://localhost:7000' }, 14 | techdocs: { 15 | storageUrl: 'http://localhost:7000/api/techdocs/static/docs', 16 | }, 17 | }, 18 | context: 'test', 19 | }, 20 | ] as any, 21 | }; 22 | 23 | const rendered = await renderWithEffects(); 24 | expect(rendered.baseElement).toBeInTheDocument(); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/App.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Navigate, Route } from 'react-router'; 3 | import { createApp, FlatRoutes } from '@backstage/core'; 4 | import { CatalogEntityPage } from '@backstage/plugin-catalog'; 5 | 6 | import { 7 | DefaultTechDocsHome, 8 | TechDocsIndexPage, 9 | TechDocsReaderPage, 10 | } from '@backstage/plugin-techdocs'; 11 | import { apis } from './apis'; 12 | import { Root } from './components/Root'; 13 | import { techDocsPage } from './components/TechDocsPage'; 14 | import * as plugins from './plugins'; 15 | 16 | const app = createApp({ 17 | apis, 18 | plugins: Object.values(plugins), 19 | }); 20 | 21 | const AppProvider = app.getProvider(); 22 | const AppRouter = app.getRouter(); 23 | 24 | const routes = ( 25 | 26 | 27 | {/* we need this route as TechDocs header links relies on it */} 28 | } 31 | /> 32 | }> 33 | 34 | 35 | } 38 | > 39 | {techDocsPage} 40 | 41 | 42 | ); 43 | 44 | const App = () => ( 45 | 46 | 47 | {routes} 48 | 49 | 50 | ); 51 | 52 | export default App; 53 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/apis.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { EntityName } from '@backstage/catalog-model'; 18 | import { Config } from '@backstage/config'; 19 | import { 20 | scmIntegrationsApiRef, 21 | ScmIntegrationsApi, 22 | } from '@backstage/integration-react'; 23 | import { 24 | AnyApiFactory, 25 | configApiRef, 26 | createApiFactory, 27 | DiscoveryApi, 28 | discoveryApiRef, 29 | IdentityApi, 30 | identityApiRef, 31 | } from '@backstage/core'; 32 | import { 33 | SyncResult, 34 | TechDocsApi, 35 | techdocsApiRef, 36 | TechDocsStorageApi, 37 | techdocsStorageApiRef, 38 | } from '@backstage/plugin-techdocs'; 39 | 40 | // TODO: Export type from plugin-techdocs and import this here 41 | // import { ParsedEntityId } from '@backstage/plugin-techdocs' 42 | 43 | /** 44 | * Note: Override TechDocs API to use local mkdocs server instead of techdocs-backend. 45 | */ 46 | 47 | class TechDocsDevStorageApi implements TechDocsStorageApi { 48 | public configApi: Config; 49 | public discoveryApi: DiscoveryApi; 50 | public identityApi: IdentityApi; 51 | 52 | constructor({ 53 | configApi, 54 | discoveryApi, 55 | identityApi, 56 | }: { 57 | configApi: Config; 58 | discoveryApi: DiscoveryApi; 59 | identityApi: IdentityApi; 60 | }) { 61 | this.configApi = configApi; 62 | this.discoveryApi = discoveryApi; 63 | this.identityApi = identityApi; 64 | } 65 | 66 | async getApiOrigin() { 67 | return ( 68 | this.configApi.getOptionalString('techdocs.requestUrl') ?? 69 | (await this.discoveryApi.getBaseUrl('techdocs')) 70 | ); 71 | } 72 | 73 | async getStorageUrl() { 74 | return ( 75 | this.configApi.getOptionalString('techdocs.storageUrl') ?? 76 | `${await this.discoveryApi.getBaseUrl('techdocs')}/static/docs` 77 | ); 78 | } 79 | 80 | async getBuilder() { 81 | return this.configApi.getString('techdocs.builder'); 82 | } 83 | 84 | async getEntityDocs(_entityId: EntityName, path: string) { 85 | const apiOrigin = await this.getApiOrigin(); 86 | // Irrespective of the entity, use mkdocs server to find the file for the path. 87 | const url = `${apiOrigin}/${path}`; 88 | 89 | const request = await fetch( 90 | `${url.endsWith('/') ? url : `${url}/`}index.html`, 91 | ); 92 | 93 | if (request.status === 404) { 94 | throw new Error('Page not found'); 95 | } 96 | 97 | return request.text(); 98 | } 99 | 100 | async syncEntityDocs(_: EntityName): Promise { 101 | // this is just stub of this function as we don't need to check if docs are up to date, 102 | // we always want to retrigger a new build 103 | return 'cached'; 104 | } 105 | 106 | // Used by transformer to modify the request to assets (CSS, Image) from inside the HTML. 107 | async getBaseUrl( 108 | oldBaseUrl: string, 109 | _entityId: EntityName, 110 | path: string, 111 | ): Promise { 112 | const apiOrigin = await this.getApiOrigin(); 113 | return new URL(oldBaseUrl, `${apiOrigin}/${path}`).toString(); 114 | } 115 | } 116 | 117 | class TechDocsDevApi implements TechDocsApi { 118 | public configApi: Config; 119 | public discoveryApi: DiscoveryApi; 120 | public identityApi: IdentityApi; 121 | 122 | constructor({ 123 | configApi, 124 | discoveryApi, 125 | identityApi, 126 | }: { 127 | configApi: Config; 128 | discoveryApi: DiscoveryApi; 129 | identityApi: IdentityApi; 130 | }) { 131 | this.configApi = configApi; 132 | this.discoveryApi = discoveryApi; 133 | this.identityApi = identityApi; 134 | } 135 | 136 | async getApiOrigin() { 137 | return ( 138 | this.configApi.getOptionalString('techdocs.requestUrl') ?? 139 | (await this.discoveryApi.getBaseUrl('techdocs')) 140 | ); 141 | } 142 | 143 | async getEntityMetadata(_entityId: any) { 144 | return { 145 | apiVersion: 'backstage.io/v1alpha1', 146 | kind: 'Component', 147 | metadata: { 148 | name: 'local', 149 | }, 150 | spec: { 151 | owner: 'test', 152 | lifecycle: 'experimental', 153 | }, 154 | }; 155 | } 156 | 157 | async getTechDocsMetadata(_entityId: EntityName) { 158 | return { 159 | site_name: 'Live preview environment', 160 | site_description: '', 161 | }; 162 | } 163 | } 164 | 165 | export const apis: AnyApiFactory[] = [ 166 | createApiFactory({ 167 | api: techdocsStorageApiRef, 168 | deps: { 169 | configApi: configApiRef, 170 | discoveryApi: discoveryApiRef, 171 | identityApi: identityApiRef, 172 | }, 173 | factory: ({ configApi, discoveryApi, identityApi }) => 174 | new TechDocsDevStorageApi({ 175 | configApi, 176 | discoveryApi, 177 | identityApi, 178 | }), 179 | }), 180 | createApiFactory({ 181 | api: techdocsApiRef, 182 | deps: { 183 | configApi: configApiRef, 184 | discoveryApi: discoveryApiRef, 185 | identityApi: identityApiRef, 186 | }, 187 | factory: ({ configApi, discoveryApi, identityApi }) => 188 | new TechDocsDevApi({ 189 | configApi, 190 | discoveryApi, 191 | identityApi, 192 | }), 193 | }), 194 | createApiFactory({ 195 | api: scmIntegrationsApiRef, 196 | deps: { configApi: configApiRef }, 197 | factory: ({ configApi }) => ScmIntegrationsApi.fromConfig(configApi), 198 | }), 199 | ]; 200 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/components/Root/LogoFull.tsx: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React from 'react'; 18 | import { makeStyles } from '@material-ui/core'; 19 | 20 | const useStyles = makeStyles({ 21 | svg: { 22 | width: 'auto', 23 | height: 30, 24 | }, 25 | path: { 26 | fill: '#7df3e1', 27 | }, 28 | }); 29 | const LogoFull = () => { 30 | const classes = useStyles(); 31 | 32 | return ( 33 | 38 | 42 | 43 | ); 44 | }; 45 | 46 | export default LogoFull; 47 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/components/Root/LogoIcon.tsx: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React from 'react'; 18 | import { makeStyles } from '@material-ui/core'; 19 | 20 | const useStyles = makeStyles({ 21 | svg: { 22 | width: 'auto', 23 | height: 28, 24 | }, 25 | path: { 26 | fill: '#7df3e1', 27 | }, 28 | }); 29 | 30 | const LogoIcon = () => { 31 | const classes = useStyles(); 32 | 33 | return ( 34 | 39 | 43 | 44 | ); 45 | }; 46 | 47 | export default LogoIcon; 48 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/components/Root/Root.tsx: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React, { useContext, PropsWithChildren } from 'react'; 18 | import { Link, makeStyles } from '@material-ui/core'; 19 | import LibraryBooks from '@material-ui/icons/LibraryBooks'; 20 | import LogoFull from './LogoFull'; 21 | import LogoIcon from './LogoIcon'; 22 | import { 23 | Sidebar, 24 | SidebarPage, 25 | sidebarConfig, 26 | SidebarContext, 27 | SidebarItem, 28 | SidebarDivider, 29 | } from '@backstage/core'; 30 | import { NavLink } from 'react-router-dom'; 31 | 32 | const useSidebarLogoStyles = makeStyles({ 33 | root: { 34 | width: sidebarConfig.drawerWidthClosed, 35 | height: 3 * sidebarConfig.logoHeight, 36 | display: 'flex', 37 | flexFlow: 'row nowrap', 38 | alignItems: 'center', 39 | marginBottom: -14, 40 | }, 41 | link: { 42 | width: sidebarConfig.drawerWidthClosed, 43 | marginLeft: 24, 44 | }, 45 | }); 46 | 47 | const SidebarLogo = () => { 48 | const classes = useSidebarLogoStyles(); 49 | const { isOpen } = useContext(SidebarContext); 50 | 51 | return ( 52 |
53 | 59 | {isOpen ? : } 60 | 61 |
62 | ); 63 | }; 64 | 65 | export const Root = ({ children }: PropsWithChildren<{}>) => ( 66 | 67 | 68 | 69 | 70 | {/* Global nav, not org-specific */} 71 | 76 | {/* End global nav */} 77 | 78 | {children} 79 | 80 | ); 81 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/components/Root/index.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export { Root } from './Root'; 18 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/components/TechDocsPage/TechDocsPage.tsx: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React from 'react'; 18 | 19 | import { Content } from '@backstage/core-components'; 20 | 21 | import { 22 | Reader, 23 | TechDocsPage, 24 | TechDocsPageHeader, 25 | } from '@backstage/plugin-techdocs'; 26 | 27 | const DefaultTechDocsPage = () => { 28 | const techDocsMetadata = { 29 | site_name: 'Live preview environment', 30 | site_description: '', 31 | }; 32 | 33 | return ( 34 | 35 | {({ entityRef, onReady }) => ( 36 | <> 37 | 41 | 42 | 47 | 48 | 49 | )} 50 | 51 | ); 52 | }; 53 | 54 | export const techDocsPage = ; 55 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/components/TechDocsPage/index.ts: -------------------------------------------------------------------------------- 1 | export * from './TechDocsPage'; 2 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/index.tsx: -------------------------------------------------------------------------------- 1 | import '@backstage/cli/asset-types'; 2 | import React from 'react'; 3 | import ReactDOM from 'react-dom'; 4 | import App from './App'; 5 | 6 | ReactDOM.render(, document.getElementById('root')); 7 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/plugins.ts: -------------------------------------------------------------------------------- 1 | export { plugin as TechDocsPlugin } from '@backstage/plugin-techdocs'; 2 | -------------------------------------------------------------------------------- /packages/embedded-techdocs-app/src/setupTests.ts: -------------------------------------------------------------------------------- 1 | import '@testing-library/jest-dom'; 2 | -------------------------------------------------------------------------------- /packages/techdocs-cli/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [require.resolve('@backstage/cli/config/eslint')], 3 | overrides: [ 4 | { 5 | files: ['**/*.ts?(x)'], 6 | rules: { 7 | 'no-restricted-imports': 0, 8 | }, 9 | }, 10 | ], 11 | }; 12 | -------------------------------------------------------------------------------- /packages/techdocs-cli/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @techdocs/cli 2 | 3 | ## 0.8.4 4 | 5 | ### Patch Changes 6 | 7 | - 8333394: The [change](https://github.com/backstage/techdocs-cli/commit/b25014cec313d46ce1c9b4f324cc09047a00fc1f) updated the `@backstage/techdocs-common` from version `0.9.0` to `0.10.2` and one of the intermediate versions, the [0.10.0](https://github.com/backstage/backstage/blob/cac4afb95fdbd130a66e53a1b0430a1e62787a7f/packages/techdocs-common/CHANGELOG.md#patch-changes-2), introduced the use of search in context that requires an implementation for the Search API. 8 | 9 | Created a custom techdocs page to disable search in the Reader component, preventing it from using the Search API, as we don't want to provide search in preview mode. 10 | 11 | ## 0.8.3 12 | 13 | ### Patch Changes 14 | 15 | - edbb988: Upgrades the techdocs common page to the latest version 0.10.2. 16 | 17 | See [@backstage/techdocs-common changelog](https://github.com/backstage/backstage/blob/cac4afb95fdbd130a66e53a1b0430a1e62787a7f/packages/techdocs-common/CHANGELOG.md#L3). 18 | 19 | - db4ebfc: Add an `etag` flag to the `generate` command that is stored in the `techdocs_metadata.json` file. 20 | 21 | ## 0.8.2 22 | 23 | ### Patch Changes 24 | 25 | - 8fc7384: Allow to execute techdocs-cli serve using docker techdocs-container on Windows 26 | 27 | ## 0.8.1 28 | 29 | ### Patch Changes 30 | 31 | - 0187424: Separate build and publish release steps 32 | 33 | ## 0.8.0 34 | 35 | ### Minor Changes 36 | 37 | - c6f437a: OpenStack Swift configuration changed due to OSS SDK Client change in @backstage/techdocs-common, it was a breaking change. 38 | PR Reference: https://github.com/backstage/backstage/pull/6839 39 | 40 | ### Patch Changes 41 | 42 | - 05f0409: Merge Jobs for Release Pull Requests and Package Publishes 43 | 44 | ## 0.7.0 45 | 46 | ### Minor Changes 47 | 48 | - 9d1f8d8: The `techdocs-cli publish` command will now publish TechDocs content to remote 49 | storage using the lowercase'd entity triplet as the storage path. This is in 50 | line with the beta release of the TechDocs plugin (`v0.11.0`). 51 | 52 | If you have been running `techdocs-cli` prior to this version, you will need to 53 | follow this [migration guide](https://backstage.io/docs/features/techdocs/how-to-guides#how-to-migrate-from-techdocs-alpha-to-beta). 54 | 55 | ## 0.6.2 56 | 57 | ### Patch Changes 58 | 59 | - f1bcf1a: Changelog (from v0.6.1 to v0.6.2) 60 | 61 | #### :bug: Bug Fix 62 | 63 | - `techdocs-cli` 64 | - [#105](https://github.com/backstage/techdocs-cli/pull/105) Add azureAccountKey parameter back to the publish command ([@emmaindal](https://github.com/emmaindal)) 65 | 66 | #### :house: Internal 67 | 68 | - `embedded-techdocs-app` 69 | - [#122](https://github.com/backstage/techdocs-cli/pull/122) chore(deps-dev): bump @types/node from 12.20.20 to 16.7.1 in /packages/embedded-techdocs-app ([@dependabot[bot]](https://github.com/apps/dependabot)) 70 | - [#120](https://github.com/backstage/techdocs-cli/pull/120) chore(deps-dev): bump @types/react-dom from 16.9.14 to 17.0.9 in /packages/embedded-techdocs-app ([@dependabot[bot]](https://github.com/apps/dependabot)) 71 | - [#119](https://github.com/backstage/techdocs-cli/pull/119) chore(deps-dev): bump @testing-library/user-event from 12.8.3 to 13.2.1 in /packages/embedded-techdocs-app ([@dependabot[bot]](https://github.com/apps/dependabot)) 72 | - [#118](https://github.com/backstage/techdocs-cli/pull/118) chore(deps-dev): bump @testing-library/react from 10.4.9 to 12.0.0 ([@dependabot[bot]](https://github.com/apps/dependabot)) 73 | - Other 74 | - [#117](https://github.com/backstage/techdocs-cli/pull/117) chore(deps): bump @backstage/plugin-catalog from 0.6.11 to 0.6.12 ([@dependabot[bot]](https://github.com/apps/dependabot)) 75 | - [#124](https://github.com/backstage/techdocs-cli/pull/124) Update release process docs ([@emmaindal](https://github.com/emmaindal)) 76 | - [#116](https://github.com/backstage/techdocs-cli/pull/116) ignore dependabot branches for project board workflow ([@emmaindal](https://github.com/emmaindal)) 77 | - [#106](https://github.com/backstage/techdocs-cli/pull/106) Configure dependabot for all packages ([@emmaindal](https://github.com/emmaindal)) 78 | - [#102](https://github.com/backstage/techdocs-cli/pull/102) readme: add information about running techdocs-common locally ([@vcapretz](https://github.com/vcapretz)) 79 | - [#103](https://github.com/backstage/techdocs-cli/pull/103) Introduce changesets and improve the publish workflow ([@minkimcello](https://github.com/minkimcello)) 80 | - [#101](https://github.com/backstage/techdocs-cli/pull/101) update yarn lockfile to get rid of old version of node-forge ([@emmaindal](https://github.com/emmaindal)) 81 | 82 | #### Committers: 3 83 | 84 | Thank you for contributing ❤️ 85 | 86 | - Emma Indal ([@emmaindal](https://github.com/emmaindal)) 87 | - Min Kim ([@minkimcello](https://github.com/minkimcello)) 88 | - Vitor Capretz ([@vcapretz](https://github.com/vcapretz)) 89 | -------------------------------------------------------------------------------- /packages/techdocs-cli/README.md: -------------------------------------------------------------------------------- 1 | # techdocs-cli 2 | 3 | - [Usage Docs](https://github.com/backstage/techdocs-cli/docs/README.md) 4 | - [Local Development](https://github.com/backstage/techdocs-cli/README.md) 5 | -------------------------------------------------------------------------------- /packages/techdocs-cli/bin/techdocs-cli: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | /* 3 | * Copyright 2020 Spotify AB 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /* eslint-disable no-restricted-syntax */ 19 | const path = require('path'); 20 | 21 | // Figure out whether we're running inside the backstage repo or as an installed dependency 22 | const isLocal = require('fs').existsSync(path.resolve(__dirname, '../src')); 23 | 24 | if (!isLocal) { 25 | require('..'); 26 | } else { 27 | require('ts-node').register({ 28 | transpileOnly: true, 29 | project: path.resolve(__dirname, '../../../tsconfig.json'), 30 | compilerOptions: { 31 | module: 'CommonJS', 32 | }, 33 | }); 34 | 35 | require('../src'); 36 | } 37 | -------------------------------------------------------------------------------- /packages/techdocs-cli/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@techdocs/cli", 3 | "description": "Utility CLI for managing TechDocs sites in Backstage.", 4 | "version": "0.8.4", 5 | "publishConfig": { 6 | "access": "public" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/backstage/techdocs-cli" 11 | }, 12 | "keywords": [ 13 | "backstage", 14 | "techdocs" 15 | ], 16 | "license": "Apache-2.0", 17 | "main": "dist/index.cjs.js", 18 | "types": "", 19 | "scripts": { 20 | "start": "nodemon --", 21 | "build": "./scripts/build.sh", 22 | "clean": "backstage-cli clean", 23 | "lint": "backstage-cli lint", 24 | "test": "backstage-cli test", 25 | "pretest": "yarn build" 26 | }, 27 | "bin": { 28 | "techdocs-cli": "bin/techdocs-cli" 29 | }, 30 | "devDependencies": { 31 | "@backstage/cli": "^0.7.7", 32 | "@types/commander": "^2.12.2", 33 | "@types/fs-extra": "^9.0.6", 34 | "@types/http-proxy": "^1.17.4", 35 | "@types/jest": "^26.0.15", 36 | "@types/node": "^14.14.7", 37 | "@types/react-dev-utils": "^9.0.4", 38 | "@types/serve-handler": "^6.1.0", 39 | "@types/webpack-env": "^1.15.3", 40 | "embedded-techdocs-app": "0.0.0", 41 | "nodemon": "^2.0.6", 42 | "ts-node": "^9.0.0" 43 | }, 44 | "files": [ 45 | "bin", 46 | "dist" 47 | ], 48 | "nodemonConfig": { 49 | "watch": "./src", 50 | "exec": "bin/techdocs-cli", 51 | "ext": "ts" 52 | }, 53 | "dependencies": { 54 | "@backstage/backend-common": "^0.9.0", 55 | "@backstage/catalog-model": "^0.9.0", 56 | "@backstage/config": "^0.1.6", 57 | "@backstage/techdocs-common": "^0.10.2", 58 | "@types/dockerode": "^3.2.1", 59 | "commander": "^6.1.0", 60 | "dockerode": "^3.2.1", 61 | "fs-extra": "^9.0.1", 62 | "http-proxy": "^1.18.1", 63 | "react-dev-utils": "^11.0.4", 64 | "serve-handler": "^6.1.3", 65 | "winston": "^3.2.1" 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /packages/techdocs-cli/scripts/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2020 Spotify AB 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | set -e 18 | 19 | # Build the TechDocs CLI 20 | npx backstage-cli -- build --outputs cjs 21 | 22 | # Make sure to do `yarn run build` in packages/embedded-techdocs before building here. 23 | 24 | TECHDOCS_PREVIEW_SOURCE=../embedded-techdocs-app/dist 25 | TECHDOCS_PREVIEW_DEST=dist/techdocs-preview-bundle 26 | 27 | cp -r $TECHDOCS_PREVIEW_SOURCE $TECHDOCS_PREVIEW_DEST 28 | 29 | # Write to console 30 | echo "[techdocs-cli]: Built the dist/ folder" 31 | echo "[techdocs-cli]: Imported @backstage/plugin-techdocs dist/ folder into techdocs-preview-bundle/" 32 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/commands/generate/generate.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import { resolve } from 'path'; 17 | import { Command } from 'commander'; 18 | import fs from 'fs-extra'; 19 | import Docker from 'dockerode'; 20 | import { 21 | TechdocsGenerator, 22 | ParsedLocationAnnotation, 23 | } from '@backstage/techdocs-common'; 24 | import { DockerContainerRunner } from '@backstage/backend-common'; 25 | import { ConfigReader } from '@backstage/config'; 26 | import { 27 | convertTechDocsRefToLocationAnnotation, 28 | createLogger, 29 | } from '../../lib/utility'; 30 | import { stdout } from 'process'; 31 | 32 | export default async function generate(cmd: Command) { 33 | // Use techdocs-common package to generate docs. Keep consistency between Backstage and CI generating docs. 34 | // Docs can be prepared using actions/checkout or git clone, or similar paradigms on CI. The TechDocs CI workflow 35 | // will run on the CI pipeline containing the documentation files. 36 | 37 | const logger = createLogger({ verbose: cmd.verbose }); 38 | 39 | const sourceDir = resolve(cmd.sourceDir); 40 | const outputDir = resolve(cmd.outputDir); 41 | const dockerImage = cmd.dockerImage; 42 | const pullImage = cmd.pull; 43 | 44 | logger.info(`Using source dir ${sourceDir}`); 45 | logger.info(`Will output generated files in ${outputDir}`); 46 | 47 | logger.verbose('Creating output directory if it does not exist.'); 48 | 49 | await fs.ensureDir(outputDir); 50 | 51 | const config = new ConfigReader({ 52 | techdocs: { 53 | generator: { 54 | runIn: cmd.docker ? 'docker' : 'local', 55 | dockerImage, 56 | pullImage, 57 | }, 58 | }, 59 | }); 60 | 61 | // Docker client (conditionally) used by the generators, based on techdocs.generators config. 62 | const dockerClient = new Docker(); 63 | const containerRunner = new DockerContainerRunner({ dockerClient }); 64 | 65 | let parsedLocationAnnotation = {} as ParsedLocationAnnotation; 66 | if (cmd.techdocsRef) { 67 | try { 68 | parsedLocationAnnotation = convertTechDocsRefToLocationAnnotation( 69 | cmd.techdocsRef, 70 | ); 71 | } catch (err) { 72 | logger.error(err.message); 73 | } 74 | } 75 | 76 | // Generate docs using @backstage/techdocs-common 77 | const techdocsGenerator = await TechdocsGenerator.fromConfig(config, { 78 | logger, 79 | containerRunner, 80 | }); 81 | 82 | logger.info('Generating documentation...'); 83 | 84 | await techdocsGenerator.run({ 85 | inputDir: sourceDir, 86 | outputDir, 87 | ...(cmd.techdocsRef 88 | ? { 89 | parsedLocationAnnotation, 90 | } 91 | : {}), 92 | logger, 93 | etag: cmd.etag, 94 | ...(process.env.LOG_LEVEL === 'debug' ? { logStream: stdout } : {}), 95 | }); 96 | 97 | logger.info('Done!'); 98 | } 99 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/commands/index.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { CommanderStatic } from 'commander'; 18 | import { TechdocsGenerator } from '@backstage/techdocs-common'; 19 | 20 | const defaultDockerImage = TechdocsGenerator.defaultDockerImage; 21 | 22 | export function registerCommands(program: CommanderStatic) { 23 | program 24 | .command('generate') 25 | .description('Generate TechDocs documentation site using MkDocs.') 26 | .option( 27 | '--source-dir ', 28 | 'Source directory containing mkdocs.yml and docs/ directory.', 29 | '.', 30 | ) 31 | .option( 32 | '--output-dir ', 33 | 'Output directory containing generated TechDocs site.', 34 | './site/', 35 | ) 36 | .option( 37 | '--docker-image ', 38 | 'The mkdocs docker container to use', 39 | defaultDockerImage, 40 | ) 41 | .option('--no-pull', 'Do not pull the latest docker image', false) 42 | .option( 43 | '--no-docker', 44 | 'Do not use Docker, use MkDocs executable and plugins in current user environment.', 45 | ) 46 | .option( 47 | '--techdocs-ref ', 48 | 'The repository hosting documentation source files e.g. github:https://ghe.mycompany.net.com/org/repo.' + 49 | '\nThis value is same as the backstage.io/techdocs-ref annotation of the corresponding Backstage entity.' + 50 | '\nIt is completely fine to skip this as it is only being used to set repo_url in mkdocs.yml if not found.\n', 51 | ) 52 | .option( 53 | '--etag ', 54 | 'A unique identifier for the prepared tree e.g. commit SHA. If provided it will be stored in techdocs_metadata.json.', 55 | ) 56 | .option('-v --verbose', 'Enable verbose output.', false) 57 | .alias('build') 58 | .action(lazy(() => import('./generate/generate').then(m => m.default))); 59 | 60 | program 61 | .command('migrate') 62 | .description( 63 | 'Migrate objects with case-sensitive entity triplets to lower-case versions.', 64 | ) 65 | .requiredOption( 66 | '--publisher-type ', 67 | '(Required always) awsS3 | googleGcs | azureBlobStorage | openStackSwift - same as techdocs.publisher.type in Backstage app-config.yaml', 68 | ) 69 | .requiredOption( 70 | '--storage-name ', 71 | '(Required always) In case of AWS/GCS, use the bucket name. In case of Azure, use container name. Same as techdocs.publisher.[TYPE].bucketName', 72 | ) 73 | .option( 74 | '--azureAccountName ', 75 | '(Required for Azure) specify when --publisher-type azureBlobStorage', 76 | ) 77 | .option( 78 | '--azureAccountKey ', 79 | 'Azure Storage Account key to use for authentication. If not specified, you must set AZURE_TENANT_ID, AZURE_CLIENT_ID & AZURE_CLIENT_SECRET as environment variables.', 80 | ) 81 | .option( 82 | '--awsRoleArn ', 83 | 'Optional AWS ARN of role to be assumed.', 84 | ) 85 | .option( 86 | '--awsEndpoint ', 87 | 'Optional AWS endpoint to send requests to.', 88 | ) 89 | .option( 90 | '--awsS3ForcePathStyle', 91 | 'Optional AWS S3 option to force path style.', 92 | ) 93 | .option( 94 | '--osCredentialId ', 95 | '(Required for OpenStack) specify when --publisher-type openStackSwift', 96 | ) 97 | .option( 98 | '--osSecret ', 99 | '(Required for OpenStack) specify when --publisher-type openStackSwift', 100 | ) 101 | .option( 102 | '--osAuthUrl ', 103 | '(Required for OpenStack) specify when --publisher-type openStackSwift', 104 | ) 105 | .option( 106 | '--osSwiftUrl ', 107 | '(Required for OpenStack) specify when --publisher-type openStackSwift', 108 | ) 109 | .option( 110 | '--removeOriginal', 111 | 'Optional Files are copied by default. If flag is set, files are renamed/moved instead.', 112 | false, 113 | ) 114 | .option( 115 | '--concurrency ', 116 | 'Optional Controls the number of API requests allowed to be performed simultaneously.', 117 | '25', 118 | ) 119 | .option('-v --verbose', 'Enable verbose output.', false) 120 | .action(lazy(() => import('./migrate/migrate').then(m => m.default))); 121 | 122 | program 123 | .command('publish') 124 | .description( 125 | 'Publish generated TechDocs site to an external storage AWS S3, Google GCS, etc.', 126 | ) 127 | .requiredOption( 128 | '--publisher-type ', 129 | '(Required always) awsS3 | googleGcs | azureBlobStorage | openStackSwift - same as techdocs.publisher.type in Backstage app-config.yaml', 130 | ) 131 | .requiredOption( 132 | '--storage-name ', 133 | '(Required always) In case of AWS/GCS, use the bucket name. In case of Azure, use container name. Same as techdocs.publisher.[TYPE].bucketName', 134 | ) 135 | .requiredOption( 136 | '--entity ', 137 | '(Required always) Entity uid separated by / in namespace/kind/name order (case-sensitive). Example: default/Component/myEntity ', 138 | ) 139 | .option( 140 | '--legacyUseCaseSensitiveTripletPaths', 141 | 'Publishes objects with cased entity triplet prefix when set (e.g. namespace/Kind/name). Only use if your TechDocs backend is configured the same way.', 142 | false, 143 | ) 144 | .option( 145 | '--azureAccountName ', 146 | '(Required for Azure) specify when --publisher-type azureBlobStorage', 147 | ) 148 | .option( 149 | '--azureAccountKey ', 150 | 'Azure Storage Account key to use for authentication. If not specified, you must set AZURE_TENANT_ID, AZURE_CLIENT_ID & AZURE_CLIENT_SECRET as environment variables.', 151 | ) 152 | .option( 153 | '--awsRoleArn ', 154 | 'Optional AWS ARN of role to be assumed.', 155 | ) 156 | .option( 157 | '--awsEndpoint ', 158 | 'Optional AWS endpoint to send requests to.', 159 | ) 160 | .option( 161 | '--awsS3ForcePathStyle', 162 | 'Optional AWS S3 option to force path style.', 163 | ) 164 | .option( 165 | '--osCredentialId ', 166 | '(Required for OpenStack) specify when --publisher-type openStackSwift', 167 | ) 168 | .option( 169 | '--osSecret ', 170 | '(Required for OpenStack) specify when --publisher-type openStackSwift', 171 | ) 172 | .option( 173 | '--osAuthUrl ', 174 | '(Required for OpenStack) specify when --publisher-type openStackSwift', 175 | ) 176 | .option( 177 | '--osSwiftUrl ', 178 | '(Required for OpenStack) specify when --publisher-type openStackSwift', 179 | ) 180 | .option( 181 | '--directory ', 182 | 'Path of the directory containing generated files to publish', 183 | './site/', 184 | ) 185 | .action(lazy(() => import('./publish/publish').then(m => m.default))); 186 | 187 | program 188 | .command('serve:mkdocs') 189 | .description('Serve a documentation project locally using MkDocs serve.') 190 | .option( 191 | '-i, --docker-image ', 192 | 'The mkdocs docker container to use', 193 | defaultDockerImage, 194 | ) 195 | .option( 196 | '--no-docker', 197 | 'Do not use Docker, run `mkdocs serve` in current user environment.', 198 | ) 199 | .option('-p, --port ', 'Port to serve documentation locally', '8000') 200 | .option('-v --verbose', 'Enable verbose output.', false) 201 | .action(lazy(() => import('./serve/mkdocs').then(m => m.default))); 202 | 203 | program 204 | .command('serve') 205 | .description( 206 | 'Serve a documentation project locally in a Backstage app-like environment', 207 | ) 208 | .option( 209 | '-i, --docker-image ', 210 | 'The mkdocs docker container to use', 211 | defaultDockerImage, 212 | ) 213 | .option( 214 | '--no-docker', 215 | 'Do not use Docker, use MkDocs executable in current user environment.', 216 | ) 217 | .option('--mkdocs-port ', 'Port for MkDocs server to use', '8000') 218 | .option('-v --verbose', 'Enable verbose output.', false) 219 | .action(lazy(() => import('./serve/serve').then(m => m.default))); 220 | } 221 | 222 | // Wraps an action function so that it always exits and handles errors 223 | // Humbly taken from backstage-cli's registerCommands 224 | function lazy( 225 | getActionFunc: () => Promise<(...args: any[]) => Promise>, 226 | ): (...args: any[]) => Promise { 227 | return async (...args: any[]) => { 228 | try { 229 | const actionFunc = await getActionFunc(); 230 | await actionFunc(...args); 231 | process.exit(0); 232 | } catch (error) { 233 | // eslint-disable-next-line no-console 234 | console.error(error.message); 235 | process.exit(1); 236 | } 237 | }; 238 | } 239 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/commands/migrate/migrate.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 The Backstage Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { SingleHostDiscovery } from "@backstage/backend-common"; 18 | import { Publisher } from "@backstage/techdocs-common"; 19 | import { Command } from "commander"; 20 | import { createLogger } from "../../lib/utility"; 21 | import { PublisherConfig } from "../../lib/PublisherConfig"; 22 | 23 | export default async function migrate(cmd: Command) { 24 | const logger = createLogger({ verbose: cmd.verbose }); 25 | 26 | const config = PublisherConfig.getValidConfig(cmd); 27 | const discovery = SingleHostDiscovery.fromConfig(config); 28 | const publisher = await Publisher.fromConfig(config, { logger, discovery }); 29 | 30 | if (!publisher.migrateDocsCase) { 31 | throw new Error(`Migration not implemented for ${cmd.publisherType}`); 32 | } 33 | 34 | // Check that the publisher's underlying storage is ready and available. 35 | const { isAvailable } = await publisher.getReadiness(); 36 | if (!isAvailable) { 37 | // Error messages printed in getReadiness() call. This ensures exit code 1. 38 | throw new Error(""); 39 | } 40 | 41 | // Validate and parse migration arguments. 42 | const removeOriginal = cmd.removeOriginal; 43 | const numericConcurrency = parseInt(cmd.concurrency, 10); 44 | 45 | if (!Number.isInteger(numericConcurrency) || numericConcurrency <= 0) { 46 | throw new Error( 47 | `Concurrency must be a number greater than 1. ${cmd.concurrency} provided.` 48 | ); 49 | } 50 | 51 | await publisher.migrateDocsCase({ 52 | concurrency: numericConcurrency, 53 | removeOriginal, 54 | }); 55 | } 56 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/commands/publish/publish.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { resolve } from "path"; 18 | import { Command } from "commander"; 19 | import { createLogger } from "../../lib/utility"; 20 | import { SingleHostDiscovery } from "@backstage/backend-common"; 21 | import { Publisher } from "@backstage/techdocs-common"; 22 | import { Entity } from "@backstage/catalog-model"; 23 | import { PublisherConfig } from "../../lib/PublisherConfig"; 24 | 25 | export default async function publish(cmd: Command): Promise { 26 | const logger = createLogger({ verbose: cmd.verbose }); 27 | 28 | const config = PublisherConfig.getValidConfig(cmd); 29 | const discovery = SingleHostDiscovery.fromConfig(config); 30 | const publisher = await Publisher.fromConfig(config, { logger, discovery }); 31 | 32 | // Check that the publisher's underlying storage is ready and available. 33 | const { isAvailable } = await publisher.getReadiness(); 34 | if (!isAvailable) { 35 | // Error messages printed in getReadiness() call. This ensures exit code 1. 36 | return Promise.reject(new Error("")); 37 | } 38 | 39 | const [namespace, kind, name] = cmd.entity.split("/"); 40 | const entity = { 41 | kind, 42 | metadata: { 43 | namespace, 44 | name 45 | } 46 | } as Entity; 47 | 48 | const directory = resolve(cmd.directory); 49 | await publisher.publish({ entity, directory }); 50 | 51 | return true; 52 | } 53 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/commands/serve/mkdocs.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import { Command } from "commander"; 17 | import openBrowser from "react-dev-utils/openBrowser"; 18 | import { createLogger } from "../../lib/utility"; 19 | import { runMkdocsServer } from "../../lib/mkdocsServer"; 20 | import { LogFunc, waitForSignal } from "../../lib/run"; 21 | 22 | export default async function serveMkdocs(cmd: Command) { 23 | const logger = createLogger({ verbose: cmd.verbose }); 24 | 25 | const dockerAddr = `http://0.0.0.0:${cmd.port}`; 26 | const localAddr = `http://127.0.0.1:${cmd.port}`; 27 | const expectedDevAddr = cmd.docker ? dockerAddr : localAddr; 28 | // We want to open browser only once based on a log. 29 | let boolOpenBrowserTriggered = false; 30 | 31 | const logFunc: LogFunc = data => { 32 | // Sometimes the lines contain an unnecessary extra new line in between 33 | const logLines = data.toString().split("\n"); 34 | const logPrefix = cmd.docker ? "[docker/mkdocs]" : "[mkdocs]"; 35 | logLines.forEach(line => { 36 | if (line === "") { 37 | return; 38 | } 39 | 40 | // Logs from container is verbose. 41 | logger.verbose(`${logPrefix} ${line}`); 42 | 43 | // When the server has started, open a new browser tab for the user. 44 | if ( 45 | !boolOpenBrowserTriggered && 46 | line.includes(`Serving on ${expectedDevAddr}`) 47 | ) { 48 | // Always open the local address, since 0.0.0.0 belongs to docker 49 | logger.info(`\nStarting mkdocs server on ${localAddr}\n`); 50 | openBrowser(localAddr); 51 | boolOpenBrowserTriggered = true; 52 | } 53 | }); 54 | }; 55 | // mkdocs writes all of its logs to stderr by default, and not stdout. 56 | // https://github.com/mkdocs/mkdocs/issues/879#issuecomment-203536006 57 | // Had me questioning this whole implementation for half an hour. 58 | 59 | // Commander stores --no-docker in cmd.docker variable 60 | const childProcess = await runMkdocsServer({ 61 | port: cmd.port, 62 | dockerImage: cmd.dockerImage, 63 | useDocker: cmd.docker, 64 | stdoutLogFunc: logFunc, 65 | stderrLogFunc: logFunc 66 | }); 67 | 68 | // Keep waiting for user to cancel the process 69 | await waitForSignal([childProcess]); 70 | } 71 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/commands/serve/serve.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import { Command } from "commander"; 17 | import path from "path"; 18 | import openBrowser from "react-dev-utils/openBrowser"; 19 | import HTTPServer from "../../lib/httpServer"; 20 | import { runMkdocsServer } from "../../lib/mkdocsServer"; 21 | import { LogFunc, waitForSignal } from "../../lib/run"; 22 | import { createLogger } from "../../lib/utility"; 23 | 24 | export default async function serve(cmd: Command) { 25 | const logger = createLogger({ verbose: cmd.verbose }); 26 | 27 | // Determine if we want to run in local dev mode or not 28 | // This will run the backstage http server on a different port and only used 29 | // for proxying mkdocs to the backstage app running locally (e.g. with webpack-dev-server) 30 | const isDevMode = Object.keys(process.env).includes("TECHDOCS_CLI_DEV_MODE") 31 | ? true 32 | : false; 33 | 34 | // TODO: Backstage app port should also be configurable as a CLI option. However, since we bundle 35 | // a backstage app, we define app.baseUrl in the app-config.yaml. 36 | // Hence, it is complicated to make this configurable. 37 | const backstagePort = 3000; 38 | const backstageBackendPort = 7000; 39 | 40 | const mkdocsDockerAddr = `http://0.0.0.0:${cmd.mkdocsPort}`; 41 | const mkdocsLocalAddr = `http://127.0.0.1:${cmd.mkdocsPort}`; 42 | const mkdocsExpectedDevAddr = cmd.docker ? mkdocsDockerAddr : mkdocsLocalAddr; 43 | 44 | let mkdocsServerHasStarted = false; 45 | const mkdocsLogFunc: LogFunc = data => { 46 | // Sometimes the lines contain an unnecessary extra new line 47 | const logLines = data.toString().split("\n"); 48 | const logPrefix = cmd.docker ? "[docker/mkdocs]" : "[mkdocs]"; 49 | logLines.forEach(line => { 50 | if (line === "") { 51 | return; 52 | } 53 | 54 | logger.verbose(`${logPrefix} ${line}`); 55 | 56 | // When the server has started, open a new browser tab for the user. 57 | if ( 58 | !mkdocsServerHasStarted && 59 | line.includes(`Serving on ${mkdocsExpectedDevAddr}`) 60 | ) { 61 | mkdocsServerHasStarted = true; 62 | } 63 | }); 64 | }; 65 | // mkdocs writes all of its logs to stderr by default, and not stdout. 66 | // https://github.com/mkdocs/mkdocs/issues/879#issuecomment-203536006 67 | // Had me questioning this whole implementation for half an hour. 68 | logger.info("Starting mkdocs server."); 69 | const mkdocsChildProcess = await runMkdocsServer({ 70 | port: cmd.mkdocsPort, 71 | dockerImage: cmd.dockerImage, 72 | useDocker: cmd.docker, 73 | stdoutLogFunc: mkdocsLogFunc, 74 | stderrLogFunc: mkdocsLogFunc 75 | }); 76 | 77 | // Wait until mkdocs server has started so that Backstage starts with docs loaded 78 | // Takes 1-5 seconds 79 | for (let attempt = 0; attempt < 10; attempt++) { 80 | await new Promise(r => setTimeout(r, 1000)); 81 | if (mkdocsServerHasStarted) { 82 | break; 83 | } 84 | logger.info("Waiting for mkdocs server to start..."); 85 | } 86 | 87 | if (!mkdocsServerHasStarted) { 88 | logger.error( 89 | "mkdocs server did not start. Exiting. Try re-running command with -v option for more details." 90 | ); 91 | } 92 | 93 | // Run the embedded-techdocs Backstage app 94 | const techdocsPreviewBundlePath = path.join( 95 | path.dirname(require.resolve("@techdocs/cli/package.json")), 96 | "dist", 97 | "techdocs-preview-bundle" 98 | ); 99 | 100 | const httpServer = new HTTPServer( 101 | techdocsPreviewBundlePath, 102 | isDevMode ? backstageBackendPort : backstagePort, 103 | cmd.mkdocsPort, 104 | cmd.verbose 105 | ); 106 | 107 | httpServer 108 | .serve() 109 | .catch(err => { 110 | logger.error(err); 111 | mkdocsChildProcess.kill(); 112 | process.exit(1); 113 | }) 114 | .then(() => { 115 | // The last three things default/component/local/ don't matter. They can be anything. 116 | openBrowser( 117 | `http://localhost:${backstagePort}/docs/default/component/local/` 118 | ); 119 | logger.info( 120 | `Serving docs in Backstage at http://localhost:${backstagePort}/docs/default/component/local/\nOpening browser.` 121 | ); 122 | }); 123 | 124 | await waitForSignal([mkdocsChildProcess]); 125 | } 126 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/e2e.test.ts: -------------------------------------------------------------------------------- 1 | import { spawn } from 'child_process'; 2 | 3 | describe('end-to-end', () => { 4 | it('shows help text', async () => { 5 | const proc = await executeTechDocsCliCommand(['--help']); 6 | 7 | expect(proc.combinedStdOutErr).toContain('Usage: techdocs-cli [options]'); 8 | expect(proc.exit).toEqual(0); 9 | }); 10 | 11 | it('can generate', async () => { 12 | jest.setTimeout(10000); 13 | const proc = await executeTechDocsCliCommand(['generate', '--no-docker'], { 14 | cwd: '../../', 15 | killAfter: 8000, 16 | }); 17 | 18 | expect(proc.combinedStdOutErr).toContain('Successfully generated docs'); 19 | expect(proc.exit).toEqual(0); 20 | }); 21 | 22 | it('can serve in mkdocs', async () => { 23 | jest.setTimeout(10000); 24 | const proc = await executeTechDocsCliCommand( 25 | ['serve:mkdocs', '--no-docker'], 26 | { 27 | cwd: '../../', 28 | killAfter: 8000, 29 | }, 30 | ); 31 | 32 | expect(proc.combinedStdOutErr).toContain('Starting mkdocs server'); 33 | expect(proc.exit).toEqual(0); 34 | }); 35 | 36 | it('can serve in backstage', async () => { 37 | jest.setTimeout(10000); 38 | const proc = await executeTechDocsCliCommand( 39 | ['serve', '--no-docker', '--mkdocs-port=8888'], 40 | { 41 | cwd: '../../', 42 | killAfter: 8000, 43 | }, 44 | ); 45 | 46 | expect(proc.combinedStdOutErr).toContain('Starting mkdocs server'); 47 | expect(proc.combinedStdOutErr).toContain('Serving docs in Backstage at'); 48 | expect(proc.exit).toEqual(0); 49 | }); 50 | }); 51 | 52 | type CommandResponse = { 53 | stdout: string; 54 | stderr: string; 55 | combinedStdOutErr: string; 56 | exit: number; 57 | }; 58 | 59 | type ExecuteCommandOptions = { 60 | killAfter?: number; 61 | cwd?: string; 62 | }; 63 | 64 | function executeTechDocsCliCommand( 65 | args: string[], 66 | opts: ExecuteCommandOptions = {}, 67 | ): Promise { 68 | return new Promise(resolve => { 69 | const pathToCli = `${__dirname}/../bin/techdocs-cli`; 70 | const commandResponse = { 71 | stdout: '', 72 | stderr: '', 73 | combinedStdOutErr: '', 74 | exit: 0, 75 | }; 76 | 77 | const listen = spawn(pathToCli, args, { 78 | cwd: opts.cwd, 79 | }); 80 | 81 | const stdOutChunks: any[] = []; 82 | const stdErrChunks: any[] = []; 83 | const combinedChunks: any[] = []; 84 | 85 | listen.stdout.on('data', data => { 86 | stdOutChunks.push(data); 87 | combinedChunks.push(data); 88 | }); 89 | 90 | listen.stderr.on('data', data => { 91 | stdErrChunks.push(data); 92 | combinedChunks.push(data); 93 | }); 94 | 95 | listen.on('exit', code => { 96 | commandResponse.exit = code as number; 97 | commandResponse.stdout = Buffer.concat(stdOutChunks).toString('utf8'); 98 | commandResponse.stderr = Buffer.concat(stdErrChunks).toString('utf8'); 99 | commandResponse.combinedStdOutErr = 100 | Buffer.concat(combinedChunks).toString('utf8'); 101 | resolve(commandResponse); 102 | }); 103 | 104 | if (opts.killAfter) { 105 | setTimeout(() => { 106 | listen.kill('SIGTERM'); 107 | }, opts.killAfter); 108 | } 109 | }); 110 | } 111 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/index.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import program from "commander"; 17 | import { registerCommands } from "./commands"; 18 | import { version } from "../package.json"; 19 | 20 | const main = (argv: string[]) => { 21 | program.name("techdocs-cli").version(version); 22 | 23 | registerCommands(program); 24 | 25 | program.parse(argv); 26 | }; 27 | 28 | main(process.argv); 29 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/lib/PublisherConfig.test.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 The Backstage Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Command } from 'commander'; 18 | import { PublisherConfig } from './PublisherConfig'; 19 | 20 | describe('getValidPublisherConfig', () => { 21 | it('should not allow unknown publisher types', () => { 22 | const invalidConfig = { 23 | publisherType: 'unknown publisher', 24 | } as unknown as Command; 25 | 26 | expect(() => PublisherConfig.getValidConfig(invalidConfig)).toThrowError( 27 | `Unknown publisher type ${invalidConfig.publisherType}`, 28 | ); 29 | }); 30 | 31 | describe('for azureBlobStorage', () => { 32 | it('should require --azureAccountName', () => { 33 | const config = { 34 | publisherType: 'azureBlobStorage', 35 | } as unknown as Command; 36 | 37 | expect(() => PublisherConfig.getValidConfig(config)).toThrowError( 38 | 'azureBlobStorage requires --azureAccountName to be specified', 39 | ); 40 | }); 41 | 42 | it('should return valid ConfigReader', () => { 43 | const config = { 44 | publisherType: 'azureBlobStorage', 45 | azureAccountName: 'someAccountName', 46 | storageName: 'someContainer', 47 | } as unknown as Command; 48 | 49 | const actualConfig = PublisherConfig.getValidConfig(config); 50 | expect(actualConfig.getString('techdocs.publisher.type')).toBe( 51 | 'azureBlobStorage', 52 | ); 53 | expect( 54 | actualConfig.getString( 55 | 'techdocs.publisher.azureBlobStorage.containerName', 56 | ), 57 | ).toBe('someContainer'); 58 | expect( 59 | actualConfig.getString( 60 | 'techdocs.publisher.azureBlobStorage.credentials.accountName', 61 | ), 62 | ).toBe('someAccountName'); 63 | }); 64 | }); 65 | 66 | describe('for awsS3', () => { 67 | it('should return valid ConfigReader', () => { 68 | const config = { 69 | publisherType: 'awsS3', 70 | storageName: 'someStorageName', 71 | } as unknown as Command; 72 | 73 | const actualConfig = PublisherConfig.getValidConfig(config); 74 | expect(actualConfig.getString('techdocs.publisher.type')).toBe('awsS3'); 75 | expect( 76 | actualConfig.getString('techdocs.publisher.awsS3.bucketName'), 77 | ).toBe('someStorageName'); 78 | }); 79 | }); 80 | 81 | describe('for openStackSwift', () => { 82 | it('should throw error on missing parameters', () => { 83 | const config = { 84 | publisherType: 'openStackSwift', 85 | osCredentialId: 'someCredentialId', 86 | osSecret: 'someSecret', 87 | } as unknown as Command; 88 | 89 | expect(() => PublisherConfig.getValidConfig(config)).toThrowError( 90 | `openStackSwift requires the following params to be specified: ${[ 91 | 'osAuthUrl', 92 | 'osSwiftUrl', 93 | ].join(', ')}`, 94 | ); 95 | }); 96 | 97 | it('should return valid ConfigReader', () => { 98 | const config = { 99 | publisherType: 'openStackSwift', 100 | storageName: 'someStorageName', 101 | osCredentialId: 'someCredentialId', 102 | osSecret: 'someSecret', 103 | osAuthUrl: 'someAuthUrl', 104 | osSwiftUrl: 'someSwiftUrl', 105 | } as unknown as Command; 106 | 107 | const actualConfig = PublisherConfig.getValidConfig(config); 108 | expect(actualConfig.getString('techdocs.publisher.type')).toBe( 109 | 'openStackSwift', 110 | ); 111 | expect( 112 | actualConfig.getConfig('techdocs.publisher.openStackSwift').get(), 113 | ).toMatchObject({ 114 | containerName: 'someStorageName', 115 | credentials: { 116 | id: 'someCredentialId', 117 | secret: 'someSecret', 118 | }, 119 | authUrl: 'someAuthUrl', 120 | swiftUrl: 'someSwiftUrl', 121 | }); 122 | }); 123 | }); 124 | 125 | describe('for googleGcs', () => { 126 | it('should return valid ConfigReader', () => { 127 | const config = { 128 | publisherType: 'googleGcs', 129 | storageName: 'someStorageName', 130 | } as unknown as Command; 131 | 132 | const actualConfig = PublisherConfig.getValidConfig(config); 133 | expect(actualConfig.getString('techdocs.publisher.type')).toBe( 134 | 'googleGcs', 135 | ); 136 | expect( 137 | actualConfig.getString('techdocs.publisher.googleGcs.bucketName'), 138 | ).toBe('someStorageName'); 139 | }); 140 | }); 141 | }); 142 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/lib/PublisherConfig.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 The Backstage Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import { ConfigReader } from '@backstage/config'; 17 | import { Command } from 'commander'; 18 | 19 | type Publisher = keyof typeof PublisherConfig['configFactories']; 20 | type PublisherConfiguration = { 21 | [p in Publisher]?: any; 22 | } & { 23 | type: Publisher; 24 | }; 25 | 26 | /** 27 | * Helper when working with publisher-related configurations. 28 | */ 29 | export class PublisherConfig { 30 | /** 31 | * Maps publisher-specific config keys to config getters. 32 | */ 33 | private static configFactories = { 34 | awsS3: PublisherConfig.getValidAwsS3Config, 35 | azureBlobStorage: PublisherConfig.getValidAzureConfig, 36 | googleGcs: PublisherConfig.getValidGoogleGcsConfig, 37 | openStackSwift: PublisherConfig.getValidOpenStackSwiftConfig, 38 | }; 39 | 40 | /** 41 | * Returns Backstage config suitable for use when instantiating a Publisher. If 42 | * there are any missing or invalid options provided, an error is thrown. 43 | * 44 | * Note: This assumes that proper credentials are set in Environment 45 | * variables for the respective GCS/AWS clients to work. 46 | */ 47 | static getValidConfig(cmd: Command): ConfigReader { 48 | const publisherType = cmd.publisherType; 49 | 50 | if (!PublisherConfig.isKnownPublisher(publisherType)) { 51 | throw new Error(`Unknown publisher type ${cmd.publisherType}`); 52 | } 53 | 54 | return new ConfigReader({ 55 | // This backend config is not used at all. Just something needed a create a mock discovery instance. 56 | backend: { 57 | baseUrl: 'http://localhost:7000', 58 | listen: { 59 | port: 7000, 60 | }, 61 | }, 62 | techdocs: { 63 | publisher: PublisherConfig.configFactories[publisherType](cmd), 64 | legacyUseCaseSensitiveTripletPaths: 65 | cmd.legacyUseCaseSensitiveTripletPaths, 66 | }, 67 | }); 68 | } 69 | 70 | /** 71 | * Typeguard to ensure the publisher has a known config getter. 72 | */ 73 | private static isKnownPublisher( 74 | type: string, 75 | ): type is keyof typeof PublisherConfig['configFactories'] { 76 | return PublisherConfig.configFactories.hasOwnProperty(type); 77 | } 78 | 79 | /** 80 | * Retrieve valid AWS S3 configuration based on the command. 81 | */ 82 | private static getValidAwsS3Config(cmd: Command): PublisherConfiguration { 83 | return { 84 | type: 'awsS3', 85 | awsS3: { 86 | bucketName: cmd.storageName, 87 | ...(cmd.awsRoleArn && { credentials: { roleArn: cmd.awsRoleArn } }), 88 | ...(cmd.awsEndpoint && { endpoint: cmd.awsEndpoint }), 89 | ...(cmd.awsS3ForcePathStyle && { s3ForcePathStyle: true }), 90 | }, 91 | }; 92 | } 93 | 94 | /** 95 | * Retrieve valid Azure Blob Storage configuration based on the command. 96 | */ 97 | private static getValidAzureConfig(cmd: Command): PublisherConfiguration { 98 | if (!cmd.azureAccountName) { 99 | throw new Error( 100 | `azureBlobStorage requires --azureAccountName to be specified`, 101 | ); 102 | } 103 | 104 | return { 105 | type: 'azureBlobStorage', 106 | azureBlobStorage: { 107 | containerName: cmd.storageName, 108 | credentials: { 109 | accountName: cmd.azureAccountName, 110 | accountKey: cmd.azureAccountKey, 111 | }, 112 | }, 113 | }; 114 | } 115 | 116 | /** 117 | * Retrieve valid GCS configuration based on the command. 118 | */ 119 | private static getValidGoogleGcsConfig(cmd: Command): PublisherConfiguration { 120 | return { 121 | type: 'googleGcs', 122 | googleGcs: { 123 | bucketName: cmd.storageName, 124 | }, 125 | }; 126 | } 127 | 128 | /** 129 | * Retrieves valid OpenStack Swift configuration based on the command. 130 | */ 131 | private static getValidOpenStackSwiftConfig( 132 | cmd: Command, 133 | ): PublisherConfiguration { 134 | const missingParams = [ 135 | 'osCredentialId', 136 | 'osSecret', 137 | 'osAuthUrl', 138 | 'osSwiftUrl', 139 | ].filter((param: string) => !cmd[param]); 140 | 141 | if (missingParams.length) { 142 | throw new Error( 143 | `openStackSwift requires the following params to be specified: ${missingParams.join( 144 | ', ', 145 | )}`, 146 | ); 147 | } 148 | 149 | return { 150 | type: 'openStackSwift', 151 | openStackSwift: { 152 | containerName: cmd.storageName, 153 | credentials: { 154 | id: cmd.osCredentialId, 155 | secret: cmd.osSecret, 156 | }, 157 | authUrl: cmd.osAuthUrl, 158 | swiftUrl: cmd.osSwiftUrl, 159 | }, 160 | }; 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/lib/httpServer.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import serveHandler from "serve-handler"; 18 | import http from "http"; 19 | import httpProxy from "http-proxy"; 20 | import { createLogger } from "./utility"; 21 | 22 | export default class HTTPServer { 23 | private readonly proxyEndpoint: string; 24 | private readonly backstageBundleDir: string; 25 | private readonly backstagePort: number; 26 | private readonly mkdocsPort: number; 27 | private readonly verbose: boolean; 28 | 29 | constructor( 30 | backstageBundleDir: string, 31 | backstagePort: number, 32 | mkdocsPort: number, 33 | verbose: boolean 34 | ) { 35 | this.proxyEndpoint = "/api/"; 36 | this.backstageBundleDir = backstageBundleDir; 37 | this.backstagePort = backstagePort; 38 | this.mkdocsPort = mkdocsPort; 39 | this.verbose = verbose; 40 | } 41 | 42 | // Create a Proxy for mkdocs server 43 | private createProxy() { 44 | const proxy = httpProxy.createProxyServer({ 45 | target: `http://localhost:${this.mkdocsPort}` 46 | }); 47 | 48 | return (request: http.IncomingMessage): [httpProxy, string] => { 49 | // If the request goes to /api/ we want to remove /api/ from the prefix of the request URL. 50 | // e.g. ['/', 'api', pathChunks] 51 | const [, , ...pathChunks] = request.url?.split("/") ?? []; 52 | const forwardPath = pathChunks.join("/"); 53 | 54 | return [proxy, forwardPath]; 55 | }; 56 | } 57 | 58 | public async serve(): Promise { 59 | return new Promise((resolve, reject) => { 60 | const proxyHandler = this.createProxy(); 61 | const server = http.createServer( 62 | (request: http.IncomingMessage, response: http.ServerResponse) => { 63 | if (request.url?.startsWith(this.proxyEndpoint)) { 64 | const [proxy, forwardPath] = proxyHandler(request); 65 | 66 | proxy.on("error", (error: Error) => { 67 | reject(error); 68 | }); 69 | 70 | response.setHeader("Access-Control-Allow-Origin", "*"); 71 | response.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS"); 72 | 73 | request.url = forwardPath; 74 | return proxy.web(request, response); 75 | } 76 | 77 | return serveHandler(request, response, { 78 | public: this.backstageBundleDir, 79 | trailingSlash: true, 80 | rewrites: [{ source: "**", destination: "index.html" }] 81 | }); 82 | } 83 | ); 84 | 85 | const logger = createLogger({ verbose: false }); 86 | server.listen(this.backstagePort, () => { 87 | if (this.verbose) { 88 | logger.info( 89 | `[techdocs-preview-bundle] Running local version of Backstage at http://localhost:${this.backstagePort}` 90 | ); 91 | } 92 | resolve(server); 93 | }); 94 | 95 | server.on("error", (error: Error) => { 96 | reject(error); 97 | }); 98 | }); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/lib/mkdocsServer.test.ts: -------------------------------------------------------------------------------- 1 | import { runMkdocsServer } from './mkdocsServer'; 2 | import { run } from './run'; 3 | 4 | jest.mock('./run', () => { 5 | return { 6 | run: jest.fn(), 7 | }; 8 | }); 9 | 10 | describe('runMkdocsServer', () => { 11 | beforeEach(() => { 12 | jest.resetAllMocks(); 13 | }); 14 | 15 | describe('docker', () => { 16 | it('should run docker directly by default', async () => { 17 | await runMkdocsServer({}); 18 | 19 | const quotedCwd = `"${process.cwd()}":/content`; 20 | expect(run).toHaveBeenCalledWith( 21 | 'docker', 22 | expect.arrayContaining([ 23 | 'run', 24 | quotedCwd, 25 | '8000:8000', 26 | 'serve', 27 | '--dev-addr', 28 | '0.0.0.0:8000', 29 | 'spotify/techdocs', 30 | ]), 31 | expect.objectContaining({}), 32 | ); 33 | }); 34 | 35 | it('should accept port option', async () => { 36 | await runMkdocsServer({ port: '5678' }); 37 | expect(run).toHaveBeenCalledWith( 38 | 'docker', 39 | expect.arrayContaining(['5678:5678', '0.0.0.0:5678']), 40 | expect.objectContaining({}), 41 | ); 42 | }); 43 | 44 | it('should accept custom docker image', async () => { 45 | await runMkdocsServer({ dockerImage: 'my-org/techdocs' }); 46 | expect(run).toHaveBeenCalledWith( 47 | 'docker', 48 | expect.arrayContaining(['my-org/techdocs']), 49 | expect.objectContaining({}), 50 | ); 51 | }); 52 | }); 53 | 54 | describe('mkdocs', () => { 55 | it('should run mkdocs if specified', async () => { 56 | await runMkdocsServer({ useDocker: false }); 57 | 58 | expect(run).toHaveBeenCalledWith( 59 | 'mkdocs', 60 | expect.arrayContaining(['serve', '--dev-addr', '127.0.0.1:8000']), 61 | expect.objectContaining({}), 62 | ); 63 | }); 64 | 65 | it('should accept port option', async () => { 66 | await runMkdocsServer({ useDocker: false, port: '5678' }); 67 | expect(run).toHaveBeenCalledWith( 68 | 'mkdocs', 69 | expect.arrayContaining(['127.0.0.1:5678']), 70 | expect.objectContaining({}), 71 | ); 72 | }); 73 | }); 74 | }); 75 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/lib/mkdocsServer.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import { ChildProcess } from 'child_process'; 17 | import { run, LogFunc } from './run'; 18 | 19 | export const runMkdocsServer = async (options: { 20 | port?: string; 21 | useDocker?: boolean; 22 | dockerImage?: string; 23 | stdoutLogFunc?: LogFunc; 24 | stderrLogFunc?: LogFunc; 25 | }): Promise => { 26 | const port = options.port ?? '8000'; 27 | const useDocker = options.useDocker ?? true; 28 | const dockerImage = options.dockerImage ?? 'spotify/techdocs'; 29 | 30 | if (useDocker) { 31 | return await run( 32 | 'docker', 33 | [ 34 | 'run', 35 | '--rm', 36 | '-w', 37 | '/content', 38 | '-v', 39 | `"${process.cwd()}":/content`, 40 | '-p', 41 | `${port}:${port}`, 42 | dockerImage, 43 | 'serve', 44 | '--dev-addr', 45 | `0.0.0.0:${port}`, 46 | ], 47 | { 48 | stdoutLogFunc: options.stdoutLogFunc, 49 | stderrLogFunc: options.stderrLogFunc, 50 | }, 51 | ); 52 | } 53 | 54 | return await run('mkdocs', ['serve', '--dev-addr', `127.0.0.1:${port}`], { 55 | stdoutLogFunc: options.stdoutLogFunc, 56 | stderrLogFunc: options.stderrLogFunc, 57 | }); 58 | }; 59 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/lib/run.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import { spawn, SpawnOptions, ChildProcess } from "child_process"; 17 | 18 | export type LogFunc = (data: Buffer | string) => void; 19 | type SpawnOptionsPartialEnv = Omit & { 20 | env?: Partial; 21 | // Pipe stdout to this log function 22 | stdoutLogFunc?: LogFunc; 23 | // Pipe stderr to this log function 24 | stderrLogFunc?: LogFunc; 25 | }; 26 | 27 | // TODO: Accept log functions to pipe logs with. 28 | // Runs a child command, returning the child process instance. 29 | // Use it along with waitForSignal to run a long running process e.g. mkdocs serve 30 | export const run = async ( 31 | name: string, 32 | args: string[] = [], 33 | options: SpawnOptionsPartialEnv = {} 34 | ): Promise => { 35 | const { stdoutLogFunc, stderrLogFunc } = options; 36 | 37 | const env: NodeJS.ProcessEnv = { 38 | ...process.env, 39 | FORCE_COLOR: "true", 40 | ...(options.env ?? {}) 41 | }; 42 | 43 | // Refer: https://nodejs.org/api/child_process.html#child_process_subprocess_stdio 44 | const stdio = [ 45 | "inherit", 46 | stdoutLogFunc ? "pipe" : "inherit", 47 | stderrLogFunc ? "pipe" : "inherit" 48 | ] as ("inherit" | "pipe")[]; 49 | 50 | const childProcess = spawn(name, args, { 51 | stdio: stdio, 52 | shell: true, 53 | ...options, 54 | env 55 | }); 56 | 57 | if (stdoutLogFunc && childProcess.stdout) { 58 | childProcess.stdout.on("data", stdoutLogFunc); 59 | } 60 | if (stderrLogFunc && childProcess.stderr) { 61 | childProcess.stderr.on("data", stderrLogFunc); 62 | } 63 | 64 | return childProcess; 65 | }; 66 | 67 | // Block indefinitely and wait for a signal to kill the child process(es) 68 | // Throw error if any child process errors 69 | // Resolves only when all processes exit with status code 0 70 | export async function waitForSignal( 71 | childProcesses: Array 72 | ): Promise { 73 | const promises: Array> = []; 74 | 75 | childProcesses.forEach(childProcess => { 76 | if (typeof childProcess.exitCode === "number") { 77 | if (childProcess.exitCode) { 78 | throw new Error(`Non zero exit code from child process`); 79 | } 80 | return; 81 | } 82 | 83 | for (const signal of ["SIGINT", "SIGTERM"] as const) { 84 | process.on(signal, () => { 85 | childProcess.kill(signal); 86 | // exit instead of resolve. The process is shutting down and resolving a promise here logs an error 87 | process.exit(); 88 | }); 89 | } 90 | 91 | promises.push( 92 | new Promise((resolve, reject) => { 93 | childProcess.once("error", error => reject(error)); 94 | childProcess.once("exit", code => { 95 | if (code) { 96 | reject(new Error(`Non zero exit code from child process`)); 97 | } else { 98 | resolve(); 99 | } 100 | }); 101 | }) 102 | ); 103 | }); 104 | 105 | await Promise.all(promises); 106 | } 107 | -------------------------------------------------------------------------------- /packages/techdocs-cli/src/lib/utility.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Spotify AB 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import { 17 | RemoteProtocol, 18 | ParsedLocationAnnotation 19 | } from "@backstage/techdocs-common"; 20 | import * as winston from "winston"; 21 | 22 | export const convertTechDocsRefToLocationAnnotation = ( 23 | techdocsRef: string 24 | ): ParsedLocationAnnotation => { 25 | // Split on the first colon for the protocol and the rest after the first split 26 | // is the location. 27 | const [type, target] = techdocsRef.split(/:(.+)/) as [ 28 | RemoteProtocol?, 29 | string? 30 | ]; 31 | 32 | if (!type || !target) { 33 | throw new Error( 34 | `Can not parse --techdocs-ref ${techdocsRef}. Should be of type HOST:URL.` 35 | ); 36 | } 37 | 38 | return { type, target }; 39 | }; 40 | 41 | export const createLogger = ({ 42 | verbose = false 43 | }: { 44 | verbose: boolean; 45 | }): winston.Logger => { 46 | const logger = winston.createLogger({ 47 | level: verbose ? "verbose" : "info", 48 | transports: [ 49 | new winston.transports.Console({ format: winston.format.simple() }) 50 | ] 51 | }); 52 | 53 | return logger; 54 | }; 55 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@backstage/cli/config/tsconfig.json", 3 | "include": ["packages/*/src"], 4 | "compilerOptions": { 5 | "outDir": "dist-types", 6 | "rootDir": "." 7 | } 8 | } 9 | --------------------------------------------------------------------------------