Skip to content

Commit

Permalink
perf: replace sync-request with then-request
Browse files Browse the repository at this point in the history
closes #119
  • Loading branch information
antongolub committed Dec 18, 2020
1 parent 618eaa1 commit 7e1f8f6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 47 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
"license": "MIT",
"private": false,
"scripts": {
"jest": "jest -w 2 --config=jest.config.json --forceExit",
"lint": "tslint -p tsconfig.test.json src/**/*.ts",
"lint:fix": "yarn lint --fix",
"test": "yarn lint && yarn jest",
"test": "yarn lint && yarn test:unit",
"test:unit": "jest --config=jest.config.json --detectOpenHandles --forceExit",
"clean": "rm -rf lib typings",
"build": "yarn clean && yarn build:es5 && yarn build:es6 && yarn build:ts && yarn build:libdef && yarn docs",
"build:es5": "mkdir -p target/es5 && tsc -p tsconfig.es5.json",
"build:es6": "mkdir -p target/es6 && tsc -p tsconfig.es6.json",
"build:ts": "cp -r src/main/ts/ target/ts/",
"build:libdef": "libdefkit --tsconfig=tsconfig.es5.json --tsconfig=tsconfig.es6.json",
"test:report": "yarn test && yarn coveralls:push",
"coveralls:push": "cat ./coverage/lcov.info | coveralls",
"coveralls:push": "cat ./coverage/lcov.info | coveralls || exit 0",
"docs": "typedoc src/main/ts --ignoreCompilerErrors || exit 0",
"postupdate": "yarn && npx yarn-audit-fix && yarn build && yarn test",
"publish:beta": "npm publish --no-git-tag-version --tag beta"
Expand All @@ -63,7 +63,7 @@
"lodash": "^4.17.20",
"queuefy": "^1.1.3",
"read-pkg": "^5.2.0",
"sync-request": "^6.1.0",
"then-request": "^6.0.2",
"tslib": "^2.0.3"
},
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions src/main/ts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import gitParse from 'git-url-parse'
import readPkg from 'read-pkg'
import request from 'sync-request'
import request from 'then-request'
import AggregateError from 'aggregate-error'
import dbg from 'debug'
import { castArray, omit } from 'lodash'
Expand Down Expand Up @@ -59,7 +59,7 @@ export const extractRepoToken = (repoUrl: string): string => {
/**
* @private
*/
export const getRepoUrl = (pluginConfig: TAnyMap, context: TContext, enterprise: boolean): string => {
export const getRepoUrl = async (pluginConfig: TAnyMap, context: TContext, enterprise: boolean): Promise<string> => {
const { env } = context
const urlFromEnv = getRepoUrlFromEnv(env)
const urlFromStepOpts = pluginConfig.repositoryUrl
Expand All @@ -76,7 +76,7 @@ export const getRepoUrl = (pluginConfig: TAnyMap, context: TContext, enterprise:
debug('urlFromPackage= %s', urlFromPackage)

if (GITIO_REPO_PATTERN.test(url)) {
const res: any = request('GET', urlFromOpts, { followRedirects: false, timeout: 5000 })
const res: any = await request('GET', urlFromOpts, { followRedirects: false, timeout: 5000 })
url = res.headers.location
}

Expand Down Expand Up @@ -124,10 +124,10 @@ export const reassembleRepoUrl = (redirectedUrl: string, context: TContext): str
/**
* @private
*/
export const resolveConfig = (pluginConfig: TAnyMap, context: TContext, path = PLUGIN_PATH, step?: string): IGhpagesPluginConfig => {
export const resolveConfig = async (pluginConfig: TAnyMap, context: TContext, path = PLUGIN_PATH, step?: string): Promise<IGhpagesPluginConfig> => {
const opts = resolveOptions(pluginConfig, context, path, step)
const enterprise = Boolean(opts.enterprise || pluginConfig.enterprise || DEFAULT_ENTERPRISE)
const repo = getRepoUrl(pluginConfig, context, enterprise)
const repo = await getRepoUrl(pluginConfig, context, enterprise)
const pullTagsBranch = anyDefined(opts.pullTagsBranch, opts._branch, DEFAULT_PULL_TAGS_BRANCH)
const token = getToken(context.env, repo)

Expand Down
4 changes: 2 additions & 2 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let _config: any

export const verifyConditions = async (pluginConfig: any, context: TContext) => {
const { logger } = context
const config = resolveConfig(pluginConfig, context, undefined, 'publish')
const config = await resolveConfig(pluginConfig, context, undefined, 'publish')

logger.log('verify gh-pages config')

Expand All @@ -38,7 +38,7 @@ export const verifyConditions = async (pluginConfig: any, context: TContext) =>
}

export const publish = async (pluginConfig: any, context: TContext) => {
const config = resolveConfig(pluginConfig, context, undefined, 'publish')
const config = await resolveConfig(pluginConfig, context, undefined, 'publish')
const { logger, env, cwd } = context
const message = render(config.msg, context, logger)
const pushOpts: IPushOpts = {
Expand Down
19 changes: 9 additions & 10 deletions src/test/ts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('config', () => {
})

describe('#resolveConfig', () => {
it('extracts meaningful props only', () => {
it('extracts meaningful props only', async () => {
const step = 'publish'
const path = PLUGIN_PATH
const token = 'token'
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('config', () => {
env: { GH_TOKEN: token }
}

const config = resolveConfig(pluginConfig, context, path, step)
const config = await resolveConfig(pluginConfig, context, path, step)

expect(config).toEqual({
src: 'docsdocs',
Expand All @@ -137,7 +137,7 @@ describe('config', () => {
})
})

it('fills empty values with defaults', () => {
it('fills empty values with defaults', async () => {
const step = 'publish'
const path = PLUGIN_PATH
const token = 'token'
Expand All @@ -157,7 +157,7 @@ describe('config', () => {
env: { GITHUB_TOKEN: token }
}
process.env.DEBUG = 'true'
const config = resolveConfig(pluginConfig, context, undefined, step)
const config = await resolveConfig(pluginConfig, context, undefined, step)
delete process.env.DEBUG

expect(config).toEqual({
Expand All @@ -172,7 +172,7 @@ describe('config', () => {
})
})

it('issues/60', () => {
it('issues/60', async () => {
const step = 'publish'
const path = '@qiwi/semantic-release-gh-pages-plugin'
const pluginConfig = {
Expand Down Expand Up @@ -231,7 +231,7 @@ describe('config', () => {
}
}

const config = resolveConfig(pluginConfig, context, path, step)
const config = await resolveConfig(pluginConfig, context, path, step)

expect(config).toEqual({
branch: DEFAULT_BRANCH,
Expand Down Expand Up @@ -284,7 +284,7 @@ describe('config', () => {
cases.forEach(([input, result]) => expect(extractRepoDomain(input)).toBe(result))
})

describe('#getRepoUrl', () => {
describe('#getRepoUrl', async () => {
it('returns proper value', () => {
const cases: Array<{pluginConfig: TAnyMap, context: TContext, enterprise?: boolean, result: string}> =
[
Expand Down Expand Up @@ -375,14 +375,13 @@ describe('config', () => {
}
]

cases.forEach(({
cases.forEach(async ({
pluginConfig,
context,
result,
enterprise
}) => {

expect(getRepoUrl(pluginConfig, context, !!enterprise)).toBe(result)
await expect(getRepoUrl(pluginConfig, context, !!enterprise)).resolves.toBe(result)
})
})
})
Expand Down
9 changes: 5 additions & 4 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('index', () => {
dst: DEFAULT_DST,
src: DEFAULT_SRC,
enterprise: DEFAULT_ENTERPRISE,
repo: getRepoUrl(pluginConfig, context, DEFAULT_ENTERPRISE),
repo: await getRepoUrl(pluginConfig, context, DEFAULT_ENTERPRISE),
token,
pullTagsBranch: DEFAULT_PULL_TAGS_BRANCH
})
Expand Down Expand Up @@ -196,7 +196,7 @@ describe('index', () => {
env: { GITHUB_TOKEN: token }
}
const expectedOpts = {
repo: getRepoUrl(pluginConfig, context, false),
repo: await getRepoUrl(pluginConfig, context, false),
branch: 'doc-branch',
message: 'docs updated v{{=it.nextRelease.gitTag}}',
dest: 'root'
Expand All @@ -209,6 +209,7 @@ describe('index', () => {
}

const res = await publish(pluginConfig, context)
const resolvedConfig = await resolveConfig(pluginConfig, context)

expect(fakeExeca).toHaveBeenCalledWith(
'git',
Expand All @@ -217,7 +218,7 @@ describe('index', () => {
'--tags',
'--force',
expectedOpts.repo,
resolveConfig(pluginConfig, context).pullTagsBranch
resolvedConfig.pullTagsBranch
],
execaOpts
)
Expand Down Expand Up @@ -266,7 +267,7 @@ describe('index', () => {
env: { GITHUB_TOKEN: token + 'foo' }
}
const expectedOpts = {
repo: getRepoUrl(pluginConfig, context, false),
repo: await getRepoUrl(pluginConfig, context, false),
branch: DEFAULT_BRANCH,
message: DEFAULT_MSG,
dest: DEFAULT_DST
Expand Down
23 changes: 1 addition & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1827,11 +1827,6 @@ get-package-type@^0.1.0:
resolved "https://registry.yarnpkg.com/get-package-type/-/get-package-type-0.1.0.tgz#8de2d803cff44df3bc6c456e6668b36c3926e11a"
integrity sha512-pjzuKtY64GYfWizNAJ0fr9VqttZkNiK2iS430LtIHzjBEr6bX8Am2zm4sW4Ro5wjWW5cAlRL1qAMTcXbjNAO2Q==

get-port@^3.1.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/get-port/-/get-port-3.2.0.tgz#dd7ce7de187c06c8bf353796ac71e099f0980ebc"
integrity sha1-3Xzn3hh8Bsi/NTeWrHHgmfCYDrw=

get-stream@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-4.1.0.tgz#c1b255575f3dc21d59bfc79cd3d2b46b1c3a54b5"
Expand Down Expand Up @@ -4079,22 +4074,6 @@ symbol-tree@^3.2.4:
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2"
integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==

sync-request@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/sync-request/-/sync-request-6.1.0.tgz#e96217565b5e50bbffe179868ba75532fb597e68"
integrity sha512-8fjNkrNlNCrVc/av+Jn+xxqfCjYaBoHqCsDz6mt030UMxJGr+GSfCV1dQt2gRtlL63+VPidwDVLr7V2OcTSdRw==
dependencies:
http-response-object "^3.0.1"
sync-rpc "^1.2.1"
then-request "^6.0.0"

sync-rpc@^1.2.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/sync-rpc/-/sync-rpc-1.3.6.tgz#b2e8b2550a12ccbc71df8644810529deb68665a7"
integrity sha512-J8jTXuZzRlvU7HemDgHi3pGnh/rkoqR/OZSjhTyyZrEkkYQbk7Z33AXp37mkPfPpfdOuj7Ex3H/TJM1z48uPQw==
dependencies:
get-port "^3.1.0"

terminal-link@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/terminal-link/-/terminal-link-2.1.1.tgz#14a64a27ab3c0df933ea546fba55f2d078edc994"
Expand All @@ -4112,7 +4091,7 @@ test-exclude@^6.0.0:
glob "^7.1.4"
minimatch "^3.0.4"

then-request@^6.0.0:
then-request@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/then-request/-/then-request-6.0.2.tgz#ec18dd8b5ca43aaee5cb92f7e4c1630e950d4f0c"
integrity sha512-3ZBiG7JvP3wbDzA9iNY5zJQcHL4jn/0BWtXIkagfz7QgOL/LqjCEOBQuJNZfu0XYnv5JhKh+cDxCPM4ILrqruA==
Expand Down

0 comments on commit 7e1f8f6

Please sign in to comment.