Skip to content

Commit 476b394

Browse files
committed
add rn nitro modules dep to local modules
1 parent 8ed72ef commit 476b394

File tree

5 files changed

+154
-66
lines changed

5 files changed

+154
-66
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ import { assertNpxExists, assertUserInput } from './utils/assert';
1919
import { createInitialGitCommit } from './utils/initialCommit';
2020
import { prompt } from './utils/prompt';
2121
import { resolveNpmPackageVersion } from './utils/resolveNpmPackageVersion';
22+
import {
23+
addNitroDependencyToLocalLibrary,
24+
linkLocalLibrary,
25+
promptLocalLibrary,
26+
} from './utils/local';
27+
import { determinePackageManager } from './utils/packageManager';
2228

2329
const FALLBACK_BOB_VERSION = '0.40.5';
2430
const FALLBACK_NITRO_MODULES_VERSION = '0.22.1';
@@ -143,7 +149,17 @@ async function create(_argv: yargs.Arguments<Args>) {
143149
spaces: 2,
144150
});
145151

146-
if (!local) {
152+
const packageManager = await determinePackageManager();
153+
154+
let addedNitro = false;
155+
let linkedLocalLibrary = false;
156+
if (local) {
157+
if (config.project.moduleConfig === 'nitro-modules') {
158+
addedNitro = await addNitroDependencyToLocalLibrary(config);
159+
}
160+
161+
linkedLocalLibrary = await linkLocalLibrary(config, folder, packageManager);
162+
} else {
147163
await createInitialGitCommit(folder);
148164
}
149165

@@ -153,33 +169,14 @@ async function create(_argv: yargs.Arguments<Args>) {
153169
)}!\n`
154170
);
155171

156-
await printNextSteps(local, folder, config);
157-
}
158-
159-
async function promptLocalLibrary(argv: Args) {
160-
let local = false;
161-
162-
if (typeof argv.local === 'boolean') {
163-
local = argv.local;
164-
} else {
165-
const hasPackageJson = await fs.pathExists(
166-
path.join(process.cwd(), 'package.json')
167-
);
168-
169-
if (hasPackageJson) {
170-
// If we're under a project with package.json, ask the user if they want to create a local library
171-
const answers = await prompt({
172-
type: 'confirm',
173-
name: 'local',
174-
message: `Looks like you're under a project folder. Do you want to create a local library?`,
175-
initial: true,
176-
});
177-
178-
local = answers.local;
179-
}
180-
}
181-
182-
return local;
172+
await printNextSteps({
173+
local,
174+
folder,
175+
config,
176+
packageManager,
177+
addedNitro,
178+
linkedLocalLibrary,
179+
});
183180
}
184181

185182
async function promptPath(argv: Args, local: boolean) {

packages/create-react-native-library/src/inform.ts

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,43 @@
11
import path from 'path';
2-
import fs from 'fs-extra';
32
import dedent from 'dedent';
43
import type { TemplateConfiguration } from './template';
54
import kleur from 'kleur';
65

7-
export async function printNextSteps(
8-
local: boolean,
9-
folder: string,
10-
config: TemplateConfiguration
11-
) {
6+
export async function printNextSteps({
7+
local,
8+
folder,
9+
config,
10+
linkedLocalLibrary,
11+
addedNitro,
12+
packageManager,
13+
}: {
14+
local: boolean;
15+
folder: string;
16+
config: TemplateConfiguration;
17+
linkedLocalLibrary: boolean;
18+
addedNitro: boolean;
19+
packageManager: string;
20+
}) {
1221
if (local) {
13-
let linked;
14-
15-
const packageManager = (await fs.pathExists(
16-
path.join(process.cwd(), 'yarn.lock')
17-
))
18-
? 'yarn'
19-
: 'npm';
20-
21-
const packageJsonPath = path.join(process.cwd(), 'package.json');
22-
23-
if (await fs.pathExists(packageJsonPath)) {
24-
const packageJson = await fs.readJSON(packageJsonPath);
25-
const isReactNativeProject = Boolean(
26-
packageJson.dependencies?.['react-native']
27-
);
28-
29-
if (isReactNativeProject) {
30-
packageJson.dependencies = packageJson.dependencies || {};
31-
packageJson.dependencies[config.project.slug] =
32-
packageManager === 'yarn'
33-
? `link:./${path.relative(process.cwd(), folder)}`
34-
: `file:./${path.relative(process.cwd(), folder)}`;
35-
36-
await fs.writeJSON(packageJsonPath, packageJson, {
37-
spaces: 2,
38-
});
39-
40-
linked = true;
41-
}
42-
}
43-
4422
console.log(
4523
dedent(`
4624
${kleur.magenta(
4725
`${kleur.bold('Get started')} with the project`
4826
)}${kleur.gray(':')}
4927
5028
${
51-
(linked
29+
(linkedLocalLibrary
5230
? `- Run ${kleur.blue(
5331
`${packageManager} install`
5432
)} to link the library\n`
5533
: `- Link the library at ${kleur.blue(
5634
path.relative(process.cwd(), folder)
5735
)} based on your project setup\n`) +
36+
(config.project.moduleConfig === 'nitro-modules' && !addedNitro
37+
? `- Run ${kleur.blue(
38+
`${packageManager} add react-native-nitro-modules`
39+
)} to install nitro modules \n`
40+
: '') +
5841
`- Run ${kleur.blue(
5942
'pod install --project-directory=ios'
6043
)} to install dependencies with CocoaPods\n` +

packages/create-react-native-library/src/template.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export type TemplateConfiguration = {
3939
email: string;
4040
url: string;
4141
};
42+
/** Git repo URL */
4243
repo: string;
4344
example: ExampleApp;
4445
year: number;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import fs from 'fs-extra';
2+
import path from 'path';
3+
import { prompt } from './prompt';
4+
import type { TemplateConfiguration } from '../template';
5+
import type { Args } from '../input';
6+
7+
type PackageJson = {
8+
dependencies?: Record<string, string>;
9+
};
10+
11+
export async function promptLocalLibrary(argv: Args): Promise<boolean> {
12+
if (typeof argv.local === 'boolean') {
13+
return argv.local;
14+
}
15+
16+
const hasPackageJson = findAppPackageJsonPath() !== null;
17+
if (!hasPackageJson) {
18+
return false;
19+
}
20+
21+
// If we're under a project with package.json, ask the user if they want to create a local library
22+
const answers = await prompt({
23+
type: 'confirm',
24+
name: 'local',
25+
message: `Looks like you're under a project folder. Do you want to create a local library?`,
26+
initial: true,
27+
});
28+
29+
return answers.local;
30+
}
31+
32+
/** @returns `true` if successfull */
33+
export async function addNitroDependencyToLocalLibrary(
34+
config: TemplateConfiguration
35+
): Promise<boolean> {
36+
if (config.versions.nitroModules === undefined) {
37+
return false;
38+
}
39+
40+
const appPackageJsonPath = await findAppPackageJsonPath();
41+
if (appPackageJsonPath === null) {
42+
return false;
43+
}
44+
45+
const appPackageJson: PackageJson = await fs.readJson(appPackageJsonPath);
46+
const dependencies = appPackageJson['dependencies'] ?? {};
47+
48+
dependencies['react-native-nitro-modules'] = config.versions.nitroModules;
49+
50+
appPackageJson['dependencies'] = dependencies;
51+
await fs.writeJson(appPackageJsonPath, appPackageJson, {
52+
spaces: 2,
53+
});
54+
55+
return true;
56+
}
57+
58+
/** @returns `true` if successfull */
59+
export async function linkLocalLibrary(
60+
config: TemplateConfiguration,
61+
folder: string,
62+
packageManager: string
63+
): Promise<boolean> {
64+
const appPackageJsonPath = await findAppPackageJsonPath();
65+
if (appPackageJsonPath === null) {
66+
return false;
67+
}
68+
69+
const appPackageJson: PackageJson = await fs.readJson(appPackageJsonPath);
70+
71+
const isReactNativeProject = Boolean(
72+
appPackageJson.dependencies?.['react-native']
73+
);
74+
75+
if (!isReactNativeProject) {
76+
return false;
77+
}
78+
79+
const dependencies = appPackageJson['dependencies'] ?? {};
80+
dependencies[config.project.slug] =
81+
packageManager === 'yarn'
82+
? `link:./${path.relative(process.cwd(), folder)}`
83+
: `file:./${path.relative(process.cwd(), folder)}`;
84+
85+
await fs.writeJSON(appPackageJsonPath, appPackageJson, {
86+
spaces: 2,
87+
});
88+
89+
return true;
90+
}
91+
92+
async function findAppPackageJsonPath(): Promise<string | null> {
93+
const cwdPackageJson = path.join(process.cwd(), 'package.json');
94+
if (!(await fs.pathExists(cwdPackageJson))) {
95+
return null;
96+
}
97+
98+
return cwdPackageJson;
99+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import fs from 'fs-extra';
2+
import path from 'path';
3+
4+
export async function determinePackageManager() {
5+
return (await fs.pathExists(path.join(process.cwd(), 'yarn.lock')))
6+
? 'yarn'
7+
: 'npm';
8+
}

0 commit comments

Comments
 (0)