diff --git a/modules/package.js b/modules/package.js index 2f4f317..2befdd8 100644 --- a/modules/package.js +++ b/modules/package.js @@ -101,8 +101,10 @@ module.exports = class Package { console.log("Done"); } - install(flags, ...packages){ + async install(flags, packages){ parser.load(); + await parser.addDependencies(packages); parser.install((flags.dev | false)); + parser.saveChanges(); } } \ No newline at end of file diff --git a/modules/parser.js b/modules/parser.js index 441842a..28d8a4f 100644 --- a/modules/parser.js +++ b/modules/parser.js @@ -21,6 +21,7 @@ module.exports = class PackageParser { throw Error("Invalid JSON file"); } + this.path = path; this.pkg = json; this.verify(); @@ -28,6 +29,45 @@ module.exports = class PackageParser { return json; } + async addDependencies(deps){ + if(!deps)return; + + for(var i = 0; i < deps.length; i++){ + var depi = deps[i].split(/[a-z]@/gi); + var dep = depi[0]; + var version = depi[1] || null; + + try { + var pkg = JSON.parse(await request(global.config.repository + dep)); + } catch(e){ + console.error("An error occured during parsing information"); + } + + if(pkg.error)throw Error(pkg.error); + + if (!pkg["dist-tags"]) { + console.warn("Couldn't install " + deps[i] + " - no published version"); + continue; + } + if (!pkg["dist-tags"].latest) { + console.warn("Couldn't install " + deps[i] + " - no published version"); + continue; + } + + if(!this.pkg.dependencies)this.pkg.dependencies = {}; + + if(!version){ + this.pkg.dependencies[dep] = "^" + pkg["dist-tags"].latest; + } else { + this.pkg.dependencies[dep] = version; + } + } + } + + saveChanges(){ + return fs.writeFileSync(this.path, JSON.stringify(this.pkg, null, 2) + "\n"); + } + verify(){ var pkg = this.pkg; @@ -114,7 +154,7 @@ module.exports = class PackageParser { fs.mkdirSync(path.dirname(process.cwd() + "/node_modules/" + pk.name)); } - console.log("Creating symlink"); + //console.log("Creating symlink"); fs.symlinkSync(path.relative(process.cwd() + "/node_modules/" + pk.name, this.sanitizeName(pk.name, deps.version)), process.cwd() + "/node_modules/" + pk.name); }