Skip to content

Commit

Permalink
feat: add convertToGlobbyPath
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Sep 10, 2019
1 parent 6eb31ba commit 884cda9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/printConfig.js
Expand Up @@ -2,6 +2,7 @@
"use strict";

const _ = require("lodash");
const convertToGlobbyPath = require("./utils/convertToGlobbyPath");
const createStylelint = require("./createStylelint");
const globby /*: Function*/ = require("globby");
const path = require("path");
Expand Down Expand Up @@ -29,7 +30,7 @@ module.exports = function(

const filePath = files[0];

if (globby.hasMagic(filePath.replace(/\\/g, "/"))) {
if (globby.hasMagic(convertToGlobbyPath(filePath))) {
return Promise.reject(
new Error("The --print-config option does not support globs.")
);
Expand Down
5 changes: 3 additions & 2 deletions lib/standalone.js
Expand Up @@ -2,6 +2,7 @@
"use strict";

const _ = require("lodash");
const convertToGlobbyPath = require("./utils/convertToGlobbyPath");
const createStylelint = require("./createStylelint");
const createStylelintResult = require("./createStylelintResult");
const debug = require("debug")("stylelint:standalone");
Expand Down Expand Up @@ -170,7 +171,7 @@ module.exports = function(
});
}

let fileList = [].concat(files).map(file => file.replace(/\\/g, "/"));
let fileList = [].concat(files).map(convertToGlobbyPath);

if (!options.disableDefaultIgnores) {
fileList = fileList.concat(ALWAYS_IGNORED_GLOBS.map(glob => "!" + glob));
Expand Down Expand Up @@ -200,7 +201,7 @@ module.exports = function(

if (!filePaths.length) {
if (!allowEmptyInput) {
throw new NoFilesFoundError(fileList);
throw new NoFilesFoundError([].concat(files));
}

return Promise.all([]);
Expand Down
17 changes: 17 additions & 0 deletions lib/utils/convertToGlobbyPath.js
@@ -0,0 +1,17 @@
"use strict";

const isWin = process.platform === "win32";

/**
* Convert file path to globby paths
* If you want escape any symbol use double backslashes, this way we can correctly support windows-like paths
* @param {string} path
* @return {string} result of conversion
*/
module.exports = function convertToGlobbyPath(path) {
if (!isWin) {
return path.replace(/\\\\/g, "\\");
}

return path.replace(/\\/g, "/").replace(/\/\//g, "\\");
};
7 changes: 1 addition & 6 deletions lib/utils/noFilesFoundError.js
@@ -1,7 +1,5 @@
"use strict";

const isWin = process.platform === "win32";

class NoFilesFoundError extends Error {
constructor(fileList) {
super();
Expand All @@ -10,10 +8,7 @@ class NoFilesFoundError extends Error {
fileList = [fileList];
}

const pattern = fileList
.filter(i => !i.startsWith("!"))
.map(file => (isWin ? file.replace(/\//g, "\\") : file))
.join(", ");
const pattern = fileList.filter(i => !i.startsWith("!")).join(", ");

this.message = `No files matching the pattern "${pattern}" were found.`;
}
Expand Down

0 comments on commit 884cda9

Please sign in to comment.