Skip to content

Commit

Permalink
fix(telemetry): missing telemetry wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-crouzet committed Sep 16, 2024
1 parent d27cb52 commit 9a9e2ab
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
14 changes: 12 additions & 2 deletions packages/@o3r/design/builders/generate-css/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GenerateCssSchematicsSchema } from './schema';
import { BuilderOutput, createBuilder } from '@angular-devkit/architect';
import type { BuilderWrapper } from '@o3r/telemetry';
import {
getCssTokenDefinitionRenderer,
getCssTokenValueRenderer,
Expand All @@ -16,11 +17,20 @@ import * as globby from 'globby';
import { EOL } from 'node:os';
import { readFile } from 'node:fs/promises';

const createBuilderWithMetricsIfInstalled: BuilderWrapper = (builderFn) => async (opts, ctx) => {
let wrapper: BuilderWrapper = (fn) => fn;
try {
const { createBuilderWithMetrics } = await import('@o3r/telemetry');
wrapper = createBuilderWithMetrics;
} catch {}
return wrapper(builderFn)(opts, ctx);
};

/**
* Generate CSS from Design Token files
* @param options
*/
export default createBuilder<GenerateCssSchematicsSchema>(async (options, context): Promise<BuilderOutput> => {
export default createBuilder<GenerateCssSchematicsSchema>(createBuilderWithMetricsIfInstalled(async (options, context): Promise<BuilderOutput> => {
const designTokenFilePatterns = Array.isArray(options.designTokenFilePatterns) ? options.designTokenFilePatterns : [options.designTokenFilePatterns];
const determineFileToUpdate = options.output ? () => resolve(context.workspaceRoot, options.output!) :
(token: DesignTokenVariableStructure) => {
Expand Down Expand Up @@ -151,4 +161,4 @@ export default createBuilder<GenerateCssSchematicsSchema>(async (options, contex
return { success: false, error: String(err) };
}
}
});
}));
15 changes: 13 additions & 2 deletions packages/@o3r/design/builders/generate-jsonschema/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GenerateJsonSchemaSchematicsSchema } from './schema';
import { BuilderOutput, createBuilder } from '@angular-devkit/architect';
import type { BuilderWrapper } from '@o3r/telemetry';
import {
getJsonSchemaStyleContentUpdater,
getJsonSchemaTokenDefinitionRenderer,
Expand All @@ -10,11 +11,21 @@ import type { DesignTokenVariableSet, DesignTokenVariableStructure } from '../..
import { resolve } from 'node:path';
import * as globby from 'globby';


const createBuilderWithMetricsIfInstalled: BuilderWrapper = (builderFn) => async (opts, ctx) => {
let wrapper: BuilderWrapper = (fn) => fn;
try {
const { createBuilderWithMetrics } = await import('@o3r/telemetry');
wrapper = createBuilderWithMetrics;
} catch {}
return wrapper(builderFn)(opts, ctx);
};

/**
* Generate Json Schema from Design Token files
* @param options
*/
export default createBuilder<GenerateJsonSchemaSchematicsSchema>(async (options, context): Promise<BuilderOutput> => {
export default createBuilder<GenerateJsonSchemaSchematicsSchema>(createBuilderWithMetricsIfInstalled(async (options, context): Promise<BuilderOutput> => {
const designTokenFilePatterns = Array.isArray(options.designTokenFilePatterns) ? options.designTokenFilePatterns : [options.designTokenFilePatterns];

const execute = async (): Promise<BuilderOutput> => {
Expand Down Expand Up @@ -80,4 +91,4 @@ export default createBuilder<GenerateJsonSchemaSchematicsSchema>(async (options,
return { success: false, error: String(err) };
}
}
});
}));
5 changes: 5 additions & 0 deletions packages/@o3r/design/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@o3r/core": "workspace:^",
"@o3r/schematics": "workspace:^",
"@o3r/styling": "workspace:^",
"@o3r/telemetry": "workspace:^",
"chokidar": "^3.5.2",
"globby": "^11.1.0",
"sass": "~1.77.0"
Expand All @@ -69,6 +70,9 @@
"@o3r/styling": {
"optional": true
},
"@o3r/telemetry": {
"optional": true
},
"chokidar": {
"optional": true
},
Expand Down Expand Up @@ -105,6 +109,7 @@
"@o3r/eslint-plugin": "workspace:^",
"@o3r/schematics": "workspace:^",
"@o3r/styling": "workspace:^",
"@o3r/telemetry": "workspace:^",
"@o3r/test-helpers": "workspace:^",
"@schematics/angular": "~18.1.0",
"@stylistic/eslint-plugin-ts": "~2.4.0",
Expand Down
22 changes: 20 additions & 2 deletions packages/@o3r/design/schematics/extract-token/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { Rule } from '@angular-devkit/schematics';
import type { ExtractTokenSchematicsSchema } from './schema';
import type { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import { posix, resolve } from 'node:path';
import { AUTO_GENERATED_END, AUTO_GENERATED_START, DesignToken, DesignTokenGroup, DesignTokenNode } from '../../src/public_api';
import type { ExtractTokenSchematicsSchema } from './schema';

const patternToDetect = 'o3r.var';

/**
* Extract the token from o3r mixin sass file
* @param options
*/
export function extractToken(options: ExtractTokenSchematicsSchema): Rule {
function extractTokenFn(options: ExtractTokenSchematicsSchema): Rule {

const updateFileContent = (content: string): string => {
const start = content.indexOf(patternToDetect);
Expand Down Expand Up @@ -104,3 +105,20 @@ export function extractToken(options: ExtractTokenSchematicsSchema): Rule {
}
};
}

/**
* Extract the token from o3r mixin sass file
* @param options
*/
export const extractToken = (options: ExtractTokenSchematicsSchema) => async () => {
let createSchematicWithMetrics: typeof createSchematicWithMetricsIfInstalled | undefined;
try {
({ createSchematicWithMetricsIfInstalled: createSchematicWithMetrics } = await import('@o3r/schematics'));
} catch {
// No @o3r/schematics detected
}
if (!createSchematicWithMetrics) {
return extractTokenFn(options);
}
return createSchematicWithMetrics(extractTokenFn)(options);
};
23 changes: 21 additions & 2 deletions packages/@o3r/design/schematics/generate-css/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { GenerateCssSchematicsSchema } from './schema';
import type { Rule } from '@angular-devkit/schematics';
import { globInTree } from '@o3r/schematics';
import type { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import { parseDesignTokenFile, renderDesignTokens } from '@o3r/design';
import type { DesignTokenRendererOptions, DesignTokenVariableSet, DesignTokenVariableStructure } from '@o3r/design';

/**
* Generate CSS from Design Token files
* @param options
*/
export function generateCss(options: GenerateCssSchematicsSchema): Rule {
function generateCssFn(options: GenerateCssSchematicsSchema): Rule {
return async (tree, context) => {
const writeFile = (filePath: string, content: string) => tree.exists(filePath) ? tree.overwrite(filePath, content) : tree.create(filePath, content);
const readFile = tree.readText;
Expand All @@ -29,6 +29,8 @@ export function generateCss(options: GenerateCssSchematicsSchema): Rule {
logger: context.logger
};

const { globInTree } = await import('@o3r/schematics');

const files = globInTree(tree, Array.isArray(options.designTokenFilePatterns) ? options.designTokenFilePatterns : [options.designTokenFilePatterns]);

const duplicatedToken: DesignTokenVariableStructure[] = [];
Expand All @@ -48,3 +50,20 @@ export function generateCss(options: GenerateCssSchematicsSchema): Rule {
await renderDesignTokens(tokens, renderDesignTokenOptions);
};
}

/**
* Generate CSS from Design Token files
* @param options
*/
export const generateCss = (options: GenerateCssSchematicsSchema) => async () => {
let createSchematicWithMetrics: typeof createSchematicWithMetricsIfInstalled | undefined;
try {
({ createSchematicWithMetricsIfInstalled: createSchematicWithMetrics } = await import('@o3r/schematics'));
} catch {
// No @o3r/schematics detected
}
if (!createSchematicWithMetrics) {
return generateCssFn(options);
}
return createSchematicWithMetrics(generateCssFn)(options);
};
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7022,6 +7022,7 @@ __metadata:
"@o3r/eslint-plugin": "workspace:^"
"@o3r/schematics": "workspace:^"
"@o3r/styling": "workspace:^"
"@o3r/telemetry": "workspace:^"
"@o3r/test-helpers": "workspace:^"
"@schematics/angular": "npm:~18.1.0"
"@stylistic/eslint-plugin-ts": "npm:~2.4.0"
Expand Down Expand Up @@ -7062,6 +7063,7 @@ __metadata:
"@o3r/core": "workspace:^"
"@o3r/schematics": "workspace:^"
"@o3r/styling": "workspace:^"
"@o3r/telemetry": "workspace:^"
chokidar: ^3.5.2
globby: ^11.1.0
sass: ~1.77.0
Expand All @@ -7072,6 +7074,8 @@ __metadata:
optional: true
"@o3r/styling":
optional: true
"@o3r/telemetry":
optional: true
chokidar:
optional: true
globby:
Expand Down

0 comments on commit 9a9e2ab

Please sign in to comment.