aboutsummaryrefslogtreecommitdiff
path: root/dev/lib/handlebars/scripts/print_ast.js
diff options
context:
space:
mode:
authorDarius Jahandarie <djahandarie@gmail.com>2023-11-09 13:30:31 +0000
committerGitHub <noreply@github.com>2023-11-09 13:30:31 +0000
commit5c5a167b4792af379cdacf633513cebf20728cd2 (patch)
tree5b6be3620a557d0b9177047003f6d742d9d2a32d /dev/lib/handlebars/scripts/print_ast.js
parentb64f51c3b13a46af4dd7f1e43048ac19c781ca7b (diff)
parent0f4d36938fd0d844f548aa5a7f7e7842df8dfb41 (diff)
Merge pull request #307 from themoeway/modernize
Modernize codebase
Diffstat (limited to 'dev/lib/handlebars/scripts/print_ast.js')
-rwxr-xr-xdev/lib/handlebars/scripts/print_ast.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/dev/lib/handlebars/scripts/print_ast.js b/dev/lib/handlebars/scripts/print_ast.js
new file mode 100755
index 00000000..b97fb5a6
--- /dev/null
+++ b/dev/lib/handlebars/scripts/print_ast.js
@@ -0,0 +1,64 @@
+#!/usr/bin/env node
+/*
+ * Elasticsearch B.V licenses this file to you under the MIT License.
+ * See `packages/kbn-handlebars/LICENSE` for more information.
+ */
+'use strict'; // eslint-disable-line strict
+
+const { relative } = require('path');
+const { inspect } = require('util');
+
+const { parse } = require('handlebars');
+const argv = require('minimist')(process.argv.slice(2));
+
+const DEFAULT_FILTER = 'loc,strip,openStrip,inverseStrip,closeStrip';
+
+const filter = argv['show-all'] ? [''] : (argv.filter || DEFAULT_FILTER).split(',');
+const hideEmpty = argv['hide-empty'] || false;
+const template = argv._[0];
+
+if (template === undefined) {
+ const script = relative(process.cwd(), process.argv[1]);
+ console.log(`Usage: ${script} [options] <template>`);
+ console.log();
+ console.log('Options:');
+ console.log(' --filter=... A comma separated list of keys to filter from the output.');
+ console.log(` Default: ${DEFAULT_FILTER}`);
+ console.log(' --hide-empty Do not display empty properties.');
+ console.log(' --show-all Do not filter out any properties. Equivalent to --filter="".');
+ console.log();
+ console.log('Example:');
+ console.log(` ${script} --hide-empty -- 'hello {{name}}'`);
+ console.log();
+ process.exit(1);
+}
+
+console.log(inspect(reduce(parse(template, filter)), { colors: true, depth: null }));
+
+function reduce(ast) {
+ if (Array.isArray(ast)) {
+ for (let i = 0; i < ast.length; i++) {
+ ast[i] = reduce(ast[i]);
+ }
+ } else {
+ for (const k of filter) {
+ delete ast[k];
+ }
+
+ if (hideEmpty) {
+ for (const [k, v] of Object.entries(ast)) {
+ if (v === undefined || v === null || (Array.isArray(v) && v.length === 0)) {
+ delete ast[k];
+ }
+ }
+ }
+
+ for (const [k, v] of Object.entries(ast)) {
+ if (typeof v === 'object' && v !== null) {
+ ast[k] = reduce(v);
+ }
+ }
+ }
+
+ return ast;
+}