ngx-pendo
Version:
A simple wrapper to load Pendo by angular way
80 lines • 4.18 kB
JavaScript
;
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.default = default_1;
const schematics_1 = require("@angular-devkit/schematics");
const utility_1 = require("@schematics/angular/utility");
const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
const workspace_1 = require("@schematics/angular/utility/workspace");
const workspace_models_1 = require("@schematics/angular/utility/workspace-models");
const package_config_1 = require("./package-config");
const ast_1 = require("../utils/ast");
const change_1 = require("../utils/change");
const project_1 = require("../utils/project");
const standalone_1 = require("../utils/standalone");
const packageJson = require('../../package.json');
function addImportToNgModule(mainFile, options) {
return (host) => {
const appModulePath = (0, ng_ast_utils_1.getAppModulePath)(host, mainFile);
const moduleSource = (0, ast_1.parseSourceFile)(host, appModulePath);
const ngModuleName = `NgxPendoModule.forRoot({
pendoApiKey: '${options.pendoApiKey}',
pendoIdFormatter: (pendoId: string) => pendoId.toLowerCase()
})`;
(0, change_1.applyChangesToFile)(host, appModulePath, [
(0, ast_utils_1.insertImport)(moduleSource, appModulePath, 'NgxPendoModule', 'ngx-pendo'),
...(0, ast_utils_1.addSymbolToNgModuleMetadata)(moduleSource, appModulePath, 'imports', ngModuleName, null)
]);
};
}
function addStandaloneConfig(mainFile, options) {
return (host) => {
const providerFn = 'provideNgxPendo';
if ((0, standalone_1.callsProvidersFunction)(host, mainFile, providerFn)) {
// exit because the ngx-pendo config is already provided
return host;
}
return (0, utility_1.addRootProvider)(options.project, ({ code, external }) => {
return code `${external(providerFn, 'ngx-pendo')}({ pendoApiKey: '${options.pendoApiKey}', pendoIdFormatter: (pendoId: string) => pendoId.toLowerCase() })`;
});
};
}
function addNgxPendoToPackageJson() {
return (host) => {
(0, package_config_1.addPackageToPackageJson)(host, 'ngx-pendo', `~${packageJson.version}`);
return host;
};
}
function default_1(options) {
return (0, schematics_1.chain)([
addNgxPendoToPackageJson(),
(host, context) => __awaiter(this, void 0, void 0, function* () {
const workspace = yield (0, workspace_1.getWorkspace)(host);
const project = (0, project_1.getProjectFromWorkspace)(workspace, options.project);
const mainFile = (0, project_1.getProjectMainFile)(project);
if (project.extensions['projectType'] === workspace_models_1.ProjectType.Application) {
if ((0, ng_ast_utils_1.isStandaloneApp)(host, mainFile)) {
return addStandaloneConfig(mainFile, options);
}
else {
return addImportToNgModule(mainFile, options);
}
}
context.logger.warn('ngx-pendo has been set up in your workspace. There is no additional setup ' +
'required for consuming ngx-pendo in your library project.\n\n' +
'If you intended to run the schematic on a different project, pass the `--project` ' +
'option.');
return;
})
]);
}
//# sourceMappingURL=index.js.map