mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1const crypto = require('crypto')
2const fs = require('fs')
3const fsp = fs.promises
4const path = require('path')
5
6const IOS_DIST_DIR = './ios-dist'
7const ANDROID_DIST_DIR = './android-dist'
8
9const BUNDLES_DIR = '/_expo/static/js'
10
11const IOS_BUNDLE_DIR = path.join(IOS_DIST_DIR, BUNDLES_DIR, '/ios')
12const ANDROID_BUNDLE_DIR = path.join(ANDROID_DIST_DIR, BUNDLES_DIR, '/android')
13
14const IOS_METADATA_PATH = path.join(IOS_DIST_DIR, '/metadata.json')
15const ANDROID_METADATA_PATH = path.join(ANDROID_DIST_DIR, '/metadata.json')
16
17const DEST_DIR = './bundleTempDir'
18
19// Weird, don't feel like figuring out _why_ it wants this
20const IOS_METADATA = require(`../${IOS_METADATA_PATH}`)
21const ANDROID_METADATA = require(`../${ANDROID_METADATA_PATH}`)
22
23const IOS_METADATA_ASSETS = IOS_METADATA.fileMetadata.ios.assets
24const ANDROID_METADATA_ASSETS = ANDROID_METADATA.fileMetadata.android.assets
25
26const getMd5 = async path => {
27 return new Promise(res => {
28 const hash = crypto.createHash('md5')
29 const rStream = fs.createReadStream(path)
30 rStream.on('data', data => {
31 hash.update(data)
32 })
33 rStream.on('end', () => {
34 res(hash.digest('hex'))
35 })
36 })
37}
38
39const moveFiles = async () => {
40 console.log('Making directory...')
41 await fsp.mkdir(DEST_DIR)
42 await fsp.mkdir(path.join(DEST_DIR, '/assets'))
43
44 console.log('Getting ios md5...')
45 const iosCurrPath = path.join(
46 IOS_BUNDLE_DIR,
47 (await fsp.readdir(IOS_BUNDLE_DIR))[0],
48 )
49 const iosMd5 = await getMd5(iosCurrPath)
50 const iosNewPath = `bundles/${iosMd5}.bundle`
51
52 console.log('Copying ios bundle...')
53 await fsp.cp(iosCurrPath, path.join(DEST_DIR, iosNewPath))
54
55 console.log('Getting android md5...')
56 const androidCurrPath = path.join(
57 ANDROID_BUNDLE_DIR,
58 (await fsp.readdir(ANDROID_BUNDLE_DIR))[0],
59 )
60 const androidMd5 = await getMd5(androidCurrPath)
61 const androidNewPath = `bundles/${androidMd5}.bundle`
62
63 console.log('Copying android bundle...')
64 await fsp.cp(androidCurrPath, path.join(DEST_DIR, androidNewPath))
65
66 const iosAssets = []
67 const androidAssets = []
68
69 console.log('Getting ios asset md5s and moving them...')
70 for (const asset of IOS_METADATA_ASSETS) {
71 const currPath = path.join(IOS_DIST_DIR, asset.path)
72 const md5 = await getMd5(currPath)
73 const withExtPath = `assets/${md5}.${asset.ext}`
74 iosAssets.push(withExtPath)
75 await fsp.cp(currPath, path.join(DEST_DIR, withExtPath))
76 }
77
78 console.log('Getting android asset md5s and moving them...')
79 for (const asset of ANDROID_METADATA_ASSETS) {
80 const currPath = path.join(ANDROID_DIST_DIR, asset.path)
81 const md5 = await getMd5(currPath)
82 const withExtPath = `assets/${md5}.${asset.ext}`
83 androidAssets.push(withExtPath)
84 await fsp.cp(currPath, path.join(DEST_DIR, withExtPath))
85 }
86
87 const result = {
88 version: 0,
89 bundler: 'metro',
90 fileMetadata: {
91 ios: {
92 bundle: iosNewPath,
93 assets: iosAssets,
94 },
95 android: {
96 bundle: androidNewPath,
97 assets: androidAssets,
98 },
99 },
100 }
101
102 console.log('Writing metadata...')
103 await fsp.writeFile(
104 path.join(DEST_DIR, 'metadata.json'),
105 JSON.stringify(result),
106 )
107
108 console.log('Finished!')
109 console.log('Metadata:', result)
110}
111
112moveFiles()