From 22bad0fd21fad30da61ca29cc3623f5481be2bd3 Mon Sep 17 00:00:00 2001 From: Amphiluke Date: Sat, 22 Jun 2019 18:54:32 +0700 Subject: [PATCH 1/5] Implement command line interface for the module --- .eslintrc.json | 4 ++ bin/less-compile-roots.js | 80 +++++++++++++++++++++++++++++++++++++++ package-lock.json | 2 +- package.json | 8 +++- 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 bin/less-compile-roots.js diff --git a/.eslintrc.json b/.eslintrc.json index f19a2bb..1d1e020 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -73,6 +73,10 @@ "new-parens": [ "error" ], + "no-console": [ + "error", + {"allow": ["info", "warn", "error"]} + ], "no-multi-spaces": [ "error" ], diff --git a/bin/less-compile-roots.js b/bin/less-compile-roots.js new file mode 100644 index 0000000..0f7bfd4 --- /dev/null +++ b/bin/less-compile-roots.js @@ -0,0 +1,80 @@ +#!/usr/bin/env node + +let lessCompileRoots = require("less-compile-roots"); +let {name, version, description, homepage} = require("../package.json"); + +let handlers = { + help() { + console.info(` +${name} v${version} +${description} +${homepage} + +Usage: + less-compile-roots --pattern= + less-compile-roots --config= + less-compile-roots --help + less-compile-roots --version + +Options: + --pattern= Glob pattern (or several comma-separated patterns) + --config= Use config from the specified file + --help Display usage info + --version Print the installed package version`); + }, + + version() { + console.info(version); + }, + + compile(config) { + console.info("Compiling, please wait..."); + lessCompileRoots.compileRoots(config) + .then(() => { + console.info("Done!"); + }) + .catch(reason => { + console.error(reason); + }); + } +}; + +let [,, ...args] = process.argv; +function getArg(name) { + for (let arg of args) { + if (arg === name) { + return true; + } + if (arg.startsWith(name + "=")) { + return arg.slice(name.length + 1); + } + } + return undefined; +} + +(() => { + if (getArg("--help")) { + handlers.help(); + return; + } + + if (getArg("--version")) { + handlers.version(); + return; + } + + let pattern = getArg("--pattern"); + let configPath = getArg("--config"); + if (pattern) { + if (configPath) { + console.warn("The ‘--config’ option is ignored when the ‘--pattern’ option is present!"); + } + handlers.compile({pattern: pattern.split(",")}); + } else if (configPath) { + handlers.compile(require(configPath)); + } else { + console.error("You must either specify the file pattern or provide the config file path"); + console.info("Run ‘less-compile-roots --help’ to get usage info"); + process.exitCode = 1; + } +})(); diff --git a/package-lock.json b/package-lock.json index 81645c9..8d84287 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "less-compile-roots", - "version": "0.1.0", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 9e12130..feaec3d 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,12 @@ { "name": "less-compile-roots", - "version": "0.1.0", + "version": "0.2.0", "description": "Tool for extracting and compiling root Less files", "main": "dist/index.js", "module": "src/index.mjs", + "bin": { + "less-compile-roots": "./bin/less-compile-roots.js" + }, "scripts": { "lint": "eslint src/**/*.mjs", "build": "rollup -c" @@ -13,7 +16,8 @@ "url": "git+https://github.com/Amphiluke/less-compile-roots.git" }, "keywords": [ - "less" + "less", + "glob" ], "author": "Amphiluke", "license": "MIT", From d6ad2187b95f9a7d55d9f5c7330c3fc575550ab3 Mon Sep 17 00:00:00 2001 From: Amphiluke Date: Sat, 22 Jun 2019 19:26:29 +0700 Subject: [PATCH 2/5] Fix for the config path handling routine --- bin/less-compile-roots.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/less-compile-roots.js b/bin/less-compile-roots.js index 0f7bfd4..ccaeda0 100644 --- a/bin/less-compile-roots.js +++ b/bin/less-compile-roots.js @@ -71,6 +71,8 @@ function getArg(name) { } handlers.compile({pattern: pattern.split(",")}); } else if (configPath) { + let path = require("path"); + configPath = path.join(process.cwd(), configPath); handlers.compile(require(configPath)); } else { console.error("You must either specify the file pattern or provide the config file path"); From 61498199158a5e911544c1adb4d70e5332a762b7 Mon Sep 17 00:00:00 2001 From: Amphiluke Date: Sat, 22 Jun 2019 19:30:31 +0700 Subject: [PATCH 3/5] Update dependencies --- package-lock.json | 107 +++++++++++++++++++++++++++------------------- package.json | 4 +- 2 files changed, 66 insertions(+), 45 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8d84287..d2f452f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -54,9 +54,9 @@ "dev": true }, "@types/node": { - "version": "12.0.8", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.0.8.tgz", - "integrity": "sha512-b8bbUOTwzIY3V5vDTY1fIJ+ePKDUBqt2hC2woVGotdQQhG/2Sh62HOKHrT7ab+VerXAcPyAiTEipPu/FsreUtg==", + "version": "12.0.10", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.0.10.tgz", + "integrity": "sha512-LcsGbPomWsad6wmMNv7nBLw7YYYyfdYcz6xryKYQhx89c3XXan+8Q6AJ43G5XDIaklaVkK3mE4fCb0SBvMiPSQ==", "dev": true }, "acorn": { @@ -215,6 +215,15 @@ "which": "^1.2.9" } }, + "debug": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", + "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "dev": true, + "requires": { + "ms": "^2.1.1" + } + }, "deep-is": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz", @@ -243,13 +252,13 @@ "dev": true }, "eslint": { - "version": "5.16.0", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-5.16.0.tgz", - "integrity": "sha512-S3Rz11i7c8AA5JPv7xAH+dOyq/Cu/VXHiHXBPOU1k/JAM5dXqQPt3qcrhpHSorXmrpu2g0gkIBVXAqCpzfoZIg==", + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-6.0.0.tgz", + "integrity": "sha512-SrrIfcd4tOgsspOKTSwamuTOAMZOUigHQhVMrzNjz4/B9Za6SHQDIocMIyIDfwDgx6MhS15nS6HC8kumCV2qBQ==", "dev": true, "requires": { "@babel/code-frame": "^7.0.0", - "ajv": "^6.9.1", + "ajv": "^6.10.0", "chalk": "^2.1.0", "cross-spawn": "^6.0.5", "debug": "^4.0.1", @@ -257,18 +266,19 @@ "eslint-scope": "^4.0.3", "eslint-utils": "^1.3.1", "eslint-visitor-keys": "^1.0.0", - "espree": "^5.0.1", + "espree": "^6.0.0", "esquery": "^1.0.1", "esutils": "^2.0.2", "file-entry-cache": "^5.0.1", "functional-red-black-tree": "^1.0.1", - "glob": "^7.1.2", + "glob-parent": "^3.1.0", "globals": "^11.7.0", "ignore": "^4.0.6", "import-fresh": "^3.0.0", "imurmurhash": "^0.1.4", "inquirer": "^6.2.2", - "js-yaml": "^3.13.0", + "is-glob": "^4.0.0", + "js-yaml": "^3.13.1", "json-stable-stringify-without-jsonify": "^1.0.1", "levn": "^0.3.0", "lodash": "^4.17.11", @@ -276,7 +286,6 @@ "mkdirp": "^0.5.1", "natural-compare": "^1.4.0", "optionator": "^0.8.2", - "path-is-inside": "^1.0.2", "progress": "^2.0.0", "regexpp": "^2.0.1", "semver": "^5.5.1", @@ -286,20 +295,26 @@ "text-table": "^0.2.0" }, "dependencies": { - "debug": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", - "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "glob-parent": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-3.1.0.tgz", + "integrity": "sha1-nmr2KZ2NO9K9QEMIMr0RPfkGxa4=", "dev": true, "requires": { - "ms": "^2.1.1" + "is-glob": "^3.1.0", + "path-dirname": "^1.0.0" + }, + "dependencies": { + "is-glob": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-3.1.0.tgz", + "integrity": "sha1-e6WuJCF4BKxwcHuWkiVnSGzD6Eo=", + "dev": true, + "requires": { + "is-extglob": "^2.1.0" + } + } } - }, - "ms": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true } } }, @@ -326,9 +341,9 @@ "dev": true }, "espree": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/espree/-/espree-5.0.1.tgz", - "integrity": "sha512-qWAZcWh4XE/RwzLJejfcofscgMc9CamR6Tn1+XRXNzrvUSSbiAjGOI/fggztjIi7y9VLPqnICMIPiGyr8JaZ0A==", + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/espree/-/espree-6.0.0.tgz", + "integrity": "sha512-lJvCS6YbCn3ImT3yKkPe0+tJ+mH6ljhGNjHQH9mRtiO6gjhVAOhVXW1yjnwqGwTkK3bGbye+hb00nFNmu0l/1Q==", "dev": true, "requires": { "acorn": "^6.0.7", @@ -553,15 +568,15 @@ } }, "inherits": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", - "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", "dev": true }, "inquirer": { - "version": "6.3.1", - "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-6.3.1.tgz", - "integrity": "sha512-MmL624rfkFt4TG9y/Jvmt8vdmOo836U7Y0Hxr2aFk3RelZEGX4Igk0KabWrcaaZaTv9uzglOqWh1Vly+FAWAXA==", + "version": "6.4.1", + "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-6.4.1.tgz", + "integrity": "sha512-/Jw+qPZx4EDYsaT6uz7F4GJRNFMRdKNeUZw3ZnKV8lyuUgz/YWRCSUAJMZSVhSq4Ec0R2oYnyi6b3d4JXcL5Nw==", "dev": true, "requires": { "ansi-escapes": "^3.2.0", @@ -720,6 +735,12 @@ "minimist": "0.0.8" } }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true + }, "mute-stream": { "version": "0.0.7", "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.7.tgz", @@ -785,18 +806,18 @@ "callsites": "^3.0.0" } }, + "path-dirname": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/path-dirname/-/path-dirname-1.0.2.tgz", + "integrity": "sha1-zDPSTVJeCZpTiMAzbG4yuRYGCeA=", + "dev": true + }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", "dev": true }, - "path-is-inside": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/path-is-inside/-/path-is-inside-1.0.2.tgz", - "integrity": "sha1-NlQX3t5EQw0cEa9hAn+s8HS9/FM=", - "dev": true - }, "path-key": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", @@ -863,9 +884,9 @@ } }, "rollup": { - "version": "1.15.6", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.15.6.tgz", - "integrity": "sha512-s3Vn3QJQ5YVFfIG4nXoG9VdL1I37IZsft+4ZyeBhxE0df1kCFz9e+4bEAbR4mKH3pvBO9e9xjdxWPhhIp0r9ow==", + "version": "1.16.2", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.16.2.tgz", + "integrity": "sha512-UAZxaQvH0klYZdF+90xv9nGb+m4p8jdoaow1VL5/RzDK/gN/4CjvaMmJNcOIv1/+gtzswKhAg/467mzF0sLpAg==", "dev": true, "requires": { "@types/estree": "0.0.39", @@ -981,9 +1002,9 @@ } }, "table": { - "version": "5.4.0", - "resolved": "https://registry.npmjs.org/table/-/table-5.4.0.tgz", - "integrity": "sha512-nHFDrxmbrkU7JAFKqKbDJXfzrX2UBsWmrieXFTGxiI5e4ncg3VqsZeI4EzNmX0ncp4XNGVeoxIWJXfCIXwrsvw==", + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/table/-/table-5.4.1.tgz", + "integrity": "sha512-E6CK1/pZe2N75rGZQotFOdmzWQ1AILtgYbMAbAjvms0S1l5IDB47zG3nCnFGB/w+7nB3vKofbLXCH7HPBo864w==", "dev": true, "requires": { "ajv": "^6.9.1", diff --git a/package.json b/package.json index feaec3d..f48deb3 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,8 @@ "fast-glob": "^3.0.1" }, "devDependencies": { - "eslint": "^5.16.0", - "rollup": "^1.15.6" + "eslint": "^6.0.0", + "rollup": "^1.16.2" }, "peerDependencies": { "less": ">=2.0.0" From fcb0feb13f266013db95c07a7bce5bd312e812b8 Mon Sep 17 00:00:00 2001 From: Amphiluke Date: Sat, 22 Jun 2019 20:23:13 +0700 Subject: [PATCH 4/5] Documentation for command line usage --- README.md | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d81adfc..6f899ed 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ npm install less-compile-roots ## Usage -Here is a basic usage example: +Here is a basic programmatic usage example: ```javascript let lessCompileRoots = require("less-compile-roots"); @@ -35,6 +35,8 @@ lessCompileRoots.compileRoots({ }); ``` +If you prefer using the tool through the command line, please refer the [Command line usage](#command-line-usage) section. + ## API The following methods are exported by the `less-compile-roots` module: @@ -45,14 +47,49 @@ The method picks out the root Less files from all files matching the provided gl The supported options are: -* `pattern` _(required)_: a glob pattern (or a list of patterns) matching your source Less files. Please refer the [fast-glob docs](https://github.com/mrmlnc/fast-glob#patterns) for details. -* `lessOptions` _(optional)_: the options object to pass to the [`less.render` method](http://lesscss.org/usage/#programmatic-usage). The [available options](http://lesscss.org/usage/#less-options) are listed in the official Less documentation. +* `pattern` _(required)_: a glob pattern (or a list of patterns) matching your source Less files. Please refer the [fast-glob docs](https://github.com/mrmlnc/fast-glob#patterns) for details; +* `lessOptions` _(optional)_: the options object to pass to the [`less.render` method](http://lesscss.org/usage/#programmatic-usage). The [available options](http://lesscss.org/usage/#less-options) are listed in the official Less documentation; * `globOptions` _(optional)_: the options object to pass to the fast-glob function. See their [docs](https://github.com/mrmlnc/fast-glob#options-1) for details. ### `getRoots(options)` This methods just returns a Promise that resolves with a list of the root file paths. It accepts the same options as the [`compileRoots`](#compilerootsoptions) method except the `lessOptions` parameter. This method may be useful if you just need to get the list of root Less files without compiling them. +## Command line usage + +You may also use `less-compile-roots` through the command line interface: + +```sh +# Compile root files using default options +less-compile-roots --pattern=src/**/*.less + +# or use custom config from a specified file +less-compile-roots --config=less-compile-config.js +``` + +Available options: + +* `--pattern=`: a glob pattern (or several comma-separated patterns) matching your source Less files; +* `--config=`: path to a config module; +* `--help`: print CLI usage info; +* `--version`: print the installed package version. + +Note that you cannot use the options `--pattern` and `--config` together. Specifying the `--pattern` option makes the module compile Less files using all default parameters. If you need to customize the parameters, create a config file and specify the path to it through the `--config` option (or just use the module [programmatically](#api) rather than in command line). Here is an example of such config file: + +```javascript +let LessPlugin = require('less-plugin-myplugin'); +module.exports = { + pattern: ["project-1/css/**/*.less", "project-2/css/**/*.less"], + lessOptions: { + plugins: [LessPlugin], + sourceMap: {sourceMapFileInline: true}, + urlArgs: "t=" + Date.now() + } +}; +``` + +In fact, the config module just exports an object which is used as the `options` parameter for the [compileRoots](#compilerootsoptions) method. + ## Requirements * NodeJS engine v9.11.2+ From 80d03785a0e549013fda22ca641b313afbe98960 Mon Sep 17 00:00:00 2001 From: Amphiluke Date: Sat, 22 Jun 2019 20:31:15 +0700 Subject: [PATCH 5/5] Minor content correction --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6f899ed..0216ab2 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ module.exports = { }; ``` -In fact, the config module just exports an object which is used as the `options` parameter for the [compileRoots](#compilerootsoptions) method. +In fact, the config module just exports an object which is then used as the `options` parameter for the [compileRoots](#compilerootsoptions) method. ## Requirements