-
Notifications
You must be signed in to change notification settings - Fork 24.6k
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
Extract CLI out #22174
Closed
Closed
Extract CLI out #22174
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b84afe6
Extract CLI out and fix call-sites (some of them)
grabbou bf36177
Return CLI for global CLI
grabbou ecb6d98
Fix tests - transform new location of CLI
grabbou 47e0658
Move bin property to react-native-local-cli
grabbou 586f54f
Fix Jest tests - move assetPathUtils from CLI to here, RN should not …
grabbou 5373544
Bump dependency on React Native for review
grabbou af5e7cf
Bump CLI version for review
grabbou c200278
Bump CLI dependency to fix tests
grabbou 83ae4ba
Fix CLI version
grabbou 2287be4
Bump CLI dependency
grabbou 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 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 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,85 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {PackagerAsset} from './AssetRegistry'; | ||
|
||
/** | ||
* FIXME: using number to represent discrete scale numbers is fragile in essence because of | ||
* floating point numbers imprecision. | ||
*/ | ||
function getAndroidAssetSuffix(scale: number): string { | ||
switch (scale) { | ||
case 0.75: | ||
return 'ldpi'; | ||
case 1: | ||
return 'mdpi'; | ||
case 1.5: | ||
return 'hdpi'; | ||
case 2: | ||
return 'xhdpi'; | ||
case 3: | ||
return 'xxhdpi'; | ||
case 4: | ||
return 'xxxhdpi'; | ||
} | ||
throw new Error('no such scale'); | ||
} | ||
|
||
// See https://developer.android.com/guide/topics/resources/drawable-resource.html | ||
const drawableFileTypes = new Set([ | ||
'gif', | ||
'jpeg', | ||
'jpg', | ||
'png', | ||
'svg', | ||
'webp', | ||
'xml', | ||
]); | ||
|
||
function getAndroidResourceFolderName(asset: PackagerAsset, scale: number) { | ||
if (!drawableFileTypes.has(asset.type)) { | ||
return 'raw'; | ||
} | ||
var suffix = getAndroidAssetSuffix(scale); | ||
if (!suffix) { | ||
throw new Error( | ||
"Don't know which android drawable suffix to use for asset: " + | ||
JSON.stringify(asset), | ||
); | ||
} | ||
const androidFolder = 'drawable-' + suffix; | ||
return androidFolder; | ||
} | ||
|
||
function getAndroidResourceIdentifier(asset: PackagerAsset) { | ||
var folderPath = getBasePath(asset); | ||
return (folderPath + '/' + asset.name) | ||
.toLowerCase() | ||
.replace(/\//g, '_') // Encode folder structure in file name | ||
.replace(/([^a-z0-9_])/g, '') // Remove illegal chars | ||
.replace(/^assets_/, ''); // Remove "assets_" prefix | ||
} | ||
|
||
function getBasePath(asset: PackagerAsset) { | ||
var basePath = asset.httpServerLocation; | ||
if (basePath[0] === '/') { | ||
basePath = basePath.substr(1); | ||
} | ||
return basePath; | ||
} | ||
|
||
module.exports = { | ||
getAndroidAssetSuffix: getAndroidAssetSuffix, | ||
getAndroidResourceFolderName: getAndroidResourceFolderName, | ||
getAndroidResourceIdentifier: getAndroidResourceIdentifier, | ||
getBasePath: getBasePath, | ||
}; |
This file contains 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 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 was deleted.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is now duplicated between React Native and React Native CLI. Shall we consider making it a separate package, e.g.
react-native-cli-path-utils
and publish tonpm
?CC: @TheSavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that to keep RN and the CLI separated but having them both using these utils the separate package option is a valid solution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. For now, I think we can ship the copy and figure out this as a next PR. Problem with this one is that I need to update
local-cli
folder every time something changes which is very hard. Hopefully, we can get this reviewed ASAP.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this file is for more than just images, so it does need to be in RN, to be used by other asset types.