Skip to content

build: example list outputting quoted components #13955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions tools/example-module/generate-example-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ function buildImportsTemplate(data: ExampleMetadata): string {
/** Inlines the example module template with the specified parsed data. */
function inlineExampleModuleTemplate(parsedData: ExampleMetadata[]): string {
const exampleImports = parsedData.map(m => buildImportsTemplate(m)).join('\n');
const quotePlaceholder = '◬';
const exampleList = parsedData.reduce((result, data) => {
return result.concat(data.component).concat(data.additionalComponents);
}, [] as string[]).join(',');

const exampleComponents = parsedData.map((metaData) => {
const fields = [
`title: '${metaData.title.trim()}'`,
`component: ${metaData.component}`,
];

if (metaData.additionalFiles.length) {
fields.push(`additionalFiles: ${JSON.stringify(metaData.additionalFiles)}`);
}
if (metaData.selectorName.length) {
fields.push(`selectorName: '${metaData.selectorName.join(', ')}'`);
}
const data = '\n' + fields.map(field => ' ' + field).join(',\n');
return `'${metaData.id}': {${data}\n }`;
const exampleComponents = parsedData.reduce((result, data) => {
result[data.id] = {
title: data.title,
// Since we use JSON.stringify to output the data below, the `component` will be wrapped
// in quotes, whereas we want a reference to the class. Add placeholder characters next to
// where the quotes will be so that we can strip them away afterwards.
component: `${quotePlaceholder}${data.component}${quotePlaceholder}`,
additionalFiles: data.additionalFiles,
selectorName: data.selectorName.join(', '),
};

return result;
}, {} as any);

return fs.readFileSync(require.resolve('./example-module.template'), 'utf8')
.replace('${exampleImports}', exampleImports)
.replace('${exampleComponents}', `{\n ${exampleComponents.join(',\n ')}}`)
.replace('${exampleList}', `[${exampleList}]`);
.replace('${exampleComponents}', JSON.stringify(exampleComponents))
.replace('${exampleList}', `[${exampleList}]`)
.replace(new RegExp(`"${quotePlaceholder}|${quotePlaceholder}"`, 'g'), '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only part I am not sure I am understanding is the or in regex here?

why are we looking for ◬|◬ instead of just since we are global matching

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crisbeto add merge ready once your ready.

Copy link
Member Author

@crisbeto crisbeto Nov 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the quotes inside it. It looks for "◬|◬" (e.g. "◬something◬").

}

/** Converts a given camel-cased string to a dash-cased string. */
Expand Down