Skip to content
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
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Libraries/Image/AssetSourceResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {PackagerAsset} from 'AssetRegistry';
const PixelRatio = require('PixelRatio');
const Platform = require('Platform');

const assetPathUtils = require('react-native-local-cli/bundle/assetPathUtils');
const assetPathUtils = require('./assetPathUtils');
const invariant = require('fbjs/lib/invariant');

/**
Expand Down
85 changes: 85 additions & 0 deletions Libraries/Image/assetPathUtils.js
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 {
Copy link
Contributor Author

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 to npm?

CC: @TheSavior

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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.

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,
};
1 change: 0 additions & 1 deletion jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const generate = require('@babel/generator').default;

const nodeFiles = new RegExp(
[
'/react-native-local-cli/', // cli
'/metro(?:-[^/]*)?/', // metro, metro-core, metro-source-map, metro-etc
].join('|'),
);
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"testPathIgnorePatterns": [
"Libraries/Renderer",
"/node_modules/",
"local-cli/templates/",
"RNTester/e2e"
],
"haste": {
Expand Down
27 changes: 0 additions & 27 deletions setupBabel.js

This file was deleted.

Loading