Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 68x 68x 68x 68x 64x 64x 64x 64x 64x 64x 1x 63x 63x 63x 62x 62x 62x 62x 62x 62x 62x 62x 64x 62x 62x 62x 62x 62x 1x 61x 61x 61x 60x 60x 60x 64x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 64x 60x 60x 60x 60x 60x 60x 60x 60x 60x 64x 60x 60x 60x 60x 60x 60x 60x 60x 60x 64x 60x 60x 60x 60x 120x 120x 120x 120x 64x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x | import {Schema as InitOptions} from './schema'; import { apply, chain, MergeStrategy, mergeWith, move, Rule, schematic, SchematicsException, template, Tree, url } from '@angular-devkit/schematics'; import {strings} from '@angular-devkit/core'; import {updateWorkspace} from '@schematics/angular/utility/workspace'; import * as path from 'path'; import {EOL} from 'os'; import {forceOverwrite, getProjectName, getProjectRoot, sortByKey} from '../utils/yang-utils'; import {CodeUtils} from '../utils/code-utils'; import {extraDependencies, extraDevDependencies} from './dependencies'; import * as CJSON from 'comment-json'; export default function (options: InitOptions): Rule { return (host: Tree) => { options.name = options.name || path.basename(process.cwd()); Iif (!options.name) { throw new SchematicsException(`Invalid options, "name" is required.`); } const root = getProjectRoot(host, options); const projectName = getProjectName(host, options); return chain([ mergeWith(apply(url('./files/root'), [ template({ ...strings, ...options, buildDate: new Date().toISOString() }), move(root), forceOverwrite(host) ]), MergeStrategy.Overwrite), updatePackageJson(), updateTsConfig(options), updateGitIgnore(), updatePolyfills(options), updateKarmaTest(options), updateEnvironments(options), updateProjectWorkspace(options), schematic('feature', { project: projectName, name: 'home', component: true, inlineTemplate: false, inlineStyle: false }), mergeWith(apply(url('./files/home'), [ template({ ...strings, ...options }), move(path.join(root, 'src/app/features/home')), ]), MergeStrategy.Overwrite) ]); }; } function updatePackageJson(): (host: Tree) => Tree { return (host: Tree) => { const pkg = require('../../package.json'); const filePath = 'package.json'; if (!host.exists(filePath)) { throw new SchematicsException(`File ${filePath} does not exist.`); } const source = host.read(filePath); Iif (!source) { throw new SchematicsException(`File ${filePath} is empty.`); } const json = CJSON.parse(source.toString('utf-8')); json.scripts = { ...json.scripts, 'build': 'ng build --prod' }; json.dependencies = { ...json.dependencies, ...extraDependencies }; json.devDependencies[pkg.name] = `~${pkg.version}`; json.devDependencies = { ...json.devDependencies, ...extraDevDependencies }; // Sort dependencies by name json.dependencies = sortByKey(json.dependencies); json.devDependencies = sortByKey(json.devDependencies); host.overwrite(filePath, CJSON.stringify(json, null, 2)); return host; }; } function updateTsConfig(options: InitOptions): (host: Tree) => Tree { return (host: Tree) => { const projectRoot = getProjectRoot(host, options, true); let filePath = 'tsconfig.base.json'; Eif (!host.exists(filePath)) { filePath = 'tsconfig.json'; } if (!host.exists(filePath)) { throw new SchematicsException(`File ${filePath} does not exist.`); } const source = host.read(filePath); Iif (!source) { throw new SchematicsException(`File ${filePath} is empty.`); } const json = CJSON.parse(source.toString('utf-8')); json.compilerOptions = { ...json.compilerOptions, "paths": { "@app/*": [projectRoot + 'src/app/*'], "@env/*": [projectRoot + 'src/environments/*'] } }; host.overwrite(filePath, CJSON.stringify(json, null, 2)); return host; }; } function updateGitIgnore(): (host: Tree) => Tree { return (host: Tree) => { const filePath = '.gitignore'; Iif (!host.exists(filePath)) return host; const source = host.read(filePath); Iif (!source) return host; let content = source.toString('utf-8'); content += `${EOL}# Custom Files${EOL}`; content += `*.iml${EOL}`; content += `/src/assets/app-manifest.json${EOL}`; host.overwrite(filePath, content); return host; }; } function updatePolyfills(options: InitOptions): (host: Tree) => Tree { return (host: Tree) => { const projectRoot = getProjectRoot(host, options); const filePath = path.join(projectRoot, 'src/polyfills.ts'); Iif (!host.exists(filePath)) return host; const source = host.read(filePath); Iif (!source) return host; let content = source.toString('utf-8'); content += `import 'whatwg-fetch';${EOL}`; host.overwrite(filePath, content); return host; }; } function updateKarmaTest(options: InitOptions): (host: Tree) => Tree { return (host: Tree) => { const projectRoot = getProjectRoot(host, options); const file = path.join(projectRoot, 'src/test.ts'); Eif (host.exists(file)) { const source = host.read(file); Iif (!source) return host; let content = source.toString('utf-8'); content += `${EOL}window[\`APP_MANIFEST\`] = {};${EOL}`; host.overwrite(file, content); } return host; }; } function updateEnvironments(options: InitOptions): (host: Tree) => Tree { return (host: Tree) => { const projectRoot = getProjectRoot(host, options); updateEnvironment(host, path.join(projectRoot, 'src/environments/environment.ts')); updateEnvironment(host, path.join(projectRoot, 'src/environments/environment.prod.ts')); return host; }; } function updateEnvironment(host: Tree, file: string): void { Iif (!host.exists(file)) return; const sourceFile = CodeUtils.readSourceFile(host, file); CodeUtils.insertInVariableObject(sourceFile, 'environment', `apiUrl: '/api/'`); CodeUtils.writeSourceFile(host, file, sourceFile); } function updateProjectWorkspace(options: InitOptions): (host: Tree) => Rule { return (host: Tree) => { return updateWorkspace(workspace => { const projectName = getProjectName(host, options); const project = workspace.projects.get(projectName); Iif (!project) { return; } workspace.extensions.cli = { 'defaultCollection': 'yang-schematics' }; let schematics: any = project.extensions.schematics; Iif (!schematics) { schematics = {}; project.extensions['schematics'] = schematics; } const componentSchematic = schematics['@schematics/angular:component']; componentSchematic.inlineTemplate = componentSchematic.inlineTemplate ?? false; componentSchematic.inlineStyle = componentSchematic.inlineStyle ?? true; schematics['@schematics/angular:component'] = sortByKey(componentSchematic); const build = project.targets.get('build'); Iif (!build) { throw new Error(`expected node projects/${projectName}/architect/build in angular.json`); } const test = project.targets.get('test'); Iif (!test) { throw new Error(`expected node projects/${projectName}/architect/test in angular.json`); } const projectRoot = getProjectRoot(host, options, true); // Add stylePreprocessorOptions const stylePreprocessorOptions = { "includePaths": [ projectRoot + 'src/styles' ] }; (<any>build.options)['stylePreprocessorOptions'] = stylePreprocessorOptions; (<any>test.options)['stylePreprocessorOptions'] = stylePreprocessorOptions; // Add @angular-builders/custom-webpack build.builder = <any>'@angular-builders/custom-webpack:browser'; (<any>build.options)['customWebpackConfig'] = { path: projectRoot +'webpack.config.js' }; const serve = project.targets.get('serve'); Iif (!serve) { throw new Error(`expected node projects/${projectName}/architect/serve in angular.json`); } serve.builder = <any>'@angular-builders/custom-webpack:dev-server'; test.builder = <any>'@angular-builders/custom-webpack:karma'; (<any>test.options)['customWebpackConfig'] = { path: projectRoot +'webpack.config.js' }; }); } } |