├── .gitignore ├── cli.js ├── index.js ├── package.json ├── readme.md └── whereami /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var currentLocation = require('./index.js') 4 | 5 | currentLocation(function (err, loc) { 6 | if (err) { 7 | console.error(err) 8 | process.exit(1) 9 | } else { 10 | console.log(JSON.stringify(loc)) 11 | process.exit(0) 12 | } 13 | }) 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var child = require('child_process') 2 | var os = require('os') 3 | var triangulate = require('wifi-triangulate') 4 | 5 | module.exports = function (cb) { 6 | var plat = os.platform() 7 | 8 | if (plat === 'darwin') { 9 | var out, lat, lon 10 | try { 11 | out = child.execSync(__dirname + '/whereami') 12 | var lines = out.toString().split('\n') 13 | if (lines[0] && lines[0].indexOf('Latitude: ') === 0) lat = lines[0].split('Latitude: ')[1] 14 | if (lines[1] && lines[1].indexOf('Longitude: ') === 0) lon = lines[1].split('Longitude: ')[1] 15 | } catch (e) {} 16 | if (lat && lon) { 17 | return cb(null, {latitude: parseFloat(lat), longitude: parseFloat(lon)}) 18 | } 19 | } 20 | 21 | if (plat === 'linux') { 22 | // todo implement linux specific lookup 23 | } 24 | 25 | if (plat === 'win32') { 26 | // todo implement windows specific lookup 27 | } 28 | 29 | // ask google 30 | triangulate(function (err, loc) { 31 | if (err) cb(new Error('Cannot reach google servers')) 32 | else cb(null, {latitude: loc.lat, longitude: loc.lng}) 33 | }) 34 | } 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "current-location", 3 | "version": "1.1.0", 4 | "description": "Get your current location (latitude, longitude) on the command line as JSON", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "standard" 8 | }, 9 | "bin": { 10 | "current-location": "cli.js" 11 | }, 12 | "author": "max ogden", 13 | "license": "ISC", 14 | "dependencies": { 15 | "wifi-triangulate": "^1.1.1" 16 | }, 17 | "devDependencies": { 18 | "standard": "^5.4.1" 19 | }, 20 | "repository": { 21 | "type": "git", 22 | "url": "git+https://github.com/maxogden/current-location.git" 23 | }, 24 | "keywords": [], 25 | "bugs": { 26 | "url": "https://github.com/maxogden/current-location/issues" 27 | }, 28 | "homepage": "https://github.com/maxogden/current-location#readme" 29 | } 30 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # current-location 2 | 3 | Get your current location (latitude, longitude) on the command line as JSON 4 | 5 | Uses CoreLocation.framework on OS X (thanks to [WhereAmI](https://github.com/robmathers/WhereAmI)) and falls back to asking Google 6 | 7 | Used by the [`gititude`](https://github.com/maxogden/gititude) module 8 | 9 | #### TODO (send a PR) 10 | 11 | - add offline wifi geolocation database 12 | - better linux/windows support 13 | 14 | ## install 15 | 16 | ``` 17 | npm i current-location -g 18 | ``` 19 | 20 | ## usage 21 | 22 | ``` 23 | $ current-location 24 | {"latitude":45.51645,"longitude":-122.629713} 25 | ``` 26 | 27 | will exit with exit code 1 if it could not get a location 28 | 29 | -------------------------------------------------------------------------------- /whereami: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/max-mapper/current-location/138521b81cf835c36f7de34b24c57f4cd0cf7e2b/whereami --------------------------------------------------------------------------------