-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathlocal.ts
More file actions
99 lines (79 loc) · 2.57 KB
/
local.ts
File metadata and controls
99 lines (79 loc) · 2.57 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import fs from 'fs-extra';
import path from 'path';
import { prompt } from './prompt';
import type { TemplateConfiguration } from '../template';
import type { Args } from '../input';
type PackageJson = {
dependencies?: Record<string, string>;
};
export async function promptLocalLibrary(argv: Args): Promise<boolean> {
if (typeof argv.local === 'boolean') {
return argv.local;
}
const hasPackageJson = findAppPackageJsonPath() !== null;
if (!hasPackageJson) {
return false;
}
// If we're under a project with package.json, ask the user if they want to create a local library
const answers = await prompt({
type: 'confirm',
name: 'local',
message: `Looks like you're under a project folder. Do you want to create a local library?`,
initial: true,
});
return answers.local;
}
/** @returns `true` if successfull */
export async function addNitroDependencyToLocalLibrary(
config: TemplateConfiguration
): Promise<boolean> {
if (config.versions.nitroModules === undefined) {
return false;
}
const appPackageJsonPath = await findAppPackageJsonPath();
if (appPackageJsonPath === null) {
return false;
}
const appPackageJson: PackageJson = await fs.readJson(appPackageJsonPath);
const dependencies = appPackageJson['dependencies'] ?? {};
dependencies['react-native-nitro-modules'] = config.versions.nitroModules;
appPackageJson['dependencies'] = dependencies;
await fs.writeJson(appPackageJsonPath, appPackageJson, {
spaces: 2,
});
return true;
}
/** @returns `true` if successfull */
export async function linkLocalLibrary(
config: TemplateConfiguration,
folder: string,
packageManager: string
): Promise<boolean> {
const appPackageJsonPath = await findAppPackageJsonPath();
if (appPackageJsonPath === null) {
return false;
}
const appPackageJson: PackageJson = await fs.readJson(appPackageJsonPath);
const isReactNativeProject = Boolean(
appPackageJson.dependencies?.['react-native']
);
if (!isReactNativeProject) {
return false;
}
const dependencies = appPackageJson['dependencies'] ?? {};
dependencies[config.project.slug] =
packageManager === 'yarn'
? `link:./${path.relative(process.cwd(), folder)}`
: `file:./${path.relative(process.cwd(), folder)}`;
await fs.writeJSON(appPackageJsonPath, appPackageJson, {
spaces: 2,
});
return true;
}
async function findAppPackageJsonPath(): Promise<string | null> {
const cwdPackageJson = path.join(process.cwd(), 'package.json');
if (!(await fs.pathExists(cwdPackageJson))) {
return null;
}
return cwdPackageJson;
}