Skip to content

fix(barrel): alphabetized barrel exports #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});

});
});