/ scripts / audit.js
audit.js
 1  const fs = require("fs")
 2  const { join } = require("path")
 3  const { spawnSync } =require("child_process")
 4  
 5  const DONT_RUN_PKG = ["bbui"]
 6  const PACKAGES_PATH = join(__dirname, "..", "packages")
 7  
 8  function getPackages() {
 9    return fs.readdirSync(PACKAGES_PATH)
10  }
11  
12  function deleteFile(path) {
13    try {
14      fs.unlinkSync(path)
15    } catch (err) {
16      // don't error, it just doesn't exist
17    }
18  }
19  
20  function removeModules(path) {
21    if (fs.existsSync(path)) {
22      fs.rmdirSync(path, { recursive: true })
23    }
24  }
25  
26  function executeInPackage(packageName) {
27    if (DONT_RUN_PKG.includes(packageName)) {
28      return
29    }
30    const dir = join(PACKAGES_PATH, packageName)
31    if (!fs.existsSync(join(dir, "package.json"))) {
32      console.error(`SKIPPING ${packageName} directory, no package.json`)
33      return
34    }
35    const packageLockLoc = join(dir, "package-lock.json")
36    const modulesLoc = join(dir, "node_modules")
37    deleteFile(join(dir, "yarn.lock"))
38    deleteFile(packageLockLoc)
39    removeModules(modulesLoc)
40    const opts = { cwd: dir, stdio: "inherit", shell: true }
41    spawnSync("npm", ["i", "--package-lock-only"], opts)
42    spawnSync("npm", ["audit", "fix"], opts)
43    spawnSync("yarn", ["import"], opts)
44    deleteFile(packageLockLoc)
45    removeModules(modulesLoc)
46  }
47  
48  const packages = getPackages()
49  for (let pkg of packages) {
50    executeInPackage(pkg)
51  }
52  
53  spawnSync("yarn", ["bootstrap"], { cwd: join(__dirname, ".."), stdio: "inherit", shell: true })
54