Skip to content

Commit

Permalink
feat(sys): add encoding option to readFile
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Oct 14, 2020
1 parent 9598a05 commit 99ef518
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/declarations/stencil-public-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ export interface CompilerSystem {
/**
* Generates a MD5 digest encoded as HEX
*/
generateContentHash?(content: string, length?: number): Promise<string>;
generateContentHash?(content: string | any, length?: number): Promise<string>;
/**
* Get the current directory.
*/
Expand Down Expand Up @@ -939,7 +939,9 @@ export interface CompilerSystem {
/**
* Returns undefined if file is not found. Does not throw.
*/
readFile(p: string, encoding?: string): Promise<string>;
readFile(p: string): Promise<string>;
readFile(p: string, encoding: 'utf8'): Promise<string>;
readFile(p: string, encoding: 'binary'): Promise<any>;
/**
* SYNC! Returns undefined if file is not found. Does not throw.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/sys/deno/deno-sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,13 @@ export function createDenoSys(c: { Deno?: any } = {}) {
} catch (e) {}
return dirEntries;
},
async readFile(p) {
async readFile(p: string, encoding?: string) {
try {
const decoder = new TextDecoder('utf-8');
const data = await deno.readFile(p);
if (encoding === 'binary') {
return data as any;
}
const decoder = new TextDecoder('utf-8');
return decoder.decode(data);
} catch (e) {}
return undefined;
Expand Down
11 changes: 9 additions & 2 deletions src/sys/node/node-sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,15 @@ export function createNodeSys(c: { process?: any } = {}) {
} catch (e) {}
return [];
},
readFile(p) {
return new Promise(resolve => {
readFile(p: string, encoding?: string) {
if (encoding === 'binary') {
return new Promise<any>(resolve => {
fs.readFile(p, (_, data) => {
resolve(data);
});
});
}
return new Promise<string>(resolve => {
fs.readFile(p, 'utf8', (_, data) => {
resolve(data);
});
Expand Down

0 comments on commit 99ef518

Please sign in to comment.