2018-02-09 01:10:29 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-05-20 11:52:08 +00:00
|
|
|
const log = require('npmlog');
|
|
|
|
|
|
|
|
const getProjectDependencies = require('./getProjectDependencies');
|
|
|
|
const getDependencyConfig = require('./getDependencyConfig');
|
|
|
|
const difference = require('lodash').difference;
|
2016-07-30 15:59:16 +00:00
|
|
|
const filter = require('lodash').filter;
|
2016-05-20 11:52:08 +00:00
|
|
|
const flatten = require('lodash').flatten;
|
2016-08-24 17:50:54 +00:00
|
|
|
const isEmpty = require('lodash').isEmpty;
|
|
|
|
const promiseWaterfall = require('./promiseWaterfall');
|
|
|
|
const commandStub = require('./commandStub');
|
|
|
|
const promisify = require('./promisify');
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
log.heading = 'rnpm-link';
|
|
|
|
|
2018-02-13 12:57:18 +00:00
|
|
|
const unlinkDependency = (platforms, project, dependency, packageName, otherDependencies) => {
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-02-08 17:45:16 +00:00
|
|
|
Object.keys(platforms || {})
|
|
|
|
.forEach(platform => {
|
|
|
|
if (!project[platform] || !dependency[platform]) {
|
2018-02-13 12:57:18 +00:00
|
|
|
return;
|
2018-02-08 17:45:16 +00:00
|
|
|
}
|
2016-12-11 00:53:53 +00:00
|
|
|
|
2018-02-08 17:45:16 +00:00
|
|
|
const linkConfig = platforms[platform] && platforms[platform].linkConfig && platforms[platform].linkConfig();
|
|
|
|
if (!linkConfig || !linkConfig.isInstalled || !linkConfig.unregister) {
|
2018-02-13 12:57:18 +00:00
|
|
|
return;
|
2018-02-08 17:45:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const isInstalled = linkConfig.isInstalled(project[platform], dependency[platform]);
|
|
|
|
|
|
|
|
if (!isInstalled) {
|
|
|
|
log.info(`Platform '${platform}' module ${packageName} is not installed`);
|
|
|
|
return;
|
|
|
|
}
|
2016-12-11 00:53:53 +00:00
|
|
|
|
2018-02-08 17:45:16 +00:00
|
|
|
log.info(`Unlinking ${packageName} ${platform} dependency`);
|
2016-12-11 00:53:53 +00:00
|
|
|
|
2018-02-08 17:45:16 +00:00
|
|
|
linkConfig.unregister(
|
|
|
|
packageName,
|
|
|
|
dependency[platform],
|
2018-02-13 12:57:18 +00:00
|
|
|
project[platform],
|
|
|
|
otherDependencies
|
2018-02-08 17:45:16 +00:00
|
|
|
);
|
2016-12-11 00:53:53 +00:00
|
|
|
|
2018-02-08 17:45:16 +00:00
|
|
|
log.info(`Platform '${platform}' module ${dependency.name} has been successfully unlinked`);
|
|
|
|
});
|
2016-12-11 00:53:53 +00:00
|
|
|
};
|
|
|
|
|
2016-05-20 11:52:08 +00:00
|
|
|
/**
|
|
|
|
* Updates project and unlink specific dependency
|
|
|
|
*
|
|
|
|
* If optional argument [packageName] is provided, it's the only one
|
|
|
|
* that's checked
|
|
|
|
*/
|
2016-08-22 15:56:14 +00:00
|
|
|
function unlink(args, config) {
|
2016-05-20 11:52:08 +00:00
|
|
|
const packageName = args[0];
|
|
|
|
|
2018-02-08 17:45:16 +00:00
|
|
|
let platforms;
|
|
|
|
let project;
|
|
|
|
let dependency;
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
try {
|
2018-02-08 17:45:16 +00:00
|
|
|
platforms = config.getPlatformConfig();
|
2016-05-20 11:52:08 +00:00
|
|
|
project = config.getProjectConfig();
|
|
|
|
} catch (err) {
|
|
|
|
log.error(
|
|
|
|
'ERRPACKAGEJSON',
|
|
|
|
'No package found. Are you sure it\'s a React Native project?'
|
|
|
|
);
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
dependency = config.getDependencyConfig(packageName);
|
|
|
|
} catch (err) {
|
|
|
|
log.warn(
|
|
|
|
'ERRINVALIDPROJ',
|
|
|
|
`Project ${packageName} is not a react-native library`
|
|
|
|
);
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const allDependencies = getDependencyConfig(config, getProjectDependencies());
|
2016-07-30 15:59:16 +00:00
|
|
|
const otherDependencies = filter(allDependencies, d => d.name !== packageName);
|
|
|
|
|
2016-08-24 17:50:54 +00:00
|
|
|
const tasks = [
|
2017-01-12 21:45:43 +00:00
|
|
|
() => promisify(dependency.commands.preunlink || commandStub),
|
2018-02-13 12:57:18 +00:00
|
|
|
() => unlinkDependency(platforms, project, dependency, packageName, otherDependencies),
|
2017-01-12 21:45:43 +00:00
|
|
|
() => promisify(dependency.commands.postunlink || commandStub)
|
2016-08-24 17:50:54 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return promiseWaterfall(tasks)
|
|
|
|
.then(() => {
|
2017-01-12 21:45:43 +00:00
|
|
|
// @todo move all these to `tasks` array, just like in
|
|
|
|
// link
|
2016-08-24 17:50:54 +00:00
|
|
|
const assets = difference(
|
|
|
|
dependency.assets,
|
|
|
|
flatten(allDependencies, d => d.assets)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isEmpty(assets)) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2018-02-13 12:57:18 +00:00
|
|
|
Object.keys(platforms || {})
|
|
|
|
.forEach(platform => {
|
|
|
|
const linkConfig = platforms[platform] && platforms[platform].linkConfig && platforms[platform].linkConfig();
|
|
|
|
if (!linkConfig || !linkConfig.unlinkAssets) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`Unlinking assets from ${platform} project`);
|
|
|
|
linkConfig.unlinkAssets(assets, project[platform]);
|
|
|
|
});
|
2016-08-24 17:50:54 +00:00
|
|
|
|
|
|
|
log.info(
|
|
|
|
`${packageName} assets has been successfully unlinked from your project`
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
log.error(
|
|
|
|
`It seems something went wrong while unlinking. Error: ${err.message}`
|
|
|
|
);
|
|
|
|
throw err;
|
|
|
|
});
|
2017-10-10 00:37:08 +00:00
|
|
|
}
|
2016-08-22 15:56:14 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
func: unlink,
|
|
|
|
description: 'unlink native dependency',
|
|
|
|
name: 'unlink <packageName>',
|
|
|
|
};
|