diff --git a/config/default.js b/config/default.js index 8cc59fa3c..838bb24e8 100644 --- a/config/default.js +++ b/config/default.js @@ -2,4 +2,5 @@ module.exports = { // eslint-disable-next-line global-require cli: require('../package.json').name, host: 'https://dash.readme.io', + hub: 'https://{project}.readme.io' }; diff --git a/config/localhost.json b/config/localhost.json index 4afe78584..beed57634 100644 --- a/config/localhost.json +++ b/config/localhost.json @@ -1,3 +1,4 @@ { - "host": "http://dash.readme.local:3000" + "host": "http://dash.readme.local:3000", + "hub": "http://{project}.readme.local:3000" } diff --git a/lib/open.js b/lib/open.js new file mode 100644 index 000000000..f4e602d27 --- /dev/null +++ b/lib/open.js @@ -0,0 +1,20 @@ +const config = require('config'); +const open = require('opn'); + +exports.desc = 'Open your current ReadMe project in the browser'; +exports.category = 'utilities'; +exports.weight = 1; + +const configStore = require('../lib/configstore'); + +exports.run = function ({ opts }) { + const project = configStore.get('project'); + + if (!project) { + return Promise.reject(new Error(`Please login using ${config.cli} login`)); + } + + return (opts.mockOpen || open)(config.hub.replace('{project}', project), { + wait: false, + }).then(() => null); +} diff --git a/package-lock.json b/package-lock.json index 78298cb55..9e5ee9b05 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4371,6 +4371,11 @@ "integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==", "dev": true }, + "is-wsl": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-1.1.0.tgz", + "integrity": "sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=" + }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -6187,6 +6192,14 @@ "json-pointer": "^0.6.0" } }, + "opn": { + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/opn/-/opn-5.4.0.tgz", + "integrity": "sha512-YF9MNdVy/0qvJvDtunAOzFw9iasOQHpVthTCvGzxt61Il64AYSGdK+rYwld7NAfk9qJ7dt+hymBNSc9LNYS+Sw==", + "requires": { + "is-wsl": "^1.1.0" + } + }, "optimist": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz", diff --git a/package.json b/package.json index 221432415..b06bbeeb3 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "isemail": "^3.1.3", "minimist": "^1.2.0", "oas": "^0.8.8", + "opn": "^5.4.0", "read": "^1.0.7", "request": "^2.88.0", "request-promise-native": "^1.0.5" diff --git a/test/login.test.js b/test/login.test.js index b3951b8d1..90927b19b 100644 --- a/test/login.test.js +++ b/test/login.test.js @@ -1,11 +1,9 @@ const nock = require('nock'); const config = require('config'); const assert = require('assert'); -const Configstore = require('configstore'); - -const pkg = require('../package'); const login = require('../cli').bind(null, 'login'); +const configStore = require('../lib/configstore'); describe('login command', () => { beforeAll(() => nock.disableNetConnect()); @@ -35,10 +33,10 @@ describe('login command', () => { return login([], { email, password, project }).then(() => { mock.done(); - const conf = new Configstore(pkg.name); - assert.equal(conf.get('apiKey'), apiKey); - assert.equal(conf.get('email'), email); - assert.equal(conf.get('project'), project); + assert.equal(configStore.get('apiKey'), apiKey); + assert.equal(configStore.get('email'), email); + assert.equal(configStore.get('project'), project); + configStore.clear(); }); }); diff --git a/test/open.test.js b/test/open.test.js new file mode 100644 index 000000000..dd01cc840 --- /dev/null +++ b/test/open.test.js @@ -0,0 +1,29 @@ +const assert = require('assert'); +const config = require('config'); + +const configStore = require('../lib/configstore'); + +const open = require('../cli').bind(null, 'open'); + +describe('open command', () => { + it('should error if no project provided', (done) => { + configStore.delete('project'); + + open([], {}).catch(err => { + assert.equal(err.message, `Please login using ${config.cli} login`); + return done(); + }) + }); + + it('should open the project', (done) => { + configStore.set('project', 'subdomain'); + + function mockOpen(url) { + assert.equal(url, 'https://subdomain.readme.io'); + done(); + return Promise.resolve(); + } + + open([], { mockOpen }); + }); +});