UNPKG

@alyle/ui

Version:

Minimal Design, a set of components for Angular

166 lines • 8.29 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 core_1 = require("@angular-devkit/core"); const ts = require("typescript"); const vendored_ast_utils_1 = require("../utils/vendored-ast-utils"); const schematics_1 = require("@angular/cdk/schematics"); const schematics_2 = require("@angular-devkit/schematics"); const gestures_1 = require("./gestures"); const fonts_1 = require("./fonts"); const styles_1 = require("../utils/styles"); const ast_1 = require("../utils/ast"); function updateAppModule(options) { return (host, _context) => __awaiter(this, void 0, void 0, function* () { _context.logger.debug('Updating appmodule'); const themes = options.themes.length ? options.themes : ['minima-light']; const theme = options.themes[0]; const modulePath = (yield (0, schematics_1.findModuleFromOptions)(host, options)); _context.logger.debug(`module path: ${modulePath}`); // add `import { BrowserAnimationsModule } ...` let moduleSource = (0, schematics_1.parseSourceFile)(host, modulePath); const browserAnimationsModuleName = 'BrowserAnimationsModule'; let importPath = '@angular/platform-browser/animations'; if (!(0, vendored_ast_utils_1.isImported)(moduleSource, browserAnimationsModuleName, importPath)) { const change = (0, vendored_ast_utils_1.insertImport)(moduleSource, modulePath, browserAnimationsModuleName, importPath); if (change) { const recorder = host.beginUpdate(modulePath); recorder.insertLeft(change.pos, change.toAdd); host.commitUpdate(recorder); } } // add `imports: [ BrowserAnimationsModule ] ...` moduleSource = (0, schematics_1.parseSourceFile)(host, modulePath); let metadataChanges = (0, vendored_ast_utils_1.addSymbolToNgModuleMetadata)(moduleSource, modulePath, 'imports', browserAnimationsModuleName); if (metadataChanges) { const recorder = host.beginUpdate(modulePath); metadataChanges.forEach((change) => { recorder.insertRight(change.pos, change.toAdd); }); host.commitUpdate(recorder); } // add `import { HammerModuleName } ...` moduleSource = (0, schematics_1.parseSourceFile)(host, modulePath); const HammerModuleName = 'HammerModuleName'; const HammerModuleImportPath = '@angular/platform-browser/animations'; if (!(0, vendored_ast_utils_1.isImported)(moduleSource, HammerModuleName, HammerModuleImportPath)) { const change = (0, vendored_ast_utils_1.insertImport)(moduleSource, modulePath, HammerModuleName, HammerModuleImportPath); if (change) { const recorder = host.beginUpdate(modulePath); recorder.insertLeft(change.pos, change.toAdd); host.commitUpdate(recorder); } } // add `imports: [ HammerModuleName ] ...` moduleSource = (0, schematics_1.parseSourceFile)(host, modulePath); metadataChanges = (0, vendored_ast_utils_1.addSymbolToNgModuleMetadata)(moduleSource, modulePath, 'imports', HammerModuleName); if (metadataChanges) { const recorder = host.beginUpdate(modulePath); metadataChanges.forEach((change) => { recorder.insertRight(change.pos, change.toAdd); }); host.commitUpdate(recorder); } // add import theme ['LY_THEME', 'LY_THEME_NAME'].forEach((_import) => { moduleSource = (0, schematics_1.parseSourceFile)(host, modulePath); const importModule = _import; importPath = '@alyle/ui'; if (!(0, vendored_ast_utils_1.isImported)(moduleSource, importModule, importPath)) { const change = (0, vendored_ast_utils_1.insertImport)(moduleSource, modulePath, importModule, importPath); if (change) { const recorder = host.beginUpdate(modulePath); recorder.insertLeft(change.pos, change.toAdd); host.commitUpdate(recorder); } } }); [ 'StyleRenderer', 'LyTheme2' ].forEach(provider => { (0, ast_1.addProvider)(host, modulePath, 'NgModule', provider, '@alyle/ui'); }); // set theme moduleSource = (0, schematics_1.parseSourceFile)(host, modulePath); const themeSimbolName = `{ provide: LY_THEME_NAME, useValue: '${theme}' }`; metadataChanges = (0, vendored_ast_utils_1.addSymbolToNgModuleMetadata)(moduleSource, modulePath, 'providers', themeSimbolName); if (metadataChanges) { const recorder = host.beginUpdate(modulePath); metadataChanges.forEach((change) => { recorder.insertRight(change.pos, change.toAdd); }); host.commitUpdate(recorder); } themes.forEach(_themeName => { const [themePath] = _themeName.split('-'); // register providers moduleSource = getTsSourceFile(host, modulePath); const simbolName = `{ provide: LY_THEME, useClass: ${core_1.strings.classify(_themeName)}, multi: true }`; metadataChanges = (0, vendored_ast_utils_1.addSymbolToNgModuleMetadata)(moduleSource, modulePath, 'providers', simbolName); if (metadataChanges) { const recorder = host.beginUpdate(modulePath); metadataChanges.forEach((change) => { recorder.insertRight(change.pos, change.toAdd); }); host.commitUpdate(recorder); } // add import themes moduleSource = getTsSourceFile(host, modulePath); const importModuleThemeName = core_1.strings.classify(_themeName); importPath = `@alyle/ui/themes/${themePath}`; if (!(0, vendored_ast_utils_1.isImported)(moduleSource, importModuleThemeName, importPath)) { const change = (0, vendored_ast_utils_1.insertImport)(moduleSource, modulePath, importModuleThemeName, importPath); if (change) { const recorder = host.beginUpdate(modulePath); recorder.insertLeft(change.pos, change.toAdd); host.commitUpdate(recorder); } } }); }); } function getTsSourceFile(host, path) { const buffer = host.read(path); if (!buffer) { throw new schematics_2.SchematicsException(`Could not read file (${path}).`); } const content = buffer.toString(); const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); return source; } const STYLES = `\n\nconst STYLES = (theme: ThemeVariables, ref: ThemeRef) => { const __ = ref.selectorsOf(STYLES); return { $global: lyl \`{ body { background-color: \${theme.background.default} color: \${theme.text.default} font-family: \${theme.typography.fontFamily} margin: 0 direction: \${theme.direction} } }\`, root: lyl \`{ display: block }\` }; };`; function default_1(options) { return (0, schematics_2.chain)([ (0, gestures_1.addHammerJsToMain)(options), updateAppModule(options), (0, fonts_1.addFontsToIndex)(options), (0, styles_1.setUpStyles)(options, undefined, undefined, STYLES), ]); } //# sourceMappingURL=setup-project.js.map