UNPKG

ngx-matomo-client

Version:

Matomo (fka. Piwik) client for Angular applications

190 lines 9.32 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ngAdd = ngAdd; const schematics_1 = require("@angular-devkit/schematics"); const tasks_1 = require("@angular-devkit/schematics/tasks"); const utility_1 = require("@schematics/angular/utility"); const typescript_1 = require("typescript"); const utils_1 = require("../utils"); const version_1 = require("../version"); function hasAngularRouterDependency(host) { return (0, utils_1.getPackageJsonDependency)(host, '@angular/router') != null; } function checkRequiredRouterDependency(host) { if (!hasAngularRouterDependency(host)) { throw new schematics_1.SchematicsException(`You chose to automatically track page view, but this requires @angular/router as a dependency.\n` + `You can run "ng add @angular/router" to add it to your application.`); } } function addPackageJsonDependencies(options) { return (host, context) => { (0, utility_1.addDependency)('ngx-matomo-client', version_1.version, { type: utility_1.DependencyType.Default, }); if (!options.skipLegacyPackageMigration) { (0, utils_1.removePackageJsonDependency)(host, '@ngx-matomo/tracker'); (0, utils_1.removePackageJsonDependency)(host, '@ngx-matomo/router'); } if (options.router) { checkRequiredRouterDependency(host); } context.addTask(new tasks_1.NodePackageInstallTask()); return host; }; } function buildTrackerConfig(options, context) { const trackerUrl = (0, utils_1.escapeLiteral)(options.serverUrl || ''); const siteId = (0, utils_1.escapeLiteral)(options.siteId || ''); const scriptUrl = (0, utils_1.escapeLiteral)(options.scriptUrl || ''); const embeddedMode = !!scriptUrl && !trackerUrl && !siteId; let config; if (embeddedMode) { config = `{ scriptUrl: '${scriptUrl}' }`; } else { if (scriptUrl) { config = `{ trackerUrl: '${trackerUrl}', siteId: '${siteId}', scriptUrl: '${scriptUrl}' }`; } else { config = `{ trackerUrl: '${trackerUrl}', siteId: '${siteId}' }`; } if (!trackerUrl || !siteId) { context.logger.warn('Configuration properties "siteId" and "trackerUrl" are usually required. ' + 'You will need to manually update your configuration.'); } } return config; } function getDefaultProjectName(workspace) { var _a; const keys = Array.from(workspace.projects.keys()); return ((_a = keys.find(key => { var _a; return ((_a = workspace.projects.get(key)) === null || _a === void 0 ? void 0 : _a.extensions.projectType) === 'application'; })) !== null && _a !== void 0 ? _a : keys[0]); } function getProjectName(options, host) { return __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, utility_1.readWorkspace)(host); const projectName = options.project || getDefaultProjectName(workspace); const project = workspace.projects.get(projectName); if (!project) { throw new schematics_1.SchematicsException(options.project ? `No project found with name "${options.project}"` : `No project found in the workspace!`); } return projectName; }); } function getProjectPath(options, host) { return __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, utility_1.readWorkspace)(host); const projectName = options.project || Array.from(workspace.projects.keys())[0]; const project = workspace.projects.get(projectName); if (!project) { throw new schematics_1.SchematicsException(options.project ? `No project found with name "${options.project}"` : `No project found in the workspace!`); } return `/${project.root}`; }); } function addProviders(options, context) { return (host) => __awaiter(this, void 0, void 0, function* () { const projectName = yield getProjectName(options, host); const trackerConfig = buildTrackerConfig(options, context); const entryPoints = getImportEntryPoints(host, options); return (0, utility_1.addRootProvider)(projectName, ({ code, external }) => { if (options.router) { return code `${external('provideMatomo', entryPoints.core)}( ${trackerConfig}, ${external('withRouter', entryPoints.router)}() )`; } else { return code `${external('provideMatomo', entryPoints.core)}(${trackerConfig})`; } }); }); } function addImports(options, context) { return (host) => __awaiter(this, void 0, void 0, function* () { const projectName = yield getProjectName(options, host); const trackerConfig = buildTrackerConfig(options, context); const entryPoints = getImportEntryPoints(host, options); const rules = [ (0, utility_1.addRootImport)(projectName, ({ code, external }) => code `${external('MatomoModule', entryPoints.core)}.forRoot(${trackerConfig})`), ]; if (options.router) { rules.push((0, utility_1.addRootImport)(projectName, ({ code, external }) => code `${external('MatomoRouterModule', entryPoints.router)}`)); } return (0, schematics_1.chain)(rules); }); } function getImportEntryPoints(host, options) { const useSecondaryEntryPoint = !hasAngularRouterDependency(host) && !options.router; const core = useSecondaryEntryPoint ? 'ngx-matomo-client/core' : 'ngx-matomo-client'; const router = useSecondaryEntryPoint ? 'ngx-matomo-client/router' : 'ngx-matomo-client'; return { core, router }; } function migrateAllLegacyImports(options, context) { return (host) => __awaiter(this, void 0, void 0, function* () { if (!options.skipLegacyPackageMigration) { const projectPath = yield getProjectPath(options, host); const migrationRoot = host.getDir(projectPath); const entryPoints = getImportEntryPoints(host, options); context.logger.info(`Migrating imports from legacy @ngx-matomo/* packages at ${projectPath}...`); migrationRoot.visit(path => { if (path.endsWith('.ts') && path.startsWith(projectPath)) { const file = (0, utils_1.readIntoSourceFile)(host, path); const changes = []; file.forEachChild(node => { if (node.kind === typescript_1.SyntaxKind.ImportDeclaration) { const statement = node; const moduleSpecifier = statement.moduleSpecifier; const fullText = moduleSpecifier.getFullText(); const text = moduleSpecifier.text; const pos = moduleSpecifier.pos; if (text === '@ngx-matomo/tracker') { // Be sure to keep any original spacings (contained in getFullText() only) const newFullText = fullText.replace(text, entryPoints.core); changes.push(new utils_1.ReplaceChange(path, pos, fullText, newFullText)); } if (text === '@ngx-matomo/router') { // Be sure to keep any original spacings (contained in getFullText() only) const newFullText = fullText.replace(text, entryPoints.router); changes.push(new utils_1.ReplaceChange(path, pos, fullText, newFullText)); } } }); const recorder = host.beginUpdate(path); (0, utils_1.applyToUpdateRecorder)(recorder, changes); host.commitUpdate(recorder); } }); } return host; }); } function getImportRule(options, context) { if (options.skipImport) { return schematics_1.noop; } return options.useModuleImport ? addImports(options, context) : addProviders(options, context); } function ngAdd(options) { return (_host, context) => __awaiter(this, void 0, void 0, function* () { return (0, schematics_1.chain)([ addPackageJsonDependencies(options), getImportRule(options, context), migrateAllLegacyImports(options, context), ]); }); } //# sourceMappingURL=index.js.map