diff --git a/lib/utils.js b/lib/utils.js index 1a39b9537c..601d68dfe0 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,4 +1,6 @@ 'use strict' +import { resolve, sep } from 'path' +import _ from 'lodash' export function encodeHtml (str) { return str.replace(//g, '>') @@ -74,3 +76,24 @@ export function chainFn (base, fn) { fn.apply(this, arguments) } } + +function wp (p) { + /* istanbul ignore if */ + if (/^win/.test(process.platform)) { + p = p.replace(/\\/g, '\\\\') + } + return p +} + +const reqSep = /\//g +const sysSep = _.escapeRegExp(sep) +const normalize = string => string.replace(reqSep, sysSep) + +export function r () { + let args = Array.from(arguments) + if (_.last(args).includes('~')) { + return wp(_.last(args)) + } + args = args.map(normalize) + return wp(resolve.apply(null, args)) +}