From 1113135bfea72879b69edb42fb28bf4479bbdde7 Mon Sep 17 00:00:00 2001 From: Shun Sato Date: Sun, 7 Oct 2018 17:15:42 +0900 Subject: [PATCH] Support Hugo extended (#65) * wip: Support Hugo extended * fallback support, update readme.md * fixup! wip: Support Hugo extended --- README.md | 42 +++++++++++++++++++++++ cli.js | 0 index.js | 2 +- lib/index.js | 26 ++++++++++++-- lib/install.js | 21 +++++++++++- package-lock.json | 86 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 5 +-- 7 files changed, 175 insertions(+), 7 deletions(-) mode change 100644 => 100755 cli.js diff --git a/README.md b/README.md index d5fde293..e681748b 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ npm install --save-dev hugo-bin ``` +hugo-bin now supports [Extended version of Hugo](https://github.com/gohugoio/hugo/releases/tag/v0.43). See [Installation options](#installation-options) for more details. + ## Usage ### API @@ -42,6 +44,46 @@ npm run create -- 'post/my-new-post' # see below 'npm-run-script' See the [Hugo Documentation](https://gohugo.io/) for more information. +## Installation options + +hugo-bin supports options to change the variation of Hugo binaries. + +Each option can be configured in the `hugo-bin` section of your `package.json`: + +```json +{ + "name": "your-package", + "version": "0.0.1", + "hugo-bin": { + "buildTags": "extended" + } +} +``` + +Also as local or global [.npmrc](https://docs.npmjs.com/files/npmrc) configuration file: + +```ini +hugo_bin_build_tags = "extended" +``` + +Also as environment variable: + +```sh +export HUGO_BIN_BUILD_TAGS="extended" +``` + +**Note that you have to run `npm install hugo-bin` to re-install hugo-bin itself, if you change any one of these options.** + +### Options + +#### buildTags + +Default: `""` + +Set it to `extended` to download the [extended version](https://github.com/gohugoio/hugo/releases/tag/v0.43) binary. + +If this is set to `extended` but it's not available for the user's platform, then the normal version will be downloaded instead. + ## Supported versions See [the package.json commit history](https://github.com/fenneclab/hugo-bin/commits/master/package.json). diff --git a/cli.js b/cli.js old mode 100644 new mode 100755 diff --git a/index.js b/index.js index 3ffdc83e..77e8c708 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -module.exports = require('./lib').path(); +module.exports = require('./lib')(process.cwd()).path(); diff --git a/lib/index.js b/lib/index.js index a7c6487e..cf5f9c6d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,6 @@ const path = require('path'); const BinWrapper = require('bin-wrapper'); +const pkgConf = require('pkg-conf'); const pkg = require('../package'); const hugoVersion = pkg.hugoVersion; @@ -29,9 +30,23 @@ const binNameMap = { x64: 'hugo.exe' } }; +const dest = path.join(__dirname, '../vendor'); const binName = (binNameMap[process.platform] && binNameMap[process.platform][process.arch]) || ''; - -module.exports = new BinWrapper() +const extendedBin = new BinWrapper() + .src(`${baseUrl}hugo_extended_${hugoVersion}_Linux-64bit.tar.gz`, 'linux', 'x64') + .src(`${baseUrl}hugo_extended_${hugoVersion}_macOS-64bit.tar.gz`, 'darwin', 'x64') + .src(`${baseUrl}hugo_extended_${hugoVersion}_Windows-64bit.zip`, 'win32', 'x64') + // Falling back to normal version on non supported platforms + .src(`${baseUrl}hugo_${hugoVersion}_FreeBSD-32bit.tar.gz`, 'freebsd', 'x86') + .src(`${baseUrl}hugo_${hugoVersion}_FreeBSD-64bit.tar.gz`, 'freebsd', 'x64') + .src(`${baseUrl}hugo_${hugoVersion}_FreeBSD-ARM.tar.gz`, 'freebsd', 'arm') + .src(`${baseUrl}hugo_${hugoVersion}_Linux-32bit.tar.gz`, 'linux', 'x86') + .src(`${baseUrl}hugo_${hugoVersion}_Linux-ARM.tar.gz`, 'linux', 'arm') + .src(`${baseUrl}hugo_${hugoVersion}_macOS-32bit.tar.gz`, 'darwin', 'x86') + .src(`${baseUrl}hugo_${hugoVersion}_Windows-32bit.zip`, 'win32', 'x86') + .dest(dest) + .use(binName); +const normalBin = new BinWrapper() .src(`${baseUrl}hugo_${hugoVersion}_FreeBSD-32bit.tar.gz`, 'freebsd', 'x86') .src(`${baseUrl}hugo_${hugoVersion}_FreeBSD-64bit.tar.gz`, 'freebsd', 'x64') .src(`${baseUrl}hugo_${hugoVersion}_FreeBSD-ARM.tar.gz`, 'freebsd', 'arm') @@ -42,5 +57,10 @@ module.exports = new BinWrapper() .src(`${baseUrl}hugo_${hugoVersion}_macOS-64bit.tar.gz`, 'darwin', 'x64') .src(`${baseUrl}hugo_${hugoVersion}_Windows-32bit.zip`, 'win32', 'x86') .src(`${baseUrl}hugo_${hugoVersion}_Windows-64bit.zip`, 'win32', 'x64') - .dest(path.join(__dirname, '../vendor')) + .dest(dest) .use(binName); +module.exports = projectRoot => { + const config = pkgConf.sync('hugo-bin', { cwd: projectRoot }); + const extended = (process.env.HUGO_BIN_BUILD_TAGS || process.env.npm_config_hugo_bin_build_tags || config.buildTags || '') === 'extended'; + return extended ? extendedBin : normalBin; +}; diff --git a/lib/install.js b/lib/install.js index 7b9dc414..e398ab3c 100644 --- a/lib/install.js +++ b/lib/install.js @@ -1,7 +1,26 @@ +const path = require('path'); const bin = require('./'); const log = require('logalot'); -bin.run(['version']) +function getProjectRoot() { + // projectRoot on postinstall could be INIT_CWD introduced in npm>=5.4 + // see: https://github.com/npm/npm/issues/16990 + const initCwd = process.env.INIT_CWD; + if (initCwd) { + return initCwd; + } + // Fallback of getting INIT_CWD + const cwd = process.cwd(); + const paths = cwd.split(path.sep); + // If process.cwd end with 'node_modules/*' then get the dependent root directory, + // otherwise returns cwd (ordinary it will be the postinstall process of hugo-bin itself). + if (paths.length > 1 && paths[paths.length - 2] === 'node_modules') { + return path.resolve('../../', cwd); + } + return cwd; +} + +bin(getProjectRoot()).run(['version']) .then(() => { log.success('Hugo binary is installed successfully'); }) diff --git a/package-lock.json b/package-lock.json index e1e9a02d..55546bb8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1626,6 +1626,11 @@ "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.0.tgz", "integrity": "sha1-Wx85evx11ne96Lz8Dkfh+aPZqJg=" }, + "json-parse-better-errors": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz", + "integrity": "sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==" + }, "json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", @@ -1683,6 +1688,22 @@ } } }, + "locate-path": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-2.0.0.tgz", + "integrity": "sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=", + "requires": { + "p-locate": "2.0.0", + "path-exists": "3.0.0" + }, + "dependencies": { + "path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" + } + } + }, "lodash": { "version": "4.17.10", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", @@ -1996,6 +2017,22 @@ "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-1.1.0.tgz", "integrity": "sha1-nJRWmJ6fZYgBewQ01WCXZ1w9oF4=" }, + "p-limit": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-1.3.0.tgz", + "integrity": "sha512-vvcXsLAJ9Dr5rQOPk7toZQZJApBl2K4J6dANSsEuh6QI41JYcsS/qhTGa9ErIUUgK3WNQoJYvylxvjqmiqEA9Q==", + "requires": { + "p-try": "1.0.0" + } + }, + "p-locate": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-2.0.0.tgz", + "integrity": "sha1-IKAQOyIqcMj9OcwuWAaA893l7EM=", + "requires": { + "p-limit": "1.3.0" + } + }, "p-map": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/p-map/-/p-map-1.2.0.tgz", @@ -2009,6 +2046,11 @@ "p-finally": "1.0.0" } }, + "p-try": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-1.0.0.tgz", + "integrity": "sha1-y8ec26+P1CKOE/Yh8rGiN8GyB7M=" + }, "package-json": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/package-json/-/package-json-4.0.1.tgz", @@ -2124,6 +2166,50 @@ "pinkie": "2.0.4" } }, + "pkg-conf": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/pkg-conf/-/pkg-conf-2.1.0.tgz", + "integrity": "sha1-ISZRTKbyq/69FoWW3xi6V4Z/AFg=", + "requires": { + "find-up": "2.1.0", + "load-json-file": "4.0.0" + }, + "dependencies": { + "find-up": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", + "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", + "requires": { + "locate-path": "2.0.0" + } + }, + "load-json-file": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz", + "integrity": "sha1-L19Fq5HjMhYjT9U62rZo607AmTs=", + "requires": { + "graceful-fs": "4.1.11", + "parse-json": "4.0.0", + "pify": "3.0.0", + "strip-bom": "3.0.0" + } + }, + "parse-json": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-4.0.0.tgz", + "integrity": "sha1-vjX1Qlvh9/bHRxhPmKeIy5lHfuA=", + "requires": { + "error-ex": "1.3.2", + "json-parse-better-errors": "1.0.2" + } + }, + "strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=" + } + } + }, "pluralize": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-7.0.0.tgz", diff --git a/package.json b/package.json index 031b0802..e7eaaf71 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "hugo-bin", "version": "0.30.0", - "hugoVersion": "0.42.2", + "hugoVersion": "0.43", "description": "Binary wrapper for Hugo", "repository": "fenneclab/hugo-bin", "author": "satoshun00 ", @@ -13,7 +13,8 @@ "dependencies": { "bin-wrapper": "4.0.0", "del-cli": "1.1.0", - "logalot": "2.1.0" + "logalot": "2.1.0", + "pkg-conf": "2.1.0" }, "devDependencies": { "bin-check": "4.1.0",