|
| 1 | +/* eslint-disable no-console */ |
1 | 2 | const path = require('path'); |
2 | 3 | const fs = require('fs'); |
3 | 4 |
|
4 | | -const resPath = path.resolve( |
| 5 | +const buildFilePath = path.resolve(__dirname, '../build.json'); |
| 6 | +const copyToPath = path.resolve(__dirname, '../platforms/android/build.json'); |
| 7 | +const gradleFilePath = path.resolve(__dirname, '../build-extras.gradle'); |
| 8 | +const androidGradleFilePath = path.resolve( |
5 | 9 | __dirname, |
6 | | - '../platforms/android/app/src/main/res/' |
| 10 | + '../platforms/android/app/build-extras.gradle' |
7 | 11 | ); |
| 12 | +const resPath = path.resolve(__dirname, '../platforms/android/app/src/main/res/'); |
| 13 | +const localResPath = path.resolve(__dirname, '../res/android/'); |
8 | 14 |
|
9 | | -const splashScreens = [ |
10 | | - 'mipmap-ldpi', |
11 | | - 'mipmap-hdpi-v26', |
12 | | - 'mipmap-ldpi-v26', |
13 | | - 'mipmap-mdpi-v26', |
14 | | - 'mipmap-xhdpi-v26', |
15 | | - 'mipmap-xxhdpi-v26', |
16 | | - 'mipmap-xxxhdpi-v26', |
17 | | - 'drawable-land-hdpi', |
18 | | - 'drawable-land-ldpi', |
19 | | - 'drawable-land-mdpi', |
20 | | - 'drawable-land-xhdpi', |
21 | | - 'drawable-land-xxhdpi', |
22 | | - 'drawable-land-xxxhdpi', |
23 | | - 'drawable-port-hdpi', |
24 | | - 'drawable-port-ldpi', |
25 | | - 'drawable-port-mdpi', |
26 | | - 'drawable-port-xhdpi', |
27 | | - 'drawable-port-xxhdpi', |
28 | | - 'drawable-port-xxxhdpi', |
29 | | -]; |
30 | | - |
31 | | -for (let splashScreen of splashScreens) { |
32 | | - const file = path.join(resPath, splashScreen); |
33 | | - if (fs.existsSync(file)) { |
34 | | - fs.rmSync(file, { |
35 | | - recursive: true, |
| 15 | +if ( |
| 16 | + !fs.existsSync(copyToPath) |
| 17 | + && fs.existsSync(buildFilePath) |
| 18 | +) fs.copyFileSync(buildFilePath, copyToPath); |
| 19 | + |
| 20 | +if (fs.existsSync(androidGradleFilePath)) fs.unlinkSync(androidGradleFilePath); |
| 21 | +fs.copyFileSync(gradleFilePath, androidGradleFilePath); |
| 22 | + |
| 23 | +deleteDirRecursively(resPath, [ |
| 24 | + path.join('values', 'strings.xml'), |
| 25 | + path.join('values', 'colors.xml'), |
| 26 | + path.join('values', 'styles.xml'), |
| 27 | + 'anim', |
| 28 | + 'xml', |
| 29 | +]); |
| 30 | +copyDirRecursively(localResPath, resPath); |
| 31 | + |
| 32 | +/** |
| 33 | + * Copy directory recursively |
| 34 | + * @param {string} src Source directory |
| 35 | + * @param {string} dest Destination directory |
| 36 | + * @param {string[]} skip Files to not copy |
| 37 | + */ |
| 38 | +function copyDirRecursively(src, dest, skip = [], currPath = '') { |
| 39 | + const exists = fs.existsSync(src); |
| 40 | + const stats = exists && fs.statSync(src); |
| 41 | + const isDirectory = exists && stats.isDirectory(); |
| 42 | + |
| 43 | + if (!exists) { |
| 44 | + console.log(`File ${src} does not exist`); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (!fs.existsSync(dest) && isDirectory) { |
| 49 | + fs.mkdirSync(dest); |
| 50 | + } |
| 51 | + |
| 52 | + if (exists && isDirectory) { |
| 53 | + fs.mkdirSync(dest, { recursive: true }); |
| 54 | + fs.readdirSync(src).forEach((childItemName) => { |
| 55 | + const relativePath = path.join(currPath, childItemName); |
| 56 | + if (childItemName.startsWith('.')) return; |
| 57 | + if (skip.includes(childItemName) || skip.includes(relativePath)) return; |
| 58 | + copyDirRecursively( |
| 59 | + path.join(src, childItemName), |
| 60 | + path.join(dest, childItemName), |
| 61 | + skip, |
| 62 | + childItemName, |
| 63 | + ); |
| 64 | + }); |
| 65 | + } else { |
| 66 | + fs.copyFileSync(src, dest); |
| 67 | + |
| 68 | + // log |
| 69 | + const message = `copied: ${path.basename(src)}`; |
| 70 | + console.log('\x1b[32m%s\x1b[0m', message); // green |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Delete directory recursively |
| 76 | + * @param {string} dir Directory to delete |
| 77 | + * @param {string[]} except Files to not delete |
| 78 | + */ |
| 79 | +function deleteDirRecursively(dir, except = [], currPath = '') { |
| 80 | + const exists = fs.existsSync(dir); |
| 81 | + const stats = exists && fs.statSync(dir); |
| 82 | + const isDirectory = exists && stats.isDirectory(); |
| 83 | + |
| 84 | + if (!exists) { |
| 85 | + console.log(`File ${dir} does not exist`); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (exists && isDirectory) { |
| 90 | + let deleteDir = true; |
| 91 | + fs.readdirSync(dir).forEach((childItemName) => { |
| 92 | + const relativePath = path.join(currPath, childItemName); |
| 93 | + if ( |
| 94 | + childItemName.startsWith('.') |
| 95 | + || except.includes(childItemName) |
| 96 | + || except.includes(relativePath) |
| 97 | + ) { |
| 98 | + console.log('\x1b[33m%s\x1b[0m', `skipped: ${relativePath}`); // yellow |
| 99 | + deleteDir = false; |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + deleteDirRecursively( |
| 104 | + path.join(dir, childItemName), |
| 105 | + except, |
| 106 | + childItemName, |
| 107 | + ); |
36 | 108 | }); |
37 | | - console.log('Removed: ', splashScreen); |
| 109 | + |
| 110 | + if (deleteDir) { |
| 111 | + console.log('\x1b[31m%s\x1b[0m', `deleted: ${currPath || path.basename(dir)}`); // red |
| 112 | + fs.rmSync(dir, { recursive: true }); |
| 113 | + } |
| 114 | + } else { |
| 115 | + console.log('\x1b[31m%s\x1b[0m', `deleted: ${currPath || path.basename(dir)}`); // red |
| 116 | + fs.rmSync(dir); |
38 | 117 | } |
39 | 118 | } |
0 commit comments