|
| 1 | +const {spawn} = require("child_process"); |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +const PACKAGES = [ |
| 6 | + "fs-storage", |
| 7 | + "full-text-search", |
| 8 | + "full-text-search-language", |
| 9 | + "full-text-search-language-de", |
| 10 | + "full-text-search-language-en", |
| 11 | + "indexed-storage", |
| 12 | + "local-storage", |
| 13 | + "loki", |
| 14 | + "memory-storage", |
| 15 | + "partitioning-adapter", |
| 16 | +]; |
| 17 | + |
| 18 | +/// MIT © Sindre Sorhus |
| 19 | +function pathExistsSync(fp) { |
| 20 | + try { |
| 21 | + fs.accessSync(fp); |
| 22 | + return true; |
| 23 | + } catch (err) { |
| 24 | + return false; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/// MIT © Sindre Sorhus |
| 29 | +function locatePathSync(iterable, options) { |
| 30 | + options = Object.assign({ |
| 31 | + cwd: process.cwd() |
| 32 | + }, options); |
| 33 | + |
| 34 | + for (const el of iterable) { |
| 35 | + if (pathExistsSync(path.resolve(options.cwd, el))) { |
| 36 | + return el; |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// MIT © Sindre Sorhus |
| 42 | +function findUpSync(filename, opts = {}) { |
| 43 | + let dir = path.resolve(opts.cwd || ""); |
| 44 | + const {root} = path.parse(dir); |
| 45 | + |
| 46 | + const filenames = [].concat(filename); |
| 47 | + |
| 48 | + // eslint-disable-next-line no-constant-condition |
| 49 | + while (true) { |
| 50 | + const file = locatePathSync(filenames, {cwd: dir}); |
| 51 | + |
| 52 | + if (file) { |
| 53 | + return path.join(dir, file); |
| 54 | + } |
| 55 | + |
| 56 | + if (dir === root) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + dir = path.dirname(dir); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function getRootDirectory() { |
| 65 | + return process.env.INIT_CWD || path.dirname(findUpSync("package.json")); |
| 66 | +} |
| 67 | + |
| 68 | +function getRootPackageJSON() { |
| 69 | + return require(path.join(getRootDirectory(), "package.json")); |
| 70 | +} |
| 71 | + |
| 72 | +function getPackageDependencyType(packageJson, packageName) { |
| 73 | + if (packageJson.dependencies && Object.keys(packageJson.dependencies).includes(packageName)) { |
| 74 | + return "production"; |
| 75 | + } else if (packageJson.devDependencies && Object.keys(packageJson.devDependencies).includes(packageName)) { |
| 76 | + return "development"; |
| 77 | + } |
| 78 | + return null; |
| 79 | +} |
| 80 | + |
| 81 | +function run(command, args = []) { |
| 82 | + const child = spawn(command, args); |
| 83 | + child.stdout.on("data", (data) => { |
| 84 | + console.log(data.toString("utf8")); |
| 85 | + }); |
| 86 | + child.stderr.on("data", (data) => { |
| 87 | + console.error(data.toString("utf8")); |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +function print(txt, lb = "\n") { |
| 92 | + process.stdout.write(txt + lb); |
| 93 | +} |
| 94 | + |
| 95 | +module.exports = { |
| 96 | + PACKAGES, |
| 97 | + getRootDirectory, |
| 98 | + getRootPackageJSON, |
| 99 | + getPackageDependencyType, |
| 100 | + run, |
| 101 | + print |
| 102 | +}; |
0 commit comments