UNPKG

@o3r/schematics

Version:

Schematics module of the Otter framework

100 lines 5.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createOtterSchematic = exports.createSchematicWithMetricsIfInstalled = void 0; const node_fs_1 = require("node:fs"); const path = require("node:path"); const schematics_1 = require("@angular-devkit/schematics"); const confirm_1 = require("@inquirer/confirm"); const dependencies_1 = require("@schematics/angular/utility/dependencies"); const dependencies_2 = require("../rule-factories/ng-add/dependencies"); const index_1 = require("../rule-factories/options/index"); const noopSchematicWrapper = (fn) => fn; const PACKAGE_JSON_PATH = 'package.json'; const ENV_VAR_LIST = ['CI', 'CONTINUOUS_INTEGRATION']; const setupO3rMetricsInPackageJson = (activated) => (tree) => { if (tree.exists(PACKAGE_JSON_PATH)) { const packageJson = tree.readJson(PACKAGE_JSON_PATH); packageJson.config ||= {}; packageJson.config.o3r ||= {}; packageJson.config.o3r.telemetry = activated; tree.overwrite(PACKAGE_JSON_PATH, JSON.stringify(packageJson, null, 2)); } }; const enVarValueCheck = (value) => !!value && value !== 'false' && value !== '0'; const isInCI = () => { return ENV_VAR_LIST.some((label) => enVarValueCheck(process.env[label])) || Object.entries(process.env).some(([envVar, value]) => envVar.startsWith('CI_') && enVarValueCheck(value)) || !process.stdin.isTTY; }; const setupTelemetry = ({ workingDirectory, runNgAdd, exactO3rVersion }) => (_, context) => { const taskIdsFromContext = (0, dependencies_2.hasSetupInformation)(context) ? context.setupDependencies.taskIds : undefined; const version = JSON.parse((0, node_fs_1.readFileSync)(path.join(__dirname, '..', '..', 'package.json'), 'utf8')).version; return (0, dependencies_2.setupDependencies)({ dependencies: { '@o3r/telemetry': { inManifest: [{ range: `${exactO3rVersion ? '' : '~'}${version}`, types: [dependencies_1.NodeDependencyType.Dev] }], ngAddOptions: { exactO3rVersion } } }, ngAddToRun: runNgAdd ? ['@o3r/telemetry'] : [], runAfterTasks: taskIdsFromContext, workingDirectory }); }; /** * Wrapper method of a schematic to retrieve some metrics around the schematic run * if `@o3r/telemetry` is installed * NOTE: please do not use it directly, instead use {@link createOtterSchematic} to wrap your schematic * @param schematicFn */ const createSchematicWithMetricsIfInstalled = (schematicFn) => (opts) => async (tree, context) => { let wrapper = noopSchematicWrapper; let shouldInstallTelemetry; const packageJson = tree.exists(PACKAGE_JSON_PATH) ? tree.readJson(PACKAGE_JSON_PATH) : {}; try { const { createSchematicWithMetrics } = await Promise.resolve().then(() => require('@o3r/telemetry')); wrapper = createSchematicWithMetrics; } catch (e) { // Do not throw if `@o3r/telemetry is not installed if ((process.env.NX_CLI_SET !== 'true' || process.env.NX_INTERACTIVE === 'true') && context.interactive && typeof packageJson.config?.o3r?.telemetry === 'undefined' && process.env.O3R_METRICS !== 'false' && opts.o3rMetrics !== false && !isInCI()) { context.logger.debug('`@o3r/telemetry` is not available.\nAsking to add the dependency\n' + e.toString()); shouldInstallTelemetry = await (0, confirm_1.default)({ message: ` Would you like to share anonymous data about the usage of Otter builders and schematics with the Otter Team at Amadeus ? It will help us to improve our tools. For more details and instructions on how to change these settings, see https://github.com/AmadeusITGroup/otter/blob/main/docs/telemetry/PRIVACY_NOTICE.md `, default: false }); } } const subtreeDirectory = context.schematic.description.name === 'typescript-shell' ? opts.directory.split(path.posix.sep).join(path.sep) ?? '' : ''; const rule = (0, schematics_1.chain)([ typeof shouldInstallTelemetry === 'undefined' ? (0, schematics_1.noop)() : (0, schematics_1.applyToSubtree)(subtreeDirectory, [setupO3rMetricsInPackageJson(!!shouldInstallTelemetry)]), schematicFn(opts), shouldInstallTelemetry ? (0, schematics_1.applyToSubtree)(subtreeDirectory, [ setupTelemetry({ workingDirectory: subtreeDirectory || undefined, runNgAdd: !subtreeDirectory, exactO3rVersion: opts.exactO3rVersion }) ]) : (0, schematics_1.noop)(), typeof shouldInstallTelemetry !== 'undefined' && subtreeDirectory ? (0, schematics_1.applyToSubtree)(subtreeDirectory, [setupO3rMetricsInPackageJson(shouldInstallTelemetry)]) : (0, schematics_1.noop)() ]); return wrapper(() => rule)(opts); }; exports.createSchematicWithMetricsIfInstalled = createSchematicWithMetricsIfInstalled; /** * Wrapper method of an Otter schematics * @param schematicFn */ const createOtterSchematic = (schematicFn) => (0, index_1.createSchematicWithOptionsFromWorkspace)((0, exports.createSchematicWithMetricsIfInstalled)(schematicFn)); exports.createOtterSchematic = createOtterSchematic; //# sourceMappingURL=wrapper.js.map