Skip to content

Commit

Permalink
add exclude option
Browse files Browse the repository at this point in the history
  • Loading branch information
debba committed Jul 29, 2021
1 parent d1e749b commit 5c68d0f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 26 deletions.
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ $ less2scss

Will print an help:


```
Usage: less2scss [options]
This utility quickly converts all your less files to scss.
Options:
-V, --version output the version number
-s, --src <sourcePaths> source paths comma separated
-d, --dst <dstPath> destination path
-r, --recursive allow to recursively walk directories (default: false)
-h, --help display help for command
-V, --version output the version number
-s, --src <sourcePaths> comma separated source paths
-d, --dst <dstPath> destination path
-e, --exclude <excludePaths> comma separated exclude paths
-r, --recursive allow to recursively walk directories (default: false)
-h, --help display help for command
```

Options
Expand All @@ -48,8 +48,31 @@ You can pass the following options via CLI arguments.
| --------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------------------ | ------------------ | ------------------ |
| Comma separated LESS files and paths | `-s` | `--src` | Yes | - |
| 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 |
| Comma separated exclude paths | `-e` | `--exclude` | No | - |
| Allow to recursively walk directories | `-r` | `--recursive` | No | false |

**Note:**

For excluding files and path we use [ignore](https://www.npmjs.com/package/ignore) package.

_`ignore` is a manager, filter and parser which implemented in pure JavaScript according to the .gitignore spec 2.22.1.

**All paths provided must be relative to the source path**

Examples
--------

#### Convert LESS files inside a folder excluding `node_modules` and `vendor` subfolders.

```bash
$ less2scss -s ./less_folder -d ./scss_folder -r -e 'node_modules,vendor'
```

#### Convert LESS files inside a folder excluding all subfolders whose name begins with `test`.

```bash
$ less2scss -s ./less_folder -d ./scss_folder -r -e 'node_modules,vendor'
```

Notice
--------
Expand Down
6 changes: 4 additions & 2 deletions bin/less2scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ commander
.description('This utility quickly converts all your less files to scss.')
.option('-s, --src <sourcePaths>', 'comma separated source paths')
.option('-d, --dst <dstPath>', 'destination path')
.option('-e, --exclude <excludePaths>', 'comma separated exclude paths')
.option('-r, --recursive', 'allow to recursively walk directories', false)
.parse(process.argv);

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

const src = opts.src,
dst = opts.dst,
recursive = opts.recursive
recursive = opts.recursive,
exclude = opts.exclude;

less2scss(src, dst, recursive);
less2scss(src, dst, recursive, exclude);

}
else {
Expand Down
38 changes: 27 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require('fs'),
os = require('os'),
colors = require('colors/safe'),
replaceAll = require('string.prototype.replaceall');
const ignore = require("ignore");

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


const less2scss = (src, dst, recursive) => {
const less2scss = (src, dst, recursive, exclude) => {
if (src) {

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

const pathList = src.split(',');
let lessFiles = [],
destinationPath = dst;

console.info(`${MESSAGE_PREFIX.INFO} ${colors.yellow(`Recursive option is ${colors.yellow.bold(recursive ? 'ON' : 'OFF')}`)}`);
console.info(`${MESSAGE_PREFIX.INFO} ${colors.yellow(`Excluded paths: ${excludedPaths.length}`)}`);


pathList.forEach(beginPath => {

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

beginPath = path.resolve(beginPath);

let curPathType = fs.lstatSync(beginPath);

if (curPathType.isDirectory()) {
lessFiles = [...lessFiles, ...glob
.sync(`${beginPath}/${recursive ? '**/*' : '*'}.less`)
.map(lessFile => ({
src: lessFile,
relativePath: path.relative(beginPath, lessFile)
}))];

let currLessFiles = ignore()
.add(excludedPaths).filter(
glob.sync(`${beginPath}/${recursive ? '**/*' : '*'}.less`, {
mark: true,
onlyFiles: true
}).map(
p => path.relative(beginPath, p)
)
).map(
lessFile => ({
src: path.join(beginPath, lessFile),
relativePath: lessFile
})
)

lessFiles = [...lessFiles, ...currLessFiles];

}

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

if (destinationPath) {

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

if (!fs.existsSync(newPath)) {
mkdirp.sync(newPath);
Expand Down
13 changes: 9 additions & 4 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "less2scss",
"version": "1.3.0",
"version": "1.4.0",
"description": "This utility quickly converts all your less files to scss.",
"main": "index.js",
"bin": {
Expand All @@ -24,13 +24,14 @@
"convertor"
],
"bugs": {
"url": "https://github.com/debbba/less2scss/issues"
"url": "https://github.com/debba/less2scss/issues"
},
"homepage": "https://www.debbaweb.it",
"dependencies": {
"colors": "^1.4.0",
"commander": "^7.2.0",
"commander": "^8.1.0",
"glob": "^7.1.7",
"ignore": "^5.1.8",
"mkdirp": "^1.0.4",
"string.prototype.replaceall": "^1.0.5"
}
Expand Down

0 comments on commit 5c68d0f

Please sign in to comment.