cli-ext: setup linting and prettification (#4463)

This commit is contained in:
Anuj Shah 2020-05-19 16:31:46 +05:30 committed by GitHub
parent fb175f21e8
commit 3f631ca57e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 3061 additions and 149 deletions

29
cli-ext/.eslintrc Normal file
View File

@ -0,0 +1,29 @@
{
"env": {
"node": true
},
"extends": "airbnb-base",
"plugins": ["promise"],
"rules": {
"no-console": "off",
"consistent-return": "off",
"no-case-declarations": "off",
"no-useless-catch": "off",
"no-unused-vars": "off",
"no-eval": "off",
"no-new": "off",
"max-len": "off",
"no-plusplus": "off",
"import/no-unresolved": "off",
"promise/always-return": "off",
"promise/no-return-wrap": "error",
"promise/param-names": "error",
"promise/catch-or-return": "error",
"promise/no-native": "off",
"promise/no-nesting": "error",
"promise/no-promise-in-callback": "error",
"promise/no-callback-in-promise": "error",
"promise/no-return-in-finally": "error",
"prefer-arrow-callback": "error"
}
}

2847
cli-ext/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,7 +11,21 @@
"build": "rm -rf ./bin/* && pkg ./build/command.js --output ./bin/cli-ext-hasura -t node12-linux-x64,node12-macos-x64,node12-win-x64",
"pretest": "npm run transpile && babel ./tests --out-dir _tmptests",
"posttest": "rm -rf _tmptests",
"test": "node ./_tmptests/index.js"
"test": "node ./_tmptests/index.js",
"lint": "eslint 'src/**/*.js' --fix",
"format": "prettier-eslint $PWD'src/**/*.{js,json}' --write && npm run lint"
},
"lint-staged": {
"**/*.js": [
"eslint --fix",
"prettier-eslint --write",
"git add"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"author": "wawhal",
"license": "ISC",
@ -27,6 +41,13 @@
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/plugin-transform-async-to-generator": "^7.7.4",
"@babel/preset-env": "^7.7.6"
"@babel/preset-env": "^7.7.6",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-promise": "^4.2.1",
"husky": "^4.2.5",
"lint-staged": "^10.1.5",
"prettier-eslint-cli": "^5.0.0"
}
}

View File

@ -1,9 +1,7 @@
import "regenerator-runtime/runtime";
const {
sdl,
actionsCodegen
} = require('./services');
import 'regenerator-runtime/runtime';
const fs = require('fs');
const { sdl, actionsCodegen } = require('./services');
const { getFlagValue, OUTPUT_FILE_FLAG } = require('./utils/commandUtils');
const commandArgs = process.argv;
@ -12,10 +10,12 @@ const outputFilePath = getFlagValue(commandArgs, OUTPUT_FILE_FLAG);
const logOutput = (log) => {
try {
fs.writeFile(outputFilePath, log, 'utf8', () => {
console.log(JSON.stringify({
success: true,
output_file_path: outputFilePath
}));
console.log(
JSON.stringify({
success: true,
output_file_path: outputFilePath,
}),
);
});
} catch (e) {
console.error(`could not write output to "${outputFilePath}"`);
@ -25,7 +25,7 @@ const logOutput = (log) => {
const handleArgs = () => {
const rootArg = commandArgs[2];
switch(rootArg) {
switch (rootArg) {
case 'sdl':
const sdlSubCommands = commandArgs.slice(3);
return sdl(sdlSubCommands);
@ -33,25 +33,26 @@ const handleArgs = () => {
const actionCodegenSubCommands = commandArgs.slice(3);
return actionsCodegen(actionCodegenSubCommands);
default:
return;
}
}
};
try {
let cliResponse = handleArgs();
const cliResponse = handleArgs();
if (cliResponse.error) {
throw Error(cliResponse.error)
throw Error(cliResponse.error);
}
if (cliResponse.constructor.name === 'Promise') {
cliResponse.then(r => {
if (r.error) {
throw Error(r.error)
}
logOutput(JSON.stringify(r));
}).catch(e => {
console.error(e);
process.exit(1);
})
cliResponse
.then((r) => {
if (r.error) {
throw Error(r.error);
}
logOutput(JSON.stringify(r));
})
.catch((e) => {
console.error(e);
process.exit(1);
});
} else {
logOutput(JSON.stringify(cliResponse));
}

View File

@ -1,10 +1,10 @@
const PORT = process.env.PORT || 4000;
const GRAPHQL_ENGINE_REPO_OWNER = process.env.GRAPHQL_ENGINE_REPO_OWNER || 'hasura'
const GRAPHQL_ENGINE_REPO_BRANCH = process.env.GRAPHQL_ENGINE_REPO_BRANCH || 'master'
const GRAPHQL_ENGINE_REPO_OWNER = process.env.GRAPHQL_ENGINE_REPO_OWNER || 'hasura';
const GRAPHQL_ENGINE_REPO_BRANCH = process.env.GRAPHQL_ENGINE_REPO_BRANCH || 'master';
module.exports = {
PORT,
GRAPHQL_ENGINE_REPO_OWNER,
GRAPHQL_ENGINE_REPO_BRANCH
GRAPHQL_ENGINE_REPO_BRANCH,
};

View File

@ -1,27 +1,35 @@
const fetch = require('node-fetch');
const { getTemplatePath } = require('../../utils/utils');
const { parseCustomTypes, getActionTypes } = require('../../shared/utils/hasuraCustomTypeUtils')
const { getFrameworkCodegen } = require('./template');
const { getActionDefinitionSdl, getTypesSdl } = require('../../shared/utils/sdlUtils');
const { parse: sdlParse } = require('graphql/language/parser');
const { getTemplatePath } = require('../../utils/utils');
const {
parseCustomTypes,
getActionTypes,
} = require('../../shared/utils/hasuraCustomTypeUtils');
const { getFrameworkCodegen } = require('./template');
const {
getActionDefinitionSdl,
getTypesSdl,
} = require('../../shared/utils/sdlUtils');
const getActionsCodegen = async (payload) => {
const {
action_name: actionName,
sdl: {
complete: sdlComplete
},
sdl: { complete: sdlComplete },
derive,
codegen_config: codegenConfig
codegen_config: codegenConfig,
} = payload;
try {
const codegenResp = await getFrameworkCodegen(actionName, sdlComplete, derive, codegenConfig)
const codegenResp = await getFrameworkCodegen(
actionName,
sdlComplete,
derive,
codegenConfig,
);
if (codegenResp.error) {
throw Error(codegenResp.error)
throw Error(codegenResp.error);
} else {
return codegenResp.files
return codegenResp.files;
}
} catch (e) {
throw e;
@ -29,6 +37,5 @@ const getActionsCodegen = async (payload) => {
};
module.exports = {
getActionsCodegen
getActionsCodegen,
};

View File

@ -1,4 +1,4 @@
const { getInputPayload } = require('../../utils/commandUtils')
const { getInputPayload } = require('../../utils/commandUtils');
const handler = require('./handler');
const command = (subCommands) => {
@ -7,4 +7,4 @@ const command = (subCommands) => {
return handler(payload);
};
module.exports = command;
module.exports = command;

View File

@ -1,14 +1,12 @@
const { getActionsCodegen } = require('./codegen');
const { getActionsCodegen } = require('./codegen');
const handler = async (payload) => {
try {
const codegen = await getActionsCodegen(payload);
return { codegen };
} catch (e) {
return { error: e.message };
}
}
};
module.exports = handler;

View File

@ -1 +1 @@
module.exports = require('./command');
module.exports = require('./command');

View File

@ -4,19 +4,19 @@ const typescriptPlugin = require('@graphql-codegen/typescript');
const { camelize } = require('inflection');
const fetch = require('node-fetch');
const path = require('path')
const path = require('path');
const fs = require('fs');
const { getTemplatePath } = require('../../utils/utils')
const { getTemplatePath } = require('../../utils/utils');
const CODEGENERATOR_NOT_FOUND = 'given codegen framework not found';
const FILE_SYSTEM_PATH = 'fs_path';
const URL_PATH = 'url path';
const resolveCodegeneratorPath = (codegenConfig) => {
let { framework } = codegenConfig;
const { framework } = codegenConfig;
let codegeneratorPath = codegenConfig.uri;
if (!codegeneratorPath) {
codegeneratorPath = getTemplatePath(framework)
codegeneratorPath = getTemplatePath(framework);
}
return codegeneratorPath;
};
@ -28,7 +28,7 @@ const resolveCodegeneratorFromUrl = async (url) => {
if (fetchResp.status >= 300) {
throw Error(CODEGENERATOR_NOT_FOUND);
}
const codegeneratorText = await fetchResp.text()
const codegeneratorText = await fetchResp.text();
eval(`${codegeneratorText} codegenerator = templater`);
return codegenerator;
} catch (e) {
@ -39,7 +39,9 @@ const resolveCodegeneratorFromUrl = async (url) => {
const resolveCodegeneratorFromFs = async (fsPath) => {
let codegenerator;
try {
const codegeneratorText = fs.readFileSync(path.resolve(fsPath), { encoding: 'utf8'});
const codegeneratorText = fs.readFileSync(path.resolve(fsPath), {
encoding: 'utf8',
});
eval(`${codegeneratorText}\n codegenerator = templater`);
return codegenerator;
} catch (e) {
@ -48,23 +50,22 @@ const resolveCodegeneratorFromFs = async (fsPath) => {
};
const resolveCodegenerator = async (codegenConfig) => {
const codegeneratorPath = resolveCodegeneratorPath(codegenConfig);
if (!codegeneratorPath) {
throw Error(CODEGENERATOR_NOT_FOUND)
throw Error(CODEGENERATOR_NOT_FOUND);
}
let codegenerator
let codegenerator;
let pathType = URL_PATH;
try {
new URL(codegeneratorPath)
new URL(codegeneratorPath);
} catch (_) {
pathType = FILE_SYSTEM_PATH;
}
try {
if (pathType === FILE_SYSTEM_PATH) {
codegenerator = await resolveCodegeneratorFromFs(codegeneratorPath)
codegenerator = await resolveCodegeneratorFromFs(codegeneratorPath);
} else {
codegenerator = await resolveCodegeneratorFromUrl(codegeneratorPath);
}
@ -73,14 +74,17 @@ const resolveCodegenerator = async (codegenConfig) => {
}
return codegenerator;
};
}
const getCodegenFiles = async (actionName, actionsSdl, derive, codegenConfig) => {
const getCodegenFiles = async (
actionName,
actionsSdl,
derive,
codegenConfig,
) => {
let codegenerator;
try {
codegenerator = await resolveCodegenerator(codegenConfig)
codegenerator = await resolveCodegenerator(codegenConfig);
} catch (e) {
throw e;
}
@ -91,24 +95,31 @@ const getCodegenFiles = async (actionName, actionsSdl, derive, codegenConfig) =>
}
return codegenFiles;
};
}
const getFrameworkCodegen = async (actionName, actionsSdl, derive, codegenConfig) => {
const getFrameworkCodegen = async (
actionName,
actionsSdl,
derive,
codegenConfig,
) => {
try {
const codegenFiles = await getCodegenFiles(actionName, actionsSdl, derive, codegenConfig);
const codegenFiles = await getCodegenFiles(
actionName,
actionsSdl,
derive,
codegenConfig,
);
return {
files: codegenFiles
}
files: codegenFiles,
};
} catch (e) {
return {
error: e.message
}
error: e.message,
};
}
};
module.exports = {
getFrameworkCodegen
getFrameworkCodegen,
};

View File

@ -3,5 +3,5 @@ const sdlHandler = require('./sdl');
module.exports = {
actionsCodegen: actionsCodegenHandler,
sdl: sdlHandler
};
sdl: sdlHandler,
};

View File

@ -3,16 +3,15 @@ const fromCommand = require('./from/command');
const command = (subCommands) => {
const rootSubCommand = subCommands[0];
switch(rootSubCommand) {
switch (rootSubCommand) {
case 'to':
const toSubCommands = subCommands.slice(1);
return toCommand(toSubCommands)
return toCommand(toSubCommands);
case 'from':
const fromSubCommands = subCommands.slice(1);
return fromCommand(fromSubCommands);
default:
return;
}
};
module.exports = command;
module.exports = command;

View File

@ -1,5 +1,5 @@
const handler = require('./handler');
const { getInputPayload } = require('../../../utils/commandUtils')
const { getInputPayload } = require('../../../utils/commandUtils');
const command = (subCommands) => {
const rootInput = subCommands[0];
@ -7,4 +7,4 @@ const command = (subCommands) => {
return handler(payload);
};
module.exports = command;
module.exports = command;

View File

@ -1,23 +1,28 @@
const { getAllActionsFromSdl, getAllTypesFromSdl } = require('../../../shared/utils/sdlUtils');
const { reformCustomTypes } = require('../../../shared/utils/hasuraCustomTypeUtils')
const {
getAllActionsFromSdl,
getAllTypesFromSdl,
} = require('../../../shared/utils/sdlUtils');
const {
reformCustomTypes,
} = require('../../../shared/utils/hasuraCustomTypeUtils');
const handlePayload = (payload) => {
const response = {
body: null,
status: 200
status: 200,
};
const { sdl } = payload;
let customTypes, typesParseError;
let customTypes;
let typesParseError;
if (!sdl.complete.trim()) {
response.body = {
actions: [],
types: reformCustomTypes([])
}
return response
types: reformCustomTypes([]),
};
return response;
}
try {
@ -25,17 +30,17 @@ const handlePayload = (payload) => {
} catch (e) {
typesParseError = e;
}
if (typesParseError) {
response.body = {
error: typesParseError.message
error: typesParseError.message,
};
response.status = 400;
return response;
}
let allActions, actionsParseError;
let allActions;
let actionsParseError;
try {
allActions = getAllActionsFromSdl(sdl.complete);
} catch (e) {
@ -44,38 +49,24 @@ const handlePayload = (payload) => {
if (actionsParseError) {
response.body = {
error: actionsParseError.message
error: actionsParseError.message,
};
response.status = 400;
}
response.body = {
actions: allActions,
types: customTypes
types: customTypes,
};
return response;
}
};
const requestHandler = (payload) => {
const {
body, status
} = handlePayload(payload)
const { body, status } = handlePayload(payload);
return body;
};
module.exports = requestHandler;
module.exports.handlePayload = handlePayload;

View File

@ -1 +1 @@
module.exports = require('./command')
module.exports = require('./command');

View File

@ -8,4 +8,4 @@ const command = (subCommands) => {
return response;
};
module.exports = command;
module.exports = command;

View File

@ -1,29 +1,38 @@
const { getTypesSdl, getActionDefinitionSdl } = require('../../../shared/utils/sdlUtils');
const {
getTypesSdl,
getActionDefinitionSdl,
} = require('../../../shared/utils/sdlUtils');
const deriveAction = require('../../../shared/utils/deriveAction').default;
const handlePayload = (payload) => {
const { actions, types, derive } = payload;
const {
operation: toDeriveOperation,
introspection_schema: introspectionSchema,
action_name: actionName
action_name: actionName,
} = derive || {};
const response = {
body: null,
status: 200
status: 200,
};
let actionSdl = '';
let typesSdl = '';
let actionSdlError, typesSdlError, deriveActionError;
let actionSdlError;
let typesSdlError;
let deriveActionError;
if (actions) {
try {
actions.forEach(a => {
actionSdl += getActionDefinitionSdl(a.name, a.definition.type, a.definition.arguments, a.definition.output_type) + '\n';
})
actions.forEach((a) => {
actionSdl += `${getActionDefinitionSdl(
a.name,
a.definition.type,
a.definition.arguments,
a.definition.output_type,
)}\n`;
});
} catch (e) {
actionSdlError = e;
}
@ -41,10 +50,19 @@ const handlePayload = (payload) => {
if (toDeriveOperation) {
try {
const derivation = deriveAction(toDeriveOperation, introspectionSchema, actionName);
const derivedActionSdl = getActionDefinitionSdl(derivation.action.name, derivation.action.type, derivation.action.arguments, derivation.action.output_type);
const derivation = deriveAction(
toDeriveOperation,
introspectionSchema,
actionName,
);
const derivedActionSdl = getActionDefinitionSdl(
derivation.action.name,
derivation.action.type,
derivation.action.arguments,
derivation.action.output_type,
);
const derivedTypesSdl = getTypesSdl(derivation.types);
sdl = `${derivedActionSdl}\n\n${derivedTypesSdl}\n\n${sdl}`
sdl = `${derivedActionSdl}\n\n${derivedTypesSdl}\n\n${sdl}`;
} catch (e) {
deriveActionError = e;
}
@ -52,7 +70,7 @@ const handlePayload = (payload) => {
if (actionSdlError) {
response.body = {
error: 'invalid actions definition'
error: 'invalid actions definition',
};
response.status = 400;
return response;
@ -60,7 +78,7 @@ const handlePayload = (payload) => {
if (deriveActionError) {
response.body = {
error: `could not derive action: ${deriveActionError.message}`
error: `could not derive action: ${deriveActionError.message}`,
};
response.status = 400;
return response;
@ -68,7 +86,7 @@ const handlePayload = (payload) => {
if (typesSdlError) {
response.body = {
error: 'invalid types'
error: 'invalid types',
};
response.status = 400;
return response;
@ -76,23 +94,18 @@ const handlePayload = (payload) => {
response.body = {
sdl: {
complete: sdl
}
complete: sdl,
},
};
return response;
}
};
const requestHandler = (payload) => {
const {
body, status
} = handlePayload(payload)
const { body, status } = handlePayload(payload);
return body;
}
};
module.exports = requestHandler;
module.exports.handlePayload = handlePayload;

View File

@ -4,7 +4,7 @@ const OUTPUT_FILE_FLAG = 'output-file';
const INPUT_FILE_FLAG = 'input-file';
const getFlagValue = (args, flagname) => {
for (var i = args.length - 1; i >= 0; i--) {
for (let i = args.length - 1; i >= 0; i--) {
if (args[i] === `--${flagname}`) {
const flagValue = args[i + 1];
if (!flagValue) {
@ -26,5 +26,5 @@ module.exports = {
getInputPayload,
getFlagValue,
OUTPUT_FILE_FLAG,
INPUT_FILE_FLAG
}
INPUT_FILE_FLAG,
};

View File

@ -1,7 +1,8 @@
const { GRAPHQL_ENGINE_REPO_BRANCH, GRAPHQL_ENGINE_REPO_OWNER } = require("../constants");
const {
GRAPHQL_ENGINE_REPO_BRANCH,
GRAPHQL_ENGINE_REPO_OWNER,
} = require('../constants');
const getTemplatePath = (framework) => {
return `https://raw.githubusercontent.com/hasura/codegen-assets/master/${framework}/actions-codegen.js`
}
const getTemplatePath = (framework) => `https://raw.githubusercontent.com/hasura/codegen-assets/master/${framework}/actions-codegen.js`;
module.exports.getTemplatePath = getTemplatePath;