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