2016-09-12 13:13:27 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2013-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.
|
|
|
|
*/
|
|
|
|
|
2016-05-20 11:52:08 +00:00
|
|
|
const log = require('npmlog');
|
|
|
|
const path = require('path');
|
|
|
|
const uniq = require('lodash').uniq;
|
|
|
|
const flatten = require('lodash').flatten;
|
2016-09-12 13:13:27 +00:00
|
|
|
const chalk = require('chalk');
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
const isEmpty = require('lodash').isEmpty;
|
|
|
|
const promiseWaterfall = require('./promiseWaterfall');
|
|
|
|
const registerDependencyAndroid = require('./android/registerNativeModule');
|
2016-12-11 00:53:53 +00:00
|
|
|
const registerDependencyWindows = require('./windows/registerNativeModule');
|
2016-05-20 11:52:08 +00:00
|
|
|
const registerDependencyIOS = require('./ios/registerNativeModule');
|
|
|
|
const isInstalledAndroid = require('./android/isInstalled');
|
2016-12-11 00:53:53 +00:00
|
|
|
const isInstalledWindows = require('./windows/isInstalled');
|
2016-05-20 11:52:08 +00:00
|
|
|
const isInstalledIOS = require('./ios/isInstalled');
|
|
|
|
const copyAssetsAndroid = require('./android/copyAssets');
|
|
|
|
const copyAssetsIOS = require('./ios/copyAssets');
|
|
|
|
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');
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
log.heading = 'rnpm-link';
|
|
|
|
|
|
|
|
const dedupeAssets = (assets) => uniq(assets, asset => path.basename(asset));
|
|
|
|
|
|
|
|
|
|
|
|
const linkDependencyAndroid = (androidProject, dependency) => {
|
|
|
|
if (!androidProject || !dependency.config.android) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isInstalled = isInstalledAndroid(androidProject, dependency.name);
|
|
|
|
|
|
|
|
if (isInstalled) {
|
2016-09-12 13:13:27 +00:00
|
|
|
log.info(chalk.grey(`Android module ${dependency.name} is already linked`));
|
2016-05-20 11:52:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pollParams(dependency.config.params).then(params => {
|
|
|
|
log.info(`Linking ${dependency.name} android dependency`);
|
|
|
|
|
|
|
|
registerDependencyAndroid(
|
|
|
|
dependency.name,
|
|
|
|
dependency.config.android,
|
|
|
|
params,
|
|
|
|
androidProject
|
|
|
|
);
|
|
|
|
|
|
|
|
log.info(`Android module ${dependency.name} has been successfully linked`);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-12-11 00:53:53 +00:00
|
|
|
const linkDependencyWindows = (windowsProject, dependency) => {
|
|
|
|
|
|
|
|
if (!windowsProject || !dependency.config.windows) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isInstalled = isInstalledWindows(windowsProject, dependency.config.windows);
|
|
|
|
|
|
|
|
if (isInstalled) {
|
|
|
|
log.info(chalk.grey(`Windows module ${dependency.name} is already linked`));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pollParams(dependency.config.params).then(params => {
|
|
|
|
log.info(`Linking ${dependency.name} windows dependency`);
|
|
|
|
|
|
|
|
registerDependencyWindows(
|
|
|
|
dependency.name,
|
|
|
|
dependency.config.windows,
|
|
|
|
params,
|
|
|
|
windowsProject
|
|
|
|
);
|
|
|
|
|
|
|
|
log.info(`Windows module ${dependency.name} has been successfully linked`);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-05-20 11:52:08 +00:00
|
|
|
const linkDependencyIOS = (iOSProject, dependency) => {
|
|
|
|
if (!iOSProject || !dependency.config.ios) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isInstalled = isInstalledIOS(iOSProject, dependency.config.ios);
|
|
|
|
|
|
|
|
if (isInstalled) {
|
2016-09-12 13:13:27 +00:00
|
|
|
log.info(chalk.grey(`iOS module ${dependency.name} is already linked`));
|
2016-05-20 11:52:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`Linking ${dependency.name} ios dependency`);
|
|
|
|
|
|
|
|
registerDependencyIOS(dependency.config.ios, iOSProject);
|
|
|
|
|
|
|
|
log.info(`iOS module ${dependency.name} has been successfully linked`);
|
|
|
|
};
|
|
|
|
|
|
|
|
const linkAssets = (project, assets) => {
|
|
|
|
if (isEmpty(assets)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (project.ios) {
|
|
|
|
log.info('Linking assets to ios project');
|
|
|
|
copyAssetsIOS(assets, project.ios);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (project.android) {
|
|
|
|
log.info('Linking assets to android project');
|
|
|
|
copyAssetsAndroid(assets, project.android.assetsPath);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-12-11 00:53:53 +00:00
|
|
|
* Updates project and links all dependencies to it
|
2016-05-20 11:52:08 +00:00
|
|
|
*
|
|
|
|
* If optional argument [packageName] is provided, it's the only one that's checked
|
|
|
|
*/
|
2016-08-22 15:56:14 +00:00
|
|
|
function link(args, config) {
|
2016-05-20 11:52:08 +00:00
|
|
|
var project;
|
|
|
|
try {
|
|
|
|
project = config.getProjectConfig();
|
|
|
|
} catch (err) {
|
|
|
|
log.error(
|
|
|
|
'ERRPACKAGEJSON',
|
|
|
|
'No package found. Are you sure it\'s a React Native project?'
|
|
|
|
);
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const packageName = args[0];
|
|
|
|
|
|
|
|
const dependencies = getDependencyConfig(
|
|
|
|
config,
|
|
|
|
packageName ? [packageName] : getProjectDependencies()
|
|
|
|
);
|
|
|
|
|
|
|
|
const assets = dedupeAssets(dependencies.reduce(
|
|
|
|
(assets, dependency) => assets.concat(dependency.config.assets),
|
|
|
|
project.assets
|
|
|
|
));
|
|
|
|
|
|
|
|
const tasks = flatten(dependencies.map(dependency => [
|
|
|
|
() => promisify(dependency.config.commands.prelink || commandStub),
|
|
|
|
() => linkDependencyAndroid(project.android, dependency),
|
|
|
|
() => linkDependencyIOS(project.ios, dependency),
|
2016-12-11 00:53:53 +00:00
|
|
|
() => linkDependencyWindows(project.windows, dependency),
|
2016-05-20 11:52:08 +00:00
|
|
|
() => promisify(dependency.config.commands.postlink || commandStub),
|
|
|
|
]));
|
|
|
|
|
|
|
|
tasks.push(() => linkAssets(project, assets));
|
|
|
|
|
|
|
|
return promiseWaterfall(tasks).catch(err => {
|
|
|
|
log.error(
|
|
|
|
`It seems something went wrong while linking. Error: ${err.message} \n`
|
2016-09-12 13:13:27 +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,
|
|
|
|
description: 'links all native dependencies',
|
|
|
|
name: 'link [packageName]',
|
|
|
|
};
|