diff --git a/packages/amplify-codegen/sample-headless-scripts/headless-add-codegen-with-apiId.sh b/packages/amplify-codegen/sample-headless-scripts/headless-add-codegen-with-apiId.sh new file mode 100755 index 000000000..1d9dc8ff8 --- /dev/null +++ b/packages/amplify-codegen/sample-headless-scripts/headless-add-codegen-with-apiId.sh @@ -0,0 +1,17 @@ +#!/bin/bash +set -e +IFS='|' + +CODEGEN="{\ +\"generateCode\":true,\ +\"codeLanguage\":\"typescript\",\ +\"fileNamePattern\":[\"src/graphql/**/*.ts\"],\ +\"generatedFileName\":\"API.ts\",\ +\"generateDocs\":true,\ +\"maxDepth\":\"2\"\ +}" + +echo $CODEGEN | amplify codegen add \ +--headless \ +--apiId xxxxxx \ +--yes \ No newline at end of file diff --git a/packages/amplify-codegen/sample-headless-scripts/headless-add-codegen.sh b/packages/amplify-codegen/sample-headless-scripts/headless-add-codegen.sh new file mode 100755 index 000000000..916a4d691 --- /dev/null +++ b/packages/amplify-codegen/sample-headless-scripts/headless-add-codegen.sh @@ -0,0 +1,16 @@ +#!/bin/bash +set -e +IFS='|' + +CODEGEN="{\ +\"generateCode\":true,\ +\"codeLanguage\":\"typescript\",\ +\"fileNamePattern\":[\"src/graphql/**/*.ts\"],\ +\"generatedFileName\":\"API.ts\",\ +\"generateDocs\":true,\ +\"maxDepth\":\"2\"\ +}" + +echo $CODEGEN | amplify codegen add \ +--headless \ +--yes \ No newline at end of file diff --git a/packages/amplify-codegen/src/amplify-plugin-index.js b/packages/amplify-codegen/src/amplify-plugin-index.js index 863e77533..a4d596d14 100644 --- a/packages/amplify-codegen/src/amplify-plugin-index.js +++ b/packages/amplify-codegen/src/amplify-plugin-index.js @@ -1,4 +1,6 @@ const path = require('path'); +const constants = require('./constants'); +const add = require('./commands/add'); const pluginName = 'codegen'; @@ -14,7 +16,26 @@ async function handleAmplifyEvent(context, args) { context.print.info(`Received event args ${args}`); } +/** + * Entry point for headless commands + * @param {any} context The amplify context object + * @param {string} headlessPayload The serialized payload from the platform + */ +async function executeAmplifyHeadlessCommand(context, headlessPayload) { + switch (context.input.command) { + case 'add': + context.amplify.constructExeInfo(context); + context.exeInfo.inputParams[constants.Label] = JSON.parse(headlessPayload); + const apiId = context.exeInfo.inputParams.apiId || null; + await add(context, apiId); + break; + default: + context.print.error(`Headless mode for ${context.input.command} codegen is not implemented yet`); + } +}; + module.exports = { executeAmplifyCommand, + executeAmplifyHeadlessCommand, handleAmplifyEvent, }; diff --git a/packages/amplify-codegen/src/index.js b/packages/amplify-codegen/src/index.js index 5736f962f..80a0f17d1 100644 --- a/packages/amplify-codegen/src/index.js +++ b/packages/amplify-codegen/src/index.js @@ -9,7 +9,7 @@ const { isCodegenConfigured, switchToSDLSchema } = require('./utils'); const prePushAddGraphQLCodegenHook = require('./callbacks/prePushAddCallback'); const prePushUpdateGraphQLCodegenHook = require('./callbacks/prePushUpdateCallback'); const postPushGraphQLCodegenHook = require('./callbacks/postPushCallback'); -const { executeAmplifyCommand, handleAmplifyEvent } = require('./amplify-plugin-index'); +const { executeAmplifyCommand, executeAmplifyHeadlessCommand, handleAmplifyEvent } = require('./amplify-plugin-index'); module.exports = { configure, @@ -24,6 +24,7 @@ module.exports = { isCodegenConfigured, switchToSDLSchema, executeAmplifyCommand, + executeAmplifyHeadlessCommand, handleAmplifyEvent, generateModels, };