Skip to content

Commit 09fb1da

Browse files
feat: add config command (#19)
* chore: upgrade template to 0.76 * feat: add `config` command * feat: add linking ios platform with `@react-native-community/cli-config-apple` * fix: move `@react-native-community/cli-config-apple` under plugin package * fix: remove `react-native.config.cjs` * feat: bump `@react-native-community/cli` version to `15.1.0` * fix test --------- Co-authored-by: Michał Pierzchała <[email protected]>
1 parent b3e6ff5 commit 09fb1da

File tree

12 files changed

+315
-20
lines changed

12 files changed

+315
-20
lines changed

packages/cli/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
},
1010
"dependencies": {
1111
"@callstack/rnef-config": "workspace:*",
12+
"@react-native-community/cli-config": "^15.1.0",
1213
"commander": "^12.1.0",
1314
"tslib": "^2.3.0"
15+
},
16+
"devDependencies": {
17+
"@react-native-community/cli-types": "^15.1.0"
1418
}
1519
}

packages/cli/src/config.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { loadConfigAsync } from '@react-native-community/cli-config';
2+
import { Config, DependencyConfig } from '@react-native-community/cli-types';
3+
4+
function isValidRNDependency(config: DependencyConfig) {
5+
return (
6+
Object.keys(config.platforms).filter((key) =>
7+
Boolean(config.platforms[key])
8+
).length !== 0
9+
);
10+
}
11+
12+
function filterConfig(config: Config) {
13+
const filtered = { ...config };
14+
Object.keys(filtered.dependencies).forEach((item) => {
15+
if (!isValidRNDependency(filtered.dependencies[item])) {
16+
delete filtered.dependencies[item];
17+
}
18+
});
19+
return filtered;
20+
}
21+
22+
export const logConfig = async (options: { platform?: string }) => {
23+
const config = await loadConfigAsync({
24+
selectedPlatform: options.platform,
25+
});
26+
27+
console.log(JSON.stringify(filterConfig(config), null, 2));
28+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export default {
22
plugins: {},
3+
commands: [{ name: 'test', description: 'Test command', action: () => {} }],
34
};

packages/cli/src/lib/__tests__/cli.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ it('should not throw when config is there', async () => {
1313
const __dirname = dirname(fileURLToPath(import.meta.url));
1414
await cli({
1515
cwd: join(__dirname, '__fixtures__/simple-config'),
16+
argv: ['node', 'rnef', 'test'],
1617
});
1718
});

packages/cli/src/lib/cli.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Command } from 'commander';
22
import { getConfig } from '@callstack/rnef-config';
33
import { createRequire } from 'module';
4+
import { logConfig } from '../config.js';
5+
46
const require = createRequire(import.meta.url);
57

68
const { version } = require('./../../package.json');
@@ -14,16 +16,23 @@ program
1416

1517
type CliOptions = {
1618
cwd?: string;
19+
argv?: string[];
1720
};
1821

19-
export const cli = async ({ cwd }: CliOptions = {}) => {
22+
export const cli = async ({ cwd, argv }: CliOptions = {}) => {
2023
const config = await getConfig(cwd);
24+
25+
program
26+
.command('config')
27+
.option('-p, --platform <string>', 'Select platform, e.g. ios or android')
28+
.action(logConfig);
29+
2130
// Register commands from the config
2231
config.commands?.forEach((command) => {
2332
const cmd = program
2433
.command(command.name)
2534
.description(command.description || '')
26-
.action(async () => {
35+
.action(() => {
2736
try {
2837
command.action(program.args);
2938
} catch (e) {
@@ -38,5 +47,5 @@ export const cli = async ({ cwd }: CliOptions = {}) => {
3847
}
3948
});
4049

41-
program.parse();
50+
program.parse(argv);
4251
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pluginManagement { includeBuild("../node_modules/@react-native/gradle-plugin") }
22
plugins { id("com.facebook.react.settings") }
3-
extensions.configure(com.facebook.react.ReactSettingsExtension){ ex -> ex.autolinkLibrariesFromCommand() }
3+
extensions.configure(com.facebook.react.ReactSettingsExtension){ ex -> ex.autolinkLibrariesFromCommand('rnef config -p android') }
44
rootProject.name = 'HelloWorld'
55
include ':app'
66
includeBuild('../node_modules/@react-native/gradle-plugin')

packages/plugin-platform-ios/eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default [
1313
'{projectRoot}/eslint.config.{js,cjs,mjs}',
1414
'{projectRoot}/vite.config.{js,ts,mjs,mts}',
1515
],
16+
checkObsoleteDependencies: false,
1617
},
1718
],
1819
},

packages/plugin-platform-ios/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "./src/index.js",
66
"typings": "./src/index.d.ts",
77
"dependencies": {
8+
"@react-native-community/cli-config-apple": "^15.1.0",
89
"tslib": "^2.3.0"
910
},
1011
"devDependencies": {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let ios;
2+
try {
3+
ios = await import('@react-native-community/cli-config-apple');
4+
} catch {
5+
console.warn(
6+
"@react-native-community/cli-config-apple not found, we couldn't configure the iOS project"
7+
);
8+
}
9+
10+
export default {
11+
platforms: {
12+
ios: {
13+
projectConfig: ios?.getProjectConfig?.({ project: 'ios' }),
14+
dependencyConfig: ios?.getDependencyConfig?.({ project: 'ios' }),
15+
},
16+
},
17+
};

packages/plugin-platform-ios/src/template/ios/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if linkage != nil
1515
end
1616

1717
target 'HelloWorld' do
18-
config = use_native_modules!
18+
config = use_native_modules!('rnef config -p ios')
1919

2020
use_react_native!(
2121
:path => config[:reactNativePath],

0 commit comments

Comments
 (0)