Skip to content

Commit

Permalink
refactor: log function
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Mar 21, 2021
1 parent e50f9ad commit d3cadef
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import { PackageJson } from 'type-fest'

import { l } from '../log'
import { install } from './install'
import { set } from './set_add'

Expand Down Expand Up @@ -29,7 +30,7 @@ export function init(isYarn2: boolean): void {
// Write package.json
const indent = regex.exec(str)?.[0]
fs.writeFileSync('package.json', `${JSON.stringify(pkg, null, indent)}\n`)
console.log('husky - updated package.json')
l('updated package.json')

// Install husky
install()
Expand Down
8 changes: 5 additions & 3 deletions src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import cp from 'child_process'
import fs from 'fs'
import path from 'path'

import { l } from '../log'

export function install(dir = '.husky'): void {
// Ensure that we're inside a git repository
if (cp.spawnSync('git', ['rev-parse']).status !== 0) {
// Do not fail to let projects downloaded as zip files have their dependencies installed
console.log('husky - not a Git repository, skipping hooks installation')
l('not a Git repository, skipping hooks installation')
return
}

Expand Down Expand Up @@ -40,9 +42,9 @@ export function install(dir = '.husky'): void {
throw error
}
} catch (e) {
console.log('husky - Git hooks failed to install')
l('Git hooks failed to install')
throw e
}

console.log('husky - Git hooks installed')
l('Git hooks installed')
}
8 changes: 5 additions & 3 deletions src/commands/set_add.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fs from 'fs'
import path from 'path'

import { l } from '../log'

function data(cmd: string) {
return `#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
Expand All @@ -9,6 +11,7 @@ ${cmd}
`
}

// Show "./file" instead of just "file"
function format(file: string): string {
return `${path.dirname(file)}${path.sep}${path.basename(file)}`
}
Expand All @@ -21,14 +24,13 @@ export function set(file: string, cmd: string): void {

fs.writeFileSync(file, data(cmd), { mode: 0o0755 })

// Show "./file" instead of just "file"
console.log(`husky - created ${format(file)}`)
l(`created ${format(file)}`)
}

export function add(file: string, cmd: string): void {
if (fs.existsSync(file)) {
fs.appendFileSync(file, `${cmd}\n`)
console.log(`husky - updated ${format(file)}`)
l(`updated ${format(file)}`)
} else {
set(file, cmd)
}
Expand Down
3 changes: 3 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function l(msg: string): void {
console.log(`husky - ${msg}`)
}

0 comments on commit d3cadef

Please sign in to comment.