Skip to content

Commit

Permalink
#61 added new file.utils.ts for local and remote data reads
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomFractals committed Jun 23, 2019
1 parent 6a0949a commit d7c8fcb
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/utils/file.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as fs from 'fs';
import * as path from 'path';
import * as config from '../config';
import {Logger, LogLevel} from '../logger';
import {window} from 'vscode';

const logger: Logger = new Logger(`file.utils:`, config.logLevel);

/**
* Reads local data file or fetches public data source data.
* @param dataFilePath Data file path or public data source url.
* @param encoding Data file encoding: 'utf8' for text data files, null for binary data reads.
*/
export function readDataFile(dataFilePath: string, encoding:string = null): any {
let data: any;
logger.debug('readDataFile(): ', dataFilePath);
if (!dataFilePath.startsWith('http://') && !dataFilePath.startsWith('https://')) {
// read local data file via fs read file api
// TODO: change this to read data async later
data = fs.readFileSync(dataFilePath, encoding);
} else {
// TODO: fetch remote data with https://github.com/d3/d3-fetch
data = '';
}
return data;
}

0 comments on commit d7c8fcb

Please sign in to comment.