webpack检测webpack cli是否安装的方法
const isInstalled = packageName => { if (process.versions.pnp) { return true; } const path = require("path"); const fs = require("graceful-fs"); let dir = __dirname; do { try { if ( fs.statSync(path.join(dir, "node_modules", packageName)).isDirectory() ) { return true; } } catch (_error) { // Nothing } } while (dir !== (dir = path.dirname(dir))); return false; };
通过do while循环,往上面一层层的查找