Skip to content

Commit 2c91d7a

Browse files
committed
fix: remove async handlebars
1 parent 1be6cac commit 2c91d7a

File tree

5 files changed

+50
-61
lines changed

5 files changed

+50
-61
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"chalk": "^3.0.0",
1414
"fs-extra": "^10.0.1",
1515
"handlebars": "^4.7.7",
16-
"handlebars-async-helpers": "^1.0.3",
1716
"lodash.uniqby": "^4.7.0",
1817
"mkdirp": "^1.0.4",
1918
"tslib": "^2"

src/ditamap/command.ts

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,35 @@ export type CommandHelpInfo = {
2525
required: boolean;
2626
kind: string;
2727
type: string;
28+
defaultHelpValue?: string;
29+
default: string | (() => Promise<string>);
30+
};
31+
32+
const getDefault = async (flag: CommandHelpInfo): Promise<string> => {
33+
if (typeof flag.default !== 'function') {
34+
return flag.default;
35+
} else if (typeof flag.default === 'function') {
36+
try {
37+
const help = await flag.default();
38+
return help || '';
39+
} catch {
40+
return '';
41+
}
42+
} else {
43+
return '';
44+
}
2845
};
2946

3047
export class Command extends Ditamap {
48+
private flags: Dictionary<CommandHelpInfo>;
49+
3150
public constructor(topic: string, subtopic: string, command: Dictionary, commandMeta: JsonMap = {}) {
3251
const commandWithUnderscores = ensureString(command.id).replace(/:/g, '_');
3352
const filename = Ditamap.file(`cli_reference_${commandWithUnderscores}`, 'xml');
3453

3554
super(filename, {});
3655

37-
const flags = ensureObject(command.flags);
38-
const parameters = this.getParametersForTemplate(flags as Dictionary<CommandHelpInfo>);
56+
this.flags = ensureObject(command.flags);
3957

4058
const summary = punctuate(asString(command.summary));
4159

@@ -86,7 +104,6 @@ export class Command extends Ditamap {
86104
summary,
87105
description,
88106
help,
89-
parameters,
90107
isClosedPilotCommand: state === 'closedPilot',
91108
isOpenPilotCommand: state === 'openPilot',
92109
isBetaCommand: state === 'beta',
@@ -97,24 +114,35 @@ export class Command extends Ditamap {
97114
this.destination = join(Ditamap.outputDir, topic, filename);
98115
}
99116

100-
public getParametersForTemplate(flags: Dictionary<CommandHelpInfo>) {
101-
return Object.entries(flags)
102-
.filter(([, flag]) => !flag.hidden)
103-
.map(([flagName, flag]) => {
104-
const description = Array.isArray(flag.description) ? flag.description.join('\n') : flag.description || '';
105-
const entireDescription = flag.summary ? `${flag.summary}\n${description}` : description;
106-
107-
return Object.assign(flag, {
108-
name: flagName,
109-
description: this.formatParagraphs(entireDescription),
110-
optional: !flag.required,
111-
kind: flag.kind || flag.type,
112-
hasValue: flag.type !== 'boolean',
113-
});
117+
public async getParametersForTemplate(flags: Dictionary<CommandHelpInfo>) {
118+
const final = [] as CommandHelpInfo[];
119+
120+
for (const [flagName, flag] of Object.entries(flags)) {
121+
if (flag.hidden) continue;
122+
const description = Array.isArray(flag.description) ? flag.description.join('\n') : flag.description || '';
123+
const entireDescription = flag.summary ? `${flag.summary}\n${description}` : description;
124+
const updated = Object.assign(flag, {
125+
name: flagName,
126+
description: this.formatParagraphs(entireDescription),
127+
optional: !flag.required,
128+
kind: flag.kind || flag.type,
129+
hasValue: flag.type !== 'boolean',
130+
defaultFlagValue: await getDefault(flag),
114131
});
132+
final.push(updated);
133+
}
134+
return final;
115135
}
116136

117137
public getTemplateFileName(): string {
118138
return 'command.hbs';
119139
}
140+
141+
protected async transformToDitamap() {
142+
// eslint-disable-next-line no-console
143+
console.log('transformToDitamap');
144+
const parameters = await this.getParametersForTemplate(this.flags);
145+
this.data = Object.assign({}, this.data, { parameters });
146+
return super.transformToDitamap();
147+
}
120148
}

src/ditamap/ditamap.ts

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,10 @@ import * as fs from 'fs/promises';
1010
import * as mkdirp from 'mkdirp';
1111
import { JsonMap } from '@salesforce/ts-types';
1212
import * as debugCreator from 'debug';
13-
import * as handlebars from 'handlebars';
14-
import * as asyncHelpers from 'handlebars-async-helpers';
15-
16-
const hb = asyncHelpers(handlebars);
13+
import * as hb from 'handlebars';
1714

1815
const debug = debugCreator('commandreference');
1916

20-
const getDefault = async (flag: { default: () => any }): Promise<string> => {
21-
if (typeof flag.default !== 'function') {
22-
return new hb.SafeString(`<dd>Default value: ${flag.default}</dd>`);
23-
} else if (typeof flag.default === 'function') {
24-
try {
25-
const help = await flag.default();
26-
if (!help) return '';
27-
return new hb.SafeString(`<dd>Default value: ${help}</dd>`);
28-
} catch {
29-
return '';
30-
}
31-
} else {
32-
return '';
33-
}
34-
};
35-
3617
hb.registerHelper('toUpperCase', (str) => str.toUpperCase());
3718
hb.registerHelper('join', (array) => array.join(', '));
3819
hb.registerHelper('xmlFile', (...strings) => {
@@ -44,20 +25,6 @@ hb.registerHelper('uniqueId', (...strings) => {
4425
return Ditamap.file(parts.join('_'), 'xml').replace('.xml', '');
4526
});
4627

47-
hb.registerHelper('getDefault', async function (flag) {
48-
return getDefault(flag);
49-
});
50-
51-
hb.registerHelper('hasDefault', async function (flag) {
52-
if (!flag.default) {
53-
return false;
54-
} else if (Array.isArray(flag.default)) {
55-
return flag.default.length > 0;
56-
} else {
57-
return !!flag.default && !!(await getDefault(flag));
58-
}
59-
});
60-
6128
/*
6229
* Returns true if the string should be formatted as code block in docs
6330
*/
@@ -137,10 +104,10 @@ export abstract class Ditamap {
137104
* @param templateName
138105
* @returns {object}
139106
*/
140-
private async transformToDitamap() {
107+
protected async transformToDitamap() {
141108
debug(`Generating ${this.destination} from ${this.getTemplateFileName()}`);
142109
const src = await fs.readFile(this.source, 'utf8');
143-
const template = hb.compile(src);
110+
const template = hb.compile(src, { noEscape: false });
144111
return template(this.data);
145112
}
146113
}

templates/command.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ IF YOU WANT TO CHANGE THIS CONTENT, CONTACT [email protected] FOR D
100100
{{/if}}<dd>{{#if optional}}Optional{{else}}Required{{/if}}</dd>
101101
<dd>{{#each description}}<p>{{[]}}</p>{{/each}}</dd>{{#if kind}}
102102
<dd>Type: {{kind}}</dd>{{/if}}{{#if options}}
103-
<dd>Permissible values are: {{join options}}</dd>{{/if}}{{#if (hasDefault this)}}
104-
{{getDefault this}}{{/if}}
103+
<dd>Permissible values are: {{join options}}</dd>{{/if}}{{#if defaultFlagValue}}
104+
<dd>{{defaultFlagValue}}</dd>{{/if}}
105105
</dlentry>
106106
{{/each}}
107107
</dl>

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3920,11 +3920,6 @@ [email protected]:
39203920
resolved "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz#f2735dc2283674fa67478b10181059355c369e5e"
39213921
integrity sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==
39223922

3923-
handlebars-async-helpers@^1.0.3:
3924-
version "1.0.3"
3925-
resolved "https://registry.npmjs.org/handlebars-async-helpers/-/handlebars-async-helpers-1.0.3.tgz#30cfa24b1d32d4b7073909a38dacf6cac88901be"
3926-
integrity sha512-t2zkA054ZxkXdtDeGbHTDP9rtbabzF8iHYY46+FhzqTNaw22l0WlcNpnb36vKAv1M22MnLA1i/LUIH3A74dl1g==
3927-
39283923
handlebars@^4.7.7:
39293924
version "4.7.7"
39303925
resolved "https://registry.npmjs.org/handlebars/-/handlebars-4.7.7.tgz#9ce33416aad02dbd6c8fafa8240d5d98004945a1"

0 commit comments

Comments
 (0)