Skip to content

Instantly share code, notes, and snippets.

@armedi
Last active September 20, 2020 04:23
Show Gist options
  • Save armedi/24453dfaa86bfca36bf1651a36d51a90 to your computer and use it in GitHub Desktop.
Save armedi/24453dfaa86bfca36bf1651a36d51a90 to your computer and use it in GitHub Desktop.
kuy.sh
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const hash = require('string-hash');
let [url, shortUrl] = process.argv.slice(2);
if (!url) {
console.error('Provide a url to shorten!');
process.exit(1);
}
const hasProtocol = !!url.match(/^https?:\/\//);
if (!hasProtocol) {
url = 'https://' + url;
}
shortUrl =
'/' + (shortUrl ? shortUrl.replace(/^\//, '') : hash(url).toString(36));
const line = `${shortUrl.padEnd(20, ' ')}${url}\n`;
fs.writeFileSync(path.resolve(__dirname, '_redirects'), line, { flag: 'a' });
const message = `shorten ${url} to ${shortUrl}`;
childProcess.execSync(`git add . && git commit -m '${message}'`, {
cwd: __dirname,
});
childProcess.execSync('git push -f origin `git rev-parse --abbrev-ref HEAD`', {
cwd: __dirname,
});
console.log(message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment