-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.plugin.js
More file actions
83 lines (71 loc) · 2.54 KB
/
app.plugin.js
File metadata and controls
83 lines (71 loc) · 2.54 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
const fs = require('fs');
const path = require('path');
const PbxFile = require('xcode/lib/pbxFile');
const {
IOSConfig,
withDangerousMod,
withXcodeProject,
} = require('expo/config-plugins');
const DEFAULT_ASSETS_DIR = './assets/kittentts';
const DEFAULT_ASSET_ROOT = 'kittentts';
module.exports = function withKittenTTS(config, props = {}) {
const assetsDir = props.assetsDir || DEFAULT_ASSETS_DIR;
const assetRoot = props.assetRoot || DEFAULT_ASSET_ROOT;
config = withDangerousMod(config, [
'android',
async (nextConfig) => {
const source = path.resolve(nextConfig.modRequest.projectRoot, assetsDir);
const destination = path.join(
nextConfig.modRequest.platformProjectRoot,
'app',
'src',
'main',
'assets',
assetRoot,
);
copyDirectory(source, destination);
return nextConfig;
},
]);
config = withXcodeProject(config, async (nextConfig) => {
const source = path.resolve(nextConfig.modRequest.projectRoot, assetsDir);
const iosSourceRoot = IOSConfig.Paths.getSourceRoot(nextConfig.modRequest.projectRoot);
const destination = path.join(iosSourceRoot, assetRoot);
copyDirectory(source, destination);
addFolderReferenceToResources(
nextConfig.modResults,
path.relative(nextConfig.modRequest.platformProjectRoot, destination),
);
return nextConfig;
});
return config;
};
function copyDirectory(source, destination) {
if (!fs.existsSync(source)) return;
fs.rmSync(destination, { recursive: true, force: true });
fs.mkdirSync(destination, { recursive: true });
fs.cpSync(source, destination, { recursive: true });
}
function addFolderReferenceToResources(project, folderPath) {
IOSConfig.XcodeUtils.ensureGroupRecursively(project, 'Resources');
const resourcesGroup = project.pbxGroupByName('Resources');
const basename = path.basename(folderPath);
const existingChild = resourcesGroup?.children?.find((child) => child.comment === basename);
if (existingChild) return;
const target = project.getTarget('com.apple.product-type.application');
const file = new PbxFile(folderPath, {
lastKnownFileType: 'folder',
defaultEncoding: undefined,
});
file.target = target?.uuid;
file.uuid = project.generateUuid();
file.fileRef = project.generateUuid();
file.fileEncoding = undefined;
project.addToPbxFileReferenceSection(file);
project.addToPbxBuildFileSection(file);
project.addToPbxResourcesBuildPhase(file);
resourcesGroup.children.push({
value: file.fileRef,
comment: file.basename,
});
}