diff --git a/lib/short-doc-url.js b/lib/short-doc-url.js index a5c6dc4..6c2b141 100644 --- a/lib/short-doc-url.js +++ b/lib/short-doc-url.js @@ -1,13 +1,19 @@ 'use strict'; const got = require('got'); const locale = require('./locale'); -const isCI = require('ci-info').isCI; -const inGFW = locale === 'zh_CN' && (isCI || new Date().getTimezoneOffset() === -480); -const shorturlCache = require(inGFW ? './shorturl_cn.json' : './shorturl.json'); +const ci = require('ci-info'); const googl = require('goo.gl'); +const inGFW = require('in-gfw'); + // Set a developer key (_required by Google_; see http://goo.gl/4DvFk for more info.) googl.setKey('AIzaSyACqNSi3cybDvDfWMaPyXZEzQ6IeaPehLE'); +const isInGFW = ci.isCI && ci.name ? Promise.resolve(locale === 'zh_CN') : inGFW.os(); + +const shorturlCache = isInGFW.then(inGFW => { + return require(inGFW ? './shorturl_cn.json' : './shorturl.json'); +}); + function shortDocUrl (error) { if (!error.doc) { return error; @@ -23,20 +29,23 @@ function shortDocUrl (error) { } function shortUrl (url) { - const urlLowerCase = url.toLowerCase(); - if (shorturlCache[urlLowerCase]) { - return Promise.resolve(shorturlCache[urlLowerCase]); - } - - if (inGFW) { - return got(`http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=${encodeURIComponent(url)}`, { - json: true, - }).then(result => { - return result.body[0].url_short; + return shorturlCache.then(shorturlCache => { + const urlLowerCase = url.toLowerCase(); + if (shorturlCache[urlLowerCase]) { + return shorturlCache[urlLowerCase]; + } + return isInGFW.then(inGFW => { + if (inGFW) { + return got(`http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=${encodeURIComponent(url)}`, { + json: true, + }).then(result => { + return result.body[0].url_short; + }); + } else { + return googl.shorten(url); + } }); - } else { - return googl.shorten(url); - } + }); } module.exports = function (errors) { diff --git a/package.json b/package.json index 63df8c5..e0a2f06 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "fs-extra": "^5.0.0", "goo.gl": "^0.1.4", "got": "^8.3.0", + "in-gfw": "^1.0.0", "is-windows": "^1.0.2", "js-yaml": "^3.11.0", "junit-report-builder": "^1.3.0", diff --git a/scripts/shorturl.js b/scripts/shorturl.js index 1ff4eab..ac0ea6c 100644 --- a/scripts/shorturl.js +++ b/scripts/shorturl.js @@ -337,6 +337,7 @@ Promise.all([ 'add', 'lib/*.json', ]); + process.exitCode = 127; } }); diff --git a/test/api.test.js b/test/api.test.js index 822ab3e..67a9caf 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -27,12 +27,7 @@ describe('API', () => { if (!errors[0].docShort) { return; } - const locale = require('../lib/locale'); - if (locale !== 'zh_CN') { - assert.ok(/^https?:\/\/goo\.gl\//.test(errors[0].docShort)); - } else { - assert.ok(/^https?:\/\/(goo\.gl|t\.cn)\//.test(errors[0].docShort)); - } + assert.ok(/^https?:\/\/(goo\.gl|t\.cn)\//.test(errors[0].docShort)); }); }); @@ -44,28 +39,31 @@ describe('API', () => { }); }); - it('short-doc-url (default)', () => { + it('short-doc-url (in GFW)', () => { const shortDocUrl = proxyquire('../lib/short-doc-url', { - './locale': 'en_US', + './locale': 'zh_CN', + 'in-gfw': { + os: () => Promise.resolve(true), + }, }); return shortDocUrl([{ doc: 'https://stylelint.io/user-guide/rules/indentation/', }]).then(errors => { - assert.equal(errors[0].docShort, 'https://goo.gl/NVQ9aa'); + assert.equal(errors[0].docShort, 'http://t.cn/Ro8Mjw5'); }); }); - it('short-doc-url (in GWF)', () => { - const getTimezoneOffset = Date.prototype.getTimezoneOffset; + it('short-doc-url (out GFW)', () => { const shortDocUrl = proxyquire('../lib/short-doc-url', { - './locale': 'zh_CN', + './locale': 'en_US', + 'in-gfw': { + os: () => Promise.resolve(false), + }, }); - Date.prototype.getTimezoneOffset = () => (-480); return shortDocUrl([{ doc: 'https://stylelint.io/user-guide/rules/indentation/', }]).then(errors => { - Date.prototype.getTimezoneOffset = getTimezoneOffset; - assert.equal(errors[0].docShort, 'http://t.cn/Ro8Mjw5'); + assert.equal(errors[0].docShort, 'https://goo.gl/NVQ9aa'); }); });