Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

zapier convert: Use Prettier to format generated code #202

Merged
merged 2 commits into from
Dec 13, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"minimatch": "3.0.3",
"node-fetch": "1.7.1",
"normalize-path": "2.0.1",
"prettier": "1.9.1",
"read": "1.0.7",
"readable-stream": "2.2.2",
"semver": "5.3.0",
Expand Down
42 changes: 16 additions & 26 deletions src/tests/utils/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,10 @@ describe('convert render functions', () => {
return convert.getHeader(wbDef)
.then(string => {
string.should.eql(`const maybeIncludeAuth = (request, z, bundle) => {

request.headers['Authorization'] = bundle.authData['api_key'];

return request;
};

`);
});
});
Expand Down Expand Up @@ -239,12 +237,10 @@ describe('convert render functions', () => {
return convert.getHeader(wbDef)
.then(string => {
string.should.eql(`const maybeIncludeAuth = (request, z, bundle) => {

request.params['api_key'] = bundle.authData['api_key'];

return request;
};

`);
});
});
Expand Down Expand Up @@ -282,7 +278,6 @@ describe('convert render functions', () => {
return convert.getHeader(wbDef)
.then(string => {
string.should.eql(`const maybeIncludeAuth = (request, z, bundle) => {

request.headers['X-Token'] = bundle.authData.sessionKey;

return request;
Expand All @@ -304,24 +299,23 @@ const getSessionKey = (z, bundle) => {
const getSessionEvent = {
name: 'auth.session'
};
return legacyScriptingRunner.runEvent(getSessionEvent, z, bundle)
.then((getSessionResult) => {
// IMPORTANT NOTE:
// WB apps in scripting's get_session_info() allowed you to return any object and that would
// be added to the authData, but CLI apps require you to specifically define those.
// That means that if you return more than one key from your scripting's get_session_info(),
// you might need to manually tweak this method to return that value at the end of this method,
// and also add more fields to the authentication definition.

const resultKeys = Object.keys(getSessionResult);
const firstKeyValue = (getSessionResult && resultKeys.length > 0) ? getSessionResult[resultKeys[0]] : getSessionResult;

return {
sessionKey: firstKeyValue
};
});
return legacyScriptingRunner.runEvent(getSessionEvent, z, bundle).then(getSessionResult => {
// IMPORTANT NOTE:
// WB apps in scripting's get_session_info() allowed you to return any object and that would
// be added to the authData, but CLI apps require you to specifically define those.
// That means that if you return more than one key from your scripting's get_session_info(),
// you might need to manually tweak this method to return that value at the end of this method,
// and also add more fields to the authentication definition.

const resultKeys = Object.keys(getSessionResult);
const firstKeyValue =
getSessionResult && resultKeys.length > 0 ? getSessionResult[resultKeys[0]] : getSessionResult;

return {
sessionKey: firstKeyValue
};
});
};

`);
});
});
Expand Down Expand Up @@ -372,12 +366,10 @@ const getSessionKey = (z, bundle) => {
return convert.getHeader(wbDef)
.then(string => {
string.should.eql(`const maybeIncludeAuth = (request, z, bundle) => {

request.headers.Authorization = \`Bearer \${bundle.authData.access_token}\`;

return request;
};

`);
});
});
Expand Down Expand Up @@ -494,12 +486,10 @@ const getSessionKey = (z, bundle) => {
return convert.getHeader(wbDef)
.then(string => {
string.should.eql(`const maybeIncludeAuth = (request, z, bundle) => {

request.headers.Authorization = \`Bearer \${bundle.authData.access_token}\`;

return request;
};

`);
});
});
Expand Down
23 changes: 20 additions & 3 deletions src/utils/convert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const _ = require('lodash');
const path = require('path');
const prettier = require('prettier');
const stripComments = require('strip-comments');
const {camelCase, snakeCase} = require('./misc');
const {readFile, writeFile, ensureDir} = require('./files');
Expand Down Expand Up @@ -67,10 +68,26 @@ const stepDescriptionTemplateMap = {
search: _.template('Finds a <%= lowerNoun %>.')
};

const renderTemplate = (templateFile, templateContext) => {
const renderTemplate = (templateFile, templateContext, prettify = true) => {
return readFile(templateFile)
.then(templateBuf => templateBuf.toString())
.then(template => _.template(template, {interpolate: /<%=([\s\S]+?)%>/g})(templateContext));
.then(template => _.template(template, {interpolate: /<%=([\s\S]+?)%>/g})(templateContext))
.then(content => {
if (prettify) {
const ext = path.extname(templateFile).toLowerCase();
const prettifier = {
'.json': origString => JSON.stringify(JSON.parse(origString), null, 2),
'.js': origString => prettier.format(origString, {
singleQuote: true,
printWidth: 120
})
}[ext];
if (prettifier) {
return prettifier(content);
}
}
return content;
});
};

const createFile = (content, fileName, dir) => {
Expand Down Expand Up @@ -765,7 +782,7 @@ const renderScripting = (legacyApp) => {
templateContext.CODE = templateContext.CODE.replace("'use strict';\n", '').replace('"use strict";\n', '');

const templateFile = path.join(TEMPLATE_DIR, '/scripting.template.js');
return renderTemplate(templateFile, templateContext);
return renderTemplate(templateFile, templateContext, false);
};

const writeScripting = (legacyApp, newAppDir) => {
Expand Down