-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathlogConfig.ts
More file actions
85 lines (77 loc) · 2.35 KB
/
logConfig.ts
File metadata and controls
85 lines (77 loc) · 2.35 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
import type {
Config,
DependencyConfig,
} from '@react-native-community/cli-types';
import type { ConfigOutput, PluginApi } from '@rnef/config';
function isValidRNDependency(config: DependencyConfig) {
return (
Object.keys(config.platforms).filter((key) =>
Boolean(config.platforms[key])
).length !== 0
);
}
function filterConfig(config: Config) {
const filtered = { ...config };
// `react-native` is not a dependency. When loading it through community CLI it's not an issue,
// but in our case we don't install `@react-native-community/cli-platform-*` as a dependencies
// so the config.platforms key is empty, which makes autolinking treat it as a dependency.
delete filtered.dependencies['react-native'];
delete filtered.dependencies['react-native-tvos'];
const dependencies: Record<string, DependencyConfig> = {};
Object.keys(filtered.dependencies).forEach((item) => {
if (isValidRNDependency(filtered.dependencies[item])) {
dependencies[item] = filtered.dependencies[item];
}
});
return {
...filtered,
dependencies,
};
}
export const logConfig = async (
args: { platform?: string },
ownConfig: {
platforms: ConfigOutput['platforms'];
root: ConfigOutput['root'];
}
) => {
const { loadConfigAsync } = await import(
'@react-native-community/cli-config'
);
const config = await loadConfigAsync({
projectRoot: ownConfig.root,
selectedPlatform: args.platform,
});
const platforms =
ownConfig.platforms && args.platform
? { [args.platform]: ownConfig.platforms[args.platform] }
: ownConfig.platforms;
for (const platform in platforms) {
config.project[platform] = platforms[platform].autolinkingConfig.project;
}
console.log(JSON.stringify(filterConfig(config), null, 2));
};
export const logConfigPlugin =
(ownConfig: {
platforms: ConfigOutput['platforms'];
root: ConfigOutput['root'];
}) =>
(api: PluginApi) => {
api.registerCommand({
name: 'config',
description: 'Output autolinking config',
action: async (args) => {
await logConfig(args, ownConfig);
},
options: [
{
name: '-p, --platform <string>',
description: 'Select platform, e.g. ios or android',
},
],
});
return {
name: 'internal_config',
description: 'Configuration plugin',
};
};