From dd3ff94407ab44bbb3a11640bbb37b181acbd09a Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Fri, 1 Jun 2018 16:23:53 +0300 Subject: [PATCH 1/4] refactor(@angular/pwa): minor refactorings to make code cleaner --- packages/angular/pwa/pwa/index.ts | 81 ++++++++++++------------------- 1 file changed, 31 insertions(+), 50 deletions(-) diff --git a/packages/angular/pwa/pwa/index.ts b/packages/angular/pwa/pwa/index.ts index 96fa2c08e58c..fc5b40203427 100644 --- a/packages/angular/pwa/pwa/index.ts +++ b/packages/angular/pwa/pwa/index.ts @@ -12,7 +12,6 @@ import { SchematicsException, Tree, apply, - branchAndMerge, chain, externalSchematic, mergeWith, @@ -39,15 +38,14 @@ function addServiceWorker(options: PwaOptions): Rule { function getIndent(text: string): string { let indent = ''; - let hitNonSpace = false; - text.split('') - .forEach(char => { - if (char === ' ' && !hitNonSpace) { - indent += ' '; - } else { - hitNonSpace = true; - } - }, 0); + + for (const char of text) { + if (char === ' ' || char === '\t') { + indent += char; + } else { + break; + } + } return indent; } @@ -70,43 +68,32 @@ function updateIndexFile(options: PwaOptions): Rule { const content = buffer.toString(); const lines = content.split('\n'); let closingHeadTagLineIndex = -1; - let closingHeadTagLine = ''; let closingBodyTagLineIndex = -1; - let closingBodyTagLine = ''; - lines.forEach((line: string, index: number) => { - if (/<\/head>/.test(line) && closingHeadTagLineIndex === -1) { - closingHeadTagLine = line; + lines.forEach((line, index) => { + if (closingHeadTagLineIndex === -1 && /<\/head>/.test(line)) { closingHeadTagLineIndex = index; - } - - if (/<\/body>/.test(line) && closingBodyTagLineIndex === -1) { - closingBodyTagLine = line; + } else if (closingBodyTagLineIndex === -1 && /<\/body>/.test(line)) { closingBodyTagLineIndex = index; } }); - const headTagIndent = getIndent(closingHeadTagLine) + ' '; + const headIndent = getIndent(lines[closingHeadTagLineIndex]) + ' '; const itemsToAddToHead = [ '', '', ]; - const textToInsertIntoHead = itemsToAddToHead - .map(text => headTagIndent + text) - .join('\n'); - - const bodyTagIndent = getIndent(closingBodyTagLine) + ' '; - const itemsToAddToBody - = ''; - - const textToInsertIntoBody = bodyTagIndent + itemsToAddToBody; + const bodyIndent = getIndent(lines[closingBodyTagLineIndex]) + ' '; + const itemsToAddToBody = [ + '', + ]; const updatedIndex = [ ...lines.slice(0, closingHeadTagLineIndex), - textToInsertIntoHead, + ...itemsToAddToHead.map(line => headIndent + line), ...lines.slice(closingHeadTagLineIndex, closingBodyTagLineIndex), - textToInsertIntoBody, - ...lines.slice(closingBodyTagLineIndex), + ...itemsToAddToBody.map(line => bodyIndent + line), + ...lines.slice(closingHeadTagLineIndex), ].join('\n'); host.overwrite(path, updatedIndex); @@ -137,12 +124,9 @@ function addManifestToAssetsConfig(options: PwaOptions) { ['build', 'test'].forEach((target) => { const applyTo = architect[target].options; + const assets = applyTo.assets || (applyTo.assets = []); - if (!applyTo.assets) { - applyTo.assets = [assetEntry]; - } else { - applyTo.assets.push(assetEntry); - } + assets.push(assetEntry); }); @@ -163,27 +147,24 @@ export default function (options: PwaOptions): Rule { throw new SchematicsException(`PWA requires a project type of "application".`); } - const assetPath = join(project.root as Path, 'src', 'assets'); const sourcePath = join(project.root as Path, 'src'); + const assetsPath = join(sourcePath, 'assets'); options.title = options.title || options.project; - const templateSource = apply(url('./files/assets'), [ - template({ - ...options, - }), - move(assetPath), + const rootTemplateSource = apply(url('./files/root'), [ + template({ ...options }), + move(sourcePath), + ]); + const assetsTemplateSource = apply(url('./files/assets'), [ + template({ ...options }), + move(assetsPath), ]); return chain([ addServiceWorker(options), - branchAndMerge(chain([ - mergeWith(templateSource), - ])), - mergeWith(apply(url('./files/root'), [ - template({...options}), - move(sourcePath), - ])), + mergeWith(rootTemplateSource), + mergeWith(assetsTemplateSource), updateIndexFile(options), addManifestToAssetsConfig(options), ]); From 9d92390270f22d48a9020bbcfece0565beb31a3f Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Mon, 11 Jun 2018 16:46:46 +0300 Subject: [PATCH 2/4] refactor(@schematics/angular): minor refactorings --- packages/schematics/angular/service-worker/index.ts | 4 ++-- packages/schematics/angular/utility/dependencies.ts | 2 +- packages/schematics/angular/utility/json-utils.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/schematics/angular/service-worker/index.ts b/packages/schematics/angular/service-worker/index.ts index 77c6902576f3..838a134fe964 100644 --- a/packages/schematics/angular/service-worker/index.ts +++ b/packages/schematics/angular/service-worker/index.ts @@ -75,11 +75,11 @@ function addDependencies(): Rule { if (coreDep === null) { throw new SchematicsException('Could not find version.'); } - const platformServerDep = { + const serviceWorkerDep = { ...coreDep, name: packageName, }; - addPackageJsonDependency(host, platformServerDep); + addPackageJsonDependency(host, serviceWorkerDep); return host; }; diff --git a/packages/schematics/angular/utility/dependencies.ts b/packages/schematics/angular/utility/dependencies.ts index 892dece42926..92d827182b71 100644 --- a/packages/schematics/angular/utility/dependencies.ts +++ b/packages/schematics/angular/utility/dependencies.ts @@ -37,7 +37,7 @@ export function addPackageJsonDependency(tree: Tree, dependency: NodeDependency) // Haven't found the dependencies key, add it to the root of the package.json. appendPropertyInAstObject(recorder, packageJsonAst, dependency.type, { [dependency.name]: dependency.version, - }, 4); + }, 2); } else if (depsNode.kind === 'object') { // check if package already added const depNode = findPropertyInAstObject(depsNode, dependency.name); diff --git a/packages/schematics/angular/utility/json-utils.ts b/packages/schematics/angular/utility/json-utils.ts index 2894b01c2f07..6a509ca7cd4c 100644 --- a/packages/schematics/angular/utility/json-utils.ts +++ b/packages/schematics/angular/utility/json-utils.ts @@ -84,7 +84,7 @@ export function insertPropertyInAstObjectInOrder( recorder.insertRight( insertIndex, - `${indentStr}` + indentStr + `"${propertyName}": ${JSON.stringify(value, null, 2).replace(/\n/g, indentStr)}` + ',', ); From 44d947a4b1d0bd97ab76f8e246f4de4e12b2ac00 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Fri, 1 Jun 2018 16:28:29 +0300 Subject: [PATCH 3/4] fix(@schematics/angular): allow ServiceWorker to work with `baseHref` Fixes angular/angular-cli#8515 --- packages/schematics/angular/service-worker/index.ts | 2 +- packages/schematics/angular/service-worker/index_spec.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/schematics/angular/service-worker/index.ts b/packages/schematics/angular/service-worker/index.ts index 838a134fe964..795637a7b12d 100644 --- a/packages/schematics/angular/service-worker/index.ts +++ b/packages/schematics/angular/service-worker/index.ts @@ -131,7 +131,7 @@ function updateAppModule(options: ServiceWorkerOptions): Rule { // register SW in app module const importText = - `ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production })`; + `ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })`; moduleSource = getTsSourceFile(host, modulePath); const metadataChanges = addSymbolToNgModuleMetadata( moduleSource, modulePath, 'imports', importText); diff --git a/packages/schematics/angular/service-worker/index_spec.ts b/packages/schematics/angular/service-worker/index_spec.ts index 9a724e4dcf81..fae453d35327 100644 --- a/packages/schematics/angular/service-worker/index_spec.ts +++ b/packages/schematics/angular/service-worker/index_spec.ts @@ -87,8 +87,8 @@ describe('Service Worker Schematic', () => { const tree = schematicRunner.runSchematic('service-worker', defaultOptions, appTree); const pkgText = tree.readContent('/projects/bar/src/app/app.module.ts'); // tslint:disable-next-line:max-line-length - const regex = /ServiceWorkerModule\.register\('\/ngsw-worker.js\', { enabled: environment.production }\)/; - expect(pkgText).toMatch(regex); + const expectedText = 'ServiceWorkerModule.register(\'ngsw-worker.js\', { enabled: environment.production })'; + expect(pkgText).toContain(expectedText); }); it('should put the ngsw-config.json file in the project root', () => { From ef144649f9d5fbe5ca1f20bd37ed7de213ba5426 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Fri, 1 Jun 2018 15:51:26 +0300 Subject: [PATCH 4/4] style(@schematics/angular): minor improvements in `ngsw-config.json` --- .../service-worker/files/ngsw-config.json | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/packages/schematics/angular/service-worker/files/ngsw-config.json b/packages/schematics/angular/service-worker/files/ngsw-config.json index 7c6b82cdb4e8..fa9410f347f6 100644 --- a/packages/schematics/angular/service-worker/files/ngsw-config.json +++ b/packages/schematics/angular/service-worker/files/ngsw-config.json @@ -1,24 +1,26 @@ { "index": "/index.html", - "assetGroups": [{ - "name": "app", - "installMode": "prefetch", - "resources": { - "files": [ - "/favicon.ico", - "/index.html", - "/*.css", - "/*.js" - ] + "assetGroups": [ + { + "name": "app", + "installMode": "prefetch", + "resources": { + "files": [ + "/favicon.ico", + "/index.html", + "/*.css", + "/*.js" + ] + } + }, { + "name": "assets", + "installMode": "lazy", + "updateMode": "prefetch", + "resources": { + "files": [ + "/assets/**" + ] + } } - }, { - "name": "assets", - "installMode": "lazy", - "updateMode": "prefetch", - "resources": { - "files": [ - "/assets/**" - ] - } - }] -} \ No newline at end of file + ] +}