Skip to content

Commit

Permalink
fix(onboarding): Resolve onboarding directories as for views.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh committed Mar 21, 2018
1 parent f5e5713 commit 8b760bd
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 0 deletions.
47 changes: 47 additions & 0 deletions plugins/copy-onboarding/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const path = require('path');

var dirs = [];

var pluginRegex = /-(config|plugin)-/;
const resolver = function(pack, projectDir, depth) {
if (pack.directories && pack.directories.onboarding) {
dirs.push({
path: path.join(projectDir, pack.directories.onboarding, '*'),
depth: depth - (pluginRegex.test(pack.name) ? 1 : 0)
});
}

return Promise.resolve();
};

const sort = function(a, b) {
return b.depth - a.depth;
};

const writer = function(thisPackage, dir) {
if (thisPackage.build.type === 'app') {
dirs.sort(sort);
var file = dirs.map(function(item) {
return item.path;
}).join('\n');

var filename = path.join(dir, 'copy-onboarding-args');
console.log('Writing ' + filename);
return fs.writeFileAsync(filename, file);
}

return Promise.resolve();
};

const clear = function() {
dirs = [];
};

module.exports = {
clear: clear,
resolver: resolver,
writer: writer
};
118 changes: 118 additions & 0 deletions test/plugins/copy-onboarding/copy-onboarding.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use strict';

const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const path = require('path');
const expect = require('chai').expect;
const copy = require('../../../plugins/copy-onboarding');
const rimraf = require('rimraf');

describe('copy onboarding resolver', () => {
afterEach(() => {
copy.clear();
rimraf.sync(path.join(outputDir, '*'));
});

var outputDir = path.join(process.cwd(), '.test');

var run = pack => {
return copy.resolver(pack, '.', 0).then(() => {
return copy.writer(pack, outputDir);
});
};

var file = path.join(outputDir, 'copy-onboarding-args');

it('should not pick up files from undefined directories', () => {
var pack = {
name: 'thing',
build: {
type: 'app'
}
};

return run(pack).then(() => {
expect(fs.readFileSync(file, 'utf-8')).to.equal('');
});
});

it('should not pick up files from undefined onboarding', () => {
var pack = {
name: 'thing-plugin-test',
directories: {
},
build: {
type: 'app'
}
};

return run(pack).then(() => {
expect(fs.readFileSync(file, 'utf-8')).to.equal('');
});
});

it('should only write the file for app projects', () => {
var pack = {
name: 'thing',
build: {
type: 'lib'
}
};

return run(pack).then(() => {
expect(fs.existsSync(file)).to.be.false;
});
});

it('should pick up onboarding from directories', () => {
var pack = {
name: 'thing',
directories: {
onboarding: 'foo'
},
build: {
type: 'app'
}
};

return run(pack).then(() => {
expect(fs.readFileSync(file, 'utf-8')).to.equal(path.join('foo', '*'));
});
});

it('should pick up onboarding from multiple projects and sort them properly', () => {
var pack1 = {
name: 'thing-foo',
directories: {
onboarding: 'foo'
},
build: {
type: 'app'
}
};

var pack2 = {
name: 'thing-foo-plugin-bar',
directories: {
onboarding: 'bar'
},
build: {
type: 'app'
}
};

return Promise.join(
copy.resolver(pack1, pack1.name, 0),
copy.resolver(pack2, pack2.name, 1))
.then(() => {
return copy.writer(pack1, outputDir);
})
.then(() => {
var expected = [
path.join(pack1.name, 'foo', '*'),
path.join(pack2.name, 'bar', '*')];

expect(fs.readFileSync(file, 'utf-8')).to.equal(expected.join('\n'));
});
});
});

0 comments on commit 8b760bd

Please sign in to comment.