const argv = process.argv.slice(2); if (argv.length === 0) { console.log('Please specify the wanted package manager: only-allow <npm|pnpm|yarn>'); process.exit(1); } // 用户规定的包管理器,例如 npx only-allow yarn ,那么wantedPM 为 yarn const wantedPM = argv[0]; if (wantedPM !== 'npm' && wantedPM !== 'pnpm' && wantedPM !== 'yarn') { console.log(`"${wantedPM}" is not a valid package manager. Available package managers are: npm, pnpm, or yarn.`); process.exit(1); }
// 用户当前安装时 使用的包管理器 const usedPM = whichPMRuns();
// 希望使用的包管理器 不相等,则报错。 // - npm 提示使用 npm install // - pnpm 提示使用 pnpm install // - yarn 提示使用 yarn install // 最后退出进程 if (usedPM && usedPM.name !== wantedPM) { // boxenOPts: boxen 的配置 // boxen 能让terminal的输出展示盒装的样式,让终端提示更明显 const boxenOpts = { borderColor: 'red', borderStyle: 'double', padding: 1 }; switch (wantedPM) { case'npm': console.log(boxen('Use "npm install" for installation in this project', boxenOpts)); break; case'pnpm': console.log( boxen( `Use "pnpm install" for installation in this project. If you don't have pnpm, install it via "npm i -g pnpm". For more details, go to https://pnpm.js.org/`, boxenOpts ) ); break; case'yarn': console.log( boxen( `Use "yarn" for installation in this project. If you don't have Yarn, install it via "npm i -g yarn". For more details, go to https://yarnpkg.com/`, boxenOpts ) ); break; } process.exit(1); }