|
| 1 | +let { rm, existsSync } = require('fs') |
| 2 | +let path = require('path') |
| 3 | +let fs = require('fs/promises') |
| 4 | + |
| 5 | +let chokidar = require('chokidar') |
| 6 | + |
| 7 | +let resolveToolRoot = require('./resolve-tool-root') |
| 8 | + |
| 9 | +module.exports = function ({ |
| 10 | + /** Output directory, relative to the tool. */ |
| 11 | + output = 'dist', |
| 12 | + |
| 13 | + /** Input directory, relative to the tool. */ |
| 14 | + input = 'src', |
| 15 | + |
| 16 | + /** Whether or not you want to cleanup the output directory. */ |
| 17 | + cleanup = true, |
| 18 | +} = {}) { |
| 19 | + let toolRoot = resolveToolRoot() |
| 20 | + let fileCache = {} |
| 21 | + |
| 22 | + let absoluteOutputFolder = path.resolve(toolRoot, output) |
| 23 | + let absoluteInputFolder = path.resolve(toolRoot, input) |
| 24 | + |
| 25 | + if (cleanup) { |
| 26 | + beforeAll((done) => rm(absoluteOutputFolder, { recursive: true, force: true }, done)) |
| 27 | + afterEach((done) => rm(absoluteOutputFolder, { recursive: true, force: true }, done)) |
| 28 | + } |
| 29 | + |
| 30 | + // Restore all written files |
| 31 | + afterEach(async () => { |
| 32 | + await Promise.all( |
| 33 | + Object.entries(fileCache).map(([file, content]) => fs.writeFile(file, content, 'utf8')) |
| 34 | + ) |
| 35 | + }) |
| 36 | + |
| 37 | + async function readdir(start, parent = []) { |
| 38 | + let files = await fs.readdir(start, { withFileTypes: true }) |
| 39 | + let resolvedFiles = await Promise.all( |
| 40 | + files.map((file) => { |
| 41 | + if (file.isDirectory()) { |
| 42 | + return readdir(path.resolve(start, file.name), [...parent, file.name]) |
| 43 | + } |
| 44 | + return parent.concat(file.name).join(path.sep) |
| 45 | + }) |
| 46 | + ) |
| 47 | + return resolvedFiles.flat(Infinity) |
| 48 | + } |
| 49 | + |
| 50 | + async function resolveFile(fileOrRegex, directory) { |
| 51 | + if (fileOrRegex instanceof RegExp) { |
| 52 | + let files = await readdir(directory) |
| 53 | + if (files.length === 0) { |
| 54 | + throw new Error(`No files exists in "${directory}"`) |
| 55 | + } |
| 56 | + |
| 57 | + let filtered = files.filter((file) => fileOrRegex.test(file)) |
| 58 | + if (filtered.length === 0) { |
| 59 | + throw new Error(`Not a single file matched: ${fileOrRegex}`) |
| 60 | + } else if (filtered.length > 1) { |
| 61 | + throw new Error(`Multiple files matched: ${fileOrRegex}`) |
| 62 | + } |
| 63 | + |
| 64 | + return filtered[0] |
| 65 | + } |
| 66 | + |
| 67 | + return fileOrRegex |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + async readOutputFile(file) { |
| 72 | + file = await resolveFile(file, absoluteOutputFolder) |
| 73 | + return fs.readFile(path.resolve(absoluteOutputFolder, file), 'utf8') |
| 74 | + }, |
| 75 | + async appendToInputFile(file, contents) { |
| 76 | + let filePath = path.resolve(absoluteInputFolder, file) |
| 77 | + if (!fileCache[filePath]) { |
| 78 | + fileCache[filePath] = await fs.readFile(filePath, 'utf8') |
| 79 | + } |
| 80 | + |
| 81 | + return fs.appendFile(filePath, contents, 'utf8') |
| 82 | + }, |
| 83 | + async writeInputFile(file, contents) { |
| 84 | + let filePath = path.resolve(absoluteInputFolder, file) |
| 85 | + if (!fileCache[filePath]) { |
| 86 | + fileCache[filePath] = await fs.readFile(filePath, 'utf8') |
| 87 | + } |
| 88 | + |
| 89 | + return fs.writeFile(path.resolve(absoluteInputFolder, file), contents, 'utf8') |
| 90 | + }, |
| 91 | + async waitForOutputFileCreation(file) { |
| 92 | + if (file instanceof RegExp) { |
| 93 | + let r = file |
| 94 | + let watcher = chokidar.watch(absoluteOutputFolder) |
| 95 | + |
| 96 | + return new Promise((resolve) => { |
| 97 | + watcher.on('add', (file) => { |
| 98 | + if (r.test(file)) { |
| 99 | + watcher.close() |
| 100 | + resolve() |
| 101 | + } |
| 102 | + }) |
| 103 | + }) |
| 104 | + } else { |
| 105 | + let filePath = path.resolve(absoluteOutputFolder, file) |
| 106 | + let watcher = chokidar.watch(filePath) |
| 107 | + |
| 108 | + let watcherPromise = new Promise((resolve) => { |
| 109 | + watcher.once('add', () => { |
| 110 | + watcher.close() |
| 111 | + resolve() |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + if (existsSync(filePath)) { |
| 116 | + watcher.close() |
| 117 | + return Promise.resolve() |
| 118 | + } |
| 119 | + |
| 120 | + return watcherPromise |
| 121 | + } |
| 122 | + }, |
| 123 | + async waitForOutputFileChange(file, cb = () => {}) { |
| 124 | + file = await resolveFile(file, absoluteOutputFolder) |
| 125 | + |
| 126 | + let filePath = path.resolve(absoluteOutputFolder, file) |
| 127 | + let watcher = chokidar.watch(filePath) |
| 128 | + |
| 129 | + return new Promise((resolve) => { |
| 130 | + let chain = Promise.resolve() |
| 131 | + watcher.once('change', () => { |
| 132 | + watcher.close() |
| 133 | + chain.then(() => resolve()) |
| 134 | + }) |
| 135 | + chain.then(() => cb()) |
| 136 | + }) |
| 137 | + }, |
| 138 | + } |
| 139 | +} |
0 commit comments