Skip to content

Commit fc19875

Browse files
committed
feat: support scoped packages with relative paths in overrides
1 parent c51a209 commit fc19875

File tree

9 files changed

+187
-21
lines changed

9 files changed

+187
-21
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs');
22
const firstBy = require('thenby');
3+
const pathJoin = require('path.join');
34

45
const defaultConfig = Object.freeze({
56
packageJsonPath: './package.json',
@@ -60,7 +61,7 @@ function _getPackageJson(path) {
6061
function _getOverridenPaths(config, key) {
6162
const mainOverrides = config.overrides[key];
6263
if(Array.isArray(mainOverrides)) {
63-
return mainOverrides.map(path => `${config.nodeModulesPath}/${key}/${path}`);
64+
return mainOverrides.map(p => `./${pathJoin(config.nodeModulesPath, key, p)}`);
6465
} else {
6566
return [`${config.nodeModulesPath}/${key}/${mainOverrides}`];
6667
}

package-lock.json

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"jasmine": "^2.6.0"
2828
},
2929
"dependencies": {
30+
"path.join": "^1.0.0",
3031
"thenby": "^1.2.3"
3132
}
3233
}

spec/main.spec.js

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe("Main node files", function() {
1+
describe("Main node files", function () {
22
const getMainNodeFiles = require('../');
33

44
it('should not throw with missed dependencies section', () => {
@@ -63,7 +63,7 @@ describe("Main node files", function() {
6363
expect(files).toEqual([
6464
'./spec/test_node_modules/test-module-2/main.js',
6565
'./spec/test_node_modules/test-module-2/index.js'
66-
]);
66+
]);
6767
});
6868

6969
it('should return ordered array if order option specified', () => {
@@ -80,7 +80,7 @@ describe("Main node files", function() {
8080
expect(files).toEqual([
8181
'./spec/test_node_modules/test-module-4/index.js',
8282
'./spec/test_node_modules/test-module-3/index.js'
83-
]);
83+
]);
8484
});
8585

8686
it('should return alphabetically ordered array for the same specified order option values', () => {
@@ -97,8 +97,8 @@ describe("Main node files", function() {
9797
expect(files).toEqual([
9898
'./spec/test_node_modules/test-module-3/index.js',
9999
'./spec/test_node_modules/test-module-4/index.js'
100-
]);
101-
});
100+
]);
101+
});
102102

103103
it('should return ordered array with packages without specified order', () => {
104104
const options = {
@@ -111,7 +111,7 @@ describe("Main node files", function() {
111111
const files = getMainNodeFiles(options);
112112

113113
expect(files.length).toEqual(2);
114-
});
114+
});
115115

116116
it('should return ordered array with packages without specified order at the end', () => {
117117
const options = {
@@ -125,7 +125,7 @@ describe("Main node files", function() {
125125

126126
expect(files[0]).toEqual('./spec/test_node_modules/test-module-4/index.js');
127127
expect(files[1]).toEqual('./spec/test_node_modules/test-module-3/index.js');
128-
});
128+
});
129129

130130
it('should not return packages that specified in order option but are really absent', () => {
131131
const options = {
@@ -138,7 +138,7 @@ describe("Main node files", function() {
138138
const files = getMainNodeFiles(options);
139139

140140
expect(files).not.toContain('./spec/test_node_modules/test-module-5/index.js');
141-
});
141+
});
142142

143143
it('should support both overrides and order', () => {
144144
const options = {
@@ -158,8 +158,8 @@ describe("Main node files", function() {
158158
expect(files).toEqual([
159159
'./spec/test_node_modules/test-module-4/main-4.js',
160160
'./spec/test_node_modules/test-module-3/main-3.js'
161-
]);
162-
});
161+
]);
162+
});
163163

164164
it('should support both overrides with array and order', () => {
165165
const options = {
@@ -179,23 +179,80 @@ describe("Main node files", function() {
179179
'./spec/test_node_modules/test-module-4/main.js',
180180
'./spec/test_node_modules/test-module-4/index.js',
181181
'./spec/test_node_modules/test-module-3/index.js'
182-
]);
183-
});
182+
]);
183+
});
184184

185-
it('should not changed order of packages specified in package.json if order option is empty', () => {
186-
let options = {
185+
it('should not changed order of packages specified in package.json if order option is empty', () => {
186+
const options = {
187187
packageJsonPath: './spec/standard-reversed.package.json',
188188
nodeModulesPath: './spec/test_node_modules',
189-
order: { }
189+
order: {}
190190
};
191-
let files = getMainNodeFiles(options);
192-
193-
console.log('Main files', files);
191+
const files = getMainNodeFiles(options);
194192

195193
expect(files).toEqual([
196194
'./spec/test_node_modules/test-module-4/index.js',
197195
'./spec/test_node_modules/test-module-3/index.js'
198-
]);
199-
});
196+
]);
197+
});
198+
199+
it('should support scoped packages', () => {
200+
const options = {
201+
packageJsonPath: './spec/scoped.package.json',
202+
nodeModulesPath: './spec/test_node_modules'
203+
};
204+
const files = getMainNodeFiles(options);
205+
206+
expect(files).toEqual([
207+
'./spec/test_node_modules/@scoped/module-1/index.js'
208+
]);
209+
});
210+
211+
it('should support overrides for scoped packages', () => {
212+
const options = {
213+
packageJsonPath: './spec/scoped.package.json',
214+
nodeModulesPath: './spec/test_node_modules',
215+
overrides: {
216+
'@scoped/module-1': 'main.js'
217+
},
218+
};
219+
const files = getMainNodeFiles(options);
220+
221+
expect(files).toEqual([
222+
'./spec/test_node_modules/@scoped/module-1/main.js'
223+
]);
224+
});
225+
226+
it('should support array overrides for scoped packages', () => {
227+
const options = {
228+
packageJsonPath: './spec/scoped.package.json',
229+
nodeModulesPath: './spec/test_node_modules',
230+
overrides: {
231+
'@scoped/module-1': ['main.js', 'index.js']
232+
},
233+
};
234+
const files = getMainNodeFiles(options);
235+
236+
expect(files).toEqual([
237+
'./spec/test_node_modules/@scoped/module-1/main.js',
238+
'./spec/test_node_modules/@scoped/module-1/index.js',
239+
]);
240+
});
241+
242+
it('should support relative paths in overrides for scoped packages', () => {
243+
const options = {
244+
packageJsonPath: './spec/scoped.package.json',
245+
nodeModulesPath: './spec/test_node_modules',
246+
overrides: {
247+
'@scoped/module-1': ['../module-2/index.js', 'index.js']
248+
},
249+
};
250+
const files = getMainNodeFiles(options);
251+
252+
expect(files).toEqual([
253+
'./spec/test_node_modules/@scoped/module-2/index.js',
254+
'./spec/test_node_modules/@scoped/module-1/index.js'
255+
]);
256+
});
200257

201258
});

spec/scoped.package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"@scoped/module-1": "1.0.0"
4+
}
5+
}

spec/test_node_modules/@scoped/module-1/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/test_node_modules/@scoped/module-1/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/test_node_modules/@scoped/module-2/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/test_node_modules/@scoped/module-2/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)