( builder: BundleBuilder, baseURL: string, dir: string, overrideHeaders: Headers | undefined )
| 34 | } |
| 35 | |
| 36 | export function addFilesRecursively( |
| 37 | builder: BundleBuilder, |
| 38 | baseURL: string, |
| 39 | dir: string, |
| 40 | overrideHeaders: Headers | undefined |
| 41 | ) { |
| 42 | if (baseURL !== '' && !baseURL.endsWith('/')) { |
| 43 | throw new Error("Non-empty baseURL must end with '/'."); |
| 44 | } |
| 45 | const files = fs.readdirSync(dir); |
| 46 | files.sort(); // Sort entries for reproducibility. |
| 47 | for (const file of files) { |
| 48 | const filePath = path.join(dir, file); |
| 49 | if (fs.statSync(filePath).isDirectory()) { |
| 50 | addFilesRecursively( |
| 51 | builder, |
| 52 | baseURL + file + '/', |
| 53 | filePath, |
| 54 | overrideHeaders |
| 55 | ); |
| 56 | } else if (file === 'index.html') { |
| 57 | // If the file name is 'index.html', create an entry for baseURL itself |
| 58 | // and another entry for baseURL/index.html which redirects to baseURL. |
| 59 | // This matches the behavior of gen-bundle. |
| 60 | addFile(builder, baseURL, filePath, overrideHeaders); |
| 61 | builder.addExchange( |
| 62 | baseURL + file, |
| 63 | 301, |
| 64 | combineHeadersForUrl( |
| 65 | { Location: './' }, |
| 66 | overrideHeaders, |
| 67 | baseURL + file |
| 68 | ), |
| 69 | '' |
| 70 | ); |
| 71 | } else { |
| 72 | addFile(builder, baseURL + file, filePath, overrideHeaders); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | function readOptions() { |
| 78 | return commander |
no test coverage detected