Skip to content

Commit

Permalink
fix(barrel): alphabetized barrel exports
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipgreenii committed May 28, 2016
1 parent 8560d6d commit 67b577d
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 4 deletions.
26 changes: 22 additions & 4 deletions addon/ng2/utilities/barrel-management.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
var path = require('path');
var fs = require('fs');
var EOL = require('os').EOL;

module.exports = addBarrelRegistration;

function sortBarrel(contents) {
var parts = contents.split(EOL).filter(function(l){
return l.trim().length > 0;
});
parts.sort();
return parts.join(EOL) + EOL;
}

function addBarrelRegistration(blueprint, installationDir, fileName) {
var parts = installationDir.split(path.sep);

var idx = parts.lastIndexOf('shared');
if (idx < parts.length -2) {
return Promise.resolve();
}

var sharedDir = parts.slice(0, idx + 1).join(path.sep);
var relativeParts = parts.splice(idx + 1);
if (fileName) {
relativeParts.push(fileName);
}
var importFrom = './' + relativeParts.join('/');

return blueprint.insertIntoFile(
sharedDir + path.sep + 'index.ts',
`export * from '${importFrom}';${EOL}`
);
).then(function(r){
var contents = fs.readFileSync(r.path, 'utf8');

contents = sortBarrel(contents);

fs.writeFileSync(r.path, contents, 'utf8');

r.contents = contents;
return r;
});
}
169 changes: 169 additions & 0 deletions tests/acceptance/barrel-management.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
'use strict';

var expect = require('chai').expect;
var path = require('path');
var addBarrelRegistration = require('../../addon/ng2/utilities/barrel-management');
var mockFs = require('mock-fs');
var existsSync = require('exists-sync');
var EOL = require('os').EOL;

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

describe('barrel-management', () => {
var blueprint;
var installationDirectory;

beforeEach(() => {
blueprint = new Blueprint('/');
blueprint.project = {
root: '/'
}
});

describe('when not shared', () => {


beforeEach(() => {
var mockDrive = {
'/src/app/my-component': {}
};
mockFs(mockDrive);

installationDirectory = path.join('src', 'app', 'my-component');
});

afterEach(() => {
mockFs.restore();
});

it('should do nothing', () => {
return addBarrelRegistration(blueprint, installationDirectory).then(() => {
var barrelPath = path.join(installationDirectory, 'index.ts');
expect(existsSync(barrelPath)).to.equal(false);
});
});
});

describe('no pre-existing barrel', () => {

beforeEach(() => {
var mockDrive = {
'/src/app/shared/my-component': {}
};
mockFs(mockDrive);

installationDirectory = path.join('/src/app/shared/my-component');
});

afterEach(() => {
mockFs.restore();
});

it('create barrel from installation dir', () => {
return addBarrelRegistration(blueprint, installationDirectory).then(() => {
var fs = require('fs');
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
expect(existsSync(barrelPath)).to.equal(true);
var contents = fs.readFileSync(barrelPath, 'utf8');
var expectedContents = `export * from './my-component';${EOL}`;
expect(contents).to.equal(expectedContents);
});
});

it('create barrel from installation dir with file name', () => {
return addBarrelRegistration(blueprint, installationDirectory, 'my-smaller-component').then(() => {
var fs = require('fs');
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
expect(existsSync(barrelPath)).to.equal(true);
var contents = fs.readFileSync(barrelPath, 'utf8');
var expectedContents = `export * from './my-component/my-smaller-component';${EOL}`;
expect(contents).to.equal(expectedContents);
});
});

});

describe('pre-existing barrel', () => {

beforeEach(() => {
var mockDrive = {
'/src/app/shared': {
'my-component': {},
'index.ts': `export * from './another-component${EOL}export * from './other-component${EOL}`
}
};
mockFs(mockDrive);

installationDirectory = path.join('/src/app/shared/my-component');
});

afterEach(() => {
mockFs.restore();
});

it('update barrel from installation dir', () => {
return addBarrelRegistration(blueprint, installationDirectory).then(() => {
var fs = require('fs');
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
expect(existsSync(barrelPath)).to.equal(true);
var contents = fs.readFileSync(barrelPath, 'utf8');
var expectedContents = `export * from './another-component${EOL}export * from './my-component';${EOL}export * from './other-component${EOL}`;
expect(contents).to.equal(expectedContents);
});
});

it('updateA barrel from installation dir with file name', () => {
return addBarrelRegistration(blueprint, installationDirectory, 'my-smaller-component').then(() => {
var fs = require('fs');
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
expect(existsSync(barrelPath)).to.equal(true);
var contents = fs.readFileSync(barrelPath, 'utf8');
var expectedContents = `export * from './another-component${EOL}export * from './my-component/my-smaller-component';${EOL}export * from './other-component${EOL}`;
expect(contents).to.equal(expectedContents);
});
});

});

describe('pre-existing barrel with export already defined', () => {

beforeEach(() => {
var mockDrive = {
'/src/app/shared': {
'my-component': {},
'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}`
}
};
mockFs(mockDrive);

installationDirectory = path.join('/src/app/shared/my-component');
});

afterEach(() => {
mockFs.restore();
});

it('update barrel from installation dir should add nothing', () => {
return addBarrelRegistration(blueprint, installationDirectory).then(() => {
var fs = require('fs');
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
expect(existsSync(barrelPath)).to.equal(true);
var contents = fs.readFileSync(barrelPath, 'utf8');
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}`;
expect(contents).to.equal(expectedContents);
});
});

it('update barrel from installation dir with file name should add nothing', () => {
return addBarrelRegistration(blueprint, installationDirectory, 'my-smaller-component').then(() => {
var fs = require('fs');
var barrelPath = path.join(installationDirectory, '..', 'index.ts');
expect(existsSync(barrelPath)).to.equal(true);
var contents = fs.readFileSync(barrelPath, 'utf8');
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}`;
expect(contents).to.equal(expectedContents);
});
});

});
});

0 comments on commit 67b577d

Please sign in to comment.