Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
adopt new version of 'resolve' dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
igorklopov committed Dec 8, 2019
1 parent 718d974 commit 7af8d04
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 69 deletions.
23 changes: 14 additions & 9 deletions lib/follow.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import fs from 'fs';
import { sync } from 'resolve';

export default function follow (x, opts) {
// TODO async version
return new Promise((resolve) => {
resolve(sync(x, opts));
// TODO own implementation with foreign tests
// TODO async follow
/*
resolve_(x, opts, (error, result) => {
if (error) return reject(error);
resolve(result);
});
*/
resolve(sync(x, {
basedir: opts.basedir,
extensions: opts.extensions,
readFileSync: (file) => {
opts.readFile(file);
return fs.readFileSync(file);
},
packageFilter: (config, base) => {
opts.packageFilter(config, base);
return config;
}
}));
});
}
62 changes: 13 additions & 49 deletions lib/walker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ import path from 'path';

const win32 = process.platform === 'win32';

function shortFromAlias (alias) {
// alias = fs-promise or @types/node
if (alias[0] === '@') {
return alias.match(/^([^\\/]+[\\/][^\\/]+)/)[0];
} else {
return alias.match(/^[^\\/]+/)[0];
}
}

function isPublic (config) {
if (config.private) return false;
let { license, licenses } = config;
Expand Down Expand Up @@ -414,28 +405,29 @@ class Walker {
}

async stepDerivatives_ALIAS_AS_RESOLVABLE (record, marker, derivative) { // eslint-disable-line camelcase
const catcher = {};
let stage = 0;
let newPackage;
let newMarker;

catcher.readFileSync = (file) => {
// only first occurence from loadNodeModulesSync
if (stage === 2) return;
assert(stage === 0);
// was taken from resolve/lib/sync.js
const isNear = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\/])/;
const near = isNear.test(derivative.alias);

const catchReadFile = (file) => {
if (near) return;
if (stage !== 0) return;
assert(isPackageJson(file), 'walker: ' +
file + ' must be package.json');
newPackage = file;
newMarker = undefined;
stage = 1;
return fs.readFileSync(file);
};

catcher.packageFilter = (config, base) => {
assert(stage === 1);
const catchPackageFilter = (config, base) => {
if (near) return;
if (stage !== 1) return;
newMarker = { config, configPath: newPackage, base };
stage = 2;
return config;
};

let newFile, failure;
Expand All @@ -448,42 +440,13 @@ class Walker {
// is not taken in require('./typos')
// in 'normalize-package-data/lib/fixer.js'
extensions: [ '.js', '.json', '.node' ],
readFileSync: catcher.readFileSync,
packageFilter: catcher.packageFilter
readFile: catchReadFile,
packageFilter: catchPackageFilter
});
} catch (error) {
failure = error;
}

// was taken from resolve/lib/sync.js
const isNear = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\/])/;
let mainNotFound = false;

if (!isNear.test(derivative.alias)) {
const short = shortFromAlias(derivative.alias);
// 'npm' !== 'npm/bin/npm-cli.js'
if (short !== derivative.alias) {
try {
await follow(short, {
basedir: path.dirname(record.file),
extensions: [ '.js', '.json', '.node' ],
readFileSync: catcher.readFileSync,
packageFilter: catcher.packageFilter
});
} catch (error) {
// the purpose is to fire 'packageFilter'
// in cases like require('npm/bin/npm-cli.js')
// because otherwise it will not be fired
// (only loadAsFileSync is executed)
}
}
// 'babel-runtime' === 'babel-runtime'
if (short === derivative.alias) {
mainNotFound = failure && newMarker &&
newMarker.config && !newMarker.config.main;
}
}

assert((newPackage && newMarker) ||
(!newPackage && !newMarker), 'Probably, package.json is malformed');

Expand All @@ -498,6 +461,7 @@ class Walker {

if (failure) {
const { toplevel } = marker;
const mainNotFound = !newFile && newMarker;
const debug = !toplevel || derivative.mayExclude ||
(mainNotFound && derivative.fromDependencies);
const level = debug ? 'debug' : 'warn';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"multistream": "^2.1.1",
"pkg-fetch": "^2.6.4",
"progress": "^2.0.3",
"resolve": "1.6.0",
"resolve": "^1.13.1",
"stream-meter": "^1.0.4"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions test/test-50-bakery-4/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const target = process.argv[2] || 'host';
const input = './test-x-index.js';
const output = './run-time/test-output.exe';

if (/^(node|v)?0/.test(target)) return;
if (/^(node|v)?4/.test(target)) return;
if (/^(node|v)?6/.test(target)) return;
if (/^(node|v)?8/.test(target)) return;

let left;
Expand Down
4 changes: 3 additions & 1 deletion test/test-50-fs-runtime-layer-2/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ function bitty (version) {
(4 * (/^(node|v)?7/.test(version))) |
(4 * (/^(node|v)?8/.test(version))) |
(4 * (/^(node|v)?9/.test(version))) |
(8 * (/^(node|v)?10/.test(version)));
(8 * (/^(node|v)?10/.test(version))) |
(8 * (/^(node|v)?11/.test(version))) |
(8 * (/^(node|v)?12/.test(version)));
}

const version1 = process.version;
Expand Down
11 changes: 2 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2666,7 +2666,7 @@ path-key@^2.0.1:
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"
integrity sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=

path-parse@^1.0.5, path-parse@^1.0.6:
path-parse@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==
Expand Down Expand Up @@ -2918,14 +2918,7 @@ resolve-url@^0.2.1:
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=

resolve@1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.6.0.tgz#0fbd21278b27b4004481c395349e7aba60a9ff5c"
integrity sha512-mw7JQNu5ExIkcw4LPih0owX/TZXjD/ZUF/ZQ/pDnkw3ZKhDcZZw5klmBlj6gVMwjQ3Pz5Jgu7F3d0jcDVuEWdw==
dependencies:
path-parse "^1.0.5"

resolve@^1.12.0, resolve@^1.3.2, resolve@^1.8.1:
resolve@^1.12.0, resolve@^1.13.1, resolve@^1.3.2, resolve@^1.8.1:
version "1.13.1"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.13.1.tgz#be0aa4c06acd53083505abb35f4d66932ab35d16"
integrity sha512-CxqObCX8K8YtAhOBRg+lrcdn+LK+WYOS8tSjqSFbjtrI5PnS63QPhZl4+yKfrU9tdsbMu9Anr/amegT87M9Z6w==
Expand Down

0 comments on commit 7af8d04

Please sign in to comment.