Skip to content

Commit 54ad25b

Browse files
author
Alexey
committed
Merge pull request #50 from rnpm/feature/shared-libraries-struct
Fixes #21
2 parents 9c4cddd + 44cb9d1 commit 54ad25b

10 files changed

+83
-18
lines changed

src/flatMap.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function flatMap(arr, mapper) {
2+
return arr.reduce(
3+
(acc, item) => acc.concat(mapper(item)),
4+
[]
5+
);
6+
};

src/getDependencyConfig.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Given an array of dependencies - it returns their RNPM config
3+
* if they were valid.
4+
*/
5+
module.exports = function getDependencyConfig(config, deps) {
6+
return deps.reduce((acc, name) => {
7+
try {
8+
return acc.concat({
9+
config: config.getDependencyConfig(name),
10+
name,
11+
});
12+
} catch (err) {
13+
return acc;
14+
}
15+
}, []);
16+
};

src/ios/addSharedLibraries.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function addSharedLibraries(project, libraries) {
2+
3+
};

src/ios/registerNativeModule.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const createGroup = require('./createGroup');
1111
const hasLibraryImported = require('./hasLibraryImported');
1212
const addFileToProject = require('./addFileToProject');
1313
const addProjectToLibraries = require('./addProjectToLibraries');
14+
const addSharedLibraries = require('./addSharedLibraries');
1415
const isEmpty = require('../isEmpty');
1516

1617
/**
@@ -52,6 +53,8 @@ module.exports = function registerNativeModuleIOS(dependencyConfig, projectConfi
5253
});
5354
});
5455

56+
addSharedLibraries(project, dependencyConfig.sharedLibraries);
57+
5558
const headers = getHeadersInFolder(dependencyConfig.folder);
5659
if (!isEmpty(headers)) {
5760
addToHeaderSearchPaths(

src/ios/removeSharedLibraries.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function removeSharedLibraries(project, libraries) {
2+
3+
};

src/ios/unregisterNativeModule.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const removeFileFromProject = require('./removeFileFromProject');
1111
const removeProjectFromLibraries = require('./removeProjectFromLibraries');
1212
const removeFromStaticLibraries = require('./removeFromStaticLibraries');
1313
const removeFromHeaderSearchPaths = require('./removeFromHeaderSearchPaths');
14+
const removeSharedLibraries = require('./addSharedLibraries');
1415

1516
/**
1617
* Unregister native module IOS
@@ -39,6 +40,8 @@ module.exports = function unregisterNativeModule(dependencyConfig, projectConfig
3940
});
4041
});
4142

43+
removeSharedLibraries(project, dependencyConfig.sharedLibraries);
44+
4245
const headers = getHeadersInFolder(dependencyConfig.folder);
4346
if (!isEmpty(headers)) {
4447
removeFromHeaderSearchPaths(

src/link.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const registerDependencyIOS = require('./ios/registerNativeModule');
77
const copyAssetsAndroid = require('./android/copyAssets');
88
const copyAssetsIOS = require('./ios/copyAssets');
99
const getProjectDependencies = require('./getProjectDependencies');
10+
const getDependencyConfig = require('./getDependencyConfig');
1011
const dedupeAssets = require('./dedupeAssets');
1112

1213
log.heading = 'rnpm-link';
@@ -81,22 +82,10 @@ module.exports = function link(config, args) {
8182

8283
const packageName = args[0];
8384

84-
const dependencies =
85-
(packageName ? [packageName] : getProjectDependencies())
86-
.map(name => {
87-
try {
88-
return {
89-
config: config.getDependencyConfig(name),
90-
name,
91-
};
92-
} catch (err) {
93-
log.warn(
94-
'ERRINVALIDPROJ',
95-
`Project ${name} is not a react-native library`
96-
);
97-
}
98-
})
99-
.filter(dependency => dependency);
85+
const dependencies = getDependencyConfig(
86+
config,
87+
packageName ? [packageName] : getProjectDependencies()
88+
);
10089

10190
const tasks = Promise.all(dependencies.map(dependency => {
10291
const pre = promisify(dependency.config.commands.prelink || commandStub);

src/unlink.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ const path = require('path');
22
const log = require('npmlog');
33

44
const isEmpty = require('./isEmpty');
5+
const getProjectDependencies = require('./getProjectDependencies');
56
const unregisterDependencyAndroid = require('./android/unregisterNativeModule');
67
const unregisterDependencyIOS = require('./ios/unregisterNativeModule');
78
const unlinkAssetsAndroid = require('./android/unlinkAssets');
89
const unlinkAssetsIOS = require('./ios/unlinkAssets');
10+
const getDependencyConfig = require('./getDependencyConfig');
11+
const diff = require('./diff');
12+
const flatMap = require('./flatMap');
913

1014
log.heading = 'rnpm-link';
1115

@@ -32,9 +36,11 @@ module.exports = function unlink(config, args) {
3236
'ERRINVALIDPROJ',
3337
`Project ${packageName} is not a react-native library`
3438
);
35-
process.exit(1);
39+
return Promise.reject(err);
3640
}
3741

42+
const allDependencies = getDependencyConfig(config, getProjectDependencies());
43+
3844
if (project.android && dependency.android) {
3945
log.info(`Unlinking ${packageName} android dependency`);
4046

@@ -63,7 +69,7 @@ module.exports = function unlink(config, args) {
6369
}
6470
}
6571

66-
const assets = dependency.assets;
72+
const assets = diff(dependency.assets, flatMap(allDependencies, d => d.assets));
6773

6874
if (isEmpty(assets)) {
6975
return Promise.resolve();

test/flatMap.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const chai = require('chai');
2+
const expect = chai.expect;
3+
const flatMap = require('../src/flatMap');
4+
5+
describe('flatMap', () => {
6+
7+
it('should return flattened array', () => {
8+
const items = [{ assets: ['FontA.ttf'] }, { assets: ['FontB.ttf'] }];
9+
10+
expect(flatMap(items, item => item.assets)).to.deep.equals(['FontA.ttf', 'FontB.ttf']);
11+
});
12+
13+
});

test/getDependencyConfig.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const chai = require('chai');
2+
const expect = chai.expect;
3+
const getDependencyConfig = require('../src/getDependencyConfig');
4+
const sinon = require('sinon');
5+
6+
describe('getDependencyConfig', () => {
7+
it('should return an array of dependencies\' rnpm config', () => {
8+
const config = {
9+
getDependencyConfig: sinon.stub(),
10+
};
11+
12+
expect(getDependencyConfig(config, ['abcd'])).to.be.an.array;
13+
expect(config.getDependencyConfig.callCount).to.equals(1);
14+
});
15+
16+
it('should filter out invalid react-native projects', () => {
17+
const config = {
18+
getDependencyConfig: sinon.stub().throws(new Error('Cannot require')),
19+
};
20+
21+
expect(getDependencyConfig(config, ['abcd'])).to.deep.equal([]);
22+
});
23+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy