|
| 1 | +import '@jsii/check-node/run'; |
| 2 | + |
| 3 | +import * as chalk from 'chalk'; |
| 4 | +import * as yargs from 'yargs'; |
| 5 | + |
| 6 | +import { |
| 7 | + jsiiQuery, |
| 8 | + parseExpression, |
| 9 | + renderDocs, |
| 10 | + renderElement, |
| 11 | +} from '../lib/jsii-query'; |
| 12 | + |
| 13 | +async function main() { |
| 14 | + const argv = await yargs |
| 15 | + .usage( |
| 16 | + '$0 <FILE> [QUERY...]', |
| 17 | + 'Queries a jsii file for its entries.', |
| 18 | + (args) => |
| 19 | + args |
| 20 | + .positional('FILE', { |
| 21 | + type: 'string', |
| 22 | + desc: 'path to a .jsii file or directory to load', |
| 23 | + }) |
| 24 | + .positional('QUERY', { |
| 25 | + type: 'string', |
| 26 | + desc: 'a query or filter expression to include or exclude items', |
| 27 | + }), |
| 28 | + ) |
| 29 | + .option('types', { |
| 30 | + type: 'boolean', |
| 31 | + alias: 't', |
| 32 | + desc: 'after selecting API elements, show all selected types, as well as types containing selected members', |
| 33 | + default: false, |
| 34 | + }) |
| 35 | + .option('members', { |
| 36 | + type: 'boolean', |
| 37 | + alias: 'm', |
| 38 | + desc: 'after selecting API elements, show all selected members, as well as members of selected types', |
| 39 | + default: false, |
| 40 | + }) |
| 41 | + .options('docs', { |
| 42 | + type: 'boolean', |
| 43 | + alias: 'd', |
| 44 | + desc: 'show documentation for selected elements', |
| 45 | + default: false, |
| 46 | + }) |
| 47 | + .option('closure', { |
| 48 | + type: 'boolean', |
| 49 | + alias: 'c', |
| 50 | + default: false, |
| 51 | + desc: 'Load dependencies of package without assuming its a JSII package itself', |
| 52 | + }) |
| 53 | + .strict().epilogue(` |
| 54 | +REMARKS |
| 55 | +------- |
| 56 | +
|
| 57 | +There can be more than one QUERY part, which progressively filters from or adds |
| 58 | +to the list of selected elements. |
| 59 | +
|
| 60 | +QUERY is of the format: |
| 61 | +
|
| 62 | + [<op>]<kind>[:<expression>] |
| 63 | +
|
| 64 | +Where: |
| 65 | +
|
| 66 | + <op> The type of operation to apply. Absent means '.' |
| 67 | + + Adds new API elements matching the selector to the selection. |
| 68 | + If this selects types, it also includes all type's members. |
| 69 | + - Removes API elements from the current selection that match |
| 70 | + the selector. |
| 71 | + . Removes API elements from the current selection that do NOT |
| 72 | + match the selector (i.e., retain only those that DO match |
| 73 | + the selector) (default) |
| 74 | + <kind> Type of API element to select. One of 'type' or 'member', |
| 75 | + or any of its more specific sub-types such as 'class', |
| 76 | + 'interface', 'struct', 'enum', 'property', 'method', etc. |
| 77 | + Also supports aliases like 'c', 'm', 'mem', 's', 'p', etc. |
| 78 | + <expression> A JavaScript expression that will be evaluated against |
| 79 | + the member. Has access to a number of attributes like |
| 80 | + kind, ancestors, abstract, base, datatype, docs, interfaces, |
| 81 | + name, initializer, optional, overrides, protected, returns, |
| 82 | + parameters, static, variadic, type. The types are the |
| 83 | + same types as offered by the jsii-reflect class model. |
| 84 | +
|
| 85 | +If the first expression of the query has operator '+', then the query starts |
| 86 | +empty and the selector determines the initial set. Otherwise the query starts |
| 87 | +with all elements and the first expression is a filter on it. |
| 88 | +
|
| 89 | +This file evaluates the expressions as JavaScript, so this tool is not safe |
| 90 | +against untrusted input! |
| 91 | +
|
| 92 | +Don't forget to mind your shell escaping rules when you write query expressions. |
| 93 | +
|
| 94 | +Don't forget to add -- to terminate option parsing if you write negative expressions. |
| 95 | +
|
| 96 | +EXAMPLES |
| 97 | +------- |
| 98 | +
|
| 99 | +Select all enums: |
| 100 | +$ jsii-query --types node_modules/aws-cdk-lib enum |
| 101 | +
|
| 102 | +Select all methods with "grant" in their name: |
| 103 | +$ jsii-query --members node_modules/aws-cdk-lib 'method:name.includes("grant")' |
| 104 | +
|
| 105 | +Select all classes that have a grant method: |
| 106 | +$ jsii-query --types node_modules/aws-cdk-lib class 'method:name.includes("grant")' |
| 107 | + -or- |
| 108 | +$ jsii-query --types -- node_modules/aws-cdk-lib -interface 'method:name.includes("grant")' |
| 109 | + ^^^^ note this |
| 110 | +
|
| 111 | +Select all classes that have methods that are named either 'foo' or 'bar': |
| 112 | +$ jsii-query --types node_modules/some-package '+method:name=="foo"' '+method:name=="bar"' .class |
| 113 | +
|
| 114 | +`).argv; |
| 115 | + |
| 116 | + // Add some fields that we know are there but yargs typing doesn't know |
| 117 | + const options: typeof argv & { FILE: string; QUERY: string[] } = argv as any; |
| 118 | + |
| 119 | + if (!(options.types || options.members)) { |
| 120 | + throw new Error('At least --types or --members must be specified'); |
| 121 | + } |
| 122 | + |
| 123 | + // Yargs is annoying; if the user uses '--' to terminate the option list, |
| 124 | + // it will not parse positionals into `QUERY` but into `_` |
| 125 | + |
| 126 | + const expressions = [...options.QUERY, ...options._] |
| 127 | + .map(String) |
| 128 | + .map(parseExpression); |
| 129 | + |
| 130 | + const result = await jsiiQuery({ |
| 131 | + fileName: options.FILE, |
| 132 | + expressions, |
| 133 | + closure: options.closure, |
| 134 | + returnTypes: options.types, |
| 135 | + returnMembers: options.members, |
| 136 | + }); |
| 137 | + |
| 138 | + for (const element of result) { |
| 139 | + console.log(renderElement(element)); |
| 140 | + if (options.docs) { |
| 141 | + console.log( |
| 142 | + chalk.gray( |
| 143 | + renderDocs(element) |
| 144 | + .split('\n') |
| 145 | + .map((line) => ` ${line}`) |
| 146 | + .join('\n'), |
| 147 | + ), |
| 148 | + ); |
| 149 | + console.log(''); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + process.exitCode = result.length > 0 ? 0 : 1; |
| 154 | +} |
| 155 | + |
| 156 | +main().catch((e) => { |
| 157 | + console.log(e); |
| 158 | + process.exit(1); |
| 159 | +}); |
0 commit comments