-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.plugin.js
More file actions
118 lines (104 loc) · 3.52 KB
/
app.plugin.js
File metadata and controls
118 lines (104 loc) · 3.52 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
const pkg = require('./package.json');
let configPlugins;
try {
configPlugins = require('@expo/config-plugins');
} catch (error) {
const errorCode = error && error.code;
if (errorCode !== 'MODULE_NOT_FOUND') {
throw error;
}
configPlugins = require('expo/config-plugins');
}
const { createRunOncePlugin, withPodfile } = configPlugins;
const withAndroidGradleProperties =
typeof configPlugins.withAndroidGradleProperties === 'function'
? configPlugins.withAndroidGradleProperties
: configPlugins.withGradleProperties;
if (typeof withAndroidGradleProperties !== 'function') {
throw new Error(
`${pkg.name}: incompatible expo config-plugins API (missing Gradle properties helper).`
);
}
const PLUGIN_NAME = pkg.name;
const VALID_ENGINES = new Set(['native', 'zig']);
const PODFILE_BLOCK_START = '# @preeternal/react-native-file-hash begin';
const PODFILE_BLOCK_END = '# @preeternal/react-native-file-hash end';
function normalizeEngine(rawEngine) {
const normalized =
rawEngine == null ? '' : String(rawEngine).trim().toLowerCase();
const engine = normalized === '' ? 'native' : normalized;
if (!VALID_ENGINES.has(engine)) {
throw new Error(
`${PLUGIN_NAME}: invalid engine '${rawEngine}'. Expected 'native' or 'zig'.`
);
}
return engine;
}
function setAndroidEngine(config, engine) {
return withAndroidGradleProperties(config, (mod) => {
const key = 'react_native_file_hash_engine';
const existing = mod.modResults.find(
(item) => item.type === 'property' && item.key === key
);
if (existing) {
existing.value = engine;
} else {
mod.modResults.push({
type: 'property',
key,
value: engine,
});
}
return mod;
});
}
function updatePodfileContents(contents, engine) {
const managedBlock =
`${PODFILE_BLOCK_START}\n` +
`ENV['ZFH_ENGINE'] ||= '${engine}'\n` +
`${PODFILE_BLOCK_END}`;
const escapedStart = PODFILE_BLOCK_START.replace(
/[.*+?^${}()|[\]\\]/g,
'\\$&'
);
const escapedEnd = PODFILE_BLOCK_END.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const managedBlockRegex = new RegExp(
`${escapedStart}[\\s\\S]*?${escapedEnd}`,
'm'
);
if (managedBlockRegex.test(contents)) {
return contents.replace(managedBlockRegex, managedBlock);
}
const existingEnvRegex =
/ENV\[['"]ZFH_ENGINE['"]\]\s*(?:\|\|)?=\s*['"][^'"]*['"]/;
if (existingEnvRegex.test(contents)) {
return contents.replace(
existingEnvRegex,
`ENV['ZFH_ENGINE'] ||= '${engine}'`
);
}
const firstNonCommentLineRegex = /^((?:\s*#.*\n)*)/;
const match = contents.match(firstNonCommentLineRegex);
const insertAt = match ? match[0].length : 0;
return (
contents.slice(0, insertAt) +
`${managedBlock}\n\n` +
contents.slice(insertAt)
);
}
function setIosEngine(config, engine) {
return withPodfile(config, (mod) => {
mod.modResults.contents = updatePodfileContents(
mod.modResults.contents,
engine
);
return mod;
});
}
function withFileHashEngine(config, props = {}) {
const engine = normalizeEngine(props.engine);
config = setAndroidEngine(config, engine);
config = setIosEngine(config, engine);
return config;
}
module.exports = createRunOncePlugin(withFileHashEngine, pkg.name, pkg.version);