Skip to content

Commit

Permalink
Merge branch 'v0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Amphiluke committed Jun 22, 2019
2 parents ccb3fa5 + 80d0378 commit 15dbe13
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 51 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
"new-parens": [
"error"
],
"no-console": [
"error",
{"allow": ["info", "warn", "error"]}
],
"no-multi-spaces": [
"error"
],
Expand Down
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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:
Expand All @@ -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=<glob>`: a glob pattern (or several comma-separated patterns) matching your source Less files;
* `--config=<path>`: 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 then used as the `options` parameter for the [compileRoots](#compilerootsoptions) method.

## Requirements

* NodeJS engine v9.11.2+
Expand Down
82 changes: 82 additions & 0 deletions bin/less-compile-roots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/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=<glob>
less-compile-roots --config=<path>
less-compile-roots --help
less-compile-roots --version
Options:
--pattern=<glob> Glob pattern (or several comma-separated patterns)
--config=<path> 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) {
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");
console.info("Run ‘less-compile-roots --help’ to get usage info");
process.exitCode = 1;
}
})();
109 changes: 65 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 15dbe13

Please sign in to comment.