UNPKG

@vismaux/ngx-nordic-cool

Version:
88 lines 4.55 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.ngAddSetupProject = void 0; const schematics_1 = require("@angular-devkit/schematics"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const change_1 = require("@schematics/angular/utility/change"); const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils"); const workspace_1 = require("@schematics/angular/utility/workspace"); const path = require("path"); const ts = require("typescript"); const project_1 = require("../utils/project"); const messages_1 = require("./messages"); function ngAddSetupProject(options) { return (0, schematics_1.chain)([ updateAppModule(options), addStyles(options), (0, schematics_1.externalSchematic)('@angular/localize', 'ng-add', options.project ? { name: options.project } : {}), ]); } exports.ngAddSetupProject = ngAddSetupProject; const ANIMATION_MODULE_NAME = 'BrowserAnimationsModule'; const NO_ANIMATION_MODULE_NAME = 'NoopAnimationsModule'; const ANIMATION_MODULE_PACKAGE_NAME = '@angular/platform-browser/animations'; function updateAppModule(options) { return (host) => __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, workspace_1.getWorkspace)(host); const projectName = options.project || workspace.extensions.defaultProject; const project = workspace.projects.get(projectName); if (!project) { throw new schematics_1.SchematicsException((0, messages_1.noProject)(projectName)); } const buildOptions = (0, project_1.getProjectTargetOptions)(project, 'build'); const modulePath = (0, ng_ast_utils_1.getAppModulePath)(host, buildOptions.main); const text = host.read(modulePath); if (text === null) { throw new schematics_1.SchematicsException(`File '${modulePath}' does not exist.`); } const source = ts.createSourceFile(modulePath, text.toString('utf-8'), ts.ScriptTarget.Latest, true); const changes = (0, ast_utils_1.addImportToModule)(source, modulePath, options.animations ? ANIMATION_MODULE_NAME : NO_ANIMATION_MODULE_NAME, ANIMATION_MODULE_PACKAGE_NAME); const recorder = host.beginUpdate(modulePath); for (const change of changes) { if (change instanceof change_1.InsertChange) { recorder.insertLeft(change.pos, change.toAdd); } } host.commitUpdate(recorder); }); } const SUPPORTED_BOOTSTRAP_STYLE_IMPORTS = { '.sass': `@use '@vismaux/ngx-nordic-cool'\n`, '.scss': `@use '@vismaux/ngx-nordic-cool';\n`, }; function addStyles(options) { return (host) => __awaiter(this, void 0, void 0, function* () { var _a; const workspace = yield (0, workspace_1.getWorkspace)(host); const projectName = options.project || ((_a = workspace.extensions.defaultProject) === null || _a === void 0 ? void 0 : _a.toString()); const project = workspace.projects.get(projectName); if (!project) { throw new schematics_1.SchematicsException((0, messages_1.noProject)(projectName)); } const styleFilePath = (0, project_1.getProjectStyleFile)(project) || ''; const styleFileExtension = path.extname(styleFilePath); const styleFilePatch = SUPPORTED_BOOTSTRAP_STYLE_IMPORTS[styleFileExtension]; return addToStylesFile(styleFilePath, styleFilePatch); }); } function addToStylesFile(styleFilePath, styleFilePatch) { return (host) => { const styleFileContents = host.read(styleFilePath); if (!styleFileContents) { return; } const recorder = host.beginUpdate(styleFilePath); recorder.insertRight(0, styleFilePatch); host.commitUpdate(recorder); }; } //# sourceMappingURL=setup-project.js.map