From 3f841618113ce6691a7deb952ad1389519625654 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 18 May 2017 13:14:31 +0430 Subject: [PATCH] Refactor r() into utils --- lib/utils.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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)) +}