Skip to content
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

yarn-support #488

Merged
merged 7 commits into from
Jun 17, 2017
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
27 changes: 16 additions & 11 deletions src/utils/require-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ function isValidTemplate(template) {
);
}

module.exports = function(template) {
const getOcTemplate = (path) => {
if (require.cache && !!require.cache[path]) {
delete require.cache[path];
}
return require(path);
};

module.exports = function (template) {
let ocTemplate;
const localTemplate = path.join(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to understand, in case of yarn install, why this doesn't work. The oc-template-jade module must be there if yarn installed everything correctly, if it is not we need to find out where it is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the infinite-loop-loader. It should be in the node_modules.

__dirname,
Expand All @@ -28,23 +35,21 @@ module.exports = function(template) {
const relativeTemplate = path.resolve('.', 'node_modules', template);

try {
if (require.cache && !!require.cache[localTemplate]) {
delete require.cache[localTemplate];
}
ocTemplate = require(localTemplate);
ocTemplate = getOcTemplate(template);
} catch (err) {
try {
if (require.cache && !!require.cache[relativeTemplate]) {
delete require.cache[relativeTemplate];
}
ocTemplate = require(relativeTemplate);
ocTemplate = getOcTemplate(localTemplate);
} catch (err) {
throw format(templateNotFound, template);
try {
ocTemplate = getOcTemplate(relativeTemplate);
} catch (err) {
throw new Error(format(templateNotFound, template));
}
}
}

if (!isValidTemplate(ocTemplate)) {
throw format(templateNotValid, template);
throw new Error(format(templateNotValid, template));
}

return ocTemplate;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/cli-domain-package-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('cli : domain : package-template', () => {
});

it('should show error', () => {
expect(error).to.equal('template.wha compilation failed - Error requiring oc-template: "whazaaa" not found');
expect(error).to.equal('template.wha compilation failed - Error: Error requiring oc-template: "whazaaa" not found');
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/registry-domain-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('registry : domain : repository', () => {
try {
Repository(conf);
} catch (err) {
expect(err).to.equal('Error requiring oc-template: "oc-template-react" not found');
expect(err.message).to.equal('Error requiring oc-template: "oc-template-react" not found');
}
});
});
Expand Down
81 changes: 38 additions & 43 deletions test/unit/utils-require-templates.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,47 @@
'use strict';

const expect = require('chai').expect;
const injectr = require('injectr');
const path = require('path');
const _ = require('lodash');

const requireTemplate = require('../../src/utils/require-template');

describe('utils : require-template', () => {
const globals = {
'__dirname': '.',
};

const deps = {
'path': {
join (a, b, c, template) {
return path.join(a,b,c,template);
},
resolve (a,b,template) {
const dir = path.join(
path.resolve(),
'node_modules/oc-template-handlebars/node_modules',
template
);
return dir;
}
}
};

const requireTemplate = injectr(
'../../src/utils/require-template.js', deps, globals
);

it('should return the template found if its of the correct type', () => {
const template = requireTemplate('oc-template-jade');
const templateAPIs = _.keys(template);

expect(_.includes(templateAPIs,
'getInfo',
'getCompiledTemplate',
'compile',
'render')
).to.be.true;
it('expect type of `requireTemplate` to be function', () => {
expect(typeof requireTemplate).to.equal('function');
});

it('should throw an error if the template found hasn\'t the right format', () => {
try {
requireTemplate('handlebars');
} catch (e) {
expect(e).to.equal('Error requiring oc-template: "handlebars" is not a valid oc-template');
}
describe('valid', () => {
const scenarios = [
{ name: 'oc-template-handlebars' },
{ name: 'oc-template-jade' }
];

scenarios.forEach(scenario => {
it(scenario.name, () => {
const template = requireTemplate(scenario.name);

[
'compile',
'getCompiledTemplate',
'getInfo',
'render'
].forEach(method => {
expect(template).to.have.property(method);
});
});
});
});

describe('not valid', () => {
const scenarios = [{ name: 'lodash' }, { name: 'oc-invalid-template' }];

scenarios.forEach(scenario => {
it(scenario.name, () => {
const sut = () => {
requireTemplate(scenario.name);
};

expect(sut).to.throw();
});
});
});
});