2018-02-09 01:10:29 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-02-09 01:10:29 +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.
|
2018-05-11 19:43:49 +00:00
|
|
|
*
|
|
|
|
* @format
|
2018-02-09 01:10:29 +00:00
|
|
|
*/
|
|
|
|
|
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-05-11 19:43:49 +00:00
|
|
|
const unlinkDependency = (
|
|
|
|
platforms,
|
|
|
|
project,
|
|
|
|
dependency,
|
|
|
|
packageName,
|
|
|
|
otherDependencies,
|
|
|
|
) => {
|
|
|
|
Object.keys(platforms || {}).forEach(platform => {
|
|
|
|
if (!project[platform] || !dependency[platform]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const linkConfig =
|
|
|
|
platforms[platform] &&
|
|
|
|
platforms[platform].linkConfig &&
|
|
|
|
platforms[platform].linkConfig();
|
|
|
|
if (!linkConfig || !linkConfig.isInstalled || !linkConfig.unregister) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isInstalled = linkConfig.isInstalled(
|
|
|
|
project[platform],
|
|
|
|
packageName,
|
|
|
|
dependency[platform],
|
|
|
|
);
|
2018-02-08 17:45:16 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
if (!isInstalled) {
|
|
|
|
log.info(`Platform '${platform}' module ${packageName} is not installed`);
|
|
|
|
return;
|
|
|
|
}
|
2016-12-11 00:53:53 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
log.info(`Unlinking ${packageName} ${platform} dependency`);
|
2016-12-11 00:53:53 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
linkConfig.unregister(
|
|
|
|
packageName,
|
|
|
|
dependency[platform],
|
|
|
|
project[platform],
|
|
|
|
otherDependencies,
|
|
|
|
);
|
2016-12-11 00:53:53 +00:00
|
|
|
|
2018-05-11 19:43:49 +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',
|
2018-05-11 19:43:49 +00:00
|
|
|
"No package found. Are you sure it's a React Native project?",
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
dependency = config.getDependencyConfig(packageName);
|
|
|
|
} catch (err) {
|
|
|
|
log.warn(
|
|
|
|
'ERRINVALIDPROJ',
|
2018-05-11 19:43:49 +00:00
|
|
|
`Project ${packageName} is not a react-native library`,
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const allDependencies = getDependencyConfig(config, getProjectDependencies());
|
2018-05-11 19:43:49 +00:00
|
|
|
const otherDependencies = filter(
|
|
|
|
allDependencies,
|
|
|
|
d => d.name !== packageName,
|
|
|
|
);
|
2016-07-30 15:59:16 +00:00
|
|
|
|
2016-08-24 17:50:54 +00:00
|
|
|
const tasks = [
|
2017-01-12 21:45:43 +00:00
|
|
|
() => promisify(dependency.commands.preunlink || commandStub),
|
2018-05-11 19:43:49 +00:00
|
|
|
() =>
|
|
|
|
unlinkDependency(
|
|
|
|
platforms,
|
|
|
|
project,
|
|
|
|
dependency,
|
|
|
|
packageName,
|
|
|
|
otherDependencies,
|
|
|
|
),
|
|
|
|
() => 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,
|
2018-05-11 19:43:49 +00:00
|
|
|
flatten(allDependencies, d => d.assets),
|
2016-08-24 17:50:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (isEmpty(assets)) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
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.unlinkAssets) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-22 15:04:35 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
log.info(`Unlinking assets from ${platform} project`);
|
|
|
|
linkConfig.unlinkAssets(assets, project[platform]);
|
|
|
|
});
|
2016-08-24 17:50:54 +00:00
|
|
|
|
|
|
|
log.info(
|
2018-05-11 19:43:49 +00:00
|
|
|
`${packageName} assets has been successfully unlinked from your project`,
|
2016-08-24 17:50:54 +00:00
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
log.error(
|
2018-05-11 19:43:49 +00:00
|
|
|
`It seems something went wrong while unlinking. Error: ${err.message}`,
|
2016-08-24 17:50:54 +00:00
|
|
|
);
|
|
|
|
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>',
|
|
|
|
};
|