|
| 1 | +import { |
| 2 | + CLIError, |
| 3 | + getLoader, |
| 4 | + logger, |
| 5 | + prompt, |
| 6 | +} from '@react-native-community/cli-tools'; |
| 7 | +import {Config} from '@react-native-community/cli-types'; |
| 8 | +import {join} from 'path'; |
| 9 | +import {readFileSync} from 'fs'; |
| 10 | +import chalk from 'chalk'; |
| 11 | +import {install, PackageManager} from './../../tools/packageManager'; |
| 12 | +import npmFetch from 'npm-registry-fetch'; |
| 13 | +import semver from 'semver'; |
| 14 | +import {checkGitInstallation, isGitTreeDirty} from '../init/git'; |
| 15 | +import { |
| 16 | + changePlaceholderInTemplate, |
| 17 | + getTemplateName, |
| 18 | +} from '../init/editTemplate'; |
| 19 | +import { |
| 20 | + copyTemplate, |
| 21 | + executePostInitScript, |
| 22 | + getTemplateConfig, |
| 23 | + installTemplatePackage, |
| 24 | +} from '../init/template'; |
| 25 | +import {tmpdir} from 'os'; |
| 26 | +import {mkdtempSync} from 'graceful-fs'; |
| 27 | + |
| 28 | +type Options = { |
| 29 | + packageName: string; |
| 30 | + version: string; |
| 31 | + pm: PackageManager; |
| 32 | + title: string; |
| 33 | +}; |
| 34 | + |
| 35 | +const NPM_REGISTRY_URL = 'http://registry.npmjs.org'; // TODO: Support local registry |
| 36 | + |
| 37 | +const getAppName = async (root: string) => { |
| 38 | + logger.log(`Reading ${chalk.cyan('name')} from package.json…`); |
| 39 | + const pkgJsonPath = join(root, 'package.json'); |
| 40 | + |
| 41 | + if (!pkgJsonPath) { |
| 42 | + throw new CLIError(`Unable to find package.json inside ${root}`); |
| 43 | + } |
| 44 | + |
| 45 | + let {name} = JSON.parse(readFileSync(pkgJsonPath, 'utf8')); |
| 46 | + |
| 47 | + if (!name) { |
| 48 | + const appJson = join(root, 'app.json'); |
| 49 | + if (appJson) { |
| 50 | + logger.log(`Reading ${chalk.cyan('name')} from app.json…`); |
| 51 | + name = JSON.parse(readFileSync(appJson, 'utf8')).name; |
| 52 | + } |
| 53 | + |
| 54 | + if (!name) { |
| 55 | + throw new CLIError('Please specify name in package.json or app.json.'); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return name; |
| 60 | +}; |
| 61 | + |
| 62 | +const getPackageMatchingVersion = async ( |
| 63 | + packageName: string, |
| 64 | + version: string, |
| 65 | +) => { |
| 66 | + const npmResponse = await npmFetch.json(packageName, { |
| 67 | + registry: NPM_REGISTRY_URL, |
| 68 | + }); |
| 69 | + |
| 70 | + if ('dist-tags' in npmResponse) { |
| 71 | + const distTags = npmResponse['dist-tags'] as Record<string, string>; |
| 72 | + if (version in distTags) { |
| 73 | + return distTags[version]; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if ('versions' in npmResponse) { |
| 78 | + const versions = Object.keys( |
| 79 | + npmResponse.versions as Record<string, unknown>, |
| 80 | + ); |
| 81 | + if (versions.length > 0) { |
| 82 | + const candidates = versions |
| 83 | + .filter((v) => semver.satisfies(v, version)) |
| 84 | + .sort(semver.rcompare); |
| 85 | + |
| 86 | + if (candidates.length > 0) { |
| 87 | + return candidates[0]; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + throw new Error( |
| 93 | + `Cannot find matching version of ${packageName} to react-native${version}, please provide version manually with --version flag.`, |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +async function addPlatform( |
| 98 | + [packageName]: string[], |
| 99 | + {root, reactNativeVersion}: Config, |
| 100 | + {version, pm, title}: Options, |
| 101 | +) { |
| 102 | + if (!packageName) { |
| 103 | + throw new CLIError('Please provide package name e.g. react-native-macos'); |
| 104 | + } |
| 105 | + |
| 106 | + const isGitAvailable = await checkGitInstallation(); |
| 107 | + |
| 108 | + if (isGitAvailable) { |
| 109 | + const dirty = await isGitTreeDirty(root); |
| 110 | + |
| 111 | + if (dirty) { |
| 112 | + logger.warn( |
| 113 | + 'Your git tree is dirty. We recommend committing or stashing changes first.', |
| 114 | + ); |
| 115 | + const {proceed} = await prompt({ |
| 116 | + type: 'confirm', |
| 117 | + name: 'proceed', |
| 118 | + message: 'Would you like to proceed?', |
| 119 | + }); |
| 120 | + |
| 121 | + if (!proceed) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + logger.info('Proceeding with the installation'); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + const projectName = await getAppName(root); |
| 130 | + |
| 131 | + const matchingVersion = await getPackageMatchingVersion( |
| 132 | + packageName, |
| 133 | + version ?? reactNativeVersion, |
| 134 | + ); |
| 135 | + |
| 136 | + logger.log( |
| 137 | + `Found matching version ${chalk.cyan(matchingVersion)} for ${chalk.cyan( |
| 138 | + packageName, |
| 139 | + )}`, |
| 140 | + ); |
| 141 | + |
| 142 | + const loader = getLoader({ |
| 143 | + text: `Installing ${packageName}@${matchingVersion}`, |
| 144 | + }); |
| 145 | + |
| 146 | + loader.start(); |
| 147 | + |
| 148 | + try { |
| 149 | + await install([`${packageName}@${matchingVersion}`], { |
| 150 | + packageManager: pm, |
| 151 | + silent: true, |
| 152 | + root, |
| 153 | + }); |
| 154 | + loader.succeed(); |
| 155 | + } catch (e) { |
| 156 | + loader.fail(); |
| 157 | + throw e; |
| 158 | + } |
| 159 | + |
| 160 | + loader.start('Copying template files'); |
| 161 | + const templateSourceDir = mkdtempSync(join(tmpdir(), 'rncli-init-template-')); |
| 162 | + await installTemplatePackage( |
| 163 | + `${packageName}@${matchingVersion}`, |
| 164 | + templateSourceDir, |
| 165 | + pm, |
| 166 | + ); |
| 167 | + |
| 168 | + const templateName = getTemplateName(templateSourceDir); |
| 169 | + const templateConfig = getTemplateConfig(templateName, templateSourceDir); |
| 170 | + |
| 171 | + if (!templateConfig.platformName) { |
| 172 | + throw new CLIError( |
| 173 | + `Template ${templateName} is missing platformName in its template.config.js`, |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + await copyTemplate( |
| 178 | + templateName, |
| 179 | + templateConfig.templateDir, |
| 180 | + templateSourceDir, |
| 181 | + templateConfig.platformName, |
| 182 | + ); |
| 183 | + |
| 184 | + loader.succeed(); |
| 185 | + loader.start('Processing template'); |
| 186 | + |
| 187 | + await changePlaceholderInTemplate({ |
| 188 | + projectName, |
| 189 | + projectTitle: title, |
| 190 | + placeholderName: templateConfig.placeholderName, |
| 191 | + placeholderTitle: templateConfig.titlePlaceholder, |
| 192 | + projectPath: join(root, templateConfig.platformName), |
| 193 | + }); |
| 194 | + |
| 195 | + loader.succeed(); |
| 196 | + |
| 197 | + const {postInitScript} = templateConfig; |
| 198 | + if (postInitScript) { |
| 199 | + logger.debug('Executing post init script '); |
| 200 | + await executePostInitScript( |
| 201 | + templateName, |
| 202 | + postInitScript, |
| 203 | + templateSourceDir, |
| 204 | + ); |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +export default addPlatform; |
0 commit comments