UNPKG

ng-afelio

Version:
194 lines (192 loc) 7.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const tasks_1 = require("@angular-devkit/schematics/tasks"); const dependencies_1 = require("@schematics/angular/utility/dependencies"); const find_module_1 = require("@schematics/angular/utility/find-module"); const workspace_1 = require("@schematics/angular/utility/workspace"); const packages_util_1 = require("../util/packages-util"); function installDependencies(options) { const needBootstrap = !!options && options.type === 'bootstrap'; return (host, context) => { const toInstall = []; if (needBootstrap) { toInstall.push({ type: dependencies_1.NodeDependencyType.Default, name: 'bootstrap', version: '4.1.3', overwrite: true, }); toInstall.push({ type: dependencies_1.NodeDependencyType.Default, name: 'font-awesome', version: '4.7.0', overwrite: true, }); } if (needBootstrap || options.type === 'afelio') { toInstall.push({ type: dependencies_1.NodeDependencyType.Dev, name: 'ngx-highlight-js', version: '10.0.3', overwrite: true, }); toInstall.push({ type: dependencies_1.NodeDependencyType.Default, name: '@ng-select/ng-select', version: '7.3.0', overwrite: true, }); } if (options.type === 'tailwind') { toInstall.push({ type: dependencies_1.NodeDependencyType.Dev, name: 'tailwindcss', version: '^3.3.2', overwrite: true, }); toInstall.push({ type: dependencies_1.NodeDependencyType.Dev, name: 'postcss', version: '^8.4.24', overwrite: true, }); toInstall.push({ type: dependencies_1.NodeDependencyType.Dev, name: 'autoprefixer', version: '^10.4.13', overwrite: true, }); toInstall.push({ type: dependencies_1.NodeDependencyType.Dev, name: 'postcss-import', version: '^15.1.0', overwrite: true, }); toInstall.push({ type: dependencies_1.NodeDependencyType.Dev, name: 'postcss-nested-ancestors', version: '^3.0.0', overwrite: true, }); toInstall.push({ type: dependencies_1.NodeDependencyType.Dev, name: 'prettier-plugin-tailwindcss', version: '^0.3.0', overwrite: true, }); toInstall.push({ type: dependencies_1.NodeDependencyType.Dev, name: 'prettier', version: '^2.8.8', overwrite: true, }); } toInstall.forEach(dep => { (0, dependencies_1.addPackageJsonDependency)(host, dep); }); context.addTask(new tasks_1.NodePackageInstallTask(), []); }; } function updateScripts() { return (host) => { const packageFile = '/package.json'; const text = host.read(packageFile); if (!text) { throw new schematics_1.SchematicsException(`Can not find "package.json" file in your project.`); } const sourceText = text.toString('utf8'); const toInsert = `"style": "ng-afelio style"`; const alreadyInstalled = sourceText.indexOf(toInsert) !== -1; if (!alreadyInstalled) { const toReplaceMatch = sourceText.match(/"start": "(.+)"/); if (toReplaceMatch) { const jsonContent = JSON.parse(sourceText); jsonContent.scripts['start'] = 'ng-afelio start'; jsonContent.scripts['style'] = 'ng-afelio style'; jsonContent.scripts['style-watch'] = 'ng-afelio style --watch projects/ui-kit'; host.overwrite(packageFile, JSON.stringify(jsonContent, null, 2)); } } return host; }; } function addLinesToMainStyleFile(options) { return (host) => { const styleFilePath = (0, core_1.join)(options.projectPath, 'styles.scss'); const text = host.read(styleFilePath); if (!text) { throw new schematics_1.SchematicsException(`Can not find "styles.scss" file in your project.`); } const sourceText = text.toString('utf8'); const relativeMainStylePath = (0, find_module_1.buildRelativePath)(styleFilePath, '/styles/main.scss'); const toInsert = ` @tailwind base; @tailwind components; @tailwind utilities; @import '${relativeMainStylePath}'; `; const alreadyInstalled = sourceText.indexOf('@tailwind base') !== -1; if (!alreadyInstalled) { const newStyleContent = `${sourceText}${toInsert}`; host.overwrite(styleFilePath, newStyleContent); } return host; }; } async function applyUiKitTemplate(options, projectUiKitPath = '/projects/ui-kit/src') { const templateSource = (0, schematics_1.apply)((0, schematics_1.url)((0, core_1.join)('./files', options.type)), [ (0, schematics_1.template)({ ...core_1.strings, ...options, }), (0, schematics_1.move)(projectUiKitPath), ]); return (0, schematics_1.mergeWith)(templateSource, schematics_1.MergeStrategy.Overwrite); } function default_1(options) { return async (host) => { const workspaceConfigBuffer = host.read('angular.json'); if (!workspaceConfigBuffer) { throw new schematics_1.SchematicsException('Not an Angular CLI workspace'); } const workspace = await (0, workspace_1.getWorkspace)(host); const project = workspace.projects.get(options.project); let projectAppPath; if (project) { projectAppPath = (0, workspace_1.buildDefaultPath)(project); } else { throw new schematics_1.SchematicsException(`Project "${options.project}" not found.`); } options.projectPath = (0, core_1.join)(projectAppPath, '..'); let rules = []; if (options.type === 'bootstrap' || options.type === 'afelio') { rules = [ (0, schematics_1.externalSchematic)('@schematics/angular', 'application', { name: 'ui-kit', routing: true, style: 'scss', skipTests: true, skipInstall: true, minimal: true, }), await applyUiKitTemplate(options), updateScripts(), installDependencies(options), ]; } else if (options.type === 'tailwind') { rules = [ await applyUiKitTemplate(options, '/'), (0, packages_util_1.installNpmSchematicPackage)('@angular/material'), installDependencies(options), (0, schematics_1.externalSchematic)('@angular/material', 'ng-add', {}), addLinesToMainStyleFile(options) ]; } return (0, schematics_1.chain)(rules); }; } exports.default = default_1;