mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 01:25:39 +00:00
ec884890b1
Summary: Android uses the name of the package, not the config, for the `isInstalled` check. Sending both parameters to `isInstalled` so we have a consistent API. <!-- Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html Happy contributing! --> A bug was uncovered in the react-native link command where Android would not unlink because the wrong parameters were being sent to `isInstalled`. Successfully linked and unlinked `react-native-fs` on Windows and Mac. Jest tests pass. <!-- 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][BUGFIX][local-cli/link/link.js] - Fix issue with `isInstalled` check for Android [CLI][BUGFIX][local-cli/link/unlink.js] - Fix issue with `isInstalled` check for Android [CLI][BUGFIX][local-cli/link/ios/common/unregisterNativeModule.js] - Fix references to unregister implementations. Closes https://github.com/facebook/react-native/pull/18207 Differential Revision: D7180885 Pulled By: hramos fbshipit-source-id: 5f479cd9d7b1ebd8626b461e9dc1f22988e2c61f
163 lines
5.3 KiB
JavaScript
163 lines
5.3 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @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.name, 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]',
|
|
};
|