Skip to content

Commit

Permalink
feat(telemetry): add telemetry wrapper for all cli (#2227)
Browse files Browse the repository at this point in the history
## Proposed change
Add telemetry wrapper for all cli

## Related issues

- 🚀 Feature #1201
  • Loading branch information
matthieu-crouzet authored Oct 4, 2024
2 parents 87b4e6d + ecdd24f commit 553fbdd
Show file tree
Hide file tree
Showing 23 changed files with 201 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/@ama-sdk/create/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
'files': ['package.json'],
'rules': {
'@o3r/json-dependency-versions-harmonize': ['error', {
'ignoredPackages': ['@o3r/build-helpers'],
'ignoredPackages': ['@o3r/build-helpers', '@o3r/telemetry'],
ignoredDependencies: ['yarn'],
'alignPeerDependencies': false
}]
Expand Down
1 change: 1 addition & 0 deletions packages/@ama-sdk/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@o3r/build-helpers": "workspace:^",
"@o3r/eslint-config-otter": "workspace:^",
"@o3r/eslint-plugin": "workspace:^",
"@o3r/telemetry": "workspace:^",
"@o3r/test-helpers": "workspace:^",
"@stylistic/eslint-plugin-ts": "~2.4.0",
"@types/jest": "~29.5.2",
Expand Down
12 changes: 12 additions & 0 deletions packages/@ama-sdk/create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { execSync, spawnSync } from 'node:child_process';
import { dirname, extname, join, parse, relative, resolve } from 'node:path';
import * as minimist from 'minimist';
import { LOCAL_SPEC_FILENAME, SPEC_JSON_EXTENSION, SPEC_YAML_EXTENSION } from '@ama-sdk/schematics';
import type { CliWrapper } from '@o3r/telemetry';

const packageManagerEnv = process.env.npm_config_user_agent?.split('/')[0];
const binPath = resolve(require.resolve('@angular-devkit/schematics-cli/package.json'), '../bin/schematics.js');
Expand Down Expand Up @@ -145,3 +146,14 @@ const run = () => {
};

run();

void (async () => {
let wrapper: CliWrapper = (fn: any) => fn;
try {
const { createCliWithMetrics } = await import('@o3r/telemetry');
wrapper = createCliWithMetrics;
} catch {
// Do not throw if `@o3r/telemetry` is not installed
}
return wrapper(run, '@o3r/create:create')();
})();
14 changes: 13 additions & 1 deletion packages/@ama-sdk/schematics/cli/clear-index.cts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Remove deleted models' exports
*/

import type { CliWrapper } from '@o3r/telemetry';
import * as minimist from 'minimist';
import { promises as fs, statSync } from 'node:fs';
import { resolve } from 'node:path';
Expand All @@ -20,7 +21,7 @@ if (help) {
process.exit(0);
}

void (async () => {
const run = async () => {
const models = await fs.readdir(baseDir);
const shouldRemoveModels = (
await Promise.all(
Expand All @@ -41,4 +42,15 @@ void (async () => {
return fs.unlink(resolve(baseDir, model, 'index.ts'));
})
);
};

void (async () => {
let wrapper: CliWrapper = (fn: any) => fn;
try {
const { createCliWithMetrics } = await import('@o3r/telemetry');
wrapper = createCliWithMetrics;
} catch {
// Do not throw if `@o3r/telemetry` is not installed
}
return wrapper(run, '@ama-sdk/schematics:clear-index')();
})();
15 changes: 14 additions & 1 deletion packages/@ama-sdk/schematics/cli/files-pack.cts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Update the Typescript SDK Package to expose the sub modules
*/

import type { CliWrapper } from '@o3r/telemetry';
import { copyFileSync, mkdirSync } from 'node:fs';
import * as minimist from 'minimist';
import * as path from 'node:path';
Expand Down Expand Up @@ -65,7 +66,7 @@ const updateExports = async () => {
await fs.writeFile(path.join(baseDir, distFolder, 'package.json'), JSON.stringify(packageJson, null, 2));
};

void (async () => {
const run = async () => {

const copyToDist = (file: string, cwdForCopy: string) => {
const distFile = path.resolve(baseDir, distFolder, path.relative(cwdForCopy, file));
Expand Down Expand Up @@ -99,4 +100,16 @@ void (async () => {
if (!noExports && !watch) {
await updateExports();
}
};


void (async () => {
let wrapper: CliWrapper = (fn: any) => fn;
try {
const { createCliWithMetrics } = await import('@o3r/telemetry');
wrapper = createCliWithMetrics;
} catch {
// Do not throw if `@o3r/telemetry` is not installed
}
return wrapper(run, '@ama-sdk/schematics:files-pack')();
})();
14 changes: 13 additions & 1 deletion packages/@ama-sdk/schematics/cli/update-spec-from-npm.cts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Update the OpenAPI spec from an NPM package
*/

import type { CliWrapper } from '@o3r/telemetry';
import * as minimist from 'minimist';
import { existsSync } from 'node:fs';
import { createRequire } from 'node:module';
Expand Down Expand Up @@ -42,7 +43,7 @@ if (!packageName) {
process.exit(-1);
}

void (async () => {
const run = async () => {
let specSourcePath;
const appRequire = createRequire(posix.join(process.cwd(), 'package.json'));
const packageJsonPath = appRequire.resolve(`${packageName}/package.json`);
Expand Down Expand Up @@ -79,4 +80,15 @@ void (async () => {

logger.info(`Updating spec file from "${specSourcePath}" to "${specDestinationPath}" (CWD: "${process.cwd()}")`);
await copyFile(specSourcePath, specDestinationPath);
};

void (async () => {
let wrapper: CliWrapper = (fn: any) => fn;
try {
const { createCliWithMetrics } = await import('@o3r/telemetry');
wrapper = createCliWithMetrics;
} catch {
// Do not throw if `@o3r/telemetry` is not installed
}
return wrapper(run, '@ama-sdk/schematics:update-spec-from-npm')();
})();
1 change: 1 addition & 0 deletions packages/@ama-sdk/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"@o3r/build-helpers": "workspace:^",
"@o3r/eslint-plugin": "workspace:^",
"@o3r/schematics": "workspace:^",
"@o3r/telemetry": "workspace:^",
"@o3r/test-helpers": "workspace:^",
"@openapitools/openapi-generator-cli": "~2.13.0",
"@schematics/angular": "~18.2.0",
Expand Down
13 changes: 13 additions & 0 deletions packages/@ama-terasu/cli/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,18 @@ module.exports = {
},
'extends': [
'../../../.eslintrc.js'
],
'overrides': [
{
'files': ['**/package.json'],
'rules': {
'@nx/dependency-checks': ['error', {
'buildTargets': ['build', 'build-builders', 'compile', 'test'],
'checkObsoleteDependencies': false,
'checkVersionMismatches': false,
'ignoredDependencies': ['ora', '@o3r/test-helpers', '@o3r/telemetry']
}]
}
}
]
};
1 change: 1 addition & 0 deletions packages/@ama-terasu/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@o3r/amaterasu-sdk": "workspace:^",
"@o3r/build-helpers": "workspace:^",
"@o3r/eslint-plugin": "workspace:^",
"@o3r/telemetry": "workspace:^",
"@schematics/angular": "~18.2.0",
"@stylistic/eslint-plugin-ts": "~2.4.0",
"@types/jest": "~29.5.2",
Expand Down
14 changes: 13 additions & 1 deletion packages/@ama-terasu/cli/src/cli/ama.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#!/usr/bin/env node

import type { CliWrapper } from '@o3r/telemetry';
import * as prompts from 'prompts';
import { amaYargs } from '../modules/base-yargs';
import * as minimist from 'minimist';

void (async () => {
const run = async () => {
const override = await amaYargs(minimist(process.argv.slice(2)));
return (prompts as any).override(override.argv);
};

void (async () => {
let wrapper: CliWrapper = (fn: any) => fn;
try {
const { createCliWithMetrics } = await import('@o3r/telemetry');
wrapper = createCliWithMetrics;
} catch {
// Do not throw if `@o3r/telemetry` is not installed
}
return wrapper(run, '@ama-terasu/cli:ama')();
})();
13 changes: 13 additions & 0 deletions packages/@o3r/artifactory-tools/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,18 @@ module.exports = {
},
'extends': [
'../../../.eslintrc.js'
],
'overrides': [
{
'files': ['**/package.json'],
'rules': {
'@nx/dependency-checks': ['error', {
'buildTargets': ['build', 'build-builders', 'compile', 'test'],
'checkObsoleteDependencies': false,
'checkVersionMismatches': false,
'ignoredDependencies': ['@o3r/test-helpers', '@o3r/telemetry']
}]
}
}
]
};
1 change: 1 addition & 0 deletions packages/@o3r/artifactory-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@nx/jest": "~19.5.0",
"@o3r/build-helpers": "workspace:^",
"@o3r/eslint-plugin": "workspace:^",
"@o3r/telemetry": "workspace:^",
"@stylistic/eslint-plugin-ts": "~2.4.0",
"@types/fs-extra": "^11.0.0",
"@types/jest": "~29.5.2",
Expand Down
15 changes: 13 additions & 2 deletions packages/@o3r/artifactory-tools/src/cli/artifact-cleaner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node



import type { CliWrapper } from '@o3r/telemetry';
import { program } from 'commander';
import * as winston from 'winston';

Expand Down Expand Up @@ -60,7 +60,7 @@ url += (url.endsWith('/') ? '' : '/') +

logger.info(`Url called : ${url}`);

void (async () => {
const run = async () => {
logger.info(`Requesting old artifacts using ${url}`);
let responseSearch;
let responseSearchObj: { results: { uri: string }[] };
Expand All @@ -83,4 +83,15 @@ void (async () => {
logger.info(response);
}
}
};

void (async () => {
let wrapper: CliWrapper = (fn: any) => fn;
try {
const { createCliWithMetrics } = await import('@o3r/telemetry');
wrapper = createCliWithMetrics;
} catch {
// Do not throw if `@o3r/telemetry` is not installed
}
return wrapper(run, '@o3r/artifactory-tools:artifact-cleaner', { logger, preParsedOptions: opts })();
})();
16 changes: 13 additions & 3 deletions packages/@o3r/artifactory-tools/src/cli/pr-artifact-cleaner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import type { CliWrapper } from '@o3r/telemetry';
import { program } from 'commander';
import * as winston from 'winston';

Expand Down Expand Up @@ -78,8 +79,7 @@ const fetchOptions = {
logger.debug(`AQL search executed : ${fetchOptions.body}`);
logger.info(`Url called : ${url}`);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
const run = async () => {
logger.info(`Requesting old artifacts using ${url}`);
let responseSearch: any;
let responseSearchObj: { results: { repo: string; path: string; name: string }[] };
Expand Down Expand Up @@ -149,5 +149,15 @@ logger.info(`Url called : ${url}`);
logger.info(response);
}
}
})();
};

void (async () => {
let wrapper: CliWrapper = (fn: any) => fn;
try {
const { createCliWithMetrics } = await import('@o3r/telemetry');
wrapper = createCliWithMetrics;
} catch {
// Do not throw if `@o3r/telemetry` is not installed
}
return wrapper(run, '@o3r/artifactory-tools:pr-artifact-cleaner', { logger, preParsedOptions: programOptions })();
})();
13 changes: 13 additions & 0 deletions packages/@o3r/azure-tools/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,18 @@ module.exports = {
},
'extends': [
'../../../.eslintrc.js'
],
'overrides': [
{
'files': ['**/package.json'],
'rules': {
'@nx/dependency-checks': ['error', {
'buildTargets': ['build', 'build-builders', 'compile', 'test'],
'checkObsoleteDependencies': false,
'checkVersionMismatches': false,
'ignoredDependencies': ['@o3r/test-helpers', '@o3r/telemetry']
}]
}
}
]
};
1 change: 1 addition & 0 deletions packages/@o3r/azure-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@nx/jest": "~19.5.0",
"@o3r/build-helpers": "workspace:^",
"@o3r/eslint-plugin": "workspace:^",
"@o3r/telemetry": "workspace:^",
"@stylistic/eslint-plugin-ts": "~2.4.0",
"@types/jest": "~29.5.2",
"@types/node": "^20.0.0",
Expand Down
14 changes: 13 additions & 1 deletion packages/@o3r/azure-tools/src/cli/comment-pr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import type { CliWrapper } from '@o3r/telemetry';
import { Option, program } from 'commander';
import * as winston from 'winston';
import { PullRequestService } from '../helpers/index';
Expand Down Expand Up @@ -35,7 +36,7 @@ const logger = winston.createLogger({
transports: new winston.transports.Console()
});

void (async () => {
const run = async () => {
logger.info('Commenting PR...');

if (!comment) {
Expand Down Expand Up @@ -74,4 +75,15 @@ void (async () => {
threads.map((thread) => prService.addCommentToThread(repositoryId, pullRequestId, thread, comment!))
);
}
};

void (async () => {
let wrapper: CliWrapper = (fn: any) => fn;
try {
const { createCliWithMetrics } = await import('@o3r/telemetry');
wrapper = createCliWithMetrics;
} catch {
// Do not throw if `@o3r/telemetry` is not installed
}
return wrapper(run, '@o3r/azure-tools:comment-pr', { logger, preParsedOptions: opts })();
})();
2 changes: 1 addition & 1 deletion packages/@o3r/create/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
'files': ['package.json'],
'rules': {
'@o3r/json-dependency-versions-harmonize': ['error', {
'ignoredPackages': ['@o3r/build-helpers'],
'ignoredPackages': ['@o3r/build-helpers', '@o3r/telemetry'],
ignoredDependencies: ['yarn'],
'alignPeerDependencies': false
}]
Expand Down
22 changes: 19 additions & 3 deletions packages/@o3r/create/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import type { CliWrapper } from '@o3r/telemetry';
import { spawnSync, type SpawnSyncOptionsWithBufferEncoding } from 'node:child_process';
import { join, resolve } from 'node:path';
import { readFileSync, writeFileSync } from 'node:fs';
Expand Down Expand Up @@ -281,6 +282,21 @@ const addOtterFramework = (relativeDirectory = '.', projectPackageManager = 'npm
const projectFolder = argv._[0]?.replaceAll(' ', '-').toLowerCase() || '.';

console.info(logo);
createNgProject();
prepareWorkspace(projectFolder, packageManager);
addOtterFramework(projectFolder, packageManager);

const run = () => {
createNgProject();
prepareWorkspace(projectFolder, packageManager);
addOtterFramework(projectFolder, packageManager);
};


void (async () => {
let wrapper: CliWrapper = (fn: any) => fn;
try {
const { createCliWithMetrics } = await import('@o3r/telemetry');
wrapper = createCliWithMetrics;
} catch {
// Do not throw if `@o3r/telemetry` is not installed
}
return wrapper(run, '@o3r/create:create')();
})();
Loading

0 comments on commit 553fbdd

Please sign in to comment.