2019-04-18 15:49:17 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as path from 'path';
|
|
|
|
import { MarkdownFileParser } from './utils';
|
2023-11-07 22:33:48 +00:00
|
|
|
import { GlobSync } from 'glob';
|
2019-04-18 15:49:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the skeleton of CLI documentation/reference web page at:
|
|
|
|
* https://www.balena.io/docs/reference/cli/
|
|
|
|
*
|
|
|
|
* The `getCapitanoDoc` function in this module parses README.md and adds
|
|
|
|
* some content to this object.
|
2023-11-03 14:55:29 +00:00
|
|
|
*
|
|
|
|
* IMPORTANT
|
|
|
|
*
|
2024-08-22 16:03:37 +00:00
|
|
|
* All commands need to be stored under a folder in src/commands to maintain uniformity
|
2023-11-06 23:52:09 +00:00
|
|
|
* Generating docs will error out if directive not followed
|
|
|
|
* To add a custom heading for command docs, add the heading next to the folder name
|
|
|
|
* in the `commandHeadings` dictionary.
|
|
|
|
*
|
|
|
|
* This dictionary is the source of truth that creates the docs config which is used
|
|
|
|
* to generate the CLI documentation. By default, the folder name will be used.
|
2023-11-03 14:55:29 +00:00
|
|
|
*
|
2019-04-18 15:49:17 +00:00
|
|
|
*/
|
2023-11-06 23:52:09 +00:00
|
|
|
|
|
|
|
interface Category {
|
|
|
|
title: string;
|
|
|
|
files: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Documentation {
|
|
|
|
title: string;
|
|
|
|
introduction: string;
|
|
|
|
categories: Category[];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mapping folders names to custom headings in the docs
|
|
|
|
const commandHeadings: { [key: string]: string } = {
|
2024-05-14 14:23:16 +00:00
|
|
|
'api-key': 'API Keys',
|
2023-11-15 09:41:17 +00:00
|
|
|
login: 'Authentication',
|
|
|
|
whoami: 'Authentication',
|
|
|
|
logout: 'Authentication',
|
2024-05-14 14:23:16 +00:00
|
|
|
env: 'Environment Variables',
|
2023-11-06 23:52:09 +00:00
|
|
|
envs: 'Environment Variables',
|
|
|
|
help: 'Help and Version',
|
2024-09-25 11:32:08 +00:00
|
|
|
'ssh-key': 'SSH Keys',
|
2023-11-06 23:52:09 +00:00
|
|
|
orgs: 'Organizations',
|
|
|
|
os: 'OS',
|
|
|
|
util: 'Utilities',
|
2023-11-09 20:15:29 +00:00
|
|
|
ssh: 'Network',
|
|
|
|
scan: 'Network',
|
|
|
|
tunnel: 'Network',
|
|
|
|
build: 'Deploy',
|
|
|
|
join: 'Platform',
|
|
|
|
leave: 'Platform',
|
2024-05-14 14:23:16 +00:00
|
|
|
app: 'Apps',
|
|
|
|
block: 'Blocks',
|
|
|
|
device: 'Devices',
|
|
|
|
fleet: 'Fleets',
|
|
|
|
release: 'Releases',
|
|
|
|
tag: 'Tags',
|
2023-11-06 23:52:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Fetch all available commands
|
2023-11-07 22:33:48 +00:00
|
|
|
const allCommandsPaths = new GlobSync('build/commands/**/*.js', {
|
2023-11-06 23:52:09 +00:00
|
|
|
ignore: 'build/commands/internal/**',
|
2023-11-07 22:33:48 +00:00
|
|
|
}).found;
|
|
|
|
|
|
|
|
// Throw error if any commands found outside of command directories
|
|
|
|
const illegalCommandPaths = allCommandsPaths.filter((commandPath: string) =>
|
|
|
|
/^build\/commands\/[^/]+\.js$/.test(commandPath),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (illegalCommandPaths.length !== 0) {
|
|
|
|
throw new Error(
|
|
|
|
`Found the following commands without a command directory: ${illegalCommandPaths}\n
|
|
|
|
To resolve this error, move the respective commands to their resource directories or create new ones.\n
|
|
|
|
Refer to the automation/capitanodoc/capitanodoc.ts file for more information.`,
|
|
|
|
);
|
|
|
|
}
|
2023-11-06 23:52:09 +00:00
|
|
|
|
|
|
|
// Docs config template
|
2023-11-07 22:33:48 +00:00
|
|
|
const capitanoDoc: Documentation = {
|
2020-10-20 21:11:17 +00:00
|
|
|
title: 'balena CLI Documentation',
|
2019-04-18 15:49:17 +00:00
|
|
|
introduction: '',
|
2023-11-06 23:52:09 +00:00
|
|
|
categories: [],
|
2019-04-18 15:49:17 +00:00
|
|
|
};
|
|
|
|
|
2023-11-06 23:52:09 +00:00
|
|
|
// Helper function to capitalize each word of directory name
|
|
|
|
function formatTitle(dir: string): string {
|
|
|
|
return dir.replace(/(^\w|\s\w)/g, (word) => word.toUpperCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a map to track the categories for faster lookup
|
|
|
|
const categoriesMap: { [key: string]: Category } = {};
|
|
|
|
|
|
|
|
for (const commandPath of allCommandsPaths) {
|
|
|
|
const commandDir = path.basename(path.dirname(commandPath));
|
|
|
|
const heading = commandHeadings[commandDir] || formatTitle(commandDir);
|
|
|
|
|
|
|
|
if (!categoriesMap[heading]) {
|
|
|
|
categoriesMap[heading] = { title: heading, files: [] };
|
|
|
|
capitanoDoc.categories.push(categoriesMap[heading]);
|
|
|
|
}
|
|
|
|
|
|
|
|
categoriesMap[heading].files.push(commandPath);
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:44:32 +00:00
|
|
|
// Sort Category titles alphabetically
|
2023-11-06 23:52:09 +00:00
|
|
|
capitanoDoc.categories = capitanoDoc.categories.sort((a, b) =>
|
2023-11-07 22:33:48 +00:00
|
|
|
a.title.localeCompare(b.title),
|
2023-11-06 23:52:09 +00:00
|
|
|
);
|
|
|
|
|
2023-11-08 14:44:32 +00:00
|
|
|
// Sort Category file paths alphabetically
|
2023-11-06 23:52:09 +00:00
|
|
|
capitanoDoc.categories.forEach((category) => {
|
|
|
|
category.files.sort((a, b) => a.localeCompare(b));
|
|
|
|
});
|
|
|
|
|
2019-04-18 15:49:17 +00:00
|
|
|
/**
|
2023-11-06 23:52:09 +00:00
|
|
|
* Modify and return the `capitanoDoc` object above in order to generate the
|
|
|
|
* CLI documentation at docs/balena-cli.md
|
2019-04-18 15:49:17 +00:00
|
|
|
*
|
|
|
|
* This function parses the README.md file to extract relevant sections
|
|
|
|
* for the documentation web page.
|
|
|
|
*/
|
|
|
|
export async function getCapitanoDoc(): Promise<typeof capitanoDoc> {
|
|
|
|
const readmePath = path.join(__dirname, '..', '..', 'README.md');
|
|
|
|
const mdParser = new MarkdownFileParser(readmePath);
|
2019-04-29 15:33:09 +00:00
|
|
|
const sections: string[] = await Promise.all([
|
2019-04-18 15:49:17 +00:00
|
|
|
mdParser.getSectionOfTitle('About').then((sectionLines: string) => {
|
|
|
|
// delete the title of the 'About' section for the web page
|
|
|
|
const match = /^(#+)\s+.+?\n\s*([^]*)/.exec(sectionLines);
|
|
|
|
if (!match || match.length < 3) {
|
|
|
|
throw new Error(`Error parsing section title`);
|
|
|
|
}
|
|
|
|
// match[1] has the title, match[2] has the rest
|
|
|
|
return match && match[2];
|
|
|
|
}),
|
|
|
|
mdParser.getSectionOfTitle('Installation'),
|
2020-09-18 14:35:36 +00:00
|
|
|
mdParser.getSectionOfTitle('Choosing a shell (command prompt/terminal)'),
|
|
|
|
mdParser.getSectionOfTitle('Logging in'),
|
|
|
|
mdParser.getSectionOfTitle('Proxy support'),
|
2019-04-18 15:49:17 +00:00
|
|
|
mdParser.getSectionOfTitle('Support, FAQ and troubleshooting'),
|
2020-05-11 08:12:27 +00:00
|
|
|
mdParser.getSectionOfTitle('Deprecation policy'),
|
2019-04-29 15:33:09 +00:00
|
|
|
]);
|
|
|
|
capitanoDoc.introduction = sections.join('\n');
|
|
|
|
return capitanoDoc;
|
2019-04-18 15:49:17 +00:00
|
|
|
}
|