|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +function findBazelFiles(dir) { |
| 7 | + return fs.readdirSync(dir).reduce((files, file) => { |
| 8 | + const fullPath = path.posix.join(dir, file); |
| 9 | + const isSymbolicLink = fs.lstatSync(fullPath).isSymbolicLink(); |
| 10 | + let stat; |
| 11 | + try { |
| 12 | + stat = fs.statSync(fullPath); |
| 13 | + } catch (e) { |
| 14 | + if (isSymbolicLink) { |
| 15 | + // Filter out broken symbolic links. These cause fs.statSync(fullPath) |
| 16 | + // to fail with `ENOENT: no such file or directory ...` |
| 17 | + return files; |
| 18 | + } |
| 19 | + throw e; |
| 20 | + } |
| 21 | + const isDirectory = stat.isDirectory(); |
| 22 | + if (isDirectory && isSymbolicLink) { |
| 23 | + // Filter out symbolic links to directories. An issue in yarn versions |
| 24 | + // older than 1.12.1 creates symbolic links to folders in the .bin folder |
| 25 | + // which leads to Bazel targets that cross package boundaries. |
| 26 | + // See https://github.com/bazelbuild/rules_nodejs/issues/428 and |
| 27 | + // https://github.com/bazelbuild/rules_nodejs/issues/438. |
| 28 | + // This is tested in internal/e2e/fine_grained_symlinks. |
| 29 | + return files; |
| 30 | + } |
| 31 | + if (isDirectory) { |
| 32 | + return files.concat(findBazelFiles(fullPath)); |
| 33 | + } else { |
| 34 | + const fileUc = file.toUpperCase(); |
| 35 | + if (fileUc == 'BUILD' || fileUc == 'BUILD.BAZEL') { |
| 36 | + return files.concat(fullPath); |
| 37 | + } |
| 38 | + return files; |
| 39 | + } |
| 40 | + }, []); |
| 41 | +} |
| 42 | + |
| 43 | +function main() { |
| 44 | + // Rename all bazel files found by prefixing them with `_` |
| 45 | + for (f of findBazelFiles('node_modules')) { |
| 46 | + const d = path.posix.join(path.dirname(f), `_${path.basename(f)}`); |
| 47 | + fs.renameSync(f, d); |
| 48 | + } |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +module.exports = {main}; |
| 53 | + |
| 54 | +if (require.main === module) { |
| 55 | + process.exitCode = main(); |
| 56 | +} |
0 commit comments