|
| 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 | +} |
0 commit comments