| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | |
| 6 | const runtimeAssetPattern = /\.(json|ocd2)$/; |
| 7 | |
| 8 | function copyMatchingFiles(sourceDir, outputDir, predicate) { |
| 9 | if (!fs.existsSync(sourceDir)) { |
| 10 | return 0; |
| 11 | } |
| 12 | |
| 13 | let count = 0; |
| 14 | for (const entry of fs.readdirSync(sourceDir, { withFileTypes: true })) { |
| 15 | if (!entry.isFile() || !predicate(entry.name)) { |
| 16 | continue; |
| 17 | } |
| 18 | |
| 19 | fs.copyFileSync( |
| 20 | path.join(sourceDir, entry.name), |
| 21 | path.join(outputDir, entry.name) |
| 22 | ); |
| 23 | count += 1; |
| 24 | } |
| 25 | return count; |
| 26 | } |
| 27 | |
| 28 | function prepareArtifacts(root = path.join(__dirname, '..')) { |
| 29 | const legacySourceDir = path.join(root, 'build', 'Release'); |
| 30 | const bazelDictionaryDir = path.join(root, 'bazel-bin', 'data', 'dictionary'); |
| 31 | const configDir = path.join(root, 'data', 'config'); |
| 32 | const outputDir = path.join(root, 'prebuilds', 'assets'); |
| 33 | |
| 34 | fs.rmSync(outputDir, { recursive: true, force: true }); |
| 35 | fs.mkdirSync(outputDir, { recursive: true }); |
| 36 | |
| 37 | if (fs.existsSync(legacySourceDir)) { |
| 38 | const copied = copyMatchingFiles( |
| 39 | legacySourceDir, |
| 40 | outputDir, |
| 41 | (name) => runtimeAssetPattern.test(name) |
| 42 | ); |
| 43 | if (copied > 0) { |
| 44 | return; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | const jsonCount = copyMatchingFiles( |
| 49 | configDir, |
| 50 | outputDir, |
| 51 | (name) => name.endsWith('.json') && !name.endsWith('.schema.json') |
| 52 | ); |
| 53 | const dictionaryCount = copyMatchingFiles( |
| 54 | bazelDictionaryDir, |
| 55 | outputDir, |
| 56 | (name) => name.endsWith('.ocd2') |
| 57 | ); |
| 58 | |
| 59 | if (jsonCount === 0 || dictionaryCount === 0) { |
| 60 | throw new Error( |
| 61 | 'Could not prepare Node.js prebuild assets. Run node-gyp prebuild first, ' + |
| 62 | 'or run `bazel build -c opt //data/dictionary:binary_dictionaries` before this script.' |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (require.main === module) { |
| 68 | prepareArtifacts(); |
| 69 | } |
| 70 | |
| 71 | module.exports = { prepareArtifacts }; |