UNPKG

1.49 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("child_process"),n=require("./api"),a=require("./utils");exports.shouldUseYarn=(()=>a.canRunCommand("yarnpkg --version")),exports.canUseNpm=(async n=>{let a=null;try{a=e.spawnSync("npm",["config","list"],{cwd:n}).output.join("")}catch(e){return!0}if("string"!=typeof a)return!0;const r=a.match(/^; cwd = (.*)$/m);return null===r||r[1]===n});const r={[n.PackageManagers.Npm]:{addDependency:"install",installAllDependencies:"install",execBinary:"npx"},[n.PackageManagers.Yarn]:{addDependency:"add",installAllDependencies:"install",execBinary:"yarn"}};class s{constructor(e){this.binary=e}get commands(){return r[this.binary]}addDependency(e,n){return this.exec([this.commands.addDependency,e],n)}installAllDependencies(n){return new Promise((a,r)=>{e.exec(`${this.binary} ${this.commands.installAllDependencies}`,n,e=>{if(e)return r(e);a()})})}exec(n,a={}){const r=e.spawn(this.binary,n,a);return new Promise((e,n)=>{r.on("close",()=>{e()}),r.on("error",e=>{n(e)})})}execBinary(n,a){e.execSync(`${this.commands.execBinary} ${n}`,a)}}let t;exports.PackageManager=s,exports.getPackageManager=(async()=>{if(t)return t;const e=await exports.shouldUseYarn()?n.PackageManagers.Yarn:n.PackageManagers.Npm;if(e===n.PackageManagers.Npm&&!await exports.canUseNpm(process.cwd()))throw new Error("Unable to start an NPM process.");return t=new s(e)});
2//# sourceMappingURL=/@diez/cli-core/lib/package-manager.js.map
\No newline at end of file