Merge pull request #3 from JsSucks/master

Up to date Utils to plugin-manager
This commit is contained in:
Alexei Stukov 2018-01-16 03:53:49 +02:00 committed by GitHub
commit f160cb9c8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 2 deletions

View File

@ -1,4 +1,4 @@
export { Logger, Utils } from './utils'; export { Logger, Utils, FileUtils } from './utils';
export { PluginManager } from './pluginmanager'; export { PluginManager } from './pluginmanager';
export { Pluging } from './plugin'; export { Pluging } from './plugin';
export { BDIpc } from './bdipc'; export { BDIpc } from './bdipc';

View File

@ -10,6 +10,8 @@
const { Module } = require('./modulebase'); const { Module } = require('./modulebase');
const moment = require('moment'); const moment = require('moment');
const fs = window.require('fs');
const path = window.require('path');
const logs = []; const logs = [];
@ -48,7 +50,93 @@ class Utils {
cb(...args); cb(...args);
} }
} }
static async tryParseJson(jsonString) {
try {
return JSON.parse(jsonString);
} catch (err) {
throw ({
'message': 'Failed to parse json',
err
});
}
}
} }
module.exports = { Logger, Utils } class FileUtils {
static async fileExists(path) {
return new Promise((resolve, reject) => {
fs.stat(path, (err, stats) => {
if (err) return reject({
'message': `No such file or directory: ${err.path}`,
err
});
if (!stats.isFile()) return reject({
'message': `Not a file: ${path}`,
stats
});
resolve();
});
});
}
static async directoryExists(path) {
return new Promise(resolve => {
fs.stat(path, (err, stats) => {
if (err) return reject({
'message': `Directory does not exist: ${path}`,
err
});
if (!stats.isDirectory()) return reject({
'message': `Not a directory: ${path}`,
stats
});
resolve();
});
});
}
static async readFile(path) {
try {
await this.fileExists(path);
} catch (err) {
throw (err);
}
return new Promise(resolve => {
fs.readFile(path, 'utf-8', (err, data) => {
if (err) reject({
'message': `Could not read file: ${path}`,
err
});
resolve(data);
});
});
}
static async readJsonFromFile(path) {
let readFile;
try {
readFile = await this.readFile(path);
} catch (err) {
throw (err);
}
try {
const parsed = await Utils.tryParseJson(readFile);
return parsed;
} catch (err) {
throw (Object.assign(err, { path }));
}
}
}
module.exports = { Logger, Utils, FileUtils }