| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | const cp = require('child_process'); |
| 9 | const util = require('util'); |
| 10 | |
| 11 | function execHelper(command, options, streamStdout = false) { |
| 12 | return new Promise((resolve, reject) => { |
| 13 | const proc = cp.exec(command, options, (error, stdout) => |
| 14 | error ? reject(error) : resolve(stdout.trim()) |
| 15 | ); |
| 16 | if (streamStdout) { |
| 17 | proc.stdout.pipe(process.stdout); |
| 18 | } |
| 19 | }); |
| 20 | } |
| 21 | |
| 22 | function _spawn(command, args, options, cb) { |
| 23 | const child = cp.spawn(command, args, options); |
| 24 | child.on('close', exitCode => { |
| 25 | cb(null, exitCode); |
| 26 | }); |
| 27 | return child; |
| 28 | } |
| 29 | const spawnHelper = util.promisify(_spawn); |
| 30 | |
| 31 | async function getDateStringForCommit(commit) { |
| 32 | let dateString = await execHelper( |
| 33 | `git show -s --no-show-signature --format=%cd --date=format:%Y%m%d ${commit}` |
| 34 | ); |
| 35 | |
| 36 | // On CI environment, this string is wrapped with quotes '...'s |
| 37 | if (dateString.startsWith("'")) { |
| 38 | dateString = dateString.slice(1, 9); |
| 39 | } |
| 40 | |
| 41 | return dateString; |
| 42 | } |
| 43 | |
| 44 | module.exports = { |
| 45 | execHelper, |
| 46 | spawnHelper, |
| 47 | getDateStringForCommit, |
| 48 | }; |