-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dd2a2e4
Showing
7 changed files
with
389 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_style = space | ||
indent_width = 2 | ||
|
||
[*.js] | ||
indent_style = tab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
/** | ||
* share-cli | ||
* Quickly share files from command line to your local network | ||
* Author: Mario Nebl <https://github.com/marionebl> | ||
* License: MIT | ||
*/ | ||
const fs = require('fs'); | ||
const http = require('http'); | ||
const os = require('os'); | ||
const path = require('path'); | ||
const url = require('url'); | ||
|
||
const copyPaste = require('copy-paste'); | ||
const chalk = require('chalk'); | ||
const fp = require('lodash/fp'); | ||
const meow = require('meow'); | ||
const mime = require('mime'); | ||
const portscanner = require('portscanner'); | ||
const generate = require('project-name-generator'); | ||
|
||
const cli = meow(` | ||
Usage | ||
$ share [file] | ||
Options | ||
-n, --name Forced download name of the file | ||
Examples | ||
$ share shared.png | ||
http://192.168.1.1:1337/unequal-wish | ||
$ cat shared.png | share --name=shared.png | ||
http://192.168.1.1:1337/important-downtown | ||
`, { | ||
alias: { | ||
n: 'name' | ||
} | ||
}); | ||
|
||
let __timer = null; | ||
const stdin = process.stdin; | ||
|
||
// Kill process after 5 minutes of inactivity | ||
function resetTimer() { | ||
killTimer(); | ||
setTimer(); | ||
} | ||
|
||
function setTimer() { | ||
__timer = setTimeout(() => { | ||
process.exit(0); | ||
}, 30000); | ||
} | ||
|
||
function killTimer() { | ||
if (__timer) { | ||
clearTimeout(__timer); | ||
} | ||
} | ||
|
||
/** | ||
* Copy input to clipboard | ||
* @param {String} input | ||
* @return {Promise<String>} | ||
*/ | ||
function copy(input) { | ||
return new Promise((resolve, reject) => { | ||
copyPaste.copy(`${input}\r\n`, (error) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
resolve(input); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Check if an ip adress is external | ||
* | ||
* @return {Boolean} | ||
*/ | ||
function isExternalAddress(networkInterface) { | ||
return networkInterface.family === 'IPv4' && | ||
networkInterface.internal === false; | ||
} | ||
|
||
/** | ||
* Get the local ip addresses | ||
* | ||
* @return {Object[]} | ||
*/ | ||
function getAdresses() { | ||
return fp.flatten(fp.values(os.networkInterfaces())); | ||
} | ||
|
||
/** | ||
* Get the local ip address | ||
* | ||
* @return {String} | ||
*/ | ||
function getLocalAddress() { | ||
const found = fp.find(isExternalAddress)(getAdresses()); | ||
return found ? found.address : 'localhost'; | ||
} | ||
|
||
/** | ||
* Get an open port on localhost | ||
* | ||
* @return {Promise<Number>} | ||
*/ | ||
function getOpenPort() { | ||
return new Promise((resolve, reject) => { | ||
portscanner.findAPortNotInUse(1337, 65535, '127.0.0.1', (error, port) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
resolve(port); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Get a file object | ||
* | ||
* @param {Object} options | ||
* @param {Boolean} options.isStdin - If the input is given via stdin | ||
* @param {String} [options.filePath] - Absolute path of file to read | ||
* @param {String} [options.fileName] - Basename of file to read, defaults to path.basename(option.filePath) | ||
* | ||
* @return {File} | ||
*/ | ||
function getFile(options) { | ||
const stream = options.isStdin ? | ||
stdin : fs.createReadStream(options.filePath); | ||
|
||
const name = options.isStdin ? | ||
options.fileName : path.basename(options.filePath); | ||
|
||
return { | ||
stream, | ||
name | ||
}; | ||
} | ||
|
||
/** | ||
* Serve a File object on address with port on path id | ||
* | ||
* @param {Object} options | ||
* @param {File} options.file | ||
* @param {Number} options.port | ||
* @param {String} options.address | ||
* @param {String} options.id | ||
* @return {Promise<Object>} - started server instance | ||
*/ | ||
function serve(options) { | ||
return new Promise((resolve, reject) => { | ||
const file = options.file; | ||
|
||
const server = http.createServer((request, response) => { | ||
const id = url.parse(request.url).path | ||
.split('/') | ||
.filter(Boolean)[0]; | ||
|
||
if (id === options.id) { | ||
resetTimer(); | ||
const downloadName = file.name || options.id; | ||
response.setHeader('Content-Type', mime.lookup(downloadName)); | ||
response.setHeader('Content-Disposition', `attachment; filename=${downloadName}`); | ||
|
||
file.stream.pipe(response); | ||
|
||
// Kill the process when download completed | ||
file.stream.on('close', () => { | ||
process.exit(0); | ||
}); | ||
} else { | ||
response.writeHead(404); | ||
response.end('Not found'); | ||
} | ||
}); | ||
|
||
server.on('error', reject); | ||
|
||
server.listen(options.port, () => { | ||
resolve(server); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Serve a File object on an open port | ||
* | ||
* @param {File} file | ||
* @return {Promise<String>} - shareable address | ||
*/ | ||
function serveFile(file) { | ||
return getOpenPort() | ||
.then(port => { | ||
const address = getLocalAddress(); | ||
const id = generate().dashed; | ||
const options = { | ||
address, | ||
port, | ||
id, | ||
file | ||
}; | ||
|
||
return serve(options) | ||
.then(() => `http://${options.address}:${options.port}/${options.id}`) | ||
.then(url => copy(`${url}`)); | ||
}); | ||
} | ||
|
||
/** | ||
* Execute share-cli main procedure | ||
* | ||
* @param {String[]} input - non-flag arguments | ||
* @param {Object} args - flag arguments | ||
* @return {Promise<String>} - shareable address | ||
*/ | ||
function main(filePath, args) { | ||
return new Promise((resolve, reject) => { | ||
// Start the inactivity timer | ||
setTimer(); | ||
|
||
// Sanitize input | ||
if (stdin.isTTY && typeof filePath === 'undefined') { | ||
const error = new Error('Either stdin or [file] have to be given'); | ||
error.cli = true; | ||
return reject(error); | ||
} | ||
|
||
const isStdin = stdin.isTTY !== true && typeof filePath === 'undefined'; | ||
|
||
// Get a file object | ||
const file = getFile({ | ||
isStdin, | ||
filePath, | ||
fileName: args.name | ||
}); | ||
|
||
const address = serveFile(file); | ||
resolve(address); | ||
}); | ||
} | ||
|
||
main(cli.input[0], cli.flags) | ||
.then(output => { | ||
if (output) { | ||
console.log(output); | ||
} | ||
}) | ||
.catch(error => { | ||
if (error.cli) { | ||
if (error.message) { | ||
console.error(chalk.red(error.message)); | ||
} | ||
cli.showHelp(1); | ||
} | ||
|
||
setTimeout(() => { | ||
throw error; | ||
}); | ||
}); | ||
|
||
/** | ||
* @typedef {Object} File | ||
* @property {Stream} File.stream - Readstream of the file | ||
* @property {String} File.name - Basename of the file | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Mario Nebl and [contributors](./graphs/contributors) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "share-cli", | ||
"version": "1.0.0", | ||
"description": "Quickly share files from command line to your local network", | ||
"bin": { | ||
"share": "./cli.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/marionebl/share-cli.git" | ||
}, | ||
"keywords": [ | ||
"share", | ||
"cli", | ||
"local" | ||
], | ||
"author": "Mario Nebl <hello@herebecode.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/marionebl/share-cli/issues" | ||
}, | ||
"homepage": "https://github.com/marionebl/share-cli#readme", | ||
"dependencies": { | ||
"chalk": "^1.1.3", | ||
"copy-paste": "^1.3.0", | ||
"external-ip": "^0.2.4", | ||
"lodash": "^4.13.1", | ||
"meow": "^3.7.0", | ||
"mime": "^1.3.4", | ||
"portscanner": "^1.0.0", | ||
"project-name-generator": "^2.1.2" | ||
}, | ||
"devDependencies": { | ||
"xo": "^0.16.0" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
> Quickly share files from command line to your local network | ||
![share-cli Demo](./demo.gif) | ||
|
||
# share-cli | ||
|
||
* :rocket: Dead Simple | ||
* :sparkles: Just works | ||
* :lock: Local network only | ||
|
||
share-cli exposes single files to your LAN via http, either from stdin or a given file. | ||
The exposing address is copied to your clipboard automatically. | ||
|
||
Afer a complete download or 5 minutes of inactivity share-cli closes automatically. | ||
|
||
## Installation | ||
|
||
``` | ||
npm install -g share-cli | ||
``` | ||
|
||
## Usage | ||
|
||
``` | ||
❯ share --help | ||
Quickly share files from command line to your local network | ||
Usage | ||
$ share [file] | ||
Options | ||
-n, --name Forced download name of the file | ||
Examples | ||
$ share shared.png | ||
http://192.168.1.1:1337/unequal-wish | ||
$ cat shared.png | share --name=shared.png | ||
http://192.168.1.1:1338/important-downtown | ||
``` | ||
|
||
--- | ||
share-cli is built by [Mario Nebl](https://github.com/marionebl) and released | ||
under the [MIT](./license.md) license. |