-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject-srcpush-config.js
More file actions
172 lines (144 loc) · 4.04 KB
/
Copy pathinject-srcpush-config.js
File metadata and controls
172 lines (144 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const usage = () => {
console.error(`Usage:
node scripts/inject-srcpush-config.js \\
--android-strings <path> \\
--ios-plist <path> \\
--deployment-key-android <value> \\
--deployment-key-ios <value> \\
[--server-url <value>] \\
[--public-key <value> | --public-key-file <path>]`);
};
const parseArgs = (argv) => {
const args = {};
for (let index = 0; index < argv.length; index += 1) {
const token = argv[index];
if (!token.startsWith('--')) {
throw new Error(`Unexpected argument: ${token}`);
}
const key = token.slice(2);
const value = argv[index + 1];
if (!value || value.startsWith('--')) {
throw new Error(`Missing value for --${key}`);
}
args[key] = value;
index += 1;
}
return args;
};
const required = (args, keys) => {
for (const key of keys) {
if (!args[key]) {
throw new Error(`Missing required argument --${key}`);
}
}
};
const readFile = (filePath) => fs.readFileSync(filePath, 'utf8');
const writeFile = (filePath, content) => fs.writeFileSync(filePath, content, 'utf8');
const xmlEscape = (value) =>
value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
const upsertAndroidString = (content, name, value) => {
const escapedValue = xmlEscape(value);
const pattern = new RegExp(
`(<string\\s+name="${name}">)([\\s\\S]*?)(</string>)`,
'm',
);
if (pattern.test(content)) {
return content.replace(pattern, `$1${escapedValue}$3`);
}
if (!content.includes('</resources>')) {
throw new Error('Invalid Android strings.xml: missing </resources>');
}
return content.replace(
'</resources>',
` <string name="${name}">${escapedValue}</string>\n</resources>`,
);
};
const upsertPlistString = (content, key, value) => {
const escapedValue = xmlEscape(value);
const pattern = new RegExp(
`(<key>${key}</key>\\s*<string>)([\\s\\S]*?)(</string>)`,
'm',
);
if (pattern.test(content)) {
return content.replace(pattern, `$1${escapedValue}$3`);
}
if (!content.includes('</dict>')) {
throw new Error('Invalid Info.plist: missing </dict>');
}
return content.replace(
'</dict>',
`\t<key>${key}</key>\n\t<string>${escapedValue}</string>\n</dict>`,
);
};
const resolvePublicKey = (args) => {
if (args['public-key']) {
return args['public-key'];
}
if (args['public-key-file']) {
const publicKeyPath = path.resolve(args['public-key-file']);
return readFile(publicKeyPath).trim();
}
return null;
};
const main = () => {
const args = parseArgs(process.argv.slice(2));
required(args, [
'android-strings',
'ios-plist',
'deployment-key-android',
'deployment-key-ios',
]);
const publicKey = resolvePublicKey(args);
const androidStringsPath = path.resolve(args['android-strings']);
const iosPlistPath = path.resolve(args['ios-plist']);
let androidStrings = readFile(androidStringsPath);
androidStrings = upsertAndroidString(
androidStrings,
'CodePushDeploymentKey',
args['deployment-key-android'],
);
if (args['server-url']) {
androidStrings = upsertAndroidString(
androidStrings,
'CodePushServerUrl',
args['server-url'],
);
}
if (publicKey) {
androidStrings = upsertAndroidString(
androidStrings,
'CodePushPublicKey',
publicKey,
);
}
let iosPlist = readFile(iosPlistPath);
iosPlist = upsertPlistString(
iosPlist,
'CodePushDeploymentKey',
args['deployment-key-ios'],
);
if (args['server-url']) {
iosPlist = upsertPlistString(iosPlist, 'CodePushServerURL', args['server-url']);
}
if (publicKey) {
iosPlist = upsertPlistString(iosPlist, 'CodePushPublicKey', publicKey);
}
writeFile(androidStringsPath, androidStrings);
writeFile(iosPlistPath, iosPlist);
console.info(`Updated ${androidStringsPath}`);
console.info(`Updated ${iosPlistPath}`);
};
try {
main();
} catch (error) {
console.error(error.message);
usage();
process.exit(1);
}