Add pre/postunlink

Summary:
An attempt to address https://github.com/facebook/react-native/issues/9156.

cc grabbou Kureev

**Test plan (required)**

1. Added the following to the `package.json` of a `react-native-plugin`

```
  "rnpm": {
    "commands": {
      "preunlink": "node node_modules/react-native-plugin/scripts/preunlink"
      "postunlink": "node node_modules/react-native-plugin/scripts/postunlink"
    }
  }
```

2. Added files, `scripts/preunlink.js` and `scripts/postunlink.js` to the plugin. Each of them simply logs a string to the console.

3. Ran `react-native unlink react-native-plugin` to verify that those logs get printed.
Closes https://github.com/facebook/react-native/pull/9157

Differential Revision: D3749434

fbshipit-source-id: 40b94c9026db4f11e8f5be4a417a0670e8069be6
This commit is contained in:
Geoffrey Goh 2016-08-24 10:50:54 -07:00 committed by Facebook Github Bot 9
parent d943e28bc6
commit e7521a114f
4 changed files with 45 additions and 27 deletions

View File

@ -0,0 +1 @@
module.exports = (cb) => cb();

View File

@ -14,15 +14,13 @@ const copyAssetsIOS = require('./ios/copyAssets');
const getProjectDependencies = require('./getProjectDependencies');
const getDependencyConfig = require('./getDependencyConfig');
const pollParams = require('./pollParams');
const commandStub = require('./commandStub');
const promisify = require('./promisify');
log.heading = 'rnpm-link';
const commandStub = (cb) => cb();
const dedupeAssets = (assets) => uniq(assets, asset => path.basename(asset));
const promisify = (func) => new Promise((resolve, reject) =>
func((err, res) => err ? reject(err) : resolve(res))
);
const linkDependencyAndroid = (androidProject, dependency) => {
if (!androidProject || !dependency.config.android) {

View File

@ -0,0 +1,3 @@
module.exports = (func) => new Promise((resolve, reject) =>
func((err, res) => err ? reject(err) : resolve(res))
);

View File

@ -11,8 +11,12 @@ const getDependencyConfig = require('./getDependencyConfig');
const compact = require('lodash').compact;
const difference = require('lodash').difference;
const filter = require('lodash').filter;
const isEmpty = require('lodash').isEmpty;
const find = require('lodash').find;
const flatten = require('lodash').flatten;
const isEmpty = require('lodash').isEmpty;
const promiseWaterfall = require('./promiseWaterfall');
const commandStub = require('./commandStub');
const promisify = require('./promisify');
log.heading = 'rnpm-link';
@ -88,11 +92,18 @@ function unlink(args, config) {
const allDependencies = getDependencyConfig(config, getProjectDependencies());
const otherDependencies = filter(allDependencies, d => d.name !== packageName);
const thisDependency = find(allDependencies, d => d.name === packageName);
const iOSDependencies = compact(otherDependencies.map(d => d.config.ios));
unlinkDependencyAndroid(project.android, dependency, packageName);
unlinkDependencyIOS(project.ios, dependency, packageName, iOSDependencies);
const tasks = [
() => promisify(thisDependency.config.commands.preunlink || commandStub),
() => unlinkDependencyAndroid(project.android, dependency, packageName),
() => unlinkDependencyIOS(project.ios, dependency, packageName, iOSDependencies),
() => promisify(thisDependency.config.commands.postunlink || commandStub)
];
return promiseWaterfall(tasks)
.then(() => {
const assets = difference(
dependency.assets,
flatten(allDependencies, d => d.assets)
@ -115,8 +126,13 @@ function unlink(args, config) {
log.info(
`${packageName} assets has been successfully unlinked from your project`
);
return Promise.resolve();
})
.catch(err => {
log.error(
`It seems something went wrong while unlinking. Error: ${err.message}`
);
throw err;
});
};
module.exports = {