Skip to content

Commit

Permalink
feat(design): support for template file dependency path (AmadeusITGro…
Browse files Browse the repository at this point in the history
…up#2153)

## Proposed change

Support for template file dependency path
  • Loading branch information
kpanot authored Sep 11, 2024
2 parents 6f208bb + 1cf605c commit 4843b69
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions packages/@o3r/design/builders/generate-css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import { resolve } from 'node:path';
import { sync } from 'globby';
import { EOL } from 'node:os';
import { readFile, writeFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';

/**
* Generate CSS from Design Token files
* @param options
*/
export default createBuilder<GenerateCssSchematicsSchema>(async (options, context): Promise<BuilderOutput> => {
const templateFilePaths = options.templateFile
&& (typeof options.templateFile === 'string' ? [options.templateFile] : options.templateFile).map((templateFile) => resolve(context.workspaceRoot, templateFile))
&& (typeof options.templateFile === 'string' ? [options.templateFile] : options.templateFile)
|| undefined;
const designTokenFilePatterns = Array.isArray(options.designTokenFilePatterns) ? options.designTokenFilePatterns : [options.designTokenFilePatterns];
const determineFileToUpdate = options.output ? () => resolve(context.workspaceRoot, options.output!) :
Expand Down Expand Up @@ -85,7 +86,17 @@ export default createBuilder<GenerateCssSchematicsSchema>(async (options, contex
if (templateFilePaths) {
const templateFiles = await Promise.all(
templateFilePaths
.map(async (templateFile) => JSON.parse(await readFile(templateFile, { encoding: 'utf8' })) as DesignTokenGroupTemplate)
.map(async (templateFile) => {
let templateFilePath = resolve(context.workspaceRoot, templateFile);
if (!existsSync(templateFilePath)) {
try {
templateFilePath = require.resolve(templateFile);
} catch {
context.logger.error(`Cannot resolve the template file at '${templateFile}'`);
}
}
return JSON.parse(await readFile(templateFilePath, { encoding: 'utf8' })) as DesignTokenGroupTemplate;
})
);
template = templateFiles.reduce((acc, cur) => mergeDesignTokenTemplates(acc, cur), {});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export type DesignTokenGroup<E extends DesignTokenExtensions = DesignTokenExtens

/** Design Token Group for common properties only */
export type DesignTokenGroupTemplate<G extends DesignTokenGroupExtensions = DesignTokenGroupExtensions> =
DesignTokenGroupCommonFields<G> & { [x: string]: DesignTokenGroupTemplate<DesignTokenGroupExtensions> | G | string | boolean | undefined };
DesignTokenGroupCommonFields<G> & { [x: string]: DesignTokenGroupTemplate<G> | G | string | boolean | undefined };

/** Context of the Design Token specification document */
export type DesignTokenContext<G extends DesignTokenGroupExtensions = DesignTokenGroupExtensions> = {
Expand Down

0 comments on commit 4843b69

Please sign in to comment.