From e3ae917a1c1c6727efdc5b20f0c33c001db21083 Mon Sep 17 00:00:00 2001 From: Tatsuyuki Ishi Date: Mon, 25 Jun 2018 16:11:38 +0900 Subject: [PATCH] Use NPM packages for executing stylus --- .gitignore | 55 +++++++++++++++++++++ CONTRIBUTING.md | 10 +++- appveyor.yml | 4 +- build.rs | 62 ++++++----------------- ci/github_pages.sh | 2 +- package.json | 6 +++ yarn.lock | 119 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 207 insertions(+), 51 deletions(-) create mode 100644 package.json create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore index 1bf91cda47..d94943a836 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,58 @@ book-example/book .vscode tests/dummy_book/book/ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e703b00322..e9ac10d975 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -58,10 +58,16 @@ the CSS files will be overwritten when compiling the stylus files. Instead, you [stylus files](https://github.com/rust-lang-nursery/mdBook/tree/master/src/theme/stylus) and regenerate the CSS. For this to work, you first need [Node and NPM](https://nodejs.org/en/) installed on your machine. -Then run the following command to install both [stylus](http://stylus-lang.com/) and [nib](https://tj.github.io/nib/), you might need `sudo` to install successfully. +Then run the following command to install the necessary Node.js packages. ``` -npm install -g stylus nib +npm install +``` + +Or if you have [yarn](https://yarnpkg.com): + +``` +yarn ``` When that finished, you can simply regenerate the CSS files by building mdBook with the following command: diff --git a/appveyor.yml b/appveyor.yml index 6ab525009a..5bf6cc64ad 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -34,8 +34,8 @@ install: - cargo -V - ps: Install-Product node $env:nodejs_version - node --version - - npm --version - - npm install -g stylus nib + - yarn --version + - yarn --frozen-lockfile build: false diff --git a/build.rs b/build.rs index 1369749104..fdc88013d9 100644 --- a/build.rs +++ b/build.rs @@ -1,25 +1,28 @@ // build.rs use std::env; -use std::path::Path; +use std::path::{Path, PathBuf}; #[macro_use] extern crate error_chain; #[cfg(windows)] mod execs { + use std::ffi::OsStr; use std::process::Command; - pub fn cmd(program: &str) -> Command { + pub fn cmd(program: &OsStr) -> Command { let mut cmd = Command::new("cmd"); - cmd.args(&["/c", program]); + cmd.arg("/c"); + cmd.arg(program); cmd } } #[cfg(not(windows))] mod execs { + use std::ffi::OsStr; use std::process::Command; - pub fn cmd(program: &str) -> Command { + pub fn cmd(program: &OsStr) -> Command { Command::new(program) } } @@ -30,59 +33,26 @@ error_chain!{ } } -fn program_exists(program: &str) -> Result<()> { - execs::cmd(program) - .arg("-v") - .output() - .chain_err(|| format!("Please install '{}'!", program))?; - Ok(()) -} - -fn npm_package_exists(package: &str) -> Result<()> { - let status = execs::cmd("npm") - .args(&["list", "-g"]) - .arg(package) - .output(); - - match status { - Ok(ref out) if out.status.success() => Ok(()), - _ => bail!( - "Missing npm package '{0}' install with: 'npm -g install {0}'", - package - ), - } -} - -pub enum Resource<'a> { - Program(&'a str), - Package(&'a str), -} -use Resource::{Package, Program}; - -impl<'a> Resource<'a> { - pub fn exists(&self) -> Result<()> { - match *self { - Program(name) => program_exists(name), - Package(name) => npm_package_exists(name), - } +fn node_modules_exists() -> Result<()> { + if Path::new("node_modules").exists() { + Ok(()) + } else { + bail!("`node_modules` does not exist. Please run `yarn install`") } } fn run() -> Result<()> { if let Ok(_) = env::var("CARGO_FEATURE_REGENERATE_CSS") { - // Check dependencies - Program("npm").exists()?; - Program("node").exists().or(Program("nodejs").exists())?; - Package("nib").exists()?; - Package("stylus").exists()?; - + node_modules_exists()?; // Compile stylus stylesheet to css let manifest_dir = env::var("CARGO_MANIFEST_DIR") .chain_err(|| "Please run the script with: 'cargo build'!")?; let theme_dir = Path::new(&manifest_dir).join("src/theme/"); let stylus_dir = theme_dir.join("stylus/book.styl"); - if !execs::cmd("stylus") + let stylus_path: PathBuf = [".", "node_modules", ".bin", "stylus"].iter().collect(); + + if !execs::cmd(stylus_path.as_os_str()) .arg(stylus_dir) .arg("--out") .arg(theme_dir) diff --git a/ci/github_pages.sh b/ci/github_pages.sh index 040ab20a70..ee4ec8f226 100644 --- a/ci/github_pages.sh +++ b/ci/github_pages.sh @@ -12,7 +12,7 @@ if [ "$TRAVIS_PULL_REQUEST" != "false" ] || fi # Make sure we have the css dependencies -npm install -g stylus nib +yarn --frozen-lockfile NC='\033[39m' CYAN='\033[36m' diff --git a/package.json b/package.json new file mode 100644 index 0000000000..1b60d133da --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "devDependencies": { + "nib": "^1.1.2", + "stylus": "^0.54.5" + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000000..1b0e233daf --- /dev/null +++ b/yarn.lock @@ -0,0 +1,119 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +amdefine@>=0.0.4: + version "1.0.1" + resolved "https://registry.yarnpkg.com/amdefine/-/amdefine-1.0.1.tgz#4a5282ac164729e93619bcfd3ad151f817ce91f5" + +balanced-match@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" + +brace-expansion@^1.1.7: + version "1.1.11" + resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" + dependencies: + balanced-match "^1.0.0" + concat-map "0.0.1" + +concat-map@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" + +css-parse@1.7.x: + version "1.7.0" + resolved "https://registry.yarnpkg.com/css-parse/-/css-parse-1.7.0.tgz#321f6cf73782a6ff751111390fc05e2c657d8c9b" + +debug@*: + version "3.1.0" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" + dependencies: + ms "2.0.0" + +fs.realpath@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" + +glob@7.0.x: + version "7.0.6" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.0.6.tgz#211bafaf49e525b8cd93260d14ab136152b3f57a" + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.2" + once "^1.3.0" + path-is-absolute "^1.0.0" + +inflight@^1.0.4: + version "1.0.6" + resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" + dependencies: + once "^1.3.0" + wrappy "1" + +inherits@2: + version "2.0.3" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + +minimatch@^3.0.2: + version "3.0.4" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" + dependencies: + brace-expansion "^1.1.7" + +minimist@0.0.8: + version "0.0.8" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" + +mkdirp@0.5.x: + version "0.5.1" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" + dependencies: + minimist "0.0.8" + +ms@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" + +nib@^1.1.2: + version "1.1.2" + resolved "https://registry.yarnpkg.com/nib/-/nib-1.1.2.tgz#6a69ede4081b95c0def8be024a4c8ae0c2cbb6c7" + dependencies: + stylus "0.54.5" + +once@^1.3.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" + dependencies: + wrappy "1" + +path-is-absolute@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" + +sax@0.5.x: + version "0.5.8" + resolved "https://registry.yarnpkg.com/sax/-/sax-0.5.8.tgz#d472db228eb331c2506b0e8c15524adb939d12c1" + +source-map@0.1.x: + version "0.1.43" + resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.1.43.tgz#c24bc146ca517c1471f5dacbe2571b2b7f9e3346" + dependencies: + amdefine ">=0.0.4" + +stylus@0.54.5, stylus@^0.54.5: + version "0.54.5" + resolved "https://registry.yarnpkg.com/stylus/-/stylus-0.54.5.tgz#42b9560931ca7090ce8515a798ba9e6aa3d6dc79" + dependencies: + css-parse "1.7.x" + debug "*" + glob "7.0.x" + mkdirp "0.5.x" + sax "0.5.x" + source-map "0.1.x" + +wrappy@1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"