diff --git a/package.json b/package.json index d370bcf..293e9c0 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "string-template": "^1.0.0", "strip-bom": "^2.0.0", "thenify": "^3.1.0", + "throat": "^2.0.2", "touch": "^1.0.0", "typescript": "1.8.9", "xtend": "^4.0.0", diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 8dbd5a7..b12afff 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -8,13 +8,14 @@ import popsicleStatus = require('popsicle-status') import popsicleRetry = require('popsicle-retry') import detectIndent = require('detect-indent') import sortKeys = require('sort-keys') -import mdp = require('mkdirp') +import Mkdirp = require('mkdirp') import uniq = require('array-uniq') import lockfile = require('lockfile') -import rmrf = require('rimraf') +import Rimraf = require('rimraf') import popsicleProxy = require('popsicle-proxy-agent') +import Throat = require('throat') import promiseFinally from 'promise-finally' -import tch = require('touch') +import Touch = require('touch') import { join, dirname } from 'path' import { parse as parseUrl } from 'url' import template = require('string-template') @@ -28,18 +29,27 @@ import debug from './debug' const pkg = require('../../package.json') const registryURL = parseUrl(rc.registryURL) +const throat = Throat(Promise) export type Stats = fs.Stats -export const touch: (path: string, options?: tch.Options) => Promise = thenify(tch) -export const stat: (path: string) => Promise = thenify(fs.stat) -export const readFile: (path: string, encoding: string) => Promise = thenify(fs.readFile) -export const writeFile: (path: string, contents: string | Buffer) => Promise = thenify(fs.writeFile) -export const mkdirp: (path: string) => Promise = thenify(mdp) -export const unlink: (path: string) => Promise = thenify(fs.unlink) -export const lock: (path: string, options?: lockfile.Options) => Promise = thenify(lockfile.lock) -export const unlock: (path: string) => Promise = thenify(lockfile.unlock) -export const rimraf: (path: string) => Promise = thenify(rmrf) +export type LockOp = (path: string, options?: lockfile.Options) => Promise +export type TouchOp = (path: string, options?: Touch.Options) => Promise +export type StatOp = (path: string) => Promise +export type ReadFileOp = (path: string, encoding: string) => Promise +export type WriteFileOp = (path: string, contents: string | Buffer) => Promise +export type MkdirpOp = (path: string) => Promise +export type PathOp = (path: string) => Promise + +export const touch: TouchOp = throat(10, thenify(Touch)) +export const stat: StatOp = throat(10, thenify(fs.stat)) +export const readFile: ReadFileOp = throat(10, thenify(fs.readFile)) +export const writeFile: WriteFileOp = thenify(fs.writeFile) +export const mkdirp: MkdirpOp = throat(10, thenify(Mkdirp)) +export const unlink: PathOp = throat(10, thenify(fs.unlink)) +export const lock: LockOp = throat(10, thenify(lockfile.lock)) +export const unlock: PathOp = throat(10, thenify(lockfile.unlock)) +export const rimraf: PathOp = throat(10, thenify(Rimraf)) /** * Verify a path exists and is a file. @@ -89,7 +99,7 @@ export function parseConfig (config: ConfigJson, path: string): ConfigJson { /** * Read a file over HTTP, using a file cache and status check. */ -export function readHttp (url: string): Promise { +export const readHttp = throat(5, function readHttp (url: string): Promise { const { proxy, httpProxy, httpsProxy, noProxy, rejectUnauthorized, ca, key, cert, userAgent } = rc return popsicle.get({ @@ -156,7 +166,7 @@ export function readHttp (url: string): Promise { }) // Return only the response body. .then(response => response.body) -} +}) /** * Read a file from anywhere (HTTP or local filesystem). diff --git a/typings.json b/typings.json index c79d83d..7b86a00 100644 --- a/typings.json +++ b/typings.json @@ -28,6 +28,7 @@ "string-template": "github:typings/typed-string-template#4620eee24201636ca45e6dbc2505066703c770da", "strip-bom": "github:typings/typed-strip-bom#d1d42e7f43dcd9fce1cfd988c40421a1d37fa47b", "thenify": "github:typings/typed-thenify#edbd46753be5bb34b916787fcede5e9b1281a2e2", + "throat": "github:typed-typings/npm-throat#20e2c3c157d87a90062a8a2f4d99ab4dc475b1fd", "touch": "github:typings/typed-touch#9b10d0473fb5161c379df57d63d3d59fb5b62fb0", "typescript": "npm:typescript", "xtend": "github:typings/typed-xtend#63cccadf3295b3c15561ee45617ac006edcca9e0",