Skip to content

Commit 67b577d

Browse files
fix(barrel): alphabetized barrel exports
Fixes #582
1 parent 8560d6d commit 67b577d

File tree

2 files changed

+191
-4
lines changed

2 files changed

+191
-4
lines changed
+22-4
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
var path = require('path');
2+
var fs = require('fs');
23
var EOL = require('os').EOL;
34

45
module.exports = addBarrelRegistration;
56

7+
function sortBarrel(contents) {
8+
var parts = contents.split(EOL).filter(function(l){
9+
return l.trim().length > 0;
10+
});
11+
parts.sort();
12+
return parts.join(EOL) + EOL;
13+
}
14+
615
function addBarrelRegistration(blueprint, installationDir, fileName) {
716
var parts = installationDir.split(path.sep);
8-
17+
918
var idx = parts.lastIndexOf('shared');
1019
if (idx < parts.length -2) {
1120
return Promise.resolve();
1221
}
13-
22+
1423
var sharedDir = parts.slice(0, idx + 1).join(path.sep);
1524
var relativeParts = parts.splice(idx + 1);
1625
if (fileName) {
1726
relativeParts.push(fileName);
1827
}
1928
var importFrom = './' + relativeParts.join('/');
20-
29+
2130
return blueprint.insertIntoFile(
2231
sharedDir + path.sep + 'index.ts',
2332
`export * from '${importFrom}';${EOL}`
24-
);
33+
).then(function(r){
34+
var contents = fs.readFileSync(r.path, 'utf8');
35+
36+
contents = sortBarrel(contents);
37+
38+
fs.writeFileSync(r.path, contents, 'utf8');
39+
40+
r.contents = contents;
41+
return r;
42+
});
2543
}
+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
'use strict';
2+
3+
var expect = require('chai').expect;
4+
var path = require('path');
5+
var addBarrelRegistration = require('../../addon/ng2/utilities/barrel-management');
6+
var mockFs = require('mock-fs');
7+
var existsSync = require('exists-sync');
8+
var EOL = require('os').EOL;
9+
10+
var Blueprint = require('ember-cli/lib/models/blueprint');
11+
12+
describe('barrel-management', () => {
13+
var blueprint;
14+
var installationDirectory;
15+
16+
beforeEach(() => {
17+
blueprint = new Blueprint('/');
18+
blueprint.project = {
19+
root: '/'
20+
}
21+
});
22+
23+
describe('when not shared', () => {
24+
25+
26+
beforeEach(() => {
27+
var mockDrive = {
28+
'/src/app/my-component': {}
29+
};
30+
mockFs(mockDrive);
31+
32+
installationDirectory = path.join('src', 'app', 'my-component');
33+
});
34+
35+
afterEach(() => {
36+
mockFs.restore();
37+
});
38+
39+
it('should do nothing', () => {
40+
return addBarrelRegistration(blueprint, installationDirectory).then(() => {
41+
var barrelPath = path.join(installationDirectory, 'index.ts');
42+
expect(existsSync(barrelPath)).to.equal(false);
43+
});
44+
});
45+
});
46+
47+
describe('no pre-existing barrel', () => {
48+
49+
beforeEach(() => {
50+
var mockDrive = {
51+
'/src/app/shared/my-component': {}
52+
};
53+
mockFs(mockDrive);
54+
55+
installationDirectory = path.join('/src/app/shared/my-component');
56+
});
57+
58+
afterEach(() => {
59+
mockFs.restore();
60+
});
61+
62+
it('create barrel from installation dir', () => {
63+
return addBarrelRegistration(blueprint, installationDirectory).then(() => {
64+
var fs = require('fs');
65+
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
66+
expect(existsSync(barrelPath)).to.equal(true);
67+
var contents = fs.readFileSync(barrelPath, 'utf8');
68+
var expectedContents = `export * from './my-component';${EOL}`;
69+
expect(contents).to.equal(expectedContents);
70+
});
71+
});
72+
73+
it('create barrel from installation dir with file name', () => {
74+
return addBarrelRegistration(blueprint, installationDirectory, 'my-smaller-component').then(() => {
75+
var fs = require('fs');
76+
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
77+
expect(existsSync(barrelPath)).to.equal(true);
78+
var contents = fs.readFileSync(barrelPath, 'utf8');
79+
var expectedContents = `export * from './my-component/my-smaller-component';${EOL}`;
80+
expect(contents).to.equal(expectedContents);
81+
});
82+
});
83+
84+
});
85+
86+
describe('pre-existing barrel', () => {
87+
88+
beforeEach(() => {
89+
var mockDrive = {
90+
'/src/app/shared': {
91+
'my-component': {},
92+
'index.ts': `export * from './another-component${EOL}export * from './other-component${EOL}`
93+
}
94+
};
95+
mockFs(mockDrive);
96+
97+
installationDirectory = path.join('/src/app/shared/my-component');
98+
});
99+
100+
afterEach(() => {
101+
mockFs.restore();
102+
});
103+
104+
it('update barrel from installation dir', () => {
105+
return addBarrelRegistration(blueprint, installationDirectory).then(() => {
106+
var fs = require('fs');
107+
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
108+
expect(existsSync(barrelPath)).to.equal(true);
109+
var contents = fs.readFileSync(barrelPath, 'utf8');
110+
var expectedContents = `export * from './another-component${EOL}export * from './my-component';${EOL}export * from './other-component${EOL}`;
111+
expect(contents).to.equal(expectedContents);
112+
});
113+
});
114+
115+
it('updateA barrel from installation dir with file name', () => {
116+
return addBarrelRegistration(blueprint, installationDirectory, 'my-smaller-component').then(() => {
117+
var fs = require('fs');
118+
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
119+
expect(existsSync(barrelPath)).to.equal(true);
120+
var contents = fs.readFileSync(barrelPath, 'utf8');
121+
var expectedContents = `export * from './another-component${EOL}export * from './my-component/my-smaller-component';${EOL}export * from './other-component${EOL}`;
122+
expect(contents).to.equal(expectedContents);
123+
});
124+
});
125+
126+
});
127+
128+
describe('pre-existing barrel with export already defined', () => {
129+
130+
beforeEach(() => {
131+
var mockDrive = {
132+
'/src/app/shared': {
133+
'my-component': {},
134+
'index.ts': `export * from './other-component${EOL}export * from './my-component';${EOL}export * from './another-component${EOL}export * from './my-component/my-smaller-component';${EOL}`
135+
}
136+
};
137+
mockFs(mockDrive);
138+
139+
installationDirectory = path.join('/src/app/shared/my-component');
140+
});
141+
142+
afterEach(() => {
143+
mockFs.restore();
144+
});
145+
146+
it('update barrel from installation dir should add nothing', () => {
147+
return addBarrelRegistration(blueprint, installationDirectory).then(() => {
148+
var fs = require('fs');
149+
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
150+
expect(existsSync(barrelPath)).to.equal(true);
151+
var contents = fs.readFileSync(barrelPath, 'utf8');
152+
var expectedContents = `export * from './another-component${EOL}export * from './my-component';${EOL}export * from './my-component/my-smaller-component';${EOL}export * from './other-component${EOL}`;
153+
expect(contents).to.equal(expectedContents);
154+
});
155+
});
156+
157+
it('update barrel from installation dir with file name should add nothing', () => {
158+
return addBarrelRegistration(blueprint, installationDirectory, 'my-smaller-component').then(() => {
159+
var fs = require('fs');
160+
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
161+
expect(existsSync(barrelPath)).to.equal(true);
162+
var contents = fs.readFileSync(barrelPath, 'utf8');
163+
var expectedContents = `export * from './another-component${EOL}export * from './my-component';${EOL}export * from './my-component/my-smaller-component';${EOL}export * from './other-component${EOL}`;
164+
expect(contents).to.equal(expectedContents);
165+
});
166+
});
167+
168+
});
169+
});

0 commit comments

Comments
 (0)