Skip to content

Commit

Permalink
Windows-support (#5)
Browse files Browse the repository at this point in the history
* style: prettify files

* feat: normalize paths before feeding to glob fn on Windows machines
  • Loading branch information
larrifax authored Mar 17, 2022
1 parent fd09428 commit 03348e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const { sync: glob } = require("fast-glob");
const path = require("path");
const fs = require('fs');
const fs = require("fs");
const normalizePath = process.platform === "win32" ? require("normalize-path") : (x) => x;

function stripJsonComments (data) {
return data.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m);
function stripJsonComments(data) {
return data.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => (g ? "" : m));
}

module.exports = (relativeTsconfigPath = './tsconfig.json') => {
module.exports = (relativeTsconfigPath = "./tsconfig.json") => {
const absTsconfigPath = path.resolve(process.cwd(), relativeTsconfigPath);
let tsconfigData = fs.readFileSync(absTsconfigPath, 'utf8');
let tsconfigData = fs.readFileSync(absTsconfigPath, "utf8");
tsconfigData = stripJsonComments(tsconfigData);
const {compilerOptions} = JSON.parse(tsconfigData);
const { compilerOptions } = JSON.parse(tsconfigData);

const pathKeys = Object.keys(compilerOptions.paths);
const re = new RegExp(`^(${pathKeys.join("|")})`);
Expand All @@ -22,11 +23,11 @@ module.exports = (relativeTsconfigPath = './tsconfig.json') => {
const [pathDir] = pathKey.split("*");
const file = args.path.replace(pathDir, "");
for (const dir of compilerOptions.paths[pathKey]) {
const fileDir = path.resolve(process.cwd(), dir).replace("*", file)
const fileDir = normalizePath(path.resolve(process.cwd(), dir).replace("*", file));
let [matchedFile] = glob(`${fileDir}.*`);
if (!matchedFile) {
const [matchIndexFile] = glob(`${fileDir}/index.*`);
matchedFile = matchIndexFile
matchedFile = matchIndexFile;
}
if (matchedFile) {
return { path: matchedFile };
Expand Down
20 changes: 17 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"format": "prettier --write index.js"
},
"dependencies": {
"fast-glob": "^3.2.11"
"fast-glob": "^3.2.11",
"normalize-path": "^3.0.0"
},
"devDependencies": {
"prettier": "^2.5.1"
Expand Down

0 comments on commit 03348e0

Please sign in to comment.