Skip to content

Commit d1e749b

Browse files
committed
allow to recursively walk directories
1 parent f6d3ec5 commit d1e749b

File tree

6 files changed

+80
-27
lines changed

6 files changed

+80
-27
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
node_modules
3+
data/

README.md

+36-7
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,48 @@ This npm package will convert all your less files to scss.
88

99
You can provide an entire folder, this package will scan all less files whose find in subfolders and it'll create SCSS files in the same folder or in the new directory provided recreating the source hierarchy.
1010

11-
Installation
11+
Install
1212
-------------
1313

1414
Install it via npm:
1515

16-
```
17-
$ npm install -g less2scss
16+
```bash
17+
$ npm install less2scss --global
1818
```
1919
Usage
2020
------
21-
Go to your terminal and type the following:
21+
`less2scss` offers a single command line interface:
22+
23+
```bash
24+
$ less2scss
25+
```
26+
27+
Will print an help:
28+
29+
2230
```
23-
$ less2scss -s path1,path2,path3 -d [destinationFolder]
31+
Usage: less2scss [options]
32+
33+
This utility quickly converts all your less files to scss.
34+
35+
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
2441
```
25-
Paths could be a mixture of SCSS files and directories.
2642

27-
It will convert all of the scss files of the corresponding paths provided and will store the SCSS files in the destinationFolder if it's provided or in the same directory.
43+
Options
44+
--------
45+
You can pass the following options via CLI arguments.
46+
47+
| Description | Short command | Full command | Mandatory | Default value |
48+
| --------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------------------ | ------------------ | ------------------ |
49+
| Comma separated LESS files and paths | `-s` | `--src` | Yes | - |
50+
| 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+
| Allow to recursively walk directories | `-r` | `--recursive` | No | false |
52+
2853

2954
Notice
3055
--------
@@ -41,6 +66,10 @@ Some convertion rules are inspired by:
4166
- [Less Scss Convertor](https://github.com/tarun29061990/less-scss-convertor) by
4267
tarun29061990 .
4368

69+
Contributing
70+
--------
71+
If you have suggestions for enhancements or ideas for new features that could be useful, please open a pull request or open an issue.
72+
4473
License
4574
--------
4675
less2scss is licensed under : The Apache Software License, Version 2.0. You can find a copy of the license (http://www.apache.org/licenses/LICENSE-2.0.txt)

bin/less2scss

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ const less2scss = require('../index.js');
88
commander
99
.version(version)
1010
.description('This utility quickly converts all your less files to scss.')
11-
.option('-s, --src <sourcePaths>', 'source paths comma separated')
11+
.option('-s, --src <sourcePaths>', 'comma separated source paths')
1212
.option('-d, --dst <dstPath>', 'destination path')
13+
.option('-r, --recursive', 'allow to recursively walk directories', false)
1314
.parse(process.argv);
1415

1516
const exec = () => {
1617
const opts = commander.opts();
1718
if (opts.src){
1819

1920
const src = opts.src,
20-
dst = opts.dst;
21+
dst = opts.dst,
22+
recursive = opts.recursive
2123

22-
less2scss(src, dst);
24+
less2scss(src, dst, recursive);
2325

2426
}
2527
else {

index.js

+36-15
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ const MESSAGE_PREFIX = {
1010
INFO: colors.yellow('[INFO]'),
1111
WARNING: colors.brightRed.bold('[WARNING]'),
1212
ERROR: colors.red.bold('[ERROR]'),
13-
}
13+
};
14+
1415

15-
const less2scss = (src, dst) => {
16+
const less2scss = (src, dst, recursive) => {
1617
if (src) {
18+
19+
console.info(`${MESSAGE_PREFIX.INFO} ${colors.yellow(`Recursive option is ${colors.yellow.bold(recursive ? 'ON' : 'OFF')}`)}`);
20+
1721
const pathList = src.split(',');
1822
let lessFiles = [],
1923
destinationPath = dst;
@@ -22,10 +26,6 @@ const less2scss = (src, dst) => {
2226

2327
beginPath && beginPath.trim();
2428

25-
/**
26-
* Resolve ~ with NodeJs
27-
*/
28-
2929
if (beginPath[0] === '~') {
3030
beginPath = path.join(os.homedir(), beginPath.slice(1));
3131
}
@@ -35,21 +35,38 @@ const less2scss = (src, dst) => {
3535
let curPathType = fs.lstatSync(beginPath);
3636

3737
if (curPathType.isDirectory()) {
38-
lessFiles = glob.sync(`${beginPath}/*.less`);
38+
lessFiles = [...lessFiles, ...glob
39+
.sync(`${beginPath}/${recursive ? '**/*' : '*'}.less`)
40+
.map(lessFile => ({
41+
src: lessFile,
42+
relativePath: path.relative(beginPath, lessFile)
43+
}))];
3944
}
4045

4146
if (curPathType.isFile()) {
42-
lessFiles.push(beginPath);
47+
lessFiles = [...lessFiles, {
48+
src: beginPath,
49+
relativePath: ''
50+
}];
4351
}
4452

4553
});
4654

55+
lessFiles = lessFiles
56+
.filter(
57+
({src}, index) => lessFiles.map(lessFile => lessFile.src).indexOf(src) === index
58+
);
59+
4760
if (lessFiles.length) {
4861
lessFiles.forEach(file => {
49-
const scssContent = replaceLess(file);
50-
writeFile(file, scssContent, destinationPath);
62+
const {src, relativePath} = file;
63+
const scssContent = replaceLess(src);
64+
writeFile(src, scssContent, destinationPath, relativePath);
5165
});
5266
console.log(`${MESSAGE_PREFIX.INFO} ${colors.green('Enjoy your SCSS files ;)')}`);
67+
console.log(`${colors.blue(`\n★ Pull requests and stars are always welcome.`)}`);
68+
console.log(`${colors.blue(`https://github.com/debba/less2scss`)}`);
69+
5370
} else {
5471
console.log(`${MESSAGE_PREFIX.ERROR} ${colors.red.bold('No LESS file found.')}`);
5572
}
@@ -127,18 +144,22 @@ const replaceLess = file => {
127144
return transformedContent;
128145
};
129146

130-
const writeFile = (file, scssContent, destinationPath) => {
147+
const writeFile = (file, scssContent, destinationPath, relativePath) => {
131148

132149
let outputFile;
133150

134151
if (destinationPath) {
135-
if (!fs.existsSync(destinationPath)) {
136-
mkdirp.sync(destinationPath);
152+
153+
const newPath = relativePath !== '' ? path.dirname( destinationPath+'/'+relativePath ) : destinationPath;
154+
155+
if (!fs.existsSync(newPath)) {
156+
mkdirp.sync(newPath);
137157
}
138158

139-
outputFile = path.resolve(destinationPath, path.basename(file)).replace('.less', '.scss');
159+
outputFile = path.resolve(newPath, path.basename(file)).replace('.less', '.scss');
140160
} else {
141-
outputFile = file.replace('.less', '.scss')
161+
outputFile = file.replace('.less', '.scss');
162+
console.log(outputFile);
142163
}
143164

144165
fs.writeFileSync(outputFile, scssContent);

package-lock.json

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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "less2scss",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"description": "This utility quickly converts all your less files to scss.",
55
"main": "index.js",
66
"bin": {

0 commit comments

Comments
 (0)