typescript-member-signatures
Version:
Command line tool to extract given interface member signatures as JSON strings, recursively in the extends chain with the purpose of documentation
90 lines (86 loc) • 3.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const extractMemberSignatures_1 = require("../extractMemberSignatures");
const printInterfaces_1 = require("../printInterfaces");
const typescriptOutput_1 = require("../typescriptOutput");
function main() {
const options = require('yargs-parser')(process.argv.slice(2));
preconditions(options);
if (options.listInterfaces) {
const out = printInterfaces_1.listInterfaces(options);
console.log('Interfaces found: \n' + out.map(o => `"${o.name}" (${o.path})`).join('\n * '));
}
else {
const results = extractMemberSignatures_1.extractMemberSignatures(options);
let output = '';
if (options.generateMarkdownDocs) {
output = results.map(r => r.markdown).join('\n--------\n');
}
else if (options.typescriptOutput) {
output = results.map(result => typescriptOutput_1.resultToTypeScript({
result,
exportName: typeof options.typescriptOutput === 'string' ? options.typescriptOutput : undefined
}))
.join('\n\n\n');
}
else {
output = JSON.stringify(results, null, 2);
}
if (options.output) {
fs_1.writeFileSync(options.output, output);
}
else {
process.stdout.write(output + '\n');
}
}
}
exports.main = main;
function preconditions(options) {
if (!options.project) {
options.project = './tsconfig.json';
}
if (options.help) {
printHelp();
process.exit(0);
}
if (!options.project && !options.files || !options.target && !options.listInterfaces) {
printHelp();
fail(`Incorrect usage. --target or --listInterfaces are mandatory but none was given.`);
}
if (!fs_1.existsSync(options.project)) {
fail(`Incorrect call. --project tsconfig.json must exists. If not given './tsconfig.json' is assumed (current dir). Aborting.`);
}
}
function fail(s) {
console.error(s);
process.exit(1);
}
function printHelp() {
console.log(`
Usage: typescript-member-signatures --project foo/bar/tsconfig.json --target "**/area44/**/services/**/LoginService"
If you are having trouble selecting interface use --listInterfaces
Options:
* --project?: string: TypeScript project in which to search the target interface, must point to a tsconfig.json file.
* --target: string: Glob pattern pointing to the target interface. Example: "** /area44/** /services/** /LoginService".
* --output?: string: If given the result will be written to this file, if not to stdout.
* --files?: string: Extract from these files. If project is also provided, add this extra files to it.
Can be a file name or a glob pattern.
* --typescriptOutput?: string: Generate TypeScript code instead of JSON that exports a typed variable with given name or inferred from interface otherwise.
* --ignoreMemberWithUnderscorePrefix?: boolean: Will ignore members which names start with '_'
* --onlySignature?: boolean: Return only the signatures, don't generate jsdocsText, etc. only name and signature.
* --generateMarkdownDocs?: boolean: Will generate markdown text for the interface and its members suitable to include in README.md API section.
* --help?: boolean:
* --debug?: boolean:
* --listInterfaces?: boolean: If given prints found interfaces and their paths to stdout and exit. If target is given prints interfaces only on matched files/dirs, if none given prints all interfaces in project.
`);
}
// function test(){
// const result = extractMethodSignatureDocs({
// project: 'src/testAssets/tsconfig.json',
// target: '**/G'
// })
// console.log(JSON.stringify(result, null, 2));
// }
// test()
//# sourceMappingURL=cli.js.map