|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const |
| 4 | + fs = require("fs"), |
| 5 | + path = require("path"), |
| 6 | + replaceStream = require("replacestream"); |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * Default variables. |
| 11 | + * |
| 12 | + * .. note:: The behaviour of this function depends on two environment variables: |
| 13 | + * Either the `BUILD_ENV` should be set to one of `development`, `staging` |
| 14 | + * or `production`; or `NODE_ENV` must be set to `production`. |
| 15 | + * |
| 16 | + * .. note:: Do **NOT** use trailing slashes in the URL. |
| 17 | + * |
| 18 | + * .. attention:: `BASE_URL` must have the HTTP(S) protocol, but other URLs must not. |
| 19 | + * |
| 20 | + * @returns { {}|{DOWNLOADS_CDN: string, BASE_URL: string, MAIN_CDN: string} } |
| 21 | + */ |
| 22 | +const extractEnvVars = () => { |
| 23 | + |
| 24 | + const prod = { |
| 25 | + BASE_URL: "https://coronavirus.data.gov.uk", |
| 26 | + MAIN_CDN: "c19pub.azureedge.net", |
| 27 | + DOWNLOADS_CDN: "c19downloads.azureedge.net" |
| 28 | + }; |
| 29 | + |
| 30 | + if ( process.env.NODE_ENV === "production" && !process.env.hasOwnProperty("BUILD_ENV")) |
| 31 | + return prod; |
| 32 | + |
| 33 | + |
| 34 | + switch (process.env.BUILD_ENV) { |
| 35 | + case "development": |
| 36 | + return { |
| 37 | + BASE_URL: "https://covid19statdev.azureedge.net", |
| 38 | + MAIN_CDN: "c19pub.azureedgedev.net", |
| 39 | + DOWNLOADS_CDN: "c19downloadsdev.azureedge.net" |
| 40 | + } |
| 41 | + |
| 42 | + case "staging": |
| 43 | + return { |
| 44 | + BASE_URL: "https://Covid19StaticStaging.azureedge.net", |
| 45 | + MAIN_CDN: "c19pub.azureedgestaging.net", |
| 46 | + DOWNLOADS_CDN: "c19downloadsstaging.azureedge.net" |
| 47 | + } |
| 48 | + |
| 49 | + case "production": |
| 50 | + return prod |
| 51 | + |
| 52 | + default: |
| 53 | + return {} |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | +}; // extractEnvVars |
| 58 | + |
| 59 | + |
| 60 | +/** |
| 61 | + * Extracts ".js" and ".html" files in `directory` and its subdirectories |
| 62 | + * and returns an array of absolute paths. |
| 63 | + * |
| 64 | + * @param directory { string } |
| 65 | + * @returns { string[] } |
| 66 | + */ |
| 67 | +const getFiles = (directory) => { |
| 68 | + |
| 69 | + return fs |
| 70 | + .readdirSync(directory, { withFileTypes: true }) |
| 71 | + .reduce((acc, item) => [ |
| 72 | + ...acc, |
| 73 | + ...item.isDirectory() |
| 74 | + ? getFiles(path.join(directory, item.name)) |
| 75 | + : [ path.join(directory, item.name) ] |
| 76 | + ], []) |
| 77 | + .filter(file => /\.(js|html)$/i.exec(file)) |
| 78 | + |
| 79 | +}; // getFiles |
| 80 | + |
| 81 | + |
| 82 | +/** |
| 83 | + * Replaces placeholders formatted as `%key%` with environment |
| 84 | + * variables defined using the same key. |
| 85 | + * |
| 86 | + * @returns { Promise<void> } |
| 87 | + */ |
| 88 | +const main = async () => { |
| 89 | + |
| 90 | + const |
| 91 | + directory = path.join(__dirname, "..", "build"), |
| 92 | + files = getFiles(directory), |
| 93 | + Replacements = { |
| 94 | + ...extractEnvVars(), |
| 95 | + ...process.env |
| 96 | + }; |
| 97 | + |
| 98 | + for ( const file of files ) { |
| 99 | + |
| 100 | + const tmpFile = `${ file }.tmp`; |
| 101 | + |
| 102 | + await new Promise((resolve, reject) => { |
| 103 | + const stream = Object |
| 104 | + .keys(Replacements) |
| 105 | + .reduce((stream, key) => |
| 106 | + stream |
| 107 | + .pipe(replaceStream(`%${ key }%`, Replacements[key])) |
| 108 | + .pipe(replaceStream(`%REACT_APP_${ key }%`, Replacements[key])), |
| 109 | + fs.createReadStream(file) |
| 110 | + ) |
| 111 | + .pipe(fs.createWriteStream(tmpFile)); |
| 112 | + |
| 113 | + stream.on("finish", resolve); |
| 114 | + stream.on("error", reject); |
| 115 | + }); |
| 116 | + |
| 117 | + fs.unlinkSync(file); |
| 118 | + fs.copyFileSync(tmpFile, file); |
| 119 | + fs.unlinkSync(tmpFile); |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | +}; // main |
| 124 | + |
| 125 | + |
| 126 | +main().catch(err => { |
| 127 | + console.error(err); |
| 128 | + process.exitCode = 1; |
| 129 | +}); |
0 commit comments