-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): updated tslint for package tools
- Loading branch information
1 parent
50c0125
commit 16b40b1
Showing
16 changed files
with
203 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
/* tslint:disable:no-var-requires */ | ||
/* tslint:disable-next-line:no-var-requires */ | ||
const gulpRunSequence = require('run-sequence'); | ||
/* tslint:enable:no-var-requires */ | ||
|
||
|
||
export function sequenceTask(...args: any[]) { | ||
return (done: any) => { | ||
gulpRunSequence( | ||
...args, | ||
done | ||
); | ||
}; | ||
return (done: any) => { | ||
gulpRunSequence( | ||
...args, | ||
done | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,62 @@ | ||
// tslint:disable:no-eval | ||
|
||
import {dirname, join} from 'path'; | ||
import {readFileSync, writeFileSync} from 'fs'; | ||
import {sync as glob} from 'glob'; | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { sync as glob } from 'glob'; | ||
import { dirname, join } from 'path'; | ||
|
||
|
||
/** Finds all JavaScript files in a directory and inlines all resources of Angular components. */ | ||
export function inlineResourcesForDirectory(folderPath: string) { | ||
glob(join(folderPath, '**/*.js')).forEach(filePath => inlineResources(filePath)); | ||
/* tslint:disable-next-line:no-unnecessary-callback-wrapper */ | ||
glob(join(folderPath, '**/*.js')).forEach((filePath) => inlineResources(filePath)); | ||
} | ||
|
||
|
||
/** Inlines the external resources of Angular components of a file. */ | ||
export function inlineResources(filePath: string) { | ||
let fileContent = readFileSync(filePath, 'utf-8'); | ||
let fileContent = readFileSync(filePath, 'utf-8'); | ||
|
||
fileContent = inlineTemplate(fileContent, filePath); | ||
fileContent = inlineStyles(fileContent, filePath); | ||
fileContent = removeModuleId(fileContent); | ||
fileContent = inlineTemplate(fileContent, filePath); | ||
fileContent = inlineStyles(fileContent, filePath); | ||
fileContent = removeModuleId(fileContent); | ||
|
||
writeFileSync(filePath, fileContent, 'utf-8'); | ||
writeFileSync(filePath, fileContent, 'utf-8'); | ||
} | ||
|
||
/** Inlines the templates of Angular components for a specified source file. */ | ||
function inlineTemplate(fileContent: string, filePath: string) { | ||
return fileContent.replace(/templateUrl:\s*'([^']+?\.html)'/g, (_match, templateUrl) => { | ||
const templatePath = join(dirname(filePath), templateUrl); | ||
const templateContent = loadResourceFile(templatePath); | ||
return `template: "${templateContent}"`; | ||
}); | ||
return fileContent.replace(/templateUrl:\s*'([^']+?\.html)'/g, (_match, templateUrl) => { | ||
const templatePath = join(dirname(filePath), templateUrl); | ||
const templateContent = loadResourceFile(templatePath); | ||
|
||
return `template: "${templateContent}"`; | ||
}); | ||
} | ||
|
||
/** Inlines the external styles of Angular components for a specified source file. */ | ||
function inlineStyles(fileContent: string, filePath: string) { | ||
return fileContent.replace(/styleUrls:\s*(\[[\s\S]*?])/gm, (_match, styleUrlsValue) => { | ||
// The RegExp matches the array of external style files. This is a string right now and | ||
// can to be parsed using the `eval` method. The value looks like "['AAA.css', 'BBB.css']" | ||
const styleUrls = eval(styleUrlsValue) as string[]; | ||
|
||
const styleContents = styleUrls | ||
.map(url => join(dirname(filePath), url)) | ||
.map(path => loadResourceFile(path)); | ||
|
||
return `styles: ["${styleContents.join(' ')}"]`; | ||
}); | ||
return fileContent.replace(/styleUrls:\s*(\[[\s\S]*?])/gm, (_match, styleUrlsValue) => { | ||
// The RegExp matches the array of external style files. This is a string right now and | ||
// can to be parsed using the `eval` method. The value looks like "['AAA.css', 'BBB.css']" | ||
const styleUrls = eval(styleUrlsValue) as string[]; | ||
|
||
const styleContents = styleUrls | ||
.map((url) => join(dirname(filePath), url)) | ||
/* tslint:disable-next-line:no-unnecessary-callback-wrapper */ | ||
.map((path) => loadResourceFile(path)); | ||
|
||
return `styles: ["${styleContents.join(' ')}"]`; | ||
}); | ||
} | ||
|
||
/** Remove every mention of `moduleId: module.id` */ | ||
function removeModuleId(fileContent: string) { | ||
return fileContent.replace(/\s*moduleId:\s*module\.id\s*,?\s*/gm, ''); | ||
return fileContent.replace(/\s*moduleId:\s*module\.id\s*,?\s*/gm, ''); | ||
} | ||
|
||
/** Loads the specified resource file and drops line-breaks of the content. */ | ||
function loadResourceFile(filePath: string): string { | ||
return readFileSync(filePath, 'utf-8') | ||
.replace(/([\n\r]\s*)+/gm, ' ') | ||
.replace(/"/g, '\\"'); | ||
return readFileSync(filePath, 'utf-8') | ||
.replace(/([\n\r]\s*)+/gm, ' ') | ||
.replace(/"/g, '\\"'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,60 @@ | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { basename } from 'path'; | ||
import { sync as glob } from 'glob'; | ||
import { join } from 'path'; | ||
import { join, basename} from 'path'; | ||
|
||
|
||
/** | ||
* Recurse through a parsed metadata.json file and inline all html and css. | ||
* Note: this assumes that all html and css files have a unique name. | ||
*/ | ||
export function inlineMetadataResources(metadata: any, componentResources: Map<string, string>) { | ||
// Convert `templateUrl` to `template` | ||
if (metadata.templateUrl) { | ||
const fullResourcePath = componentResources.get(basename(metadata.templateUrl)); | ||
metadata.template = readFileSync(fullResourcePath!, 'utf-8'); | ||
delete metadata.templateUrl; | ||
} | ||
|
||
// Convert `styleUrls` to `styles` | ||
if (metadata.styleUrls && metadata.styleUrls.length) { | ||
metadata.styles = []; | ||
for (const styleUrl of metadata.styleUrls) { | ||
const fullResourcePath = componentResources.get(basename(styleUrl)); | ||
metadata.styles.push(readFileSync(fullResourcePath!, 'utf-8')); | ||
// Convert `templateUrl` to `template` | ||
if (metadata.templateUrl) { | ||
const fullResourcePath = componentResources.get(basename(metadata.templateUrl)); | ||
/* tslint:disable-next-line:no-non-null-assertion */ | ||
metadata.template = readFileSync(fullResourcePath!, 'utf-8'); | ||
delete metadata.templateUrl; | ||
} | ||
delete metadata.styleUrls; | ||
} | ||
|
||
// We we did nothing at this node, go deeper. | ||
if (!metadata.template && !metadata.styles) { | ||
for (const property in metadata) { | ||
if (typeof metadata[property] == 'object' && metadata[property]) { | ||
inlineMetadataResources(metadata[property], componentResources); | ||
} | ||
|
||
// Convert `styleUrls` to `styles` | ||
if (metadata.styleUrls && metadata.styleUrls.length) { | ||
metadata.styles = []; | ||
for (const styleUrl of metadata.styleUrls) { | ||
const fullResourcePath = componentResources.get(basename(styleUrl)); | ||
/* tslint:disable-next-line:no-non-null-assertion */ | ||
metadata.styles.push(readFileSync(fullResourcePath!, 'utf-8')); | ||
} | ||
delete metadata.styleUrls; | ||
} | ||
|
||
// We we did nothing at this node, go deeper. | ||
if (!metadata.template && !metadata.styles) { | ||
/* tslint:disable-next-line:no-for-in */ | ||
for (const property in metadata) { | ||
if (typeof metadata[property] === 'object' && metadata[property]) { | ||
inlineMetadataResources(metadata[property], componentResources); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
/** Inlines HTML and CSS resources into `metadata.json` files. */ | ||
export function inlinePackageMetadataFiles(packagePath: string) { | ||
// Create a map of fileName -> fullFilePath. This is needed because the templateUrl and | ||
// styleUrls for each component use just the filename because, in the source, the component | ||
// and the resources live in the same directory. | ||
const componentResources = new Map<string, string>(); | ||
|
||
glob(join(packagePath, '**/*.+(html|css)')).forEach(resourcePath => { | ||
componentResources.set(basename(resourcePath), resourcePath); | ||
}); | ||
|
||
// Find all metadata files. For each one, parse the JSON content, inline the resources, and | ||
// reserialize and rewrite back to the original location. | ||
glob(join(packagePath, '**/*.metadata.json')).forEach(path => { | ||
const metadata = JSON.parse(readFileSync(path, 'utf-8')); | ||
inlineMetadataResources(metadata, componentResources); | ||
writeFileSync(path , JSON.stringify(metadata), 'utf-8'); | ||
}); | ||
// Create a map of fileName -> fullFilePath. This is needed because the templateUrl and | ||
// styleUrls for each component use just the filename because, in the source, the component | ||
// and the resources live in the same directory. | ||
const componentResources = new Map<string, string>(); | ||
|
||
glob(join(packagePath, '**/*.+(html|css)')).forEach((resourcePath) => { | ||
componentResources.set(basename(resourcePath), resourcePath); | ||
}); | ||
|
||
// Find all metadata files. For each one, parse the JSON content, inline the resources, and | ||
// reserialize and rewrite back to the original location. | ||
glob(join(packagePath, '**/*.metadata.json')).forEach((path) => { | ||
const metadata = JSON.parse(readFileSync(path, 'utf-8')); | ||
inlineMetadataResources(metadata, componentResources); | ||
writeFileSync(path, JSON.stringify(metadata), 'utf-8'); | ||
}); | ||
} |
Oops, something went wrong.