| 203 | }; |
| 204 | |
| 205 | const tfCapture = async (command, args = [], options = {}) => { |
| 206 | return new Promise((resolve, reject) => { |
| 207 | const stderrCollection = []; |
| 208 | const tfProc = require('child_process').spawn(command, args, options); |
| 209 | tfProc.stdout.on('data', (buf) => { |
| 210 | const parse = (line) => { |
| 211 | if (line === '') return; |
| 212 | try { |
| 213 | const { '@level': level, '@message': message } = JSON.parse(line); |
| 214 | if (level === 'error') { |
| 215 | logger.error(`terraform error: ${message}`); |
| 216 | } else { |
| 217 | logger.info(message); |
| 218 | } |
| 219 | } catch (err) { |
| 220 | logger.info(line); |
| 221 | } |
| 222 | }; |
| 223 | buf.toString('utf8').split('\n').forEach(parse); |
| 224 | }); |
| 225 | tfProc.stderr.on('data', (buf) => { |
| 226 | stderrCollection.push(buf); |
| 227 | }); |
| 228 | tfProc.on('close', (code) => { |
| 229 | if (code !== 0) { |
| 230 | const stderrOutput = Buffer.concat(stderrCollection).toString('utf8'); |
| 231 | reject(stderrOutput); |
| 232 | } |
| 233 | resolve(); |
| 234 | }); |
| 235 | }); |
| 236 | }; |
| 237 | |
| 238 | const fileExists = (path) => |
| 239 | fs.promises.stat(path).then( |