|
| 1 | +/* eslint no-console: 0 */ |
| 2 | + |
| 3 | +const spawn = require('cross-spawn'); |
| 4 | + |
| 5 | +const ariaAtRepo = 'https://github.com/w3c/aria-at.git'; |
| 6 | +const ariaAtDefaultBranch = 'master'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Executes a git command and returns its output. |
| 10 | + * @param {string} args - The git command arguments as a string. |
| 11 | + * @param {string} cwd - The current working directory for the git command. |
| 12 | + * @returns {string} The trimmed output of the git command. |
| 13 | + * @throws {Error} If the git command fails. |
| 14 | + */ |
| 15 | +function gitRun(args, cwd) { |
| 16 | + const gitRunOutput = spawn.sync('git', args.split(' '), { cwd }); |
| 17 | + |
| 18 | + if (gitRunOutput.error) { |
| 19 | + console.info(`'git ${args}' failed with error ${gitRunOutput.error}`); |
| 20 | + process.exit(1); |
| 21 | + } |
| 22 | + |
| 23 | + return gitRunOutput.stdout.toString().trimEnd(); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Clones the aria-at repository to the specified directory. |
| 28 | + * @param {string} gitCloneDirectory - The directory where the repo will be cloned. |
| 29 | + * @throws {Error} If the cloning process fails. |
| 30 | + */ |
| 31 | +function cloneRepo(gitCloneDirectory) { |
| 32 | + console.info('Cloning aria-at repo ...'); |
| 33 | + const cloneOutput = spawn.sync('git', [ |
| 34 | + 'clone', |
| 35 | + ariaAtRepo, |
| 36 | + gitCloneDirectory |
| 37 | + ]); |
| 38 | + |
| 39 | + if (cloneOutput.error) { |
| 40 | + console.info( |
| 41 | + `git clone ${ariaAtRepo} ${gitCloneDirectory} failed with error ${cloneOutput.error}` |
| 42 | + ); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + console.info('Cloning aria-at repo complete.'); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Checks out a specific commit and retrieves its date. |
| 50 | + * @param {string} gitCloneDirectory - The directory of the cloned repo. |
| 51 | + * @param {string|null} commit - The commit to checkout. If null, uses the default branch. |
| 52 | + * @returns {Promise<{gitCommitDate: Date}>} An object containing the commit date. |
| 53 | + */ |
| 54 | +async function readCommit(gitCloneDirectory, commit) { |
| 55 | + gitRun(`checkout ${commit ?? ariaAtDefaultBranch}`, gitCloneDirectory); |
| 56 | + const gitCommitDate = new Date( |
| 57 | + gitRun(`log --format=%aI -n 1`, gitCloneDirectory) |
| 58 | + ); |
| 59 | + |
| 60 | + return { gitCommitDate }; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Reads git information for a specific directory. |
| 65 | + * @param {string} directoryPath - The path to the directory to read git info from. |
| 66 | + * @returns {{gitSha: string, gitMessage: string, gitCommitDate: Date}} An object containing git information. |
| 67 | + */ |
| 68 | +function readDirectoryGitInfo(directoryPath) { |
| 69 | + const gitSha = gitRun(`log -1 --format=%H -- .`, directoryPath); |
| 70 | + const gitMessage = gitRun(`log -1 --format=%s -- .`, directoryPath); |
| 71 | + const gitCommitDate = new Date( |
| 72 | + gitRun(`log -1 --format=%aI -- .`, directoryPath) |
| 73 | + ); |
| 74 | + |
| 75 | + return { gitSha, gitMessage, gitCommitDate }; |
| 76 | +} |
| 77 | + |
| 78 | +module.exports = { |
| 79 | + cloneRepo, |
| 80 | + readCommit, |
| 81 | + readDirectoryGitInfo |
| 82 | +}; |
0 commit comments