1
+ import fs from 'fs' ;
2
+ import path from 'path' ;
3
+ import { exec } from 'child_process' ;
4
+ import { promisify } from 'util' ;
5
+
6
+ const execAsync = promisify ( exec ) ;
7
+
8
+ interface Config {
9
+ dir : string ;
10
+ type : string ;
11
+ repo : string ;
12
+ }
13
+
14
+ async function copyFile ( source : string , destination : string ) : Promise < void > {
15
+ try {
16
+ if ( fs . existsSync ( destination ) ) {
17
+ console . log ( `Overwriting existing file: ${ destination } ` ) ;
18
+ fs . unlinkSync ( destination ) ;
19
+ }
20
+
21
+ fs . copyFileSync ( source , destination ) ;
22
+ console . log ( `Successfully copied: ${ destination } ` ) ;
23
+ } catch ( e ) {
24
+ throw new Error ( `Failed to copy ${ source } to ${ destination } : ${ e } ` ) ;
25
+ }
26
+ }
27
+
28
+ async function ensureDirectoryExists ( dirPath : string ) : Promise < void > {
29
+ if ( ! fs . existsSync ( dirPath ) ) {
30
+ fs . mkdirSync ( dirPath , { recursive : true } ) ;
31
+ }
32
+ }
33
+
34
+ async function main ( ) : Promise < void > {
35
+ try {
36
+ // Check if .keys-n-stuff directory exists
37
+ const keysDir = '.keys-n-stuff' ;
38
+
39
+ // Read the keys-n-stuff.json file
40
+ const configPath = '.keys-n-stuff.json' ;
41
+ if ( ! fs . existsSync ( configPath ) ) {
42
+ throw new Error ( 'Configuration file not found' ) ;
43
+ }
44
+
45
+ const config : Config = JSON . parse ( fs . readFileSync ( configPath , 'utf8' ) ) ;
46
+ const { dir : projectDir , type : projectType , repo } = config ;
47
+ const source = `.keys-n-stuff/${ projectDir } ` ;
48
+
49
+ console . log ( 'Project directory:' , projectDir ) ;
50
+ console . log ( 'Project type:' , projectType ) ;
51
+ console . log ( 'Repository:' , repo ) ;
52
+
53
+ if ( fs . existsSync ( keysDir ) ) {
54
+ console . log ( 'Repository already exists, pulling latest changes...' ) ;
55
+ const { stderr : pullError } = await execAsync ( 'git pull' , {
56
+ cwd : keysDir ,
57
+ } ) ;
58
+
59
+ if ( pullError ) {
60
+ throw new Error ( `Failed to pull repository: ${ pullError } ` ) ;
61
+ }
62
+ console . log ( 'Successfully pulled latest changes' ) ;
63
+ } else {
64
+ console . log ( 'Cloning repository...' ) ;
65
+ const { stderr : cloneError } = await execAsync ( `git clone ${ repo } ${ keysDir } ` ) ;
66
+
67
+ if ( cloneError ) {
68
+ throw new Error ( `Failed to clone repository: ${ cloneError } ` ) ;
69
+ }
70
+ console . log ( 'Successfully cloned repository' ) ;
71
+ }
72
+
73
+ if ( projectType === 'expo' ) {
74
+ // Create necessary directories
75
+ await ensureDirectoryExists ( 'android/app' ) ;
76
+ await ensureDirectoryExists ( 'ios' ) ;
77
+
78
+ // Copy Firebase config files
79
+ // Android
80
+ await copyFile (
81
+ `${ source } /google-services.json` ,
82
+ 'google-services.json' // Root directory
83
+ ) ;
84
+ await copyFile (
85
+ `${ source } /google-services.json` ,
86
+ 'android/app/google-services.json'
87
+ ) ;
88
+
89
+ // iOS
90
+ await copyFile (
91
+ `${ source } /GoogleService-Info.plist` ,
92
+ 'GoogleService-Info.plist' // Root directory
93
+ ) ;
94
+ await copyFile (
95
+ `${ source } /GoogleService-Info.plist` ,
96
+ 'ios/GoogleService-Info.plist'
97
+ ) ;
98
+
99
+ // Copy keystore files if they exist
100
+ const prodKeystoreFile = `${ source } /prod.jks` ;
101
+ if ( fs . existsSync ( prodKeystoreFile ) ) {
102
+ await copyFile ( prodKeystoreFile , 'android/app/prod.jks' ) ;
103
+ await copyFile ( `${ source } /prod.properties` , 'android/prod.properties' ) ;
104
+ }
105
+
106
+ const devKeystoreFile = `${ source } /dev.jks` ;
107
+ if ( fs . existsSync ( devKeystoreFile ) ) {
108
+ await copyFile ( devKeystoreFile , 'android/app/dev.jks' ) ;
109
+ await copyFile ( `${ source } /dev.properties` , 'android/dev.properties' ) ;
110
+ }
111
+
112
+ // Copy service account if it exists
113
+ const serviceAccountFile = `${ source } /service-account.json` ;
114
+ if ( fs . existsSync ( serviceAccountFile ) ) {
115
+ await copyFile ( serviceAccountFile , 'service-account.json' ) ;
116
+ }
117
+
118
+ console . log (
119
+ 'Successfully copied all configuration files to their respective directories'
120
+ ) ;
121
+ } else {
122
+ throw new Error ( `Unsupported project type: ${ projectType } ` ) ;
123
+ }
124
+
125
+ console . log ( 'Setup completed successfully' ) ;
126
+ } catch ( e ) {
127
+ console . error ( 'Error:' , e ) ;
128
+ process . exit ( 1 ) ;
129
+ }
130
+ }
131
+
132
+ main ( ) ;
0 commit comments