mirror of
https://github.com/status-im/react-native.git
synced 2025-01-30 03:05:44 +00:00
1673c570f9
Summary: This commit removes special cases for linking iOS and Android platforms. A previous commit opened up link and other commands for other platforms to provide their own behaviors. It left special cases in tact for iOS and Android. This PR removes the special case. - Added jest tests related to the link command. - Ran the `link` and `unlink` commands for iOS and Android and confirmed no changes. https://github.com/facebook/react-native/pull/17745 <!-- Help reviewers and the release process by writing your own release notes **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [-{Component}-] [ INTERNAL ] [ ENHANCEMENT ] [ {File} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| [CATEGORY] [TYPE] [LOCATION] - MESSAGE EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> [CLI][FEATURE][local-cli/link/link.js] - Removes special cases for linking in iOS and Android. Closes https://github.com/facebook/react-native/pull/17961 Differential Revision: D6975951 Pulled By: hramos fbshipit-source-id: 8dd5da35619e2124ce4b3b18db8b694757792363
165 lines
5.4 KiB
JavaScript
165 lines
5.4 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
* run Flow. */
|
|
const log = require('npmlog');
|
|
const path = require('path');
|
|
const uniqBy = require('lodash').uniqBy;
|
|
const flatten = require('lodash').flatten;
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
* run Flow. */
|
|
const chalk = require('chalk');
|
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
* run Flow. */
|
|
const isEmpty = require('lodash').isEmpty;
|
|
const promiseWaterfall = require('./promiseWaterfall');
|
|
const getProjectDependencies = require('./getProjectDependencies');
|
|
const getDependencyConfig = require('./getDependencyConfig');
|
|
const pollParams = require('./pollParams');
|
|
const commandStub = require('./commandStub');
|
|
const promisify = require('./promisify');
|
|
const findReactNativeScripts = require('../util/findReactNativeScripts');
|
|
|
|
import type {RNConfig} from '../core';
|
|
|
|
log.heading = 'rnpm-link';
|
|
|
|
const dedupeAssets = (assets) => uniqBy(assets, asset => path.basename(asset));
|
|
|
|
const linkDependency = (platforms, project, dependency) => {
|
|
Object.keys(platforms || {})
|
|
.forEach(platform => {
|
|
if (!project[platform] || !dependency.config[platform]) {
|
|
return null;
|
|
}
|
|
|
|
const linkConfig = platforms[platform] && platforms[platform].linkConfig && platforms[platform].linkConfig();
|
|
if (!linkConfig || !linkConfig.isInstalled || !linkConfig.register) {
|
|
return null;
|
|
}
|
|
|
|
const isInstalled = linkConfig.isInstalled(project[platform], dependency.config[platform]);
|
|
|
|
if (isInstalled) {
|
|
log.info(chalk.grey(`Platform '${platform}' module ${dependency.name} is already linked`));
|
|
return null;
|
|
}
|
|
|
|
return pollParams(dependency.config.params).then(params => {
|
|
log.info(`Linking ${dependency.name} ${platform} dependency`);
|
|
|
|
linkConfig.register(
|
|
dependency.name,
|
|
dependency.config[platform],
|
|
params,
|
|
project[platform]
|
|
);
|
|
|
|
log.info(`Platform '${platform}' module ${dependency.name} has been successfully linked`);
|
|
});
|
|
});
|
|
};
|
|
|
|
const linkAssets = (platforms, project, assets) => {
|
|
if (isEmpty(assets)) {
|
|
return;
|
|
}
|
|
|
|
Object.keys(platforms || {})
|
|
.forEach(platform => {
|
|
const linkConfig = platforms[platform] && platforms[platform].linkConfig && platforms[platform].linkConfig();
|
|
if (!linkConfig || !linkConfig.copyAssets) {
|
|
return;
|
|
}
|
|
|
|
log.info(`Linking assets to ${platform} project`);
|
|
linkConfig.copyAssets(assets, project[platform]);
|
|
});
|
|
|
|
log.info('Assets have been successfully linked to your project');
|
|
};
|
|
|
|
/**
|
|
* Updates project and links all dependencies to it.
|
|
*
|
|
* @param args If optional argument [packageName] is provided,
|
|
* only that package is processed.
|
|
* @param config CLI config, see local-cli/core/index.js
|
|
*/
|
|
function link(args: Array<string>, config: RNConfig) {
|
|
let project;
|
|
let platforms;
|
|
try {
|
|
project = config.getProjectConfig();
|
|
platforms = config.getPlatformConfig();
|
|
} catch (err) {
|
|
log.error(
|
|
'ERRPACKAGEJSON',
|
|
'No package found. Are you sure this is a React Native project?'
|
|
);
|
|
return Promise.reject(err);
|
|
}
|
|
|
|
const hasProjectConfig = Object.keys(platforms).reduce((acc, key) => acc || key in project, false);
|
|
if (!hasProjectConfig && findReactNativeScripts()) {
|
|
throw new Error(
|
|
'`react-native link` can not be used in Create React Native App projects. ' +
|
|
'If you need to include a library that relies on custom native code, ' +
|
|
'you might have to eject first. ' +
|
|
'See https://github.com/react-community/create-react-native-app/blob/master/EJECTING.md ' +
|
|
'for more information.'
|
|
);
|
|
}
|
|
|
|
let packageName = args[0];
|
|
// Check if install package by specific version (eg. package@latest)
|
|
if (packageName !== undefined) {
|
|
packageName = packageName.split('@')[0];
|
|
}
|
|
|
|
const dependencies = getDependencyConfig(
|
|
config,
|
|
packageName ? [packageName] : getProjectDependencies()
|
|
);
|
|
|
|
const assets = dedupeAssets(dependencies.reduce(
|
|
(acc, dependency) => acc.concat(dependency.config.assets),
|
|
project.assets
|
|
));
|
|
|
|
const tasks = flatten(dependencies.map(dependency => [
|
|
() => promisify(dependency.config.commands.prelink || commandStub),
|
|
() => linkDependency(platforms, project, dependency),
|
|
() => promisify(dependency.config.commands.postlink || commandStub),
|
|
]));
|
|
|
|
tasks.push(() => linkAssets(platforms, project, assets));
|
|
|
|
return promiseWaterfall(tasks).catch(err => {
|
|
log.error(
|
|
`Something went wrong while linking. Error: ${err.message} \n` +
|
|
'Please file an issue here: https://github.com/facebook/react-native/issues'
|
|
);
|
|
throw err;
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
func: link,
|
|
description: 'links all native dependencies (updates native build files)',
|
|
name: 'link [packageName]',
|
|
};
|