Skip to content

Commit

Permalink
feat: Add the deploy command feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdao committed Jul 12, 2017
1 parent 8a65743 commit 9f553c2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 60 deletions.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ const colors = require('colors')
/*eslint-enable */
const { askSimpleWebAppQuestions } = require('./src/questions')
const { createSimpleWebApp } = require('./src/projectInit')
const deploy = require('./src/deploy')

program
.version('1.0.0')
.command('init [dest]')
.usage('Create a new Google Cloud Function mini web server project.')
.usage('Creates a new Google Cloud Function mini web server project.')
.action(dest => askSimpleWebAppQuestions().then(options => createSimpleWebApp(Object.assign(options, { dest }))))

program
.command('deploy [env]')
.usage('Deploys a Google Cloud Functions projects locally or to a Google Cloud Account.')
.action(env => deploy(env))

/*eslint-disable */
program.parse(process.argv)
/*eslint-enable */
102 changes: 63 additions & 39 deletions src/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,95 @@ const fs = require('fs')
const colors = require('colors')
/*eslint-enable */

module.exports.deploy = env => {
module.exports.deploy = (env = 'default') => {
const startClock = Date.now()

/*eslint-disable */
const webconfigPath = path.join(process.cwd(), 'webconfig.json')
/*eslint-enable */

if (!fs.existsSync(webconfigPath)) {
console.log('Missing webconfig.json file')
console.log(`Missing webconfig.json file. Run ${`webfunc init`.italic.bold} to initialize a new one.`.red)
/*eslint-disable */
process.exit(1)
/*eslint-enable */
}

const webconfig = require(webconfigPath)
const environments = webconfig.env

// CONFIGURE ANY ENVIRONMENT BY ADDING A NEW CONFIG HERE.
const config = !env
? {
GCPproject: '{{GCPproject}}',
GCPbucket: '{{GCPbucket}',
GCFname: '{{GCFname}}',
GCFtrigger: '{{GCFtrigger}}',
GCFmainFunc: '{{GCFmainFunc}}'
}
: env == 'staging'
? {
GCPproject: '{{GCPproject}}',
GCPbucket: '{{GCPbucket}',
GCFname: '{{GCFname}}',
GCFtrigger: '{{GCFtrigger}}',
GCFmainFunc: '{{GCFmainFunc}}'
}
: env == 'prod'
? {
GCPproject: '{{GCPproject}}',
GCPbucket: '{{GCPbucket}',
GCFname: '{{GCFname}}',
GCFtrigger: '{{GCFtrigger}}',
GCFmainFunc: '{{GCFmainFunc}}'
} : (() => {
console.log(`${`Failed to deploy: Environment '${env}' is unknown. Please configure your 'deploy.js' for that environment.`.red}`)
/*eslint-disable */
process.exit(1)
/*eslint-enable */
})()

if (!env) { // Local environment. Make Sure the Google function emulator is running.
if (!environments) {
console.log(`${`webconfig.json`.italic.bold} is missing the ${`env`.italic.bold} property.`.red)
/*eslint-disable */
process.exit(1)
/*eslint-enable */
}

const config = environments[env]

if (!config) {
console.log(`${`webconfig.json`.italic.bold} does not define any ${env.italic.bold} property under its ${`env`.italic.bold} property.`.red)
/*eslint-disable */
process.exit(1)
/*eslint-enable */
}

if (!config.trigger) {
console.log(`${`webconfig.json`.italic.bold} does not define any ${`trigger`.italic.bold} property under its ${env.italic.bold} environment.`.red)
/*eslint-disable */
process.exit(1)
/*eslint-enable */
}

if (!config.entryPoint) {
console.log(`${`webconfig.json`.italic.bold} does not define any ${`entryPoint`.italic.bold} property under its ${env.italic.bold} environment.`.red)
/*eslint-disable */
process.exit(1)
/*eslint-enable */
}

if (env == 'default') { // Local environment. Make Sure the Google function emulator is running.
const emulatorsRunning = shell.exec('ps -ax | grep functions-emulator | wc -l', {silent:true}).stdout * 1

if (emulatorsRunning < 3) {
console.log('No emulator running. Time to start one!'.cyan)
shell.exec('functions start')
}

console.log(`${'LOCALLY'.italic.bold} deploying entry-point ${config.GCFmainFunc.italic.bold} using trigger type ${config.GCFtrigger.italic.bold}`.cyan)
shell.exec(`functions deploy ${config.GCFmainFunc} ${config.GCFtrigger}`)
console.log(`${'LOCALLY'.italic.bold} deploying entry-point ${config.entryPoint.italic.bold} using trigger type ${config.trigger.italic.bold}.`.cyan)
shell.exec(`functions deploy ${config.entryPoint} ${config.trigger}`)
}
else {
console.log(`Deploying entry-point ${config.GCFmainFunc.italic.bold} to ${`GOOGLE CLOUD FUNCTION ${config.GCFname}`.italic.bold} located in project ${config.GCPproject.italic.bold} using trigger type ${config.GCFtrigger.italic.bold}`.cyan)
shell.exec(`gcloud config set project ${config.GCPproject}`)
shell.exec(`gcloud beta functions deploy ${config.GCFname} --stage-bucket ${config.GCPbucket} ${config.GCFtrigger} --entry-point ${config.GCFmainFunc}`)
if (!config.functionName) {
console.log(`${`webconfig.json`.italic.bold} does not define any ${`functionName`.italic.bold} property under its ${env.italic.bold} environment.`.red)
/*eslint-disable */
process.exit(1)
/*eslint-enable */
}

if (!config.googleProject) {
console.log(`${`webconfig.json`.italic.bold} does not define any ${`googleProject`.italic.bold} property under its ${env.italic.bold} environment.`.red)
/*eslint-disable */
process.exit(1)
/*eslint-enable */
}

if (!config.bucket) {
console.log(`${`webconfig.json`.italic.bold} does not define any ${`bucket`.italic.bold} property under its ${env.italic.bold} environment.`.red)
/*eslint-disable */
process.exit(1)
/*eslint-enable */
}

console.log(`Deploying entry-point ${config.entryPoint.italic.bold} to ${`GOOGLE CLOUD FUNCTION ${config.functionName}`.italic.bold} located in project ${config.googleProject.italic.bold} using trigger type ${config.trigger.italic.bold}`.cyan)
shell.exec(`gcloud config set project ${config.googleProject}`)
shell.exec(`gcloud beta functions deploy ${config.functionName} --stage-bucket ${config.bucket} ${config.trigger} --entry-point ${config.entryPoint}`)
}

console.log(`Deployment successful (${(Date.now() - startClock)/1000} sec.)`.green)
/*eslint-disable */
process.exit(1)
/*eslint-enable */
}


Expand Down
2 changes: 1 addition & 1 deletion src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const createDir = (dirname) => {

const copyFolderContent = (src, dest) => {
/*eslint-disable */
return new Promise((onSuccess, onFailure) => ncp(src, dest, onSuccess))
return new Promise((onSuccess, onFailure) => ncp(src, dest, { clobber: false },onSuccess)) // clobber false means do not override existing files
/*eslint-enable */
}

Expand Down
2 changes: 1 addition & 1 deletion templates/simpleWebApp/webconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"Access-Control-Max-Age": "1296000"
},
"env": {
"dev": {
"default": {
"functionName": "{{functionName}}",
"trigger": "{{trigger}}",
"entryPoint": "{{entryPoint}}",
Expand Down
18 changes: 0 additions & 18 deletions webconfig.json

This file was deleted.

0 comments on commit 9f553c2

Please sign in to comment.