-
Notifications
You must be signed in to change notification settings - Fork 816
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding amplify cli predictions category (#1936)
- Loading branch information
1 parent
07c6243
commit b7b7c2c
Showing
85 changed files
with
8,228 additions
and
463 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
__tests__ | ||
__mocks__ | ||
coverage | ||
cfn-response.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extends": "airbnb-base", | ||
"rules": { | ||
"no-param-reassign": "off", | ||
"no-return-await" : "off", | ||
"no-continue": "off", | ||
"no-await-in-loop": "off", | ||
"no-use-before-define": "off", | ||
"no-console": "off", | ||
"no-plusplus": "off", | ||
"prefer-destructuring": "off", | ||
"max-len": "off", | ||
"no-loop-func": "off", | ||
"global-require": "off", | ||
"no-inner-declarations": "off", | ||
"import/no-dynamic-require": "off", | ||
"consistent-return": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Amplify CLI Predictions Plugin | ||
|
||
## Commands Summary | ||
|
||
The current set of commands supported by the Amplify Predictions Category Plugin | ||
|
||
| Command | Description | | ||
| --- | --- | | ||
| amplify predictions add | Walks you through a CLI flow to add a predictions resource to your backend | | ||
| amplify predictions update | Walks you through steps in the CLI to update a predictions resource. | | ||
| amplify predictions remove | Removes predictions resource from your local backend which would be removed from the cloud on the next push command | | ||
| amplify predictions console | Opens a web console to view your predictions resources | | ||
|
||
## Prediction Categories | ||
| Type | Description | | ||
| --- | --- | | ||
| Identify | Identify images, labels, and/or entities | | ||
| Convert | Convert text to another language, text to speech and/or speech to text | | ||
| Interpret | Interpretation on text. (Language, Entities, Keyphrases, Sentiment, Syntax) | | ||
| Infer | Infererence on models | |
31 changes: 31 additions & 0 deletions
31
packages/amplify-category-predictions/commands/predictions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const categoryName = 'predictions'; | ||
|
||
module.exports = { | ||
name: categoryName, | ||
alias: ['Predictions'], | ||
run: async (context) => { | ||
const header = `amplify ${categoryName} <subcommand>`; | ||
|
||
const commands = [ | ||
{ | ||
name: 'add', | ||
description: `Takes you through a CLI flow to add a ${categoryName} resource to your local backend`, | ||
}, | ||
{ | ||
name: 'remove', | ||
description: `Removes ${categoryName} resource from your local backend which would be removed from the cloud on the next push command`, | ||
}, | ||
{ | ||
name: 'update', | ||
description: `Takes you through steps in the CLI to update an ${categoryName} resource`, | ||
}, | ||
{ | ||
name: 'console', | ||
description: `Opens a web console to view your ${categoryName} resource`, | ||
}, | ||
]; | ||
|
||
context.amplify.showHelp(header, commands); | ||
context.print.info(''); | ||
}, | ||
}; |
36 changes: 36 additions & 0 deletions
36
packages/amplify-category-predictions/commands/predictions/add.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { promptCategory } from '../../provider-utils/supportedPredictions'; | ||
|
||
const subcommand = 'add'; | ||
const category = 'predictions'; | ||
let options; | ||
|
||
|
||
module.exports = { | ||
name: subcommand, | ||
run: async context => promptCategory() | ||
.then((result) => { | ||
result = result.predictionsCategory; | ||
options = { | ||
providerPlugin: result.provider, | ||
}; | ||
const providerController = require(`../../provider-utils/${result.provider}/index`); | ||
if (!providerController) { | ||
context.print.error('Provider not configured for this category'); | ||
return; | ||
} | ||
return providerController.addResource(context, category, result.fileName, options); | ||
}) | ||
.then((resourceName) => { | ||
const { print } = context; | ||
print.success(`Successfully added resource ${resourceName} locally`); | ||
print.info(''); | ||
print.success('Some next steps:'); | ||
print.info('"amplify push" builds all of your local backend resources and provisions them in the cloud'); | ||
print.info('"amplify publish" builds all of your local backend and front-end resources (if you added hosting category) and provisions them in the cloud'); | ||
print.info(''); | ||
}) | ||
.catch((err) => { | ||
context.print.info(err.stack); | ||
context.print.error('An error occurred when adding the predictions resource'); | ||
}), | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/amplify-category-predictions/commands/predictions/console.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const subcommand = 'console'; | ||
const indexModule = require('../../index'); | ||
|
||
module.exports = { | ||
name: subcommand, | ||
run: async (context) => { | ||
await indexModule.console(context); | ||
}, | ||
}; |
79 changes: 79 additions & 0 deletions
79
packages/amplify-category-predictions/commands/predictions/remove.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
|
||
const subcommand = 'remove'; | ||
const category = 'predictions'; | ||
const storageCategory = 'storage'; | ||
const parametersFileName = 'parameters.json'; | ||
const amplifyMetaFilename = 'amplify-meta.json'; | ||
const s3CloudFormationTemplateFile = 's3-cloudformation-template.json'; | ||
|
||
module.exports = { | ||
name: subcommand, | ||
run: async (context) => { | ||
const { amplify, parameters } = context; | ||
const resourceName = parameters.first; | ||
|
||
return amplify.removeResource(context, category, resourceName) | ||
.then(() => { | ||
const projectDetails = context.amplify.getProjectDetails(); | ||
const projectStorage = projectDetails.amplifyMeta.storage; | ||
if (!projectStorage) { | ||
process.exit(0); | ||
return; | ||
} | ||
const keys = Object.keys(projectStorage); | ||
let s3ResourceName = ''; | ||
keys.forEach((resource) => { | ||
if (projectStorage[resource].service === 'S3') { | ||
s3ResourceName = resource; | ||
} | ||
}); | ||
|
||
if (s3ResourceName === '') { | ||
process.exit(0); | ||
return; | ||
} | ||
|
||
const projectBackendDirPath = context.amplify.pathManager.getBackendDirPath(); | ||
const resourceDirPath = path.join(projectBackendDirPath, storageCategory, s3ResourceName); | ||
const parametersFilePath = path.join(resourceDirPath, parametersFileName); | ||
const bucketParameters = context.amplify.readJsonFile(parametersFilePath); | ||
const adminTriggerFunction = bucketParameters.adminTriggerFunction; | ||
|
||
if (adminTriggerFunction) { | ||
const predictionCtgWalkthroughSrc = `${__dirname}/../../provider-utils/awscloudformation/prediction-category-walkthroughs/identify-walkthrough`; | ||
const { removeS3AdminLambdaTrigger } = require(predictionCtgWalkthroughSrc); | ||
const storageCFNFilePath = path.join(projectBackendDirPath, storageCategory, s3ResourceName, s3CloudFormationTemplateFile); | ||
let storageCFNFile = context.amplify.readJsonFile(storageCFNFilePath); | ||
storageCFNFile = removeS3AdminLambdaTrigger(storageCFNFile, adminTriggerFunction); | ||
delete bucketParameters.adminTriggerFunction; | ||
const amplifyMetaFilePath = path.join(projectBackendDirPath, amplifyMetaFilename); | ||
const amplifyMetaFile = context.amplify.readJsonFile(amplifyMetaFilePath); | ||
const s3DependsOnResources = amplifyMetaFile.storage[s3ResourceName].dependsOn; | ||
const s3Resources = []; | ||
s3DependsOnResources.forEach((resource) => { | ||
if (resource.resourceName !== adminTriggerFunction) { | ||
s3Resources.push(resource); | ||
} | ||
}); | ||
|
||
const jsonString = JSON.stringify(bucketParameters, null, 4); | ||
fs.writeFileSync(parametersFilePath, jsonString, 'utf8'); | ||
|
||
const storageCFNString = JSON.stringify(storageCFNFile, null, 4); | ||
fs.writeFileSync(storageCFNFilePath, storageCFNString, 'utf8'); | ||
context.amplify.updateamplifyMetaAfterResourceUpdate( | ||
storageCategory, | ||
s3ResourceName, | ||
'dependsOn', | ||
s3Resources, | ||
); | ||
} | ||
}) | ||
.catch((err) => { | ||
context.print.info(err.stack); | ||
context.print.error('An error occurred when removing the predictions resource'); | ||
}); | ||
}, | ||
}; |
27 changes: 27 additions & 0 deletions
27
packages/amplify-category-predictions/commands/predictions/update.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { promptCategory } from '../../provider-utils/supportedPredictions'; | ||
|
||
|
||
const subcommand = 'update'; | ||
|
||
module.exports = { | ||
name: subcommand, | ||
alias: ['configure'], | ||
run: async context => promptCategory() | ||
.then((result) => { | ||
result = result.predictionsCategory; | ||
const providerController = require(`../../provider-utils/${result.provider}/index`); | ||
if (!providerController) { | ||
context.print.error('Provider not configured for this category'); | ||
return; | ||
} | ||
return providerController.updateResource(context, result.fileName); | ||
}) | ||
.then((resourceName) => { | ||
context.print.success(`Successfully updated resource ${resourceName} locally`); | ||
}) | ||
.catch((err) => { | ||
context.print.info(err.stack); | ||
context.print.error('An error occurred when updating predictions resource!'); | ||
}), | ||
}; | ||
|
Oops, something went wrong.