Skip to content

Commit

Permalink
fix: strip utf-8 bom when reading files
Browse files Browse the repository at this point in the history
Resolves #87
  • Loading branch information
dherges committed Aug 4, 2017
1 parent cb5f7e0 commit fd5f530
Show file tree
Hide file tree
Showing 9 changed files with 3,454 additions and 3,387 deletions.
9 changes: 4 additions & 5 deletions lib/ng-packagr.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as path from 'path';

// BUILD STEP IMPLEMENTATIONS
import { processAssets } from './steps/assets';
import { copyFiles } from './steps/copy';
Expand All @@ -7,7 +9,7 @@ import { rimraf } from './steps/rimraf';
import { rollup } from './steps/rollup';
import { remapSourcemap } from './steps/sorcery';
import { downlevelWithTsc } from './steps/tsc';
import { modifyJsonFiles } from './steps/json';
import { modifyJsonFiles } from './util/json';


// Logging
Expand All @@ -18,10 +20,6 @@ import { NgPackage } from './model/ng-package';
import { NgPackageConfig } from './ng-package.schema';


// There are no type definitions available for these imports.
const fs = require('mz/fs');
const path = require('path');


/** CLI arguments passed to `ng-packagr` and `ngPackage()`. */
export interface NgPackagrCliArguments {
Expand Down Expand Up @@ -98,6 +96,7 @@ export const ngPackage = (opts: NgPackagrCliArguments): Promise<any> => {
.then(() => copyFiles(`${ngPkg.src}/README.md`, ngPkg.dest))
.then(() => copyFiles(`${ngPkg.src}/LICENSE`, ngPkg.dest))
// 8. SOURCEMAPS: RELOCATE PATHS
// XX ... modifyJsonFiles() should maybe called 'relocateSourceMaps()' in 'steps' folder
.then(() => modifyJsonFiles(`${ngPkg.dest}/**/*.js.map`, (sourceMap: any): any => {
sourceMap['sources'] = sourceMap['sources']
.map((path: string): string => path.replace('../ts',
Expand Down
29 changes: 13 additions & 16 deletions lib/steps/assets.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
const fs = require('mz/fs');
const path = require('path');
const vfs = require('vinyl-fs');
import * as path from 'path';
import { debug, warn } from '../util/log';
import { readFile } from '../util/fs';

// Angular Inliner for Templates and Stylesheets
const inlineNg2Template = require('gulp-inline-ng2-template');

// CSS Tools
const autoprefixer = require('autoprefixer');
const browserslist = require('browserslist');
//const postcss = require('postcss');
import postcss = require('postcss');
import postcss = require('postcss');
const sass = require('node-sass');
import * as less from 'less';

import { debug, warn } from '../util/log';


/**
* Process Angular components assets (HTML and Stylesheets).
Expand Down Expand Up @@ -113,18 +114,14 @@ const renderSass = (sassOpts: any): Promise<string> => {

const renderLess = (lessOpts: any): Promise<string> => {

return new Promise((resolve, reject) => {

fs.readFile(lessOpts.filename, 'utf8', (err1, data) => {
if (err1) reject(err1);

less.render(data, lessOpts, (err2, result) => {
if (err2) {
reject(err2);
return readFile(lessOpts.filename)
.then((lessData: string) => new Promise<string>((resolve, reject) => {
less.render(lessData, lessOpts, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result.css.toString());
}
});
});
});
})
}));
}
4 changes: 2 additions & 2 deletions lib/steps/ngc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { main as tsc } from '@angular/tsc-wrapped';
import { readJson, writeJson } from './json';
import { debug } from '../util/log';
import { NgPackage } from '../model/ng-package';
import { readJson, writeJson } from '../util/json';
import { debug } from '../util/log';

const path = require('path');

Expand Down
18 changes: 3 additions & 15 deletions lib/steps/package.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import * as path from 'path';
import { SchemaClass, SchemaClassFactory } from '@ngtools/json-schema';
import { NgPackageConfig } from '../ng-package.schema';
import { NgPackage } from '../model/ng-package';
import { readJson, writeJson } from './json';
import { findFromDirectory } from '../util/fs';
import { readJson, writeJson } from '../util/json';

const fs = require('fs');
const path = require('path');

const findFromDirectory = (dir: string, file: string, cb: any) => {

let fileName = path.resolve(dir, file);
fs.access(fileName, fs.constants.R_OK, (err) => {
if (err) {
findFromDirectory(path.resolve(dir, '..'), file, cb);
} else {
cb(fileName);
}
});
};

/**
* Reads an Angular package definition file from 'ng-package.json'
Expand Down
10 changes: 5 additions & 5 deletions lib/steps/tsc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('mz/fs');
const path = require('path');
import * as path from 'path';
import * as ts from 'typescript';
import { ScriptTarget, ModuleKind } from 'typescript';
import { readFile, writeFile } from '../util/fs';
import { debug } from '../util/log';

/**
Expand All @@ -13,7 +13,7 @@ import { debug } from '../util/log';
export const downlevelWithTsc = (inputFile: string, outputFile: string) => {

return Promise.resolve(debug(`tsc ${inputFile} to ${outputFile}`))
.then(() => fs.readFile(inputFile))
.then(() => readFile(inputFile))
.then((input) => ts.transpileModule(trimSourceMap(input.toString()), {
fileName: path.basename(outputFile),
moduleName: path.basename(outputFile, '.js'),
Expand All @@ -30,8 +30,8 @@ export const downlevelWithTsc = (inputFile: string, outputFile: string) => {
sourceMap['sources'] = [path.basename(inputFile)];

return Promise.all([
fs.writeFile(outputFile, transpiled.outputText),
fs.writeFile(`${outputFile}.map`, JSON.stringify(sourceMap))
writeFile(outputFile, transpiled.outputText),
writeFile(`${outputFile}.map`, JSON.stringify(sourceMap))
]);
});

Expand Down
43 changes: 43 additions & 0 deletions lib/util/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as fs from 'fs';
import * as path from 'path';
const read = require('read-file');

export const readFile = (file: string): Promise<string> => {

return new Promise((resolve, reject) => {

read(file, { encoding: 'utf8', normalize: true }, (err, buffer: Buffer) => {
if (err) {
reject(err);
}

resolve(buffer.toString());
});
});
};

export const writeFile = (file: string, content: any): Promise<any> => {

return new Promise((resolve, reject) => {
fs.writeFile(file, content, (err) => {
if (err) {
reject(err);
}

resolve();
});
});
};


export const findFromDirectory = (dir: string, file: string, cb: any) => {

let fileName = path.resolve(dir, file);
fs.access(fileName, fs.constants.R_OK, (err) => {
if (err) {
findFromDirectory(path.resolve(dir, '..'), file, cb);
} else {
cb(fileName);
}
});
};
45 changes: 10 additions & 35 deletions lib/steps/json.ts → lib/util/json.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
const fs = require('fs');
// XX: TODO ... this should be moved to util folder

const glob = require('glob')
import * as mz from 'mz/fs';
import { readFile, writeFile } from '../util/fs';

/**
* Reads a JSON file.
*
* @param file Source file name.
*/
export const readJson = (file: string): Promise<any> => {

return new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
if (err) {
reject(err);
}

try {
resolve(JSON.parse(data));
} catch (e) {
reject(e);
}
});

});
}
export const readJson = (file: string): Promise<any> =>
readFile(file)
.then((content: string) => Promise.resolve(JSON.parse(content)));


/**
Expand All @@ -32,20 +19,8 @@ export const readJson = (file: string): Promise<any> => {
* @param object Object literal that is stringified.
* @param file Target file name.
*/
export const writeJson = (object: any, file: string): Promise<any> => {

return new Promise((resolve, reject) => {

const content = JSON.stringify(object, undefined, 2);
fs.writeFile(file, content, (err) => {
if (err) {
reject(err);
}

resolve();
});
});
}
export const writeJson = (object: any, file: string): Promise<any> =>
writeFile(file, JSON.stringify(object, undefined, 2));


/**
Expand All @@ -67,8 +42,8 @@ export const modifyJsonFiles = (globPattern: string, modifyFn: (jsonObj: any) =>
});
})
.then((fileNames: string[]): Promise<string[]> => Promise.all(
fileNames.map((fileName: string): Promise<string> => mz.readFile(fileName)
.then((fileContent: string) => mz.writeFile(fileName, JSON.stringify(modifyFn(JSON.parse(fileContent)))))
fileNames.map((fileName: string): Promise<string> => readFile(fileName)
.then((fileContent: string) => writeFile(fileName, JSON.stringify(modifyFn(JSON.parse(fileContent)))))
))
)
.then(() => Promise.resolve());
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
"glob": "^7.1.2",
"gulp-inline-ng2-template": "^4.0.0",
"minimist": "^1.2.0",
"mz": "^2.6.0",
"node-sass": "^4.5.3",
"postcss": "^6.0.2",
"read-file": "^0.2.0",
"rimraf": "^2.6.1",
"rollup": "^0.45.0",
"rollup-plugin-node-resolve": "^3.0.0",
Expand Down
Loading

0 comments on commit fd5f530

Please sign in to comment.