From 1cf605c2b02cc5747f721fdb9a84deb9d284b2f1 Mon Sep 17 00:00:00 2001 From: Kilian Panot Date: Wed, 11 Sep 2024 11:33:06 +0900 Subject: [PATCH] feat(design): support for template file dependency path --- .../@o3r/design/builders/generate-css/index.ts | 15 +++++++++++++-- .../design-token-specification.interface.ts | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/@o3r/design/builders/generate-css/index.ts b/packages/@o3r/design/builders/generate-css/index.ts index 3028236d0c..145e3509e5 100644 --- a/packages/@o3r/design/builders/generate-css/index.ts +++ b/packages/@o3r/design/builders/generate-css/index.ts @@ -16,6 +16,7 @@ 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 @@ -23,7 +24,7 @@ import { readFile, writeFile } from 'node:fs/promises'; */ export default createBuilder(async (options, context): Promise => { 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!) : @@ -85,7 +86,17 @@ export default createBuilder(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), {}); } diff --git a/packages/@o3r/design/src/core/design-token/design-token-specification.interface.ts b/packages/@o3r/design/src/core/design-token/design-token-specification.interface.ts index 871de71d19..c76bb01070 100644 --- a/packages/@o3r/design/src/core/design-token/design-token-specification.interface.ts +++ b/packages/@o3r/design/src/core/design-token/design-token-specification.interface.ts @@ -238,7 +238,7 @@ export type DesignTokenGroup = - DesignTokenGroupCommonFields & { [x: string]: DesignTokenGroupTemplate | G | string | boolean | undefined }; + DesignTokenGroupCommonFields & { [x: string]: DesignTokenGroupTemplate | G | string | boolean | undefined }; /** Context of the Design Token specification document */ export type DesignTokenContext = {