|
| 1 | +import { Rule, SchematicContext, SchematicsException, Tree, chain } from '@angular-devkit/schematics'; |
| 2 | +import { experimental, JsonParseMode, parseJson } from '@angular-devkit/core'; |
| 3 | +import { addPackageJsonDependency, NodeDependency, NodeDependencyType } from 'schematics-utilities'; |
| 4 | + |
| 5 | +function addPackageJsonDependencies(): Rule { |
| 6 | + return (host: Tree, context: SchematicContext) => { |
| 7 | + |
| 8 | + // always add the package under dev dependencies |
| 9 | + const dependencies: NodeDependency[] = [ |
| 10 | + { type: NodeDependencyType.Dev, version: '~0.0.0', name: '@ngx-builders/analyze' }, |
| 11 | + { type: NodeDependencyType.Dev, version: '~2.4.2', name: 'source-map-explorer' } |
| 12 | + ]; |
| 13 | + |
| 14 | + dependencies.forEach(dependency => { |
| 15 | + addPackageJsonDependency(host, dependency); |
| 16 | + context.logger.log('info', `✅️ Added "${dependency.name}" into ${dependency.type}`); |
| 17 | + }); |
| 18 | + |
| 19 | + return host; |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +function getWorkspace(host: Tree): { path: string; workspace: experimental.workspace.WorkspaceSchema } { |
| 24 | + const possibleFiles = ['/angular.json', './angular.json']; |
| 25 | + const path = possibleFiles.find(path => host.exists(path)); |
| 26 | + |
| 27 | + if (!path) { |
| 28 | + throw new SchematicsException(`Could not find angular.json`); |
| 29 | + } |
| 30 | + |
| 31 | + const configBuffer = host.read(path); |
| 32 | + if (!configBuffer) { |
| 33 | + throw new SchematicsException(`Could not find angular.json`); |
| 34 | + } |
| 35 | + |
| 36 | + const content = configBuffer.toString(); |
| 37 | + let workspace: experimental.workspace.WorkspaceSchema; |
| 38 | + |
| 39 | + try { |
| 40 | + workspace = <any>parseJson(content, JsonParseMode.Loose) as experimental.workspace.WorkspaceSchema; |
| 41 | + } catch (e) { |
| 42 | + throw new SchematicsException(`Could not parse angular.json: ${e.message}`); |
| 43 | + } |
| 44 | + |
| 45 | + return { path, workspace }; |
| 46 | +} |
| 47 | + |
| 48 | +interface NgAddOptions { |
| 49 | + project?: string; |
| 50 | + siteID: string; |
| 51 | + netlifyToken: string; |
| 52 | +} |
| 53 | + |
| 54 | +export function sourceMapBuilder(options: NgAddOptions): Rule { |
| 55 | + return (tree: Tree, _context: SchematicContext) => { |
| 56 | + // get the workspace details |
| 57 | + const { path: workspacePath, workspace } = getWorkspace(tree); |
| 58 | + |
| 59 | + // getting project name |
| 60 | + if (!options.project) { |
| 61 | + if (workspace.defaultProject) { |
| 62 | + options.project = workspace.defaultProject; |
| 63 | + } else { |
| 64 | + throw new SchematicsException( |
| 65 | + 'No Angular project selected and no default project in the workspace' |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Validating project name |
| 71 | + const project = workspace.projects[options.project]; |
| 72 | + if (!project) { |
| 73 | + throw new SchematicsException( |
| 74 | + 'The specified Angular project is not defined in this workspace' |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + // Checking if it is application |
| 79 | + if (project.projectType !== 'application') { |
| 80 | + throw new SchematicsException( |
| 81 | + `Deploy requires an Angular project type of "application" in angular.json` |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + // Getting output path from Angular.json |
| 86 | + if ( |
| 87 | + !project.architect || |
| 88 | + !project.architect.build || |
| 89 | + !project.architect.build.options || |
| 90 | + !project.architect.build.options.outputPath |
| 91 | + ) { |
| 92 | + throw new SchematicsException( |
| 93 | + `Cannot read the output path(architect.build.options.outputPath) of the Angular project "${options.project}" in angular.json` |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + // adding deploy statement for builder |
| 98 | + project.architect['analyze'] = { |
| 99 | + "builder": "@ngx-builders/analyze:analyze", |
| 100 | + "options": { |
| 101 | + "outputPath": project.architect.build.options.outputPath |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + tree.overwrite(workspacePath, JSON.stringify(workspace, null, 2)); |
| 106 | + return tree; |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +export default function (options: NgAddOptions): Rule { |
| 111 | + return chain([ |
| 112 | + sourceMapBuilder(options), |
| 113 | + addPackageJsonDependencies() |
| 114 | + ]); |
| 115 | +} |
0 commit comments