Permalink
Browse files

Add pre/postunlink

Summary:
An attempt to address #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 #9157

Differential Revision: D3749434

fbshipit-source-id: 40b94c9026db4f11e8f5be4a417a0670e8069be6
  • Loading branch information...
1 parent d943e28 commit e7521a114f299ebb196e4305614141df2f428efa @geof90 geof90 committed with Facebook Github Bot 9 Aug 24, 2016
Showing with 50 additions and 32 deletions.
  1. +1 −0 local-cli/link/commandStub.js
  2. +2 −4 local-cli/link/link.js
  3. +3 −0 local-cli/link/promisify.js
  4. +44 −28 local-cli/link/unlink.js
@@ -0,0 +1 @@
+module.exports = (cb) => cb();
@@ -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) {
@@ -0,0 +1,3 @@
+module.exports = (func) => new Promise((resolve, reject) =>
+ func((err, res) => err ? reject(err) : resolve(res))
+);
@@ -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,35 +92,47 @@ 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 assets = difference(
- dependency.assets,
- flatten(allDependencies, d => d.assets)
- );
-
- if (isEmpty(assets)) {
- return Promise.resolve();
- }
-
- if (project.ios) {
- log.info('Unlinking assets from ios project');
- unlinkAssetsIOS(assets, project.ios);
- }
-
- if (project.android) {
- log.info('Unlinking assets from android project');
- unlinkAssetsAndroid(assets, project.android.assetsPath);
- }
-
- log.info(
- `${packageName} assets has been successfully unlinked from your project`
- );
-
- return Promise.resolve();
+ 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)
+ );
+
+ if (isEmpty(assets)) {
+ return Promise.resolve();
+ }
+
+ if (project.ios) {
+ log.info('Unlinking assets from ios project');
+ unlinkAssetsIOS(assets, project.ios);
+ }
+
+ if (project.android) {
+ log.info('Unlinking assets from android project');
+ unlinkAssetsAndroid(assets, project.android.assetsPath);
+ }
+
+ 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;
+ });
};
module.exports = {

0 comments on commit e7521a1

Please sign in to comment.