2016-09-12 13:13:27 +00:00
|
|
|
/**
|
2018-02-09 01:10:29 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-09-12 13:13:27 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-02-27 17:12:14 +00:00
|
|
|
*
|
2018-05-11 19:43:49 +00:00
|
|
|
* @format
|
2017-02-27 17:12:14 +00:00
|
|
|
* @flow
|
2016-09-12 13:13:27 +00:00
|
|
|
*/
|
|
|
|
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $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. */
|
2016-05-20 11:52:08 +00:00
|
|
|
const log = require('npmlog');
|
|
|
|
const path = require('path');
|
2017-09-18 09:41:24 +00:00
|
|
|
const uniqBy = require('lodash').uniqBy;
|
2016-05-20 11:52:08 +00:00
|
|
|
const flatten = require('lodash').flatten;
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $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. */
|
2016-09-12 13:13:27 +00:00
|
|
|
const chalk = require('chalk');
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $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. */
|
2016-05-20 11:52:08 +00:00
|
|
|
const isEmpty = require('lodash').isEmpty;
|
|
|
|
const promiseWaterfall = require('./promiseWaterfall');
|
|
|
|
const getProjectDependencies = require('./getProjectDependencies');
|
|
|
|
const getDependencyConfig = require('./getDependencyConfig');
|
|
|
|
const pollParams = require('./pollParams');
|
2016-08-24 17:50:54 +00:00
|
|
|
const commandStub = require('./commandStub');
|
|
|
|
const promisify = require('./promisify');
|
2017-08-03 18:55:40 +00:00
|
|
|
const findReactNativeScripts = require('../util/findReactNativeScripts');
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2017-05-03 13:38:40 +00:00
|
|
|
import type {RNConfig} from '../core';
|
2017-02-27 17:12:14 +00:00
|
|
|
|
2016-05-20 11:52:08 +00:00
|
|
|
log.heading = 'rnpm-link';
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const dedupeAssets = assets => uniqBy(assets, asset => path.basename(asset));
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-03-20 21:41:26 +00:00
|
|
|
const linkDependency = async (platforms, project, dependency) => {
|
|
|
|
const params = await pollParams(dependency.config.params);
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
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],
|
|
|
|
);
|
2018-02-08 17:45:16 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
if (isInstalled) {
|
|
|
|
log.info(
|
|
|
|
chalk.grey(
|
|
|
|
`Platform '${platform}' module ${dependency.name} is already linked`,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
2018-02-08 17:45:16 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
log.info(`Linking ${dependency.name} ${platform} dependency`);
|
2018-02-08 17:45:16 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
linkConfig.register(
|
|
|
|
dependency.name,
|
|
|
|
dependency.config[platform],
|
|
|
|
params,
|
|
|
|
project[platform],
|
|
|
|
);
|
2018-02-08 17:45:16 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
log.info(
|
|
|
|
`Platform '${platform}' module ${
|
|
|
|
dependency.name
|
|
|
|
} has been successfully linked`,
|
|
|
|
);
|
|
|
|
});
|
2016-12-11 00:53:53 +00:00
|
|
|
};
|
|
|
|
|
2018-02-08 17:45:16 +00:00
|
|
|
const linkAssets = (platforms, project, assets) => {
|
2016-05-20 11:52:08 +00:00
|
|
|
if (isEmpty(assets)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
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]);
|
|
|
|
});
|
2018-02-08 17:45:16 +00:00
|
|
|
|
2016-09-12 13:13:27 +00:00
|
|
|
log.info('Assets have been successfully linked to your project');
|
2016-05-20 11:52:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-02-27 17:12:14 +00:00
|
|
|
* Updates project and links all dependencies to it.
|
2016-05-20 11:52:08 +00:00
|
|
|
*
|
2017-02-27 17:12:14 +00:00
|
|
|
* @param args If optional argument [packageName] is provided,
|
|
|
|
* only that package is processed.
|
|
|
|
* @param config CLI config, see local-cli/core/index.js
|
2016-05-20 11:52:08 +00:00
|
|
|
*/
|
2017-05-03 13:38:40 +00:00
|
|
|
function link(args: Array<string>, config: RNConfig) {
|
2018-02-08 17:45:16 +00:00
|
|
|
let project;
|
|
|
|
let platforms;
|
2016-05-20 11:52:08 +00:00
|
|
|
try {
|
|
|
|
project = config.getProjectConfig();
|
2018-02-08 17:45:16 +00:00
|
|
|
platforms = config.getPlatformConfig();
|
2016-05-20 11:52:08 +00:00
|
|
|
} catch (err) {
|
|
|
|
log.error(
|
|
|
|
'ERRPACKAGEJSON',
|
2018-05-11 19:43:49 +00:00
|
|
|
'No package found. Are you sure this is a React Native project?',
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const hasProjectConfig = Object.keys(platforms).reduce(
|
|
|
|
(acc, key) => acc || key in project,
|
|
|
|
false,
|
|
|
|
);
|
2018-02-08 17:45:16 +00:00
|
|
|
if (!hasProjectConfig && findReactNativeScripts()) {
|
2017-08-03 18:55:40 +00:00
|
|
|
throw new Error(
|
|
|
|
'`react-native link` can not be used in Create React Native App projects. ' +
|
2018-05-11 19:43:49 +00:00
|
|
|
'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.',
|
2017-08-03 18:55:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-02 13:09:53 +00:00
|
|
|
let packageName = args[0];
|
|
|
|
// Check if install package by specific version (eg. package@latest)
|
|
|
|
if (packageName !== undefined) {
|
|
|
|
packageName = packageName.split('@')[0];
|
|
|
|
}
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
const dependencies = getDependencyConfig(
|
|
|
|
config,
|
2018-05-11 19:43:49 +00:00
|
|
|
packageName ? [packageName] : getProjectDependencies(),
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const assets = dedupeAssets(
|
|
|
|
dependencies.reduce(
|
|
|
|
(acc, dependency) => acc.concat(dependency.config.assets),
|
|
|
|
project.assets,
|
|
|
|
),
|
|
|
|
);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const tasks = flatten(
|
|
|
|
dependencies.map(dependency => [
|
|
|
|
() => promisify(dependency.config.commands.prelink || commandStub),
|
|
|
|
() => linkDependency(platforms, project, dependency),
|
|
|
|
() => promisify(dependency.config.commands.postlink || commandStub),
|
|
|
|
]),
|
|
|
|
);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-02-08 17:45:16 +00:00
|
|
|
tasks.push(() => linkAssets(platforms, project, assets));
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
return promiseWaterfall(tasks).catch(err => {
|
|
|
|
log.error(
|
2017-02-27 17:12:14 +00:00
|
|
|
`Something went wrong while linking. Error: ${err.message} \n` +
|
2018-05-11 19:43:49 +00:00
|
|
|
'Please file an issue here: https://github.com/facebook/react-native/issues',
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
throw err;
|
|
|
|
});
|
2016-09-12 13:13:27 +00:00
|
|
|
}
|
2016-08-22 15:56:14 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
func: link,
|
2017-02-27 17:12:14 +00:00
|
|
|
description: 'links all native dependencies (updates native build files)',
|
2016-08-22 15:56:14 +00:00
|
|
|
name: 'link [packageName]',
|
|
|
|
};
|