sync-intl-tel-input.js
105 lines
| 1 | // tools/sync-intl-tel-input.js |
| 2 | const fs = require('fs'); |
| 3 | const path = require('path'); |
| 4 | |
| 5 | function ensureDir(dir) { |
| 6 | fs.mkdirSync(dir, { recursive: true }); |
| 7 | } |
| 8 | |
| 9 | function exists(p) { |
| 10 | return fs.existsSync(p); |
| 11 | } |
| 12 | |
| 13 | function rmSafe(targetPath) { |
| 14 | if (!exists(targetPath)) return; |
| 15 | fs.rmSync(targetPath, { recursive: true, force: true }); |
| 16 | } |
| 17 | |
| 18 | function copyFile(src, dest) { |
| 19 | ensureDir(path.dirname(dest)); |
| 20 | fs.copyFileSync(src, dest); |
| 21 | } |
| 22 | |
| 23 | function copyDir(srcDir, destDir) { |
| 24 | if (!exists(srcDir)) return; |
| 25 | |
| 26 | ensureDir(destDir); |
| 27 | |
| 28 | for (const entry of fs.readdirSync(srcDir, { withFileTypes: true })) { |
| 29 | const src = path.join(srcDir, entry.name); |
| 30 | const dest = path.join(destDir, entry.name); |
| 31 | |
| 32 | if (entry.isDirectory()) copyDir(src, dest); |
| 33 | else copyFile(src, dest); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | function assertExists(p, label) { |
| 38 | if (!exists(p)) { |
| 39 | console.error(`[sync] ERROR: Missing ${label}: ${p}`); |
| 40 | process.exit(1); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | function readJSON(p) { |
| 45 | return JSON.parse(fs.readFileSync(p, 'utf8')); |
| 46 | } |
| 47 | |
| 48 | // --- Resolve package root --- |
| 49 | let pkgJsonPath; |
| 50 | try { |
| 51 | pkgJsonPath = require.resolve('intl-tel-input/package.json'); |
| 52 | } catch (e) { |
| 53 | console.error('[sync] ERROR: intl-tel-input is not installed. Run: npm i intl-tel-input'); |
| 54 | process.exit(1); |
| 55 | } |
| 56 | |
| 57 | const pkgRoot = path.dirname(pkgJsonPath); |
| 58 | const pkgJSON = readJSON(pkgJsonPath); |
| 59 | const version = pkgJSON?.version || 'unknown'; |
| 60 | |
| 61 | // --- Sources in node_modules --- |
| 62 | const srcCss = path.join(pkgRoot, 'build', 'css', 'intlTelInput.min.css'); |
| 63 | const srcJs = path.join(pkgRoot, 'build', 'js', 'intlTelInputWithUtils.min.js'); |
| 64 | const srcImg = path.join(pkgRoot, 'build', 'img'); |
| 65 | const srcI18n = path.join(pkgRoot, 'build', 'js', 'i18n'); |
| 66 | |
| 67 | // Ensure critical assets exist |
| 68 | assertExists(srcCss, 'CSS intlTelInput.min.css'); |
| 69 | assertExists(srcJs, 'JS intlTelInputWithUtils.min.js'); |
| 70 | |
| 71 | // --- Destinations in plugin --- |
| 72 | const pluginRoot = path.resolve(__dirname, '..'); |
| 73 | const outBase = path.join(pluginRoot, 'assets', 'lib'); |
| 74 | |
| 75 | const outCss = path.join(outBase, 'intl-tel-input', 'intlTelInput.min.css'); |
| 76 | const outJs = path.join(outBase, 'intl-tel-input', 'intlTelInputWithUtils.min.js'); |
| 77 | const outImg = path.join(outBase, 'img'); |
| 78 | const outI18n = path.join(outBase, 'intl-tel-input', 'i18n'); |
| 79 | |
| 80 | // --- Cleanup (so no leftovers after updates) --- |
| 81 | rmSafe(outCss); |
| 82 | rmSafe(outJs); |
| 83 | rmSafe(outImg); |
| 84 | rmSafe(outI18n); |
| 85 | |
| 86 | // Recreate base folders (optional, but clearer) |
| 87 | ensureDir(path.dirname(outCss)); |
| 88 | ensureDir(path.dirname(outJs)); |
| 89 | |
| 90 | // --- Copy assets --- |
| 91 | copyFile(srcCss, outCss); |
| 92 | copyFile(srcJs, outJs); |
| 93 | copyDir(srcImg, outImg); |
| 94 | copyDir(srcI18n, outI18n); |
| 95 | |
| 96 | // --- Report --- |
| 97 | const copied = []; |
| 98 | copied.push(path.relative(pluginRoot, outCss)); |
| 99 | copied.push(path.relative(pluginRoot, outJs)); |
| 100 | if (exists(outImg)) copied.push(path.relative(pluginRoot, outImg) + '/'); |
| 101 | if (exists(outI18n)) copied.push(path.relative(pluginRoot, outI18n) + '/'); |
| 102 | |
| 103 | console.log(`[sync] intl-tel-input v${version} synced:`); |
| 104 | for (const item of copied) console.log(` - ${item}`); |
| 105 |