diff --git a/.eslintrc b/.eslintrc index 0c89313..91b2eaa 100644 --- a/.eslintrc +++ b/.eslintrc @@ -12,7 +12,6 @@ "allow": ["_id"] } ], - "no-console": "off", "import/no-named-as-default": "off" } } diff --git a/src/config.js b/src/config.js index 89bb429..9e2d689 100644 --- a/src/config.js +++ b/src/config.js @@ -18,4 +18,3 @@ export const GRAASP_IGNORE_FILE = '.graaspignore'; // deploy settings export const DEFAULT_BUILD_DIR = './build'; export const DEFAULT_APP_VERSION = 'latest'; -export const DEFAULT_ENV = '.env.dev'; diff --git a/src/createCli.js b/src/createCli.js index 2a8389d..fcca0d4 100644 --- a/src/createCli.js +++ b/src/createCli.js @@ -68,13 +68,13 @@ const createCli = (argv) => { .option('e', { alias: 'env', type: 'string', - describe: 'Environment used to load variables from', + describe: 'Environment file used to load variables from', }) .option('b', { alias: 'build', type: 'string', default: DEFAULT_BUILD_DIR, - describe: 'Path to the build directory that is deployed', + describe: 'Path to the build directory that will be deployed', }), handler: deploy, }) diff --git a/src/deploy.js b/src/deploy.js index d10e366..b5b3dc6 100644 --- a/src/deploy.js +++ b/src/deploy.js @@ -4,6 +4,7 @@ import dotenv from 'dotenv'; import fs from 'fs'; import cliProgress from 'cli-progress'; import _ from 'lodash'; +// import { promisify } from './utils'; const validateTag = (tag) => { // Both compilation hints because of backslashes used in RegExp but unecessary by conception in JS Strings @@ -12,28 +13,28 @@ const validateTag = (tag) => { // eslint-disable-next-line no-useless-escape const pattern = new RegExp('^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-[0-9A-Za-z]*)?$') if (tag === 'latest' || pattern.test(tag)) { - console.log(`info: validated tag ${tag}`); + console.info(`validated tag ${tag}`); return true; } - console.error(`error: unable to validate version '${tag}'`); + console.error(`unable to validate version '${tag}'`); return false; }; const validateEnv = (env) => { if (fs.existsSync(env)) { - console.log(`info: validated environment file ${env}`); + console.info(`validated environment file ${env}`); return true; } - console.log(`error: environment file '${env}' does not exist`); + console.error(`environment file '${env}' does not exist`); return false; }; const validateBuild = (build) => { if (fs.existsSync(build)) { - console.log(`info: validated build directory ${build}`); + console.info(`validated build directory ${build}`); return true; } - console.log(`error: build directory '${build}' does not exist`); + console.error(`build directory '${build}' does not exist`); return false; }; @@ -48,12 +49,9 @@ const validateAppVariables = ({ _.isUndefined(REACT_APP_GRAASP_APP_ID) ) { console.error( - 'error: environment variables REACT_APP_GRAASP_APP_ID, REACT_APP_GRAASP_DEVELOPER_ID and/or REACT_APP_HOST are not defined', + `environment variables REACT_APP_GRAASP_APP_ID, REACT_APP_GRAASP_DEVELOPER_ID and/or REACT_APP_HOST are not defined \n + you can specify them through a .env file in the app root folder or through another file specified with the -e flag`, ); - console.error( - 'error: you can specify them through a .env file in the app root folder', - ); - console.error('error: or through another file specified with the -e flag'); return false; } return true; @@ -70,19 +68,27 @@ const validateAwsCredentialsVariables = ({ _.isUndefined(AWS_SECRET_ACCESS_KEY) ) { console.error( - 'error: environment variables BUCKET, AWS_ACCESS_KEY_ID and/or AWS_SECRET_ACCESS_KEY are not defined', - ); - console.error( - 'error: make sure you setup your credentials file correctly using the scripts/setup.sh script', - ); - console.error( - 'error: and contact your favourite Graasp engineer if you keep running into trouble', + `environment variables BUCKET, AWS_ACCESS_KEY_ID and/or AWS_SECRET_ACCESS_KEY are not defined. \n + make sure you setup your credentials file correctly using the scripts/setup.sh script and contact your favourite Graasp engineer if you keep running into trouble`, ); return false; } return true; }; +const loadAwsCredentials = async () => { + const awsCredentials = new aws.Credentials(); + await awsCredentials.getPromise().then( + async () => true, + (err) => { + console.error(err.stack); + return false; + }, + ); + // set the AWS credentials into the global object + aws.config.credentials = awsCredentials; +}; + const deploy = async (opts) => { const { tag, env, build } = opts; @@ -92,7 +98,7 @@ const deploy = async (opts) => { return false; } - // load environment + // load environment variables dotenv.config({ path: env }); // validate environment variables @@ -112,17 +118,11 @@ const deploy = async (opts) => { DISTRIBUTION, } = process.env; - console.log( + console.info( `publishing app ${REACT_APP_GRAASP_APP_ID} version ${REACT_APP_VERSION}`, ); - // configure the deployment - aws.config.getCredentials((err) => { - if (err) { - // credentials not loaded - console.error(err.stack); - } - }); + loadAwsCredentials(); const APP_PATH = `${REACT_APP_GRAASP_DEVELOPER_ID}/${REACT_APP_GRAASP_APP_ID}/${REACT_APP_VERSION}`; @@ -158,15 +158,15 @@ const deploy = async (opts) => { // e.g. invalidate cache }); - console.log( - `info: published app to https://${REACT_APP_HOST}/${APP_PATH}/index.html`, + console.info( + `published app to https://${REACT_APP_HOST}/${APP_PATH}/index.html`, ); // ensure the correct distribution variables are defined if (_.isUndefined(DISTRIBUTION)) { - console.error('error: environment variable DISTRIBUTION is not defined'); + console.error('environment variable DISTRIBUTION is not defined'); console.error( - 'error: contact your favourite Graasp engineer if you keep running into trouble', + 'contact your favourite Graasp engineer if you keep running into trouble', ); return false; } @@ -190,7 +190,7 @@ const deploy = async (opts) => { console.error(err, err.stack); } else { // successful response - console.log(data); + console.info(data); } }); diff --git a/yarn.lock b/yarn.lock index b381618..6b50296 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3360,21 +3360,16 @@ lodash.templatesettings@^4.0.0: dependencies: lodash._reinterpolate "^3.0.0" -lodash@4.17.15, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15: +lodash@4.17.15: version "4.17.15" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548" integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A== -lodash@4.17.19, lodash@^4.0.0: +lodash@4.17.19, lodash@^4.0.0, lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.2.1: version "4.17.19" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b" integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ== -lodash@^4.17.11, lodash@^4.2.1: - version "4.17.13" - resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.13.tgz#0bdc3a6adc873d2f4e0c4bac285df91b64fc7b93" - integrity sha512-vm3/XWXfWtRua0FkUyEHBZy8kCPjErNBT9fJx8Zvs+U6zjqPbTUOpkaoum3O5uiA8sm+yNMHXfYkTUHFoMxFNA== - loose-envify@^1.0.0: version "1.4.0" resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"