Skip to content

Commit

Permalink
Merge pull request #13 from ashward/issue-12-sub-yarn
Browse files Browse the repository at this point in the history
Fixes issue #12 - Fails to run on yarn sub-process
  • Loading branch information
ashward authored Mar 11, 2021
2 parents d3bcad3 + b7a3abd commit 1e174d4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion files/.jsig/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var ChildProcess = require("child_process");
var Path = require("path");

var jsigVersion = "0.1.5";
var jsigVersion = "0.1.6";

module.exports = {
version: jsigVersion,
Expand Down
68 changes: 39 additions & 29 deletions files/.jsig/jsig-yarn.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,48 @@ var FS = require("fs");
var jsig = require("./index");

// Work out where the actual yarn installation is
var yarnDir;

try {
var yarnFile = ChildProcess.execSync("which yarn").toString().trim();

yarnDir = Path.dirname(FS.realpathSync(yarnFile));

// If 'yarn.js' isn't in the same dir as the yarn' script
// then we will try and parse its location from the script.
// This seems to happen in some Hmoebrew installations
if (!FS.existsSync(Path.resolve(yarnDir, "yarn.js"))) {
var yarnScript = FS.readFileSync(yarnFile).toString();

var match = yarnScript.match(/([^'"]+)\/yarn\.js/);

if (match) {
yarnDir = match[1];
} else {
throw new Error(
`Could not determine yarn.js path from yarn script ${yarnScript}`
);
var yarnJsFile;

if (process.env.jsig_yarn_js_file) {
// We're in a sub-process yarn, so use the original (top-level) one.
yarnJsFile = process.env.jsig_yarn_js_file;
} else {
try {
var yarnFile = ChildProcess.execSync("which yarn").toString().trim();

var yarnDir = Path.dirname(FS.realpathSync(yarnFile));

// If 'yarn.js' isn't in the same dir as the yarn' script
// then we will try and parse its location from the script.
// This seems to happen in some Homebrew installations
if (!FS.existsSync(Path.resolve(yarnDir, "yarn.js"))) {
var yarnScript = FS.readFileSync(yarnFile).toString();

var match = yarnScript.match(/([^'"]+)\/yarn\.js/);

if (match) {
yarnDir = match[1];
} else {
throw new Error(
`Could not determine yarn.js path from yarn script ${yarnScript}`
);
}
}

yarnJsFile = Path.resolve(yarnDir, "yarn.js");

// Store the actual Yarn dir in an env var so that child processes can pick it up
process.env.jsig_yarn_js_file = yarnJsFile;
} catch (e) {
// TODO: Add a way of specifying the yarn executable
console.error(
"🛑 JSInstallGuard: Could not find yarn executable using 'which yarn'"
);
console.error("🛑 JSInstallGuard:", e.message);
process.exit(1);
}
} catch (e) {
// TODO: Add a way of specifying the yarn executable
console.error(
"🛑 JSInstallGuard: Could not find yarn executable using 'which yarn'"
);
console.error("🛑 JSInstallGuard:", e.message);
process.exit(1);
}

jsig.init(false);

require(Path.resolve(yarnDir, "yarn.js"));
require(yarnJsFile);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsinstallguard",
"version": "0.1.5",
"version": "0.1.6",
"description": "Small security wrapper around the package manager which intercepts any preistall and postinstall scripts.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 1e174d4

Please sign in to comment.