From 0a8eb2102dbe1bcaa3ad75982a72508055d7c71f Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 31 May 2019 15:35:22 +0100 Subject: [PATCH 1/2] fix(npm): various fixes --- npm/install-wrangler.js | 4 +++- npm/package.json | 4 ++-- npm/run-wrangler.js | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) mode change 100644 => 100755 npm/install-wrangler.js create mode 100755 npm/run-wrangler.js diff --git a/npm/install-wrangler.js b/npm/install-wrangler.js old mode 100644 new mode 100755 index a42990b54..094b7bf7e --- a/npm/install-wrangler.js +++ b/npm/install-wrangler.js @@ -40,6 +40,8 @@ function downloadAsset(asset) { } mkdirSync(dest); + console.log("Downloading release", asset.browser_download_url); + return axios({ url: asset.browser_download_url, responseType: "stream" @@ -66,7 +68,7 @@ getLatestRelease() return downloadAsset(compatibleAssets); }) .then(() => { - console.log("wrangler was installed."); + console.log("Wrangler has been installed!"); }) .catch(err => { throw err; diff --git a/npm/package.json b/npm/package.json index 37bd3afe1..5005f838d 100644 --- a/npm/package.json +++ b/npm/package.json @@ -5,10 +5,10 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "postinstall": "node ./install-wrangler.js" + "postinstall": "node install-wrangler.js" }, "bin": { - "wrangler": "./out/wrangler" + "wrangler": "./run-wrangler.js" }, "repository": { "type": "git", diff --git a/npm/run-wrangler.js b/npm/run-wrangler.js new file mode 100755 index 000000000..73a2d0e96 --- /dev/null +++ b/npm/run-wrangler.js @@ -0,0 +1,14 @@ +#!/usr/bin/env node + +const { join, resolve } = require("path"); +const { spawnSync } = require("child_process"); + +const cwd = join(process.cwd(), "node_modules", "@cloudflare", "wrangler"); +const bin = join(cwd, "out", "wrangler"); +const [, , ...args] = process.argv; + +const opts = { + cwd: process.cwd(), + stdio: "inherit" +}; +spawnSync(bin, args, opts); From d4d3d707197474db7a957080c26c1d282832ca65 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Sat, 1 Jun 2019 11:01:34 +0100 Subject: [PATCH 2/2] feat: add flock Avoid multiple install at the same time. --- Cargo.lock | 1 + Cargo.toml | 1 + src/wranglerjs/mod.rs | 10 ++++++++++ 3 files changed, 12 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 7b02cc6da..1778b4fcd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1919,6 +1919,7 @@ dependencies = [ "dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "fs2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.23 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 334f1717b..ff65929f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ toml = "0.5.0" uuid = "0.7" which = "2.0.1" rand = "0.6.5" +fs2 = "0.4.3" [dev-dependencies] assert_cmd = "0.11.1" diff --git a/src/wranglerjs/mod.rs b/src/wranglerjs/mod.rs index 4b4989dfb..9ffa5881a 100644 --- a/src/wranglerjs/mod.rs +++ b/src/wranglerjs/mod.rs @@ -1,6 +1,7 @@ use crate::commands::publish::package::Package; use crate::install; use binary_install::Cache; +use fs2::FileExt; use log::info; use rand::distributions::Alphanumeric; use rand::{thread_rng, Rng}; @@ -191,6 +192,11 @@ pub fn run_build( // Run {npm install} in the specified directory. Skips the install if a // {node_modules} is found in the directory. pub fn run_npm_install(dir: PathBuf) -> Result<(), failure::Error> { + let flock_path = dir.join(&".install.lock"); + let flock = File::create(&flock_path)?; + // avoid running multiple {npm install} at the same time (eg. in tests) + flock.lock_exclusive()?; + if dir.join("node_modules").exists() { info!("skipping npm install because node_modules exists"); return Ok(()); @@ -202,6 +208,10 @@ pub fn run_npm_install(dir: PathBuf) -> Result<(), failure::Error> { info!("Running {:?} in directory {:?}", command, dir); let status = command.status()?; + + flock.unlock()?; + fs::remove_file(&flock_path)?; + if status.success() { Ok(()) } else {