Skip to content

Commit

Permalink
fest: automatically pull before making changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Jan 31, 2019
1 parent 15a5344 commit 2bb10f3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
28 changes: 25 additions & 3 deletions src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
validateUnique,
validateUrl,
generateCode,
pull,
commitAndPush,
} = require('../utils')

Expand Down Expand Up @@ -57,10 +58,31 @@ test('generates a random code', () => {
expect(generateCode()).toHaveLength(5)
})

test('pulls', () => {
jest.spyOn(console, 'log').mockImplementation(() => {})

pull('/my/cwd')
expect(spawnSync.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"git",
Array [
"pull",
],
Object {
"cwd": "/my/cwd",
"stdio": "inherit",
},
],
]
`)
console.log.mockRestore()
})

test('commits and pushes', () => {
jest.spyOn(console, 'log').mockImplementation(() => {})

commitAndPush('/foo', 'https://foo.com')
commitAndPush('/foo', 'https://foo.com', '/my/cwd')
expect(spawnSync.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
Expand All @@ -71,7 +93,7 @@ Array [
"/foo -> https://foo.com",
],
Object {
"cwd": undefined,
"cwd": "/my/cwd",
"stdio": "inherit",
},
],
Expand All @@ -81,7 +103,7 @@ Array [
"push",
],
Object {
"cwd": undefined,
"cwd": "/my/cwd",
"stdio": "inherit",
},
],
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const readPkg = require('read-pkg-up')
const {
format,
generateCode,
pull,
commitAndPush,
validateUrl,
validateUnique,
Expand All @@ -21,6 +22,8 @@ const {
const repoRoot = path.dirname(pkgPath)
const redirectPath = path.join(repoRoot, '_redirects')

pull(repoRoot)

const [, , longLink, code] = process.argv
const short = `/${code || generateCode()}`
const contents = fs.readFileSync(redirectPath, 'utf8')
Expand All @@ -33,7 +36,6 @@ if (longLink) {
}

fs.writeFileSync(redirectPath, format(newContents))

commitAndPush(short, longLink, repoRoot)

const link = `${baseUrl}${short}`
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function validateUnique(short, contents) {
}
}

function pull(cwd) {
console.log('pulling the latest changes')
spawnSync('git', ['pull'], {stdio: 'inherit', cwd})
}

function commitAndPush(short, longLink, cwd) {
const message = longLink ? `${short} -> ${longLink}` : 'format links'
console.log(`committing: ${message}`)
Expand Down Expand Up @@ -73,6 +78,7 @@ function generateCode() {
module.exports = {
format,
generateCode,
pull,
commitAndPush,
validateUrl,
validateUnique,
Expand Down

0 comments on commit 2bb10f3

Please sign in to comment.