|
| 1 | +import node_fs from "node:fs" |
| 2 | +import node_path from "node:path" |
| 3 | + |
| 4 | +import SemverValid from "semver/functions/valid.js" |
| 5 | + |
| 6 | +import ReactNativeEchoPackageJson from "../../package/package.json" with { type: "json" } |
| 7 | + |
| 8 | +/** |
| 9 | + * @param {string} rootDir |
| 10 | + */ |
| 11 | +export async function prepack( |
| 12 | + rootDir, |
| 13 | +) { |
| 14 | + |
| 15 | + { |
| 16 | + // Replace placeholder version at the |
| 17 | + // /react-native-echo/package/src/_internal/const/version.ts |
| 18 | + |
| 19 | + const libraryVersion = SemverValid(ReactNativeEchoPackageJson.version) |
| 20 | + |
| 21 | + if(!libraryVersion) { |
| 22 | + throw new TypeError("Library version is not a valid semver") |
| 23 | + } |
| 24 | + |
| 25 | + const filePath = node_path.join(rootDir, "package", "src", "_internal", "const", "echo", "version.ts") |
| 26 | + |
| 27 | + if(!node_fs.existsSync(filePath)) { |
| 28 | + throw new Error(`${filePath} doesn't exist`) |
| 29 | + } |
| 30 | + |
| 31 | + let fileStr = node_fs.readFileSync(filePath).toString() |
| 32 | + fileStr = fileStr.replace(/(export const VERSION = )("X\.X\.X")/, `$1"${libraryVersion}"`) |
| 33 | + |
| 34 | + node_fs.writeFileSync(filePath, fileStr, { encoding: "utf8" }) |
| 35 | + } |
| 36 | + |
| 37 | + { |
| 38 | + // Copy files |
| 39 | + |
| 40 | + const |
| 41 | + /** |
| 42 | + * @type {{ |
| 43 | + * src: string, |
| 44 | + * dest: string, |
| 45 | + * }[]} |
| 46 | + */ |
| 47 | + filesToCopy = |
| 48 | + [ |
| 49 | + { |
| 50 | + src: node_path.join(rootDir, "LICENSE"), |
| 51 | + dest: node_path.join(rootDir, "package", "LICENSE"), |
| 52 | + }, |
| 53 | + { |
| 54 | + src: node_path.join(rootDir, "README.md"), |
| 55 | + dest: node_path.join(rootDir, "package", "README.md"), |
| 56 | + }, |
| 57 | + ] |
| 58 | + |
| 59 | + await Promise.all( |
| 60 | + filesToCopy.map(file => { |
| 61 | + return node_fs.promises.cp( |
| 62 | + file.src, |
| 63 | + file.dest, |
| 64 | + ) |
| 65 | + }), |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | +} |
| 70 | + |
0 commit comments