Skip to content

Commit

Permalink
fix(bazel): ng_package(data) should support non-text files (#32721)
Browse files Browse the repository at this point in the history
PR Close #32721
  • Loading branch information
Jason Bedard authored and AndrewKushnir committed Sep 17, 2019
1 parent acebf64 commit ba1ef6b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/bazel/src/ng_package/packager.ts
Expand Up @@ -109,7 +109,7 @@ function main(args: string[]): number {
* @param inputPath Path to the file in the input tree.
* @param fileContent Content of the file.
*/
function writeFileFromInputPath(inputPath: string, fileContent: string) {
function writeFileFromInputPath(inputPath: string, fileContent: string | Buffer) {
// We want the relative path from the given file to its ancestor "root" directory.
// This root depends on whether the file lives in the source tree (srcDir) as a basic file
// input to ng_package, the bin output tree (binDir) as the output of another rule, or
Expand All @@ -127,15 +127,15 @@ function main(args: string[]): number {

// Always ensure that the target directory exists.
shx.mkdir('-p', path.dirname(outputPath));
fs.writeFileSync(outputPath, fileContent, 'utf-8');
fs.writeFileSync(outputPath, fileContent);
}

/**
* Copies a file into the package based on its input path, relativizing to the package path.
* @param inputPath a path relative to the binDir, typically from a file in the deps[]
*/
function copyFileFromInputPath(inputPath: string) {
writeFileFromInputPath(inputPath, fs.readFileSync(inputPath, 'utf-8'));
writeFileFromInputPath(inputPath, fs.readFileSync(inputPath));
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/bazel/test/ng_package/example/BUILD.bazel
Expand Up @@ -22,6 +22,7 @@ ng_package(
":arbitrary_bin_file",
":arbitrary_genfiles_file",
":extra-styles.css",
":logo.png",
],
entry_point = ":index.ts",
entry_point_name = "waffels",
Expand Down
Binary file added packages/bazel/test/ng_package/example/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/bazel/test/ng_package/example_package.golden
Expand Up @@ -42,6 +42,7 @@ fesm5
fesm5/secondary.js.map
fesm5/waffels.js
fesm5/waffels.js.map
logo.png
package.json
secondary
secondary/package.json
Expand Down Expand Up @@ -987,6 +988,10 @@ export { MyModule };
//# sourceMappingURL=waffels.js.map


--- logo.png ---

9db278d630f5fabd8e7ba16c2e329a3a

--- package.json ---

{
Expand Down
9 changes: 9 additions & 0 deletions packages/bazel/test/ng_package/example_package.spec.ts
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import * as crypto from 'crypto';
import {createPatch} from 'diff';
import * as fs from 'fs';
import * as path from 'path';
Expand Down Expand Up @@ -64,6 +65,10 @@ function getDescendantFilesContents(directoryPath: string): string[] {
result.push(...getDescendantFilesContents(path.posix.join(directoryPath, dir)));
});
}
// Binary files should equal the same as in the srcdir.
else if (path.extname(directoryPath) === '.png') {
result.push(`--- ${directoryPath} ---`, '', hashFileContents(directoryPath), '');
}
// Note that we don't want to include ".map" files in the golden file since these are not
// consistent across different environments (e.g. path delimiters)
else if (path.extname(directoryPath) !== '.map') {
Expand Down Expand Up @@ -128,6 +133,10 @@ function readFileContents(filePath: string): string {
return fs.readFileSync(filePath, 'utf8').replace(/\r/g, '');
}

function hashFileContents(filePath: string): string {
return crypto.createHash('md5').update(fs.readFileSync(filePath)).digest('hex');
}

if (require.main === module) {
const args = process.argv.slice(2);
const acceptingNewGold = (args[0] === '--accept');
Expand Down

0 comments on commit ba1ef6b

Please sign in to comment.