Skip to content

Commit

Permalink
Make script manager paths relative to template root. Fixes 185 (#186)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Roberts <[email protected]>
  • Loading branch information
mttrbrts authored Aug 9, 2018
1 parent b30d9b4 commit 2c1a46b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
22 changes: 13 additions & 9 deletions packages/cicero-core/lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,10 +959,10 @@ class Template {
acceptsDir: function (dir) {
return !isFileInNodeModuleDir(dir, path);
},
process: function (path, contents) {
let filePath = fsPath.parse(path);
if (filePath.ext.toLowerCase() === '.ergo') {
logger.debug(method, 'Compiling Ergo to JavaScript ', path);
process: function (filePath, contents) {
let pathObj = fsPath.parse(filePath);
if (pathObj.ext.toLowerCase() === '.ergo') {
logger.debug(method, 'Compiling Ergo to JavaScript ', filePath);
// re-get the updated modelfiles from the modelmanager (includes external dependencies)
if (template.getMetadata().getLanguage() === 1) {
logger.warn('Template is declared as javascript, but this is an ergo template');
Expand All @@ -974,24 +974,28 @@ class Template {

const compiled = Ergo.compileToJavaScriptAndLink([contents],newModelFiles,'javascript_cicero');
if (compiled.hasOwnProperty('error')) {
throw new Error('In: ' + path + ' [' + Ergo.ergoErrorToString(compiled.error) + ']');
throw new Error('In: ' + filePath + ' [' + Ergo.ergoErrorToString(compiled.error) + ']');
} else {
contents = compiled.success;
}
logger.debug('Compiled Ergo to Javascript:\n'+contents+'\n');
path = path.substr(0, path.lastIndexOf('.')) + '.js';
filePath.ext = '.js';
filePath = filePath.substr(0, filePath.lastIndexOf('.')) + '.js';
pathObj.ext = '.js';
if(foundJs) {
throw new Error('Templates cannot mix Ergo and JS logic');
}
foundErgo = true;
} else if (filePath.ext.toLowerCase() === '.js') {
} else if (pathObj.ext.toLowerCase() === '.js') {
if(foundErgo) {
throw new Error('Templates cannot mix Ergo and JS logic');
}
foundJs = true;
}
const jsScript = template.getScriptManager().createScript(path, filePath.ext.toLowerCase(), contents);
// Make paths for the script manager relative to the root folder of the template
const resolvedPath = fsPath.resolve(path);
const resolvedFilePath = fsPath.resolve(filePath);
const truncatedPath = resolvedFilePath.replace(resolvedPath+'/', '');
const jsScript = template.getScriptManager().createScript(truncatedPath, pathObj.ext.toLowerCase(), contents);
scriptFiles.push(jsScript);
logger.debug(method, 'Found script file ', path);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/cicero-core/test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('Template', () => {
template.getDescription().should.equal('Late Delivery and Penalty. In case of delayed delivery except for Force Majeure cases, the Seller shall pay to the Buyer for every 9 DAY of delay penalty amounting to 7% of the total value of the Equipment whose delivery has been delayed. Any fractional part of a DAY is to be considered a full DAY. The total amount of penalty shall not however, exceed 2% of the total value of the Equipment involved in late delivery. If the delay is more than 2 WEEK, the Buyer is entitled to terminate this Contract.');
template.getVersion().should.equal('0.0.1');
template.getMetadata().getSample().should.equal('Late Delivery and Penalty. In case of delayed delivery except for Force Majeure cases, the Seller shall pay to the Buyer for every 9 days of delay penalty amounting to 7% of the total value of the Equipment whose delivery has been delayed. Any fractional part of a days is to be considered a full days. The total amount of penalty shall not however, exceed 2% of the total value of the Equipment involved in late delivery. If the delay is more than 2 weeks, the Buyer is entitled to terminate this Contract.');
template.getHash().should.equal('5b221557eb1db6b88c9d0662c5f25c0b8eeff085c1e98165c39e00e9683898bf');
const buffer = await template.toArchive();
buffer.should.not.be.null;
const template2 = await Template.fromArchive(buffer);
Expand All @@ -106,6 +107,7 @@ describe('Template', () => {
template2.getScriptManager().getScripts().length.should.equal(template.getScriptManager().getScripts().length);
template2.getMetadata().getREADME().should.equal(template.getMetadata().getREADME());
template2.getMetadata().getSamples().should.eql(template.getMetadata().getSamples());
template2.getHash().should.equal(template.getHash());
const buffer2 = await template2.toArchive();
buffer2.should.not.be.null;
});
Expand Down Expand Up @@ -352,7 +354,7 @@ describe('Template', () => {
describe('#getHash', () => {
it('should return a SHA-256 hash', async () => {
const template = await Template.fromDirectory('./test/data/latedeliveryandpenalty');
template.getHash().should.equal('04fb59a60a81b940a4afc1eeff93d05718114667d44622c78d3b5458598e248e');
template.getHash().should.equal('5b221557eb1db6b88c9d0662c5f25c0b8eeff085c1e98165c39e00e9683898bf');
});
});

Expand Down

0 comments on commit 2c1a46b

Please sign in to comment.