UNPKG

ng-apexcharts

Version:
175 lines (174 loc) 8.39 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.default = default_1; const schematics_1 = require("@angular-devkit/schematics"); const workspace_1 = require("@schematics/angular/utility/workspace"); const workspace_models_1 = require("@schematics/angular/utility/workspace-models"); const rules_1 = require("@schematics/angular/utility/standalone/rules"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const change_1 = require("@schematics/angular/utility/change"); const utils_1 = require("../utils"); const ts = require("typescript"); const scriptPath = `/node_modules/apexcharts/dist/apexcharts.min.js`; function default_1(options) { return (host, context) => __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, workspace_1.getWorkspace)(host); const project = (0, utils_1.getProjectFromWorkspace)(workspace, options.project); if (project.extensions["projectType"] !== workspace_models_1.ProjectType.Application) { context.logger.warn(`project '${options.project}' is not an angular application. it look like angular library`); return; } return (0, schematics_1.chain)([addNgApexchartsModule(options), addApexchartsToScripts(options)]); }); } function addNgApexchartsModule(options) { return (host, _context) => __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, workspace_1.getWorkspace)(host); const project = (0, utils_1.getProjectFromWorkspace)(workspace, options.project); const ngxApexchartModuleName = "NgApexchartsModule"; const libraryName = "ng-apexcharts"; // Check if this is a standalone application const isStandaloneApp = yield isStandaloneApplication(host, project); if (isStandaloneApp) { // Use addRootImport for standalone applications return (0, rules_1.addRootImport)(options.project, ({ code, external }) => code `${external(ngxApexchartModuleName, libraryName)}`); } else { // Use traditional module import for module-based applications return (tree) => { try { // Find the app module file - it could be app.module.ts or app-module.ts const possibleModulePaths = [ `/projects/${options.project}/src/app/app.module.ts`, `/projects/${options.project}/src/app/app-module.ts` ]; let modulePath = null; for (const path of possibleModulePaths) { if (tree.exists(path)) { modulePath = path; break; } } if (!modulePath) { throw new Error(`Could not find app module file for project ${options.project}`); } // Manually add the import to the module addModuleImportToModule(tree, modulePath, ngxApexchartModuleName, libraryName); } catch (error) { throw error; } return tree; }; } }); } // Custom function to add module import directly to a specific module file function addModuleImportToModule(host, modulePath, moduleName, src) { const moduleSource = parseSourceFile(host, modulePath); if (!moduleSource) { throw new Error(`Module not found: ${modulePath}`); } const changes = (0, ast_utils_1.addImportToModule)(moduleSource, modulePath, moduleName, src); const recorder = host.beginUpdate(modulePath); changes.forEach((change) => { if (change instanceof change_1.InsertChange) { recorder.insertLeft(change.pos, change.toAdd); } }); host.commitUpdate(recorder); } function parseSourceFile(host, path) { const buffer = host.read(path); if (!buffer) { throw new Error(`Could not read file: ${path}`); } const content = buffer.toString(); return ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); } function isStandaloneApplication(host, project) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; try { // Get the main file path const buildOptions = (_b = (_a = project.targets) === null || _a === void 0 ? void 0 : _a.get('build')) === null || _b === void 0 ? void 0 : _b.options; if (!buildOptions) { return false; } const mainPath = (buildOptions['browser'] || buildOptions['main']); if (!mainPath || !host.exists(mainPath)) { return false; } // Read the main.ts file to check if it uses bootstrapApplication (standalone) or bootstrapModule (traditional) const mainContent = (_c = host.read(mainPath)) === null || _c === void 0 ? void 0 : _c.toString(); if (!mainContent) { return false; } // If it contains bootstrapApplication, it's a standalone app if (mainContent.includes('bootstrapApplication')) { return true; } // If it contains bootstrapModule, it's a traditional module-based app if (mainContent.includes('bootstrapModule')) { return false; } // Default to false (module-based) if we can't determine return false; } catch (error) { // Default to false (module-based) if there's any error return false; } }); } function addApexchartsToScripts(options) { return (host, _context) => __awaiter(this, void 0, void 0, function* () { ; const scriptPath = `/node_modules/apexcharts/dist/apexcharts.min.js`; return (0, schematics_1.chain)([addScripts(options.project, 'build', host, scriptPath), addScripts(options.project, 'test', host, scriptPath)]); ; }); } function addScripts(projectName, targetName, host, assetPath) { return (0, workspace_1.updateWorkspace)(workspace => { const project = (0, utils_1.getProjectFromWorkspace)(workspace, projectName); const targetOptions = (0, utils_1.getProjectTargetOptions)(project, targetName); if (!targetOptions) { return; } ; // Initialize scripts as empty array if it doesn't exist if (!targetOptions['scripts']) { targetOptions['scripts'] = []; } const scripts = targetOptions['scripts']; const existingScripts = scripts.map(s => (typeof s === 'string' ? s : s.input)); for (let [, scriptPath] of existingScripts.entries()) { if (scriptPath === assetPath) return; } scripts.unshift(assetPath); setUpdatedTargetOptions(host, targetOptions, targetName, projectName); }); } function setUpdatedTargetOptions(host, targetOptions, targetName, projectName) { if (host.exists('angular.json')) { const currentAngular = JSON.parse(host.read('angular.json').toString('utf-8')); if (currentAngular['projects'][projectName].targets) { currentAngular['projects'][projectName].targets[targetName]['options'] = targetOptions; } if (currentAngular['projects'][projectName].architect) { currentAngular['projects'][projectName].architect[targetName]['options'] = targetOptions; } host.overwrite('angular.json', JSON.stringify(currentAngular, null, 2)); } }