Skip to content

Commit

Permalink
fix: use util.readFile everywhere to handle BOM issues
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored and Gerrit0 committed Jan 25, 2020
1 parent 49035b8 commit 00a6816
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Converter } from './converter/index';
import { Renderer } from './output/renderer';
import { Serializer } from './serialization';
import { ProjectReflection } from './models/index';
import { Logger, ConsoleLogger, CallbackLogger, PluginHost, writeFile } from './utils/index';
import { Logger, ConsoleLogger, CallbackLogger, PluginHost, writeFile, readFile } from './utils/index';
import { createMinimatch } from './utils/paths';

import {
Expand Down Expand Up @@ -162,7 +162,7 @@ export class Application extends ChildableComponent<

public getTypeScriptVersion(): string {
const tsPath = this.getTypeScriptPath();
const json = JSON.parse(FS.readFileSync(Path.join(tsPath, '..', 'package.json'), 'utf8'));
const json = JSON.parse(readFile(Path.join(tsPath, '..', 'package.json')));
return json.version;
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib/converter/plugins/PackagePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Reflection } from '../../models/reflections/abstract';
import { Component, ConverterComponent } from '../components';
import { Converter } from '../converter';
import { Context } from '../context';
import { BindOption } from '../../utils';
import { BindOption, readFile } from '../../utils';

/**
* A handler that tries to find the package.json and readme.md files of the
Expand Down Expand Up @@ -118,11 +118,11 @@ export class PackagePlugin extends ConverterComponent {
private onBeginResolve(context: Context) {
const project = context.project;
if (this.readmeFile) {
project.readme = FS.readFileSync(this.readmeFile, 'utf-8');
project.readme = readFile(this.readmeFile);
}

if (this.packageFile) {
project.packageInfo = JSON.parse(FS.readFileSync(this.packageFile, 'utf-8'));
project.packageInfo = JSON.parse(readFile(this.packageFile));
if (!project.name) {
project.name = String(project.packageInfo.name);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/output/plugins/MarkedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as Handlebars from 'handlebars';

import { Component, ContextAwareRendererComponent } from '../components';
import { RendererEvent, MarkdownEvent } from '../events';
import { BindOption } from '../../utils';
import { BindOption, readFile } from '../../utils';

const customMarkedRenderer = new Marked.Renderer();

Expand Down Expand Up @@ -127,7 +127,7 @@ export class MarkedPlugin extends ContextAwareRendererComponent {
text = text.replace(this.includePattern, (match: string, path: string) => {
path = Path.join(this.includes!, path.trim());
if (FS.existsSync(path) && FS.statSync(path).isFile()) {
const contents = FS.readFileSync(path, 'utf-8');
const contents = readFile(path);
if (path.substr(-4).toLocaleLowerCase() === '.hbs') {
const template = Handlebars.compile(contents);
return template(context, { allowProtoMethodsByDefault: true, allowProtoPropertiesByDefault: true });
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export {
normalizePath,
directoryExists,
ensureDirectoriesExist,
writeFile
writeFile,
readFile
} from './fs';
export { Logger, LogLevel, ConsoleLogger, CallbackLogger } from './loggers';
export { PluginHost } from './plugins';
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Path from 'path';
import { Application } from '../application';
import { AbstractComponent, Component } from './component';
import { BindOption } from './options';
import { readFile } from './fs';

/**
* Responsible for discovering and loading plugins.
Expand Down Expand Up @@ -106,7 +107,7 @@ export class PluginHost extends AbstractComponent<Application> {
*/
function loadPackageInfo(fileName: string): any {
try {
return JSON.parse(FS.readFileSync(fileName, { encoding: 'utf-8' }));
return JSON.parse(readFile(fileName));
} catch (error) {
logger.error('Could not parse %s', fileName);
return {};
Expand Down

0 comments on commit 00a6816

Please sign in to comment.