Skip to content

Instantly share code, notes, and snippets.

@mattpocock
Created May 30, 2019 13:07
Show Gist options
  • Save mattpocock/5fd0bf5654c051e6d0d4f17f071f478d to your computer and use it in GitHub Desktop.
Save mattpocock/5fd0bf5654c051e6d0d4f17f071f478d to your computer and use it in GitHub Desktop.
const file = require('file');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const getSchema = require('./getSchema');
const generateMocks = require('./generateMocks');
const typesDir = path.resolve('../JlrLmsUI/app/types/api');
execSync(`rm -rf ${typesDir}`);
fs.mkdirSync(typesDir);
file.walkSync(path.resolve(__dirname, '../contracts'), (dir, dirs, files) => {
files.forEach((fileName) => {
if (`${fileName}`.includes('.graphql')) {
const schema = getSchema(path.resolve(dir, fileName));
const mocks = generateMocks(schema.definitions);
const callName = `${path
.relative(
path.resolve(__dirname, '../contracts'),
path.resolve(__dirname, '../contracts', dir, fileName),
)
.replace(/\//g, '')
.replace(/\.graphql/g, '')}`;
if (mocks.Response && mocks.Response.Data) {
execSync(
`echo '${JSON.stringify(mocks.Response.Data).replace(
/'/g,
'',
)}' | quicktype -o ${typesDir}/${callName}Response.ts --just-types --all-properties-optional`,
(result, err) => {
if (err) {
console.log(err);
throw new Error(err);
}
},
);
}
if (mocks.Request) {
execSync(
`echo '${JSON.stringify(mocks.Request).replace(
/'/g,
'',
)}' | quicktype -o ${typesDir}/${callName}Request.ts --just-types --all-properties-optional`,
(result, err) => {
if (err) {
console.log(err);
throw new Error(err);
}
},
);
}
console.log(`${callName} complete`);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment