Skip to content

Commit 5ec5ea5

Browse files
committed
wip
1 parent 9f5b0f1 commit 5ec5ea5

File tree

6 files changed

+101
-28
lines changed

6 files changed

+101
-28
lines changed

packages/config/src/lib/config.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ export type PluginOutput = {
1111
description: string;
1212
};
1313

14+
type StartDevServerFunction = (options: {
15+
root: string;
16+
// TODO fix type
17+
args: any;
18+
reactNativeVersion: string;
19+
reactNativePath: string;
20+
platforms: Record<string, object>;
21+
}) => Promise<void>;
22+
23+
export type BundlerPluginOutput = {
24+
name: string;
25+
description: string;
26+
start: StartDevServerFunction;
27+
};
28+
1429
export type PlatformOutput = PluginOutput & {
1530
autolinkingConfig: { project: Record<string, unknown> | undefined };
1631
};
@@ -26,12 +41,13 @@ export type PluginApi = {
2641
extraSources: string[];
2742
ignorePaths: string[];
2843
};
44+
getBundlerStart: () => ({ args: any }) => Promise<void>;
2945
};
3046

3147
type SupportedRemoteCacheProviders = 'github-actions';
3248

3349
type PluginType = (args: PluginApi) => PluginOutput;
34-
50+
type BundlerPluginType = (args: PluginApi) => BundlerPluginOutput;
3551
type PlatformType = (args: PluginApi) => PlatformOutput;
3652

3753
type ArgValue = string | string[] | number | boolean;
@@ -64,7 +80,7 @@ export type ConfigType = {
6480
root?: string;
6581
reactNativeVersion?: string;
6682
reactNativePath?: string;
67-
bundler?: PluginType;
83+
bundler?: BundlerPluginType;
6884
plugins?: PluginType[];
6985
platforms?: Record<string, PlatformType>;
7086
commands?: Array<CommandType>;
@@ -165,11 +181,22 @@ export async function getConfig(dir: string): Promise<ConfigOutput> {
165181
extraSources: string[];
166182
ignorePaths: string[];
167183
},
184+
getBundlerStart:
185+
() =>
186+
({ args: any }) =>
187+
bundler?.start({
188+
root: api.getProjectRoot(),
189+
args,
190+
reactNativeVersion: api.getReactNativeVersion(),
191+
reactNativePath: api.getReactNativePath(),
192+
platforms: api.getPlatforms(),
193+
}),
168194
};
169195

170196
if (validatedConfig.plugins) {
171197
// plugins register commands
172198
for (const plugin of validatedConfig.plugins) {
199+
// @ts-expect-error tbd
173200
assignOriginToCommand(plugin, api, validatedConfig);
174201
}
175202
}
@@ -178,15 +205,24 @@ export async function getConfig(dir: string): Promise<ConfigOutput> {
178205
if (validatedConfig.platforms) {
179206
// platforms register commands and custom platform functionality (TBD)
180207
for (const platform in validatedConfig.platforms) {
208+
// @ts-expect-error tbd
181209
const platformOutput = validatedConfig.platforms[platform](api);
182210
platforms[platform] = platformOutput;
183211
}
184212
}
185213

214+
let bundler: BundlerPluginOutput | undefined;
186215
if (validatedConfig.bundler) {
187-
assignOriginToCommand(validatedConfig.bundler, api, validatedConfig);
216+
// @ts-expect-error tbd
217+
bundler = assignOriginToCommand(
218+
validatedConfig.bundler,
219+
// @ts-expect-error tbd
220+
api,
221+
validatedConfig
222+
);
188223
}
189224

225+
// @ts-expect-error tbd
190226
const outputConfig: ConfigOutput = {
191227
root: projectRoot,
192228
commands: validatedConfig.commands ?? [],
@@ -225,16 +261,17 @@ function resolveReactNativePath(root: string) {
225261
* Assigns __origin property to each command in the config for later use in error handling.
226262
*/
227263
function assignOriginToCommand(
228-
plugin: PluginType,
264+
plugin: PluginType | BundlerPluginType,
229265
api: PluginApi,
230266
config: ConfigType
231267
) {
232268
const len = config.commands?.length ?? 0;
233-
const { name } = plugin(api);
269+
const { name, ...rest } = plugin(api);
234270
const newlen = config.commands?.length ?? 0;
235271
for (let i = len; i < newlen; i++) {
236272
if (config.commands?.[i]) {
237273
config.commands[i].__origin = name;
238274
}
239275
}
276+
return { name, ...rest };
240277
}

packages/platform-android/src/lib/commands/runAndroid/command.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export function registerRunCommand(
1818
args as Flags,
1919
projectRoot,
2020
api.getRemoteCacheProvider(),
21-
api.getFingerprintOptions()
21+
api.getFingerprintOptions(),
22+
api.getBundlerStart()
2223
);
2324
},
2425
options: runOptions,

packages/platform-android/src/lib/commands/runAndroid/runAndroid.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ export async function runAndroid(
4747
args: Flags,
4848
projectRoot: string,
4949
remoteCacheProvider: SupportedRemoteCacheProviders | undefined,
50-
fingerprintOptions: { extraSources: string[]; ignorePaths: string[] }
50+
fingerprintOptions: { extraSources: string[]; ignorePaths: string[] },
51+
startDevServer: (options: {
52+
root: string;
53+
// TODO fix type
54+
args: any;
55+
reactNativeVersion: string;
56+
reactNativePath: string;
57+
platforms: Record<string, object>;
58+
}) => Promise<void>
5159
) {
5260
intro('Running Android app');
5361

@@ -97,6 +105,14 @@ export async function runAndroid(
97105
await tryLaunchEmulator();
98106
}
99107
}
108+
console.log('xd');
109+
startDevServer({
110+
root: projectRoot,
111+
reactNativePath: './node_modules/react-native',
112+
reactNativeVersion: '0.79',
113+
platforms: { ios: {}, android: {} },
114+
args,
115+
});
100116

101117
await runGradle({ tasks, androidProject, args });
102118

packages/plugin-metro/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './lib/pluginMetro.js';
2+
export { startDevServer } from './lib/start/command.js';

packages/plugin-metro/src/lib/pluginMetro.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import type { PluginApi, PluginOutput } from '@rnef/config';
1+
import type { BundlerPluginOutput, PluginApi } from '@rnef/config';
22
import { registerBundleCommand } from './bundle/command.js';
3-
import { registerStartCommand } from './start/command.js';
3+
import { registerStartCommand, startDevServer } from './start/command.js';
44

55
export const pluginMetro =
66
() =>
7-
(api: PluginApi): PluginOutput => {
7+
(api: PluginApi): BundlerPluginOutput => {
88
registerStartCommand(api);
99
registerBundleCommand(api);
1010

1111
return {
1212
name: '@rnef/plugin-metro',
1313
description: 'RNEF plugin for Metro bundler.',
14+
start: startDevServer,
1415
};
1516
};
1617

packages/plugin-metro/src/lib/start/command.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,50 @@ import { findDevServerPort, intro } from '@rnef/tools';
1111
import type { StartCommandArgs } from './runServer.js';
1212
import runServer from './runServer.js';
1313

14+
export async function startDevServer({
15+
root,
16+
args,
17+
reactNativeVersion,
18+
reactNativePath,
19+
platforms,
20+
}: {
21+
root: string;
22+
args: StartCommandArgs;
23+
reactNativeVersion: string;
24+
reactNativePath: string;
25+
platforms: Record<string, object>;
26+
}) {
27+
const { port, startDevServer } = await findDevServerPort(
28+
args.port ?? 8081,
29+
root
30+
);
31+
32+
if (!startDevServer) {
33+
return;
34+
}
35+
36+
return runServer(
37+
{ root, reactNativeVersion, reactNativePath, platforms },
38+
{ ...args, port }
39+
);
40+
}
41+
1442
export const registerStartCommand = (api: PluginApi) => {
1543
api.registerCommand({
1644
name: 'start',
1745
action: async (args: StartCommandArgs) => {
1846
intro('Starting Metro dev server');
1947
const root = api.getProjectRoot();
20-
const { port, startDevServer } = await findDevServerPort(
21-
args.port ?? 8081,
22-
root
23-
);
24-
25-
if (!startDevServer) {
26-
return;
27-
}
28-
2948
const reactNativeVersion = api.getReactNativeVersion();
3049
const reactNativePath = api.getReactNativePath();
3150
const platforms = api.getPlatforms();
32-
return runServer(
33-
{
34-
root,
35-
reactNativeVersion,
36-
reactNativePath,
37-
platforms,
38-
},
39-
{ ...args, port }
40-
);
51+
await startDevServer({
52+
root,
53+
args,
54+
reactNativeVersion,
55+
reactNativePath,
56+
platforms,
57+
});
4158
},
4259
description: 'Start the Metro development server.',
4360
options: [

0 commit comments

Comments
 (0)