-
Notifications
You must be signed in to change notification settings - Fork 918
feat: add add-platform
command
#2298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
szymonrybczak
wants to merge
7
commits into
main
Choose a base branch
from
feat/add-platform
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e1c0066
feat: add `add-platform` command
szymonrybczak bbc9140
feat: support multiple platforms
szymonrybczak 428d3bd
feat: make sure platform isn't already initalized
szymonrybczak 4827e7f
chore: add try-catch blocks
szymonrybczak 7fbb12d
fix: map package name to template name
szymonrybczak 609aaa4
fix: do not hardcode npm registry
szymonrybczak 26ec2e1
fix: install the same version of template as package
szymonrybczak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
import { | ||
CLIError, | ||
getLoader, | ||
logger, | ||
prompt, | ||
} from '@react-native-community/cli-tools'; | ||
import {Config} from '@react-native-community/cli-types'; | ||
import {join} from 'path'; | ||
import {readFileSync} from 'fs'; | ||
import chalk from 'chalk'; | ||
import {install, PackageManager} from './../../tools/packageManager'; | ||
import npmFetch from 'npm-registry-fetch'; | ||
import semver from 'semver'; | ||
import {checkGitInstallation, isGitTreeDirty} from '../init/git'; | ||
import {changePlaceholderInTemplate} from '../init/editTemplate'; | ||
import { | ||
copyTemplate, | ||
executePostInitScript, | ||
getTemplateConfig, | ||
installTemplatePackage, | ||
} from '../init/template'; | ||
import {tmpdir} from 'os'; | ||
import {mkdtempSync} from 'graceful-fs'; | ||
import {existsSync} from 'fs'; | ||
import {getNpmRegistryUrl} from '../../tools/npm'; | ||
|
||
type Options = { | ||
packageName: string; | ||
version: string; | ||
pm: PackageManager; | ||
title: string; | ||
}; | ||
|
||
const NPM_REGISTRY_URL = getNpmRegistryUrl(); | ||
|
||
const getAppName = async (root: string) => { | ||
logger.log(`Reading ${chalk.cyan('name')} from package.json…`); | ||
const pkgJsonPath = join(root, 'package.json'); | ||
|
||
if (!pkgJsonPath) { | ||
throw new CLIError(`Unable to find package.json inside ${root}`); | ||
} | ||
|
||
let name; | ||
|
||
try { | ||
name = JSON.parse(readFileSync(pkgJsonPath, 'utf8')).name; | ||
} catch (e) { | ||
throw new CLIError(`Failed to read ${pkgJsonPath} file.`, e as Error); | ||
} | ||
|
||
if (!name) { | ||
const appJson = join(root, 'app.json'); | ||
if (appJson) { | ||
logger.log(`Reading ${chalk.cyan('name')} from app.json…`); | ||
try { | ||
name = JSON.parse(readFileSync(appJson, 'utf8')).name; | ||
} catch (e) { | ||
throw new CLIError(`Failed to read ${pkgJsonPath} file.`, e as Error); | ||
} | ||
} | ||
|
||
if (!name) { | ||
throw new CLIError('Please specify name in package.json or app.json.'); | ||
} | ||
} | ||
|
||
return name; | ||
}; | ||
|
||
const getPackageMatchingVersion = async ( | ||
packageName: string, | ||
version: string, | ||
) => { | ||
const npmResponse = await npmFetch.json(packageName, { | ||
registry: NPM_REGISTRY_URL, | ||
szymonrybczak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
if ('dist-tags' in npmResponse) { | ||
const distTags = npmResponse['dist-tags'] as Record<string, string>; | ||
if (version in distTags) { | ||
return distTags[version]; | ||
} | ||
} | ||
|
||
if ('versions' in npmResponse) { | ||
const versions = Object.keys( | ||
npmResponse.versions as Record<string, unknown>, | ||
); | ||
if (versions.length > 0) { | ||
const candidates = versions | ||
.filter((v) => semver.satisfies(v, version)) | ||
.sort(semver.rcompare); | ||
|
||
if (candidates.length > 0) { | ||
return candidates[0]; | ||
} | ||
} | ||
} | ||
|
||
throw new Error( | ||
`Cannot find matching version of ${packageName} to react-native${version}, please provide version manually with --version flag.`, | ||
); | ||
}; | ||
|
||
// From React Native 0.75 template is not longer inside `react-native` core, | ||
// so we need to map package name (fork) to template name | ||
|
||
const getTemplateNameFromPackageName = (packageName: string) => { | ||
switch (packageName) { | ||
case '@callstack/react-native-visionos': | ||
case 'react-native-visionos': | ||
return '@callstack/visionos-template'; | ||
default: | ||
return packageName; | ||
} | ||
}; | ||
|
||
async function addPlatform( | ||
[packageName]: string[], | ||
{root, reactNativeVersion}: Config, | ||
{version, pm, title}: Options, | ||
) { | ||
if (!packageName) { | ||
throw new CLIError('Please provide package name e.g. react-native-macos'); | ||
} | ||
|
||
const templateName = getTemplateNameFromPackageName(packageName); | ||
const isGitAvailable = await checkGitInstallation(); | ||
|
||
if (isGitAvailable) { | ||
const dirty = await isGitTreeDirty(root); | ||
|
||
if (dirty) { | ||
logger.warn( | ||
'Your git tree is dirty. We recommend committing or stashing changes first.', | ||
); | ||
const {proceed} = await prompt({ | ||
type: 'confirm', | ||
name: 'proceed', | ||
message: 'Would you like to proceed?', | ||
}); | ||
|
||
if (!proceed) { | ||
return; | ||
} | ||
|
||
logger.info('Proceeding with the installation'); | ||
} | ||
} | ||
|
||
const projectName = await getAppName(root); | ||
|
||
const matchingVersion = await getPackageMatchingVersion( | ||
packageName, | ||
version ?? reactNativeVersion, | ||
); | ||
|
||
logger.log( | ||
`Found matching version ${chalk.cyan(matchingVersion)} for ${chalk.cyan( | ||
packageName, | ||
)}`, | ||
); | ||
|
||
const loader = getLoader({ | ||
text: `Installing ${packageName}@${matchingVersion}`, | ||
}); | ||
|
||
loader.start(); | ||
|
||
try { | ||
await install([`${packageName}@${matchingVersion}`], { | ||
packageManager: pm, | ||
silent: true, | ||
root, | ||
}); | ||
loader.succeed(); | ||
} catch (error) { | ||
loader.fail(); | ||
throw new CLIError( | ||
`Failed to install package ${packageName}@${matchingVersion}`, | ||
(error as Error).message, | ||
); | ||
} | ||
|
||
loader.start( | ||
`Installing template packages from ${templateName}@0${matchingVersion}`, | ||
); | ||
|
||
const templateSourceDir = mkdtempSync(join(tmpdir(), 'rncli-init-template-')); | ||
|
||
try { | ||
await installTemplatePackage( | ||
`${templateName}@0${matchingVersion}`, | ||
templateSourceDir, | ||
pm, | ||
); | ||
loader.succeed(); | ||
} catch (error) { | ||
loader.fail(); | ||
throw new CLIError( | ||
`Failed to install template packages from ${templateName}@0${matchingVersion}`, | ||
(error as Error).message, | ||
); | ||
} | ||
|
||
loader.start('Copying template files'); | ||
|
||
const templateConfig = getTemplateConfig(templateName, templateSourceDir); | ||
|
||
if (!templateConfig.platforms) { | ||
throw new CLIError( | ||
`Template ${templateName} is missing "platforms" in its "template.config.js"`, | ||
); | ||
} | ||
|
||
for (const platform of templateConfig.platforms) { | ||
if (existsSync(join(root, platform))) { | ||
loader.fail(); | ||
throw new CLIError( | ||
`Platform ${platform} already exists in the project. Directory ${join( | ||
root, | ||
platform, | ||
)} is not empty.`, | ||
); | ||
} | ||
|
||
await copyTemplate( | ||
templateName, | ||
templateConfig.templateDir, | ||
templateSourceDir, | ||
platform, | ||
); | ||
} | ||
|
||
loader.succeed(); | ||
loader.start('Processing template'); | ||
|
||
for (const platform of templateConfig.platforms) { | ||
await changePlaceholderInTemplate({ | ||
projectName, | ||
projectTitle: title, | ||
placeholderName: templateConfig.placeholderName, | ||
placeholderTitle: templateConfig.titlePlaceholder, | ||
projectPath: join(root, platform), | ||
}); | ||
} | ||
|
||
loader.succeed(); | ||
|
||
const {postInitScript} = templateConfig; | ||
if (postInitScript) { | ||
logger.debug('Executing post init script '); | ||
await executePostInitScript( | ||
templateName, | ||
postInitScript, | ||
templateSourceDir, | ||
); | ||
} | ||
} | ||
|
||
export default addPlatform; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import addPlatform from './addPlatform'; | ||
|
||
export default { | ||
func: addPlatform, | ||
name: 'add-platform [packageName]', | ||
description: 'Add new platform to your React Native project.', | ||
options: [ | ||
{ | ||
name: '--version <string>', | ||
description: 'Pass version of the platform to be added to the project.', | ||
}, | ||
{ | ||
name: '--pm <string>', | ||
description: | ||
'Use specific package manager to initialize the project. Available options: `yarn`, `npm`, `bun`. Default: `yarn`', | ||
default: 'yarn', | ||
}, | ||
{ | ||
name: '--title <string>', | ||
description: 'Uses a custom app title name for application', | ||
}, | ||
], | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.