Skip to content

Commit 26a0cb8

Browse files
fix(barrel): alphabetized barrel exports
Fixes angular#582
1 parent 7783169 commit 26a0cb8

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

addon/ng2/utilities/barrel-management.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
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);
817

@@ -21,5 +30,14 @@ function addBarrelRegistration(blueprint, installationDir, fileName) {
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
}

tests/acceptance/barrel-management.spec.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var EOL = require('os').EOL;
99

1010
var Blueprint = require('ember-cli/lib/models/blueprint');
1111

12-
describe.only('barrel-management', () => {
12+
describe('barrel-management', () => {
1313
var blueprint;
1414
var installationDirectory;
1515

@@ -92,7 +92,7 @@ describe.only('barrel-management', () => {
9292
var mockDrive = {
9393
'/src/app/shared' : {
9494
'my-component': {},
95-
'index.ts': `export * from './other-component${EOL}`
95+
'index.ts': `export * from './another-component${EOL}export * from './other-component${EOL}`
9696
}
9797
};
9898
mockFs(mockDrive);
@@ -110,7 +110,7 @@ describe.only('barrel-management', () => {
110110
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
111111
expect(existsSync(barrelPath)).to.equal(true);
112112
var contents = fs.readFileSync(barrelPath, 'utf8');
113-
var expectedContents = `export * from './other-component${EOL}export * from './my-component';${EOL}`;
113+
var expectedContents = `export * from './another-component${EOL}export * from './my-component';${EOL}export * from './other-component${EOL}`;
114114
expect(contents).to.equal(expectedContents);
115115
});
116116
});
@@ -121,7 +121,7 @@ describe.only('barrel-management', () => {
121121
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
122122
expect(existsSync(barrelPath)).to.equal(true);
123123
var contents = fs.readFileSync(barrelPath, 'utf8');
124-
var expectedContents = `export * from './other-component${EOL}export * from './my-component/my-smaller-component';${EOL}`;
124+
var expectedContents = `export * from './another-component${EOL}export * from './my-component/my-smaller-component';${EOL}export * from './other-component${EOL}`;
125125
expect(contents).to.equal(expectedContents);
126126
});
127127
});
@@ -134,7 +134,7 @@ describe.only('barrel-management', () => {
134134
var mockDrive = {
135135
'/src/app/shared' : {
136136
'my-component': {},
137-
'index.ts': `export * from './other-component${EOL}export * from './my-component';${EOL}export * from './my-component/my-smaller-component';${EOL}`
137+
'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}`
138138
}
139139
};
140140
mockFs(mockDrive);
@@ -146,25 +146,24 @@ describe.only('barrel-management', () => {
146146
mockFs.restore();
147147
});
148148

149-
150-
it('update barrel from installation dir should do nothing', () => {
149+
it('update barrel from installation dir should add nothing', () => {
151150
return addBarrelRegistration(blueprint, installationDirectory).then(() => {
152151
var fs = require('fs');
153152
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
154153
expect(existsSync(barrelPath)).to.equal(true);
155154
var contents = fs.readFileSync(barrelPath, 'utf8');
156-
var expectedContents = `export * from './other-component${EOL}export * from './my-component';${EOL}export * from './my-component/my-smaller-component';${EOL}`;
155+
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}`;
157156
expect(contents).to.equal(expectedContents);
158157
});
159158
});
160159

161-
it('update barrel from installation dir with file name should do nothing', () => {
160+
it('update barrel from installation dir with file name should add nothing', () => {
162161
return addBarrelRegistration(blueprint, installationDirectory, 'my-smaller-component').then(() => {
163162
var fs = require('fs');
164163
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
165164
expect(existsSync(barrelPath)).to.equal(true);
166165
var contents = fs.readFileSync(barrelPath, 'utf8');
167-
var expectedContents = `export * from './other-component${EOL}export * from './my-component';${EOL}export * from './my-component/my-smaller-component';${EOL}`;
166+
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}`;
168167
expect(contents).to.equal(expectedContents);
169168
});
170169
});

0 commit comments

Comments
 (0)