Skip to content

Commit 5c68d0f

Browse files
committed
add exclude option
1 parent d1e749b commit 5c68d0f

File tree

5 files changed

+73
-26
lines changed

5 files changed

+73
-26
lines changed

README.md

+29-6
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ $ less2scss
2626

2727
Will print an help:
2828

29-
3029
```
3130
Usage: less2scss [options]
3231
3332
This utility quickly converts all your less files to scss.
3433
3534
Options:
36-
-V, --version output the version number
37-
-s, --src <sourcePaths> source paths comma separated
38-
-d, --dst <dstPath> destination path
39-
-r, --recursive allow to recursively walk directories (default: false)
40-
-h, --help display help for command
35+
-V, --version output the version number
36+
-s, --src <sourcePaths> comma separated source paths
37+
-d, --dst <dstPath> destination path
38+
-e, --exclude <excludePaths> comma separated exclude paths
39+
-r, --recursive allow to recursively walk directories (default: false)
40+
-h, --help display help for command
4141
```
4242

4343
Options
@@ -48,8 +48,31 @@ You can pass the following options via CLI arguments.
4848
| --------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------------------ | ------------------ | ------------------ |
4949
| Comma separated LESS files and paths | `-s` | `--src` | Yes | - |
5050
| Destination path for converted SCSS files (if provided). If not provided, it will be the same directory | `-d` | `--dst` | No | Same directory of files provided |
51+
| Comma separated exclude paths | `-e` | `--exclude` | No | - |
5152
| Allow to recursively walk directories | `-r` | `--recursive` | No | false |
5253

54+
**Note:**
55+
56+
For excluding files and path we use [ignore](https://www.npmjs.com/package/ignore) package.
57+
58+
_`ignore` is a manager, filter and parser which implemented in pure JavaScript according to the .gitignore spec 2.22.1.
59+
60+
**All paths provided must be relative to the source path**
61+
62+
Examples
63+
--------
64+
65+
#### Convert LESS files inside a folder excluding `node_modules` and `vendor` subfolders.
66+
67+
```bash
68+
$ less2scss -s ./less_folder -d ./scss_folder -r -e 'node_modules,vendor'
69+
```
70+
71+
#### Convert LESS files inside a folder excluding all subfolders whose name begins with `test`.
72+
73+
```bash
74+
$ less2scss -s ./less_folder -d ./scss_folder -r -e 'node_modules,vendor'
75+
```
5376

5477
Notice
5578
--------

bin/less2scss

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ commander
1010
.description('This utility quickly converts all your less files to scss.')
1111
.option('-s, --src <sourcePaths>', 'comma separated source paths')
1212
.option('-d, --dst <dstPath>', 'destination path')
13+
.option('-e, --exclude <excludePaths>', 'comma separated exclude paths')
1314
.option('-r, --recursive', 'allow to recursively walk directories', false)
1415
.parse(process.argv);
1516

@@ -19,9 +20,10 @@ const exec = () => {
1920

2021
const src = opts.src,
2122
dst = opts.dst,
22-
recursive = opts.recursive
23+
recursive = opts.recursive,
24+
exclude = opts.exclude;
2325

24-
less2scss(src, dst, recursive);
26+
less2scss(src, dst, recursive, exclude);
2527

2628
}
2729
else {

index.js

+27-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const fs = require('fs'),
55
os = require('os'),
66
colors = require('colors/safe'),
77
replaceAll = require('string.prototype.replaceall');
8+
const ignore = require("ignore");
89

910
const MESSAGE_PREFIX = {
1011
INFO: colors.yellow('[INFO]'),
@@ -13,15 +14,19 @@ const MESSAGE_PREFIX = {
1314
};
1415

1516

16-
const less2scss = (src, dst, recursive) => {
17+
const less2scss = (src, dst, recursive, exclude) => {
1718
if (src) {
1819

19-
console.info(`${MESSAGE_PREFIX.INFO} ${colors.yellow(`Recursive option is ${colors.yellow.bold(recursive ? 'ON' : 'OFF')}`)}`);
20+
const pathList = src.split(','),
21+
excludedPaths = exclude && exclude.length > 0 ? exclude.split(',') : [];
2022

21-
const pathList = src.split(',');
2223
let lessFiles = [],
2324
destinationPath = dst;
2425

26+
console.info(`${MESSAGE_PREFIX.INFO} ${colors.yellow(`Recursive option is ${colors.yellow.bold(recursive ? 'ON' : 'OFF')}`)}`);
27+
console.info(`${MESSAGE_PREFIX.INFO} ${colors.yellow(`Excluded paths: ${excludedPaths.length}`)}`);
28+
29+
2530
pathList.forEach(beginPath => {
2631

2732
beginPath && beginPath.trim();
@@ -31,16 +36,27 @@ const less2scss = (src, dst, recursive) => {
3136
}
3237

3338
beginPath = path.resolve(beginPath);
34-
3539
let curPathType = fs.lstatSync(beginPath);
3640

3741
if (curPathType.isDirectory()) {
38-
lessFiles = [...lessFiles, ...glob
39-
.sync(`${beginPath}/${recursive ? '**/*' : '*'}.less`)
40-
.map(lessFile => ({
41-
src: lessFile,
42-
relativePath: path.relative(beginPath, lessFile)
43-
}))];
42+
43+
let currLessFiles = ignore()
44+
.add(excludedPaths).filter(
45+
glob.sync(`${beginPath}/${recursive ? '**/*' : '*'}.less`, {
46+
mark: true,
47+
onlyFiles: true
48+
}).map(
49+
p => path.relative(beginPath, p)
50+
)
51+
).map(
52+
lessFile => ({
53+
src: path.join(beginPath, lessFile),
54+
relativePath: lessFile
55+
})
56+
)
57+
58+
lessFiles = [...lessFiles, ...currLessFiles];
59+
4460
}
4561

4662
if (curPathType.isFile()) {
@@ -150,7 +166,7 @@ const writeFile = (file, scssContent, destinationPath, relativePath) => {
150166

151167
if (destinationPath) {
152168

153-
const newPath = relativePath !== '' ? path.dirname( destinationPath+'/'+relativePath ) : destinationPath;
169+
const newPath = relativePath !== '' ? path.dirname(destinationPath + '/' + relativePath) : destinationPath;
154170

155171
if (!fs.existsSync(newPath)) {
156172
mkdirp.sync(newPath);

package-lock.json

+9-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "less2scss",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "This utility quickly converts all your less files to scss.",
55
"main": "index.js",
66
"bin": {
@@ -24,13 +24,14 @@
2424
"convertor"
2525
],
2626
"bugs": {
27-
"url": "https://github.com/debbba/less2scss/issues"
27+
"url": "https://github.com/debba/less2scss/issues"
2828
},
2929
"homepage": "https://www.debbaweb.it",
3030
"dependencies": {
3131
"colors": "^1.4.0",
32-
"commander": "^7.2.0",
32+
"commander": "^8.1.0",
3333
"glob": "^7.1.7",
34+
"ignore": "^5.1.8",
3435
"mkdirp": "^1.0.4",
3536
"string.prototype.replaceall": "^1.0.5"
3637
}

0 commit comments

Comments
 (0)