Allow installation with inline args

This commit is contained in:
danbulant 2020-02-21 17:04:08 +01:00
parent 40c00e7160
commit 94ea018158
2 changed files with 44 additions and 2 deletions

View file

@ -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();
}
}

View file

@ -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);
}