|
| 1 | +import inquirer from 'inquirer'; |
| 2 | +import ObjectId from 'bson-objectid'; |
| 3 | +import { |
| 4 | + AWS_ACCESS_KEY_ID_LENGTH, |
| 5 | + AWS_SECRET_ACCESS_KEY_LENGTH, |
| 6 | +} from './config'; |
| 7 | +import initStarter from './initStarter'; |
| 8 | + |
| 9 | +const validateGraaspDeveloperId = (value) => { |
| 10 | + // allow valid object ids or empty |
| 11 | + if (ObjectId.isValid(value) || value === '') { |
| 12 | + return true; |
| 13 | + } |
| 14 | + return 'Graasp Developer ID is not valid. Leave it empty if you do not have it yet.'; |
| 15 | +}; |
| 16 | + |
| 17 | +const validateGraaspAppId = (value) => { |
| 18 | + // allow valid object ids |
| 19 | + if (ObjectId.isValid(value) || value === '') { |
| 20 | + return true; |
| 21 | + } |
| 22 | + return 'Graasp App ID is not valid. Leave it empty so that we automatically generate one for you.'; |
| 23 | +}; |
| 24 | + |
| 25 | +const validateAwsAccessKeyId = (value) => { |
| 26 | + // allow valid length or empty strings |
| 27 | + if (value.length === AWS_ACCESS_KEY_ID_LENGTH || value === '') { |
| 28 | + return true; |
| 29 | + } |
| 30 | + return 'AWS Access Key ID is not valid. Leave it empty if you do not have it yet.'; |
| 31 | +}; |
| 32 | + |
| 33 | +const validateAwsSecretAccessKey = (value) => { |
| 34 | + // allow valid length or empty strings |
| 35 | + if (value.length === AWS_SECRET_ACCESS_KEY_LENGTH || value === '') { |
| 36 | + return true; |
| 37 | + } |
| 38 | + return 'AWS Secret Access Key is not valid. Leave it empty if you do not have it yet.'; |
| 39 | +}; |
| 40 | + |
| 41 | +const prompt = async (opts) => { |
| 42 | + const { type } = opts; |
| 43 | + |
| 44 | + const answers = await inquirer.prompt([ |
| 45 | + { |
| 46 | + type: 'input', |
| 47 | + name: 'name', |
| 48 | + message: 'Name', |
| 49 | + default: 'My App', |
| 50 | + }, |
| 51 | + { |
| 52 | + type: 'list', |
| 53 | + message: 'Type', |
| 54 | + name: 'type', |
| 55 | + choices: [ |
| 56 | + { |
| 57 | + name: 'App', |
| 58 | + checked: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'Lab', |
| 62 | + }, |
| 63 | + ], |
| 64 | + filter: val => val.toLowerCase(), |
| 65 | + when: () => Boolean(!type), |
| 66 | + }, |
| 67 | + { |
| 68 | + type: 'list', |
| 69 | + message: 'Framework', |
| 70 | + name: 'framework', |
| 71 | + choices: [ |
| 72 | + { |
| 73 | + name: 'React', |
| 74 | + checked: true, |
| 75 | + }, |
| 76 | + ], |
| 77 | + filter: val => val.toLowerCase(), |
| 78 | + }, |
| 79 | + { |
| 80 | + type: 'confirm', |
| 81 | + name: 'api', |
| 82 | + message: 'Use Graasp API', |
| 83 | + default: true, |
| 84 | + }, |
| 85 | + { |
| 86 | + type: 'confirm', |
| 87 | + name: 'ecosystem', |
| 88 | + message: 'Deploy to Graasp Ecosystem', |
| 89 | + default: true, |
| 90 | + }, |
| 91 | + { |
| 92 | + type: 'input', |
| 93 | + name: 'graaspDeveloperId', |
| 94 | + message: 'Graasp Developer ID', |
| 95 | + when: responses => Boolean(responses.ecosystem), |
| 96 | + validate: validateGraaspDeveloperId, |
| 97 | + }, |
| 98 | + { |
| 99 | + type: 'input', |
| 100 | + name: 'graaspAppId', |
| 101 | + message: 'Graasp App ID', |
| 102 | + default: () => ObjectId().str, |
| 103 | + when: responses => Boolean(responses.ecosystem), |
| 104 | + validate: validateGraaspAppId, |
| 105 | + }, |
| 106 | + { |
| 107 | + type: 'input', |
| 108 | + name: 'awsAccessKeyId', |
| 109 | + message: 'AWS Access Key ID', |
| 110 | + when: responses => Boolean(responses.ecosystem), |
| 111 | + validate: validateAwsAccessKeyId, |
| 112 | + }, |
| 113 | + { |
| 114 | + type: 'password', |
| 115 | + name: 'awsSecretAccessKey', |
| 116 | + message: 'AWS Secret Access Key', |
| 117 | + mask: '*', |
| 118 | + when: responses => Boolean(responses.ecosystem), |
| 119 | + validate: validateAwsSecretAccessKey, |
| 120 | + }, |
| 121 | + ]); |
| 122 | + |
| 123 | + // default to random graasp app id |
| 124 | + if (answers.graaspAppId === '') { |
| 125 | + answers.graaspAppId = ObjectId().str; |
| 126 | + } |
| 127 | + |
| 128 | + const config = { |
| 129 | + ...answers, |
| 130 | + ...opts, |
| 131 | + }; |
| 132 | + return initStarter(config); |
| 133 | +}; |
| 134 | + |
| 135 | +export default prompt; |
0 commit comments