Skip to content

Instantly share code, notes, and snippets.

@thyb
Last active June 29, 2017 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thyb/9b53b65c25cd964bbe962d8a9754e31f to your computer and use it in GitHub Desktop.
Save thyb/9b53b65c25cd964bbe962d8a9754e31f to your computer and use it in GitHub Desktop.
Child Process tests to run `gcloud app deploy` with realtime feedback
const which = require("which")
const childProcess = require("child_process")
let bin = `gcloud${/^win/.test(process.platform) ? '.cmd' : ''}`
which(bin, (err, fullpath) => {
if (err) {
return console.log(err)
}
console.log(`${bin} has been detected:\n${fullpath}`)
let proc = childProcess.spawn(fullpath, ['app', 'deploy'], {
stdio: "pipe"
})
proc.stdout.on("data", data => console.log(`stdout: ${data.toString()}`))
proc.stderr.on("data", err => console.log(`stderr: ${err.toString()}`))
proc.on("close", (code) => {
console.log("program terminated with code: ", code)
})
})
/*
It seems, I only have stderr without stdout:
C:\cygwin64\home\thyb\test>node index.js
gcloud.cmd has been detected:
C:\Users\thyb\google-cloud-sdk-159.0.0-windows-x86_64-bundled-python\google-cloud-sdk\bin\gcloud.cmd
stderr: WARNING: Automatic app detection is currently in Beta
*/
const which = require("which")
const childProcess = require("child_process")
const fs = require("fs")
const consts = require('constants');
function readFrom(fd, start) {
let cont = true
let lines = ''
while (cont) {
let chunk = new Buffer(1024)
let len = fs.readSync(fd, chunk, 0, 1024, start)
lines += chunk.toString().substr(0, len)
start += len
if (len < 1024) {
cont = false
}
}
return {
lines: lines,
end: start
}
}
let bin = `gcloud${/^win/.test(process.platform) ? '.cmd' : ''}`
which(bin, (err, fullpath) => {
if (err) {
return console.log(err)
}
fs.writeFileSync("deploy.log", "")
let fd = fs.openSync("deploy.log", consts.O_RDWR)
let pos = 0;
const parseLogs = () => {
let res = readFrom(fd, pos)
pos = res.end
if (res.lines.length) {
console.log(`output: ${res.lines}`)
}
}
console.log(`${bin} has been detected:\n${fullpath}`)
let proc = childProcess.spawn(fullpath, ['app', 'deploy'], {
stdio: ["pipe", fd, fd]
})
let interval = setInterval(() => parseLogs(), 500)
proc.on("close", (code) => {
clearInterval(interval)
fs.closeSync(fd)
let res = readFrom(fd, pos)
pos = res.end
console.log(res.lines + "\n")
console.log("program terminated with code: ", code)
})
})
/**
This workaround was used to workaround the 200kb buffer limit. Works on Mac OS X fully but not on Windows:
C:\cygwin64\home\thyb\test>node index.js
gcloud.cmd has been detected:
C:\Users\thyb\google-cloud-sdk-159.0.0-windows-x86_64-bundled-python\google-cloud-sdk\bin\gcloud.cmd
output: WARNING: Automatic app detection is currently in Beta
*/
const which = require("which")
const childProcess = require("child_process")
let bin = `gcloud${/^win/.test(process.platform) ? '.cmd' : ''}`
which(bin, (err, fullpath) => {
if (err) {
return console.log(err)
}
console.log(`${bin} has been detected:\n${fullpath}`)
let proc = childProcess.spawn(fullpath, ['app', 'deploy'], {
stdio: ["pipe", process.stdout, process.stderr]
})
proc.on("close", (code) => {
console.log("program terminated with code: ", code)
})
})
/*
=> works (display logs in console) but I can't read stdout/stderr in realtime from the application (cf child_process-4-stdout-bis.js)
C:\cygwin64\home\thyb\test>node index.js
gcloud.cmd has been detected:
C:\Users\thyb\google-cloud-sdk-159.0.0-windows-x86_64-bundled-python\google-cloud-sdk\bin\gcloud.cmd
WARNING: Automatic app detection is currently in Beta
Deployment to Google App Engine requires an app.yaml file. This command will run `gcloud beta app gen-config` to generate an app.yaml file for you in the current directory (if the current directory does not contain an App Engine service, please answer "no").
Do you want to continue (Y/n)?
*/
const which = require("which")
const childProcess = require("child_process")
let bin = `gcloud${/^win/.test(process.platform) ? '.cmd' : ''}`
which(bin, (err, fullpath) => {
if (err) {
return console.log(err)
}
console.log(`${bin} has been detected:\n${fullpath}`)
let proc = childProcess.spawn(fullpath, ['app', 'deploy'], {
stdio: ["pipe", process.stdout, process.stderr]
})
process.stdout.on('data', data => console.log(`stdout: ${data}`))
process.stderr.on('data', data => console.log(`stderr: ${data}`))
proc.on("close", (code) => {
console.log("program terminated with code: ", code)
})
})
/**
C:\cygwin64\home\thyb\test>node index.js
gcloud.cmd has been detected:
C:\Users\thyb\google-cloud-sdk-159.0.0-windows-x86_64-bundled-python\google-cloud-sdk\bin\gcloud.cmd
events.js:163
throw er; // Unhandled 'error' event
^
Error: read ENOTCONN
at exports._errnoException (util.js:1033:11)
at WriteStream.Socket._read (net.js:437:21)
at WriteStream.Readable.read (_stream_readable.js:348:10)
at WriteStream.Socket.read (net.js:320:43)
at resume_ (_stream_readable.js:737:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment