|
| 1 | +/** |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2016-2023 Mickael Jeanroy |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import path from 'path'; |
| 26 | +import fs from 'fs'; |
| 27 | +import _ from 'lodash'; |
| 28 | +import glob from 'glob'; |
| 29 | + |
| 30 | +/** |
| 31 | + * Find file and returns its content if file exists. |
| 32 | + * |
| 33 | + * @param {string} dir File directory. |
| 34 | + * @param {string} cwd Working directory. |
| 35 | + * @param {string|Array<string>} names Potential filenames. |
| 36 | + * @returns {string|null} File content, or `null` if file does not exist. |
| 37 | + */ |
| 38 | +export function readFile(dir, cwd, names) { |
| 39 | + const inputs = _.castArray(names); |
| 40 | + |
| 41 | + for (let i = 0; i < inputs.length; ++i) { |
| 42 | + const input = generatePattern(inputs[i]); |
| 43 | + const absolutePath = path.join(dir, input); |
| 44 | + const relativeToCwd = path.relative(cwd, absolutePath); |
| 45 | + |
| 46 | + const findings = glob.sync(relativeToCwd, {cwd}); |
| 47 | + for (let j = 0; j < findings.length; ++j) { |
| 48 | + const file = path.join(cwd, findings[j]); |
| 49 | + if (isFile(file)) { |
| 50 | + return fs.readFileSync(file, 'utf-8'); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return null; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Check that given file exists, and is a real file. |
| 60 | + * |
| 61 | + * @param {string} file File path. |
| 62 | + * @returns {boolean} `true` if `file` is a file, `false` otherwise. |
| 63 | + */ |
| 64 | +function isFile(file) { |
| 65 | + return !!fs.existsSync(file) && !!fs.lstatSync(file).isFile(); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Generate glob pattern for given input. |
| 70 | + * |
| 71 | + * @param {string} input Given input. |
| 72 | + * @returns {string} Glob pattern. |
| 73 | + */ |
| 74 | +function generatePattern(input) { |
| 75 | + let pattern = ''; |
| 76 | + |
| 77 | + for (let i = 0; i < input.length; ++i) { |
| 78 | + const c = input[i]; |
| 79 | + const up = c.toUpperCase(); |
| 80 | + const low = c.toLowerCase(); |
| 81 | + pattern += up !== low ? `[${low}${up}]` : low; |
| 82 | + } |
| 83 | + |
| 84 | + return pattern + '*'; |
| 85 | +} |
0 commit comments