/
githubmirror
/
yarn
Обзор
Документация
Войти
/
githubmirror
/
yarn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/util/fix-cmd-win-slashes.js
31 строка
1022 B
Simon Vocella
add prettier and prettying everything (#3401)
16 май 2017, 21:12
16 май 2017, 21:12
25890c8
Код
Авторство
О чём код?
/* @flow */ export function fixCmdWinSlashes(cmd: string): string { function findQuotes(quoteSymbol: string): {from: number, to: number}[] { const quotes = []; const addQuote = (_, index) => { quotes.push({from: index, to: index + _.length}); return _; }; const regEx = new RegExp(quoteSymbol + '.*' + quoteSymbol); cmd.replace(regEx, addQuote); return quotes; } const quotes = findQuotes('"').concat(findQuotes("'")); function isInsideQuotes(index: number): boolean { return quotes.reduce((result, quote) => { return result || (quote.from <= index && index <= quote.to); }, false); } const cmdPrePattern = '((?:^|&&|&|\\|\\||\\|)\\s*)'; const cmdPattern = '(".*?"|\'.*?\'|\\S*)'; const regExp = new RegExp(`${cmdPrePattern}${cmdPattern}`, 'g'); return cmd.replace(regExp, (whole, pre, cmd, index) => { if ((pre[0] === '&' || pre[0] === '|') && isInsideQuotes(index)) { return whole; } return pre + cmd.replace(/\//g, '\\'); }); }