Skip to content

Commit

Permalink
fix(importer): search for resources from package dist
Browse files Browse the repository at this point in the history
Previously `au install` and `au import` were looking for resources (css files currently) from the package root, but this should be the package path (the distribution folder of the package) as anything outside of the dist folder is unreachable
  • Loading branch information
JeroenVinke committed Sep 3, 2017
1 parent 238b863 commit 71ad598
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
18 changes: 7 additions & 11 deletions lib/importer/services/resource-inclusion.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ const glob = require('glob');

let ResourceInclusion = class {

static inject() { return [UI, 'package']; }
static inject() { return [UI, 'package', 'project']; }

constructor(ui, pkg) {
constructor(ui, pkg, project) {
this.ui = ui;
this.package = pkg;
this.project = project;
}

getCSS() {
Expand Down Expand Up @@ -57,15 +58,10 @@ let ResourceInclusion = class {
}

getResources(globExpr) {
return this.glob(globExpr, { cwd: this.package.rootPath })
.then(files => files.map(file => {
let directoryFromPath = this.package.path.substring(this.package.path.lastIndexOf('/') + 1);
let directoryFromFile = file.split('/').shift();
if (directoryFromPath === directoryFromFile) {
file = file.substring(file.indexOf('/') + 1);
}
return path.posix.join(file);
}));
const distPath = path.resolve(process.cwd(), this.project.model.paths.root, this.package.path);

return this.glob(globExpr, { cwd: distPath })
.then(files => files.map(file => path.posix.join(file)));
}

glob(globExpr, options) {
Expand Down
31 changes: 16 additions & 15 deletions spec/lib/importer/services/resource-inclusion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('The ResourceInclusion module', () => {
let sut;
let container;
let pkg;
let project;

beforeEach(() => {
mockfs = require('mock-fs');
Expand All @@ -19,7 +20,16 @@ describe('The ResourceInclusion module', () => {
path: '../node_modules/some-package'
};

project = {
model: {
paths: {
root: 'src'
}
}
};

container.registerInstance('package', pkg);
container.registerInstance('project', project);
container.registerAlias(FakeUI, UI);

ResourceInclusion = require('../../../../lib/importer/services/resource-inclusion');
Expand All @@ -31,7 +41,7 @@ describe('The ResourceInclusion module', () => {
'some-package': {
dist: {
'test.css': 'body { background-color: red }',
output: {
css: {
'test.css': 'body { background-color: red }'
}
}
Expand All @@ -41,30 +51,21 @@ describe('The ResourceInclusion module', () => {
});

describe('getResources', () => {
it('returns resource paths from package root', (done) => {
sut.getResources('?(dist|build|lib|css|style|styles)/*.css')
.then(result => {
expect(result[0]).toBe('dist/test.css');
done();
})
.catch(e => done.fail());
});

it('removes duplicate dist', (done) => {
it('returns resources directly under package path (dist folder)', (done) => {
pkg.path = '../node_modules/some-package/dist';
sut.getResources('?(dist|build|lib|css|style|styles)/*.css')
sut.getResources('*.css')
.then(result => {
expect(result[0]).toBe('test.css');
done();
})
.catch(e => done.fail());
});

it('removes duplicate dist (deeper folder structure)', (done) => {
pkg.path = '../node_modules/some-package/dist/output';
it('supports deeper folder structure', (done) => {
pkg.path = '../node_modules/some-package/dist';
sut.getResources('?(dist|build|lib|css|style|styles)/*.css')
.then(result => {
expect(result[0]).toBe('test.css');
expect(result[0]).toBe('css/test.css');
done();
})
.catch(e => done.fail());
Expand Down

0 comments on commit 71ad598

Please sign in to comment.