Skip to content

Commit

Permalink
fix: make it work better with a bash function
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
Kent C. Dodds committed Dec 12, 2018
1 parent 834d4cc commit d42b4be
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ handles the redirecting for you.

- [Installation](#installation)
- [Usage](#usage)
- [Bash Function](#bash-function)
- [FAQ](#faq)
- [What about analytics?](#what-about-analytics)
- [Can I keep my links private?](#can-i-keep-my-links-private)
Expand Down Expand Up @@ -100,6 +101,18 @@ The `netlify-shortener` does a few things:
Netlify's deploys are normally fast enough that the new URL should be deployed
by the time you've shared it to someone.

## Bash Function

If you want to be able to run this anywhere in the terminal, you can try making
a custom bash function (place this in your `~/.bash_profile` file):

```bash
shorten() { node {path-to-local-repo}/node_modules/.bin/netlify-shortener "$1" "$2"; }
```

> How to do this on windows? I don't know, if you figure it out, please open a
> PR to replace this note with instructions :)
## FAQ

## What about analytics?
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Array [
"/foo -> https://foo.com",
],
Object {
"cwd": undefined,
"stdio": "inherit",
},
],
Expand All @@ -80,6 +81,7 @@ Array [
"push",
],
Object {
"cwd": undefined,
"stdio": "inherit",
},
],
Expand Down Expand Up @@ -112,6 +114,7 @@ Array [
"format links",
],
Object {
"cwd": undefined,
"stdio": "inherit",
},
],
Expand All @@ -121,6 +124,7 @@ Array [
"push",
],
Object {
"cwd": undefined,
"stdio": "inherit",
},
],
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const {
const {
pkg: {baseUrl = 'https://update-baseUrl-in-your-package.json'},
path: pkgPath,
} = readPkg.sync()
} = readPkg.sync({cwd: path.join(__dirname, '../..')})

const redirectPath = path.join(path.dirname(pkgPath), '_redirects')
const repoRoot = path.dirname(pkgPath)
const redirectPath = path.join(repoRoot, '_redirects')

const [, , longLink, code] = process.argv
const short = `/${code || generateCode()}`
Expand All @@ -33,7 +34,7 @@ if (longLink) {

fs.writeFileSync(redirectPath, format(newContents))

commitAndPush(short, longLink)
commitAndPush(short, longLink, repoRoot)

const link = `${baseUrl}${short}`
clipboardy.writeSync(link)
Expand Down
5 changes: 3 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ function validateUnique(short, contents) {
}
}

function commitAndPush(short, longLink) {
function commitAndPush(short, longLink, cwd) {
const message = longLink ? `${short} -> ${longLink}` : 'format links'
console.log(`committing: ${message}`)
spawnSync('git', ['commit', '-am', message], {
stdio: 'inherit',
cwd,
})
console.log('pushing')
spawnSync('git', ['push'], {stdio: 'inherit'})
spawnSync('git', ['push'], {stdio: 'inherit', cwd})
}

function validateUrl(url) {
Expand Down

0 comments on commit d42b4be

Please sign in to comment.