From fc6617970c4aeedd735e223d62782851a0da138e Mon Sep 17 00:00:00 2001 From: danbulant Date: Wed, 19 Feb 2020 19:45:29 +0100 Subject: [PATCH] Move parser away --- modules/package.js | 14 ------------- modules/parser.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 modules/parser.js diff --git a/modules/package.js b/modules/package.js index 357af96..ceee253 100644 --- a/modules/package.js +++ b/modules/package.js @@ -10,20 +10,6 @@ var parser = new Parser; module.exports = class Package { pkg = {}; - load(path){ - if(!fs.existsSync(path))throw Error("Path doesn't exists!"); - - try { - var json = JSON.parse(fs.readFileSync(path)); - } catch(e){ - throw Error("Invalid JSON file"); - } - - this.pkg = json; - - return json; - } - get(pkg){ return new Promise((res, rej) => { if(!pkg){ diff --git a/modules/parser.js b/modules/parser.js new file mode 100644 index 0000000..c1d74f9 --- /dev/null +++ b/modules/parser.js @@ -0,0 +1,49 @@ +const Console = require("./console"); +var console = new Console; + +module.exports = class PackageParser { + load(path){ + if (!fs.existsSync(path)) throw Error("Path doesn't exists!"); + + try { + var json = JSON.parse(fs.readFileSync(path)); + } catch (e) { + throw Error("Invalid JSON file"); + } + + this.pkg = json; + + return json; + } + + getName(){ + if(this.pkg.name)return this.pkg.name; + throw Error("Name isn't specified in the package.json"); + } + + getDescription() { + if (this.pkg.description) return this.pkg.description; + throw Error("Description isn't specified in the package.json"); + } + + getDependencies(dev = false){ + if(dev){ + var deps = this.pkg.devDependencies; + } else { + var deps = this.pkg.dependencies; + } + if(!deps) return {}; + + return deps; + } + + getPeerDependencies(){ + if (this.pkg.peerDependencies) return this.pkg.peerDependencies; + return {}; + } + + getOptionalDependencies(){ + if (this.pkg.optionalDependencies) return this.pkg.optionalDependencies; + return {}; + } +} \ No newline at end of file