Skip to content

Commit

Permalink
fix: _try_ to gracefully handle berry pnp
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioramos committed Aug 19, 2020
1 parent 36f71bf commit 84e9bce
Show file tree
Hide file tree
Showing 4 changed files with 3,174 additions and 656 deletions.
32 changes: 27 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,31 +311,53 @@ module.exports = class {
(filename) => exists(filename),
);

const getRealPath = (pathanme) => {
try {
return realpathSync(pathanme);
// eslint-disable-next-line no-unused-vars
} catch (err) {
return pathanme;
}
};

const readFile = (fullpath) => {
try {
return readFileSync(fullpath, 'utf-8');
// eslint-disable-next-line no-unused-vars
} catch (err) {
try {
return readFileSync(require.resolve(fullpath), 'utf-8');
// eslint-disable-next-line no-unused-vars
} catch (err) {}
}
};

const handleFile = (pathname) => {
const realpath = realpathSync(pathname);
const realpath = getRealPath(pathname);

const cache = this.files.find(([src]) => src === realpath);

if (Array.isArray(cache)) {
const [fullpath] = cache;
if (fullpath) {
return readFileSync(fullpath, 'utf-8');
return readFile(fullpath);
}
}

// eslint-disable-next-line block-scoped-var
if (!tsAvailable) {
return readFileSync(pathname, 'utf-8');
return readFile(pathname);
}

if (!['.ts', '.tsx'].includes(extname(pathname))) {
return readFileSync(pathname, 'utf-8');
return readFile(pathname);
}

if (
!PathIsInside(pathname, this.servicePath) ||
pathname.split(sep).includes(['node_modules'])
) {
return readFileSync(pathname, 'utf-8');
return readFile(pathname);
}

// eslint-disable-next-line block-scoped-var
Expand Down
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Intercept = require('apr-intercept');
const { basename, dirname, extname, join, resolve, relative } = require('path');
const { isSymlinkSync } = require('path-type');
const Reduce = require('apr-reduce');
const SortBy = require('lodash.sortby');
const Setup = require('./setup');

const EVENT = {
Expand All @@ -21,7 +22,7 @@ const normalizeTree = ({ path, type, name, children = [] }) => {

return {
path: relative(__dirname, path),
children: children.map(normalizeTree),
children: SortBy(children.map(normalizeTree), 'path'),
isSymlink,
link: isSymlink ? readlinkSync(path) : undefined,
md5: isDirectory ? undefined : fromFileSync(path, { algorithm: 'md5' }),
Expand Down

0 comments on commit 84e9bce

Please sign in to comment.