Commit 172bff9

Anton Medvedev <anton@medv.io>
2023-10-17 10:45:37
Add exec() util
1 parent e49235c
Changed files (2)
src/repo.ts
@@ -1,30 +1,20 @@
 import fs from 'node:fs'
-import { spawnSync } from 'node:child_process'
 import { chdir } from 'node:process'
 import { Badge } from './badges.js'
+import { exec } from './utils.js'
 
 export function gitClone(owner: string, repo: string, token: string) {
-  spawnSync(
-    'git',
-    [
-      'clone',
-      '--depth=1',
-      `https://${owner}:${token}@github.com/${owner}/${repo}.git`,
-      'repo',
-    ],
-    {
-      stdio: 'inherit',
-    },
-  )
+  exec('git', [
+    'clone',
+    '--depth=1',
+    `https://${owner}:${token}@github.com/${owner}/${repo}.git`,
+    'repo',
+  ])
 
   chdir('repo')
 
-  spawnSync('git', ['config', 'user.name', 'My Badges'], { stdio: 'inherit' })
-  spawnSync(
-    'git',
-    ['config', 'user.email', 'my-badges@users.noreply.github.com'],
-    { stdio: 'inherit' },
-  )
+  exec('git', ['config', 'user.name', 'My Badges'])
+  exec('git', ['config', 'user.email', 'my-badges@users.noreply.github.com'])
 
   chdir('..')
 }
@@ -32,9 +22,9 @@ export function gitClone(owner: string, repo: string, token: string) {
 export function gitPush() {
   chdir('repo')
 
-  spawnSync('git', ['add', '.'], { stdio: 'inherit' })
-  spawnSync('git', ['commit', '-m', 'Update badges'], { stdio: 'inherit' })
-  spawnSync('git', ['push'], { stdio: 'inherit' })
+  exec('git', ['add', '.'])
+  exec('git', ['commit', '-m', 'Update badges'])
+  exec('git', ['push'])
 
   chdir('..')
 }
src/utils.ts
@@ -1,8 +1,6 @@
-import fs from 'node:fs/promises'
-import path from 'node:path'
-import { Octokit } from 'octokit'
+import process from 'node:process'
+import { spawnSync } from 'node:child_process'
 import { Commit, Issue, Pull } from './collect/collect.js'
-import { Badge } from './badges.js'
 
 export function linkCommit(commit: Commit): string {
   return `<a href="https://github.com/${commit.repository.owner.login}/${
@@ -34,3 +32,10 @@ export const expectType = <T>(expression: T) => void 0
 export function parseMask(value: string): RegExp {
   return new RegExp(`^${value}$`.replace('*', '.+'))
 }
+
+export function exec(command: string, args: string[]): void {
+  const p = spawnSync(command, args, { stdio: 'inherit' })
+  if (p.status !== 0) {
+    throw new Error(`Command failed: ${command} ${args.join(' ')}`)
+  }
+}