Skip to content

Commit

Permalink
Use proper directories for action files, use javascript formatted obj…
Browse files Browse the repository at this point in the history
…ects instead of JSON and use dynamic dates in sample data
  • Loading branch information
oojacoboo committed Oct 13, 2023
1 parent 33a9a98 commit 83aa5a1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 28 deletions.
91 changes: 63 additions & 28 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require('dotenv').config();

const fs = require('fs');
const path = require('path');
const JSON5 = require('json5');
const inflection = require('inflection');

const { Agent } = require('undici');
Expand Down Expand Up @@ -32,6 +33,50 @@ let config;
let cachedSchema;


/**
* A hacked JSON.stringify that will preserve functions and regexes, etc.
* This isn't perfect for sure and could use improvment, but is sufficient for single layer sample
* data at the moment. It's really nasty, I'm aware.
*
* @param {Object|Array} input
*
* @returns {String}
*/
const stringifyObject = (input, indentation = 0) => {
if (Array.isArray(input)) {
return `[
${' '.repeat(indentation > 2 ? (indentation - 2) : indentation)}${input.map((v) => stringifyObject(v, indentation)).join(`,\n${' '.repeat(indentation > 2 ? (indentation - 2) : indentation)}`)}
${' '.repeat(indentation > 2 ? (indentation - 2) : indentation)}]`;
}

if (typeof input === 'object') {
return `{
${' '.repeat(indentation)}${Object.entries(input).map(([k, v]) => `${k}: ${stringifyObject(v, indentation)}`).join(`,\n${' '.repeat(indentation)}`)}
${' '.repeat(indentation > 2 ? (indentation - 2) : indentation)}}`;
}

if (input === 'CURRENT_TIMESTAMP') {
return `new Date().toISOString()`;
}

if (typeof input === 'string') {
return `${JSON.stringify(input)}`;
}

return `${input}`;
};


/**
* Helper function to indent in every line of a multi-line string.
*
* @param {String} string
*/
const indentString = (string, spaces = 0) => {
return string.replaceAll('\n', `\n${' '.repeat(spaces)}`);
}


/**
* Gets the GraphQL request configuration and performs necessary validation.
*
Expand Down Expand Up @@ -68,16 +113,6 @@ const setConfig = (newConfig) => {
}


/**
* Helper function to indent in every line of a multi-line string.
*
* @param {String} string
*/
const indentString = (string, spaces = 0) => {
return string.replaceAll('\n', `\n${' '.repeat(spaces)}`);
}


/**
* Makes a GraphQL request to the API server.
*
Expand Down Expand Up @@ -688,7 +723,7 @@ const createSamples = (fields) => {
case 'boolean':
return true;
case 'datetime':
return new Date().toISOString();
return 'CURRENT_TIMESTAMP';
default:
throw new Error(`Unable to create sample data for "${field.key}" field`);
};
Expand Down Expand Up @@ -783,11 +818,11 @@ module.exports = {
operation: {
perform,
inputFields: ${indentString(JSON.stringify(inputFields, null, 2), 4)},
inputFields: ${indentString(JSON5.stringify(inputFields, null, 2), 4)},
sample: ${indentString(JSON.stringify(samples, null, 2), 4)},
sample: ${stringifyObject(samples, 6)},
outputFields: ${indentString(JSON.stringify(outputFields, null, 2), 4)},
outputFields: ${indentString(JSON5.stringify(outputFields, null, 2), 4)},
}
};
`;
Expand Down Expand Up @@ -869,11 +904,11 @@ module.exports = {
operation: {
perform,
inputFields: ${indentString(JSON.stringify(inputFields, null, 2), 4)},
inputFields: ${indentString(JSON5.stringify(inputFields, null, 2), 4)},
sample: ${indentString(JSON.stringify(samples, null, 2), 4)},
sample: ${stringifyObject(samples, 6)},
outputFields: ${indentString(JSON.stringify(outputFields, null, 2), 4)},
outputFields: ${indentString(JSON5.stringify(outputFields, null, 2), 4)},
}
};
`;
Expand Down Expand Up @@ -905,10 +940,10 @@ const createActionFile = async (action, operation) => {

const filename = inflection.dasherize(inflection.underscore(definition.name)) + '.js';

console.log(`Creating ${operation} action file: ${action}s/${filename}`);
console.log(`Creating ${operation} action file: ${inflection.pluralize(action)}/${filename}`);

fs.mkdirSync(`${process.cwd()}/${action}s/`, { recursive: true });
const file = `${process.cwd()}/${action}s/${filename}`;
fs.mkdirSync(`${process.cwd()}/${inflection.pluralize(action)}/`, { recursive: true });
const file = `${process.cwd()}/${inflection.pluralize(action)}/${filename}`;
fs.writeFileSync(file, contents);

return { file, contents };
Expand Down Expand Up @@ -938,7 +973,7 @@ const createTestFile = async (action, operation) => {

const filename = inflection.dasherize(inflection.underscore(definition.name)) + '.test.js';

console.log(`Creating ${queryOrMutation} test file: test/${action}s/${filename}`);
console.log(`Creating ${queryOrMutation} test file: test/${inflection.pluralize(action)}/${filename}`);

const inputFields = getInputFieldsFlattened(definition.args);
const samples = createSamples(inputFields);
Expand Down Expand Up @@ -993,19 +1028,19 @@ describe('${definition.name} ${queryOrMutation}', () => {
}
}
let results = await appTester(App.${action}s.${operation}.operation.perform, bundle);
let results = await appTester(App.${inflection.pluralize(action)}.${operation}.operation.perform, bundle);
results = results instanceof Array ? results : [results];
const firstResult = results[0];
const outputFields = App.${action}s.${operation}.operation.outputFields
const outputFields = App.${inflection.pluralize(action)}.${operation}.operation.outputFields
expect(Object.keys(firstResult).length).toBe(outputFields.length);
assertTypes(outputFields, firstResult);
});
});
`;

fs.mkdirSync(`${process.cwd()}/test/${action}s/`, { recursive: true });
const file = `${process.cwd()}/test/${action}s/${filename}`;
fs.mkdirSync(`${process.cwd()}/test/${inflection.pluralize(action)}/`, { recursive: true });
const file = `${process.cwd()}/test/${inflection.pluralize(action)}/${filename}`;
fs.writeFileSync(file, contents);

return { file, contents };
Expand Down Expand Up @@ -1071,7 +1106,7 @@ const addTriggerQuery = async (query) => {
console.log(`Adding triggers "${query}" query to index.js`);
await updateEntryFile(
`./index.js`,
`${query}Query`,
`${query}Trigger`,
`${path.parse(file).dir}/${path.parse(file).name}`,
'trigger',
query,
Expand Down Expand Up @@ -1104,7 +1139,7 @@ const addSearchQuery = async (query) => {
console.log(`Adding searches "${query}" query to index.js`);
await updateEntryFile(
`./index.js`,
`${query}Query`,
`${query}Search`,
`${path.parse(file).dir}/${path.parse(file).name}`,
'search',
query,
Expand Down Expand Up @@ -1132,7 +1167,7 @@ const addCreateMutation = async (mutation) => {
console.log(`Adding create "${mutation}" mutation to index.js`);
await updateEntryFile(
`./index.js`,
`${mutation}Mutation`,
`${mutation}Create`,
`${path.parse(file).dir}/${path.parse(file).name}`,
'create',
mutation,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"graphql": "^16.8.1",
"inflection": "^3.0.0",
"inquirer": "^8.2.6",
"json5": "^2.2.3",
"undici": "5.14.0",
"uuid": "^9.0.1",
"zapier-platform-cli": "15.3.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 83aa5a1

Please sign in to comment.