2019-03-12 22:07:57 +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.
|
|
|
|
*/
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2017-12-21 17:40:13 +00:00
|
|
|
import { OptionDefinition } from 'capitano';
|
|
|
|
import * as ent from 'ent';
|
2019-04-18 15:49:17 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as readline from 'readline';
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
export function getOptionPrefix(signature: string) {
|
|
|
|
if (signature.length > 1) {
|
|
|
|
return '--';
|
|
|
|
} else {
|
|
|
|
return '-';
|
|
|
|
}
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
export function getOptionSignature(signature: string) {
|
|
|
|
return `${getOptionPrefix(signature)}${signature}`;
|
|
|
|
}
|
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
export function parseCapitanoOption(option: OptionDefinition): string {
|
2017-12-21 17:40:13 +00:00
|
|
|
let result = getOptionSignature(option.signature);
|
|
|
|
|
2020-03-13 16:09:43 +00:00
|
|
|
if (Array.isArray(option.alias)) {
|
2019-03-12 22:07:57 +00:00
|
|
|
for (const alias of option.alias) {
|
2018-01-04 16:17:43 +00:00
|
|
|
result += `, ${getOptionSignature(alias)}`;
|
2017-12-21 17:40:13 +00:00
|
|
|
}
|
2020-03-13 16:13:21 +00:00
|
|
|
} else if (typeof option.alias === 'string') {
|
2018-01-04 16:17:43 +00:00
|
|
|
result += `, ${getOptionSignature(option.alias)}`;
|
2017-12-21 17:40:13 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 16:17:43 +00:00
|
|
|
if (option.parameter) {
|
2017-12-21 17:40:13 +00:00
|
|
|
result += ` <${option.parameter}>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ent.encode(result);
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2019-04-18 15:49:17 +00:00
|
|
|
|
|
|
|
export class MarkdownFileParser {
|
|
|
|
constructor(public mdFilePath: string) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the lines of a markdown document section with the given title.
|
|
|
|
* For example, consider this sample markdown document:
|
|
|
|
* ```
|
|
|
|
* # balena CLI
|
|
|
|
*
|
|
|
|
* ## Introduction
|
|
|
|
* Lorem ipsum dolor sit amet, consectetur adipiscing elit,
|
|
|
|
*
|
|
|
|
* ## Getting Started
|
|
|
|
* sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
|
|
|
*
|
|
|
|
* ### Prerequisites
|
|
|
|
* - Foo
|
|
|
|
* - Bar
|
|
|
|
*
|
|
|
|
* ## Support
|
|
|
|
* Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Calling getSectionOfTitle('Getting Started') for the markdown doc above
|
|
|
|
* returns everything from line '## Getting Started' (included) to line
|
|
|
|
* '## Support' (excluded). This method counts the number of '#' characters
|
|
|
|
* to determine that subsections should be included as part of the parent
|
|
|
|
* section.
|
|
|
|
*
|
|
|
|
* @param title The section title without '#' chars, eg. 'Getting Started'
|
|
|
|
*/
|
|
|
|
public async getSectionOfTitle(
|
|
|
|
title: string,
|
|
|
|
includeSubsections = true,
|
|
|
|
): Promise<string> {
|
|
|
|
let foundSectionLines: string[];
|
|
|
|
let foundSectionLevel = 0;
|
|
|
|
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: fs.createReadStream(this.mdFilePath),
|
|
|
|
crlfDelay: Infinity,
|
|
|
|
});
|
|
|
|
|
|
|
|
rl.on('line', line => {
|
|
|
|
// try to match a line like "## Getting Started", where the number
|
|
|
|
// of '#' characters is the sectionLevel ('##' -> 2), and the
|
|
|
|
// sectionTitle is "Getting Started"
|
|
|
|
const match = /^(#+)\s+(.+)/.exec(line);
|
|
|
|
if (match) {
|
|
|
|
const sectionLevel = match[1].length;
|
|
|
|
const sectionTitle = match[2];
|
|
|
|
|
|
|
|
// If the target section had already been found: append a line, or end it
|
|
|
|
if (foundSectionLines) {
|
|
|
|
if (!includeSubsections || sectionLevel <= foundSectionLevel) {
|
|
|
|
// end previously found section
|
|
|
|
rl.close();
|
|
|
|
}
|
|
|
|
} else if (sectionTitle === title) {
|
|
|
|
// found the target section
|
|
|
|
foundSectionLevel = sectionLevel;
|
|
|
|
foundSectionLines = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (foundSectionLines) {
|
|
|
|
foundSectionLines.push(line);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
rl.on('close', () => {
|
|
|
|
if (foundSectionLines) {
|
|
|
|
resolve(foundSectionLines.join('\n'));
|
|
|
|
} else {
|
|
|
|
reject(
|
|
|
|
new Error(
|
2020-01-20 21:21:05 +00:00
|
|
|
`Markdown section not found: title="${title}" file="${this.mdFilePath}"`,
|
2019-04-18 15:49:17 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|