@adamlui/scss-to-css 1.4.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +19 -9
  2. package/package.json +1 -1
  3. package/scss-to-css.js +64 -46
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  <a href="https://www.npmjs.com/package/@adamlui/scss-to-css"><img height=31 src="https://img.shields.io/npm/dt/%40adamlui%2Fscss-to-css?logo=npm&logoColor=white&labelColor=464646&style=for-the-badge"></a>
6
6
  <a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-fcde7b.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
7
- <a href="https://www.npmjs.com/package/@adamlui/scss-to-css?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.1-fc7811.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
7
+ <a href="https://www.npmjs.com/package/@adamlui/scss-to-css?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.5.0-fc7811.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
8
8
  <a href="https://www.npmjs.com/package/@adamlui/scss-to-css?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/%40adamlui%2Fscss-to-css?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646"></a>
9
9
 
10
10
  <img height=8px width="100%" src="https://github.com/adamlui/js-utils/blob/main/docs/images/aqua-separator.png">
@@ -28,9 +28,11 @@ npm install -D @adamlui/scss-to-css
28
28
  The basic **global command** is:
29
29
 
30
30
  ```
31
- scsstocss
31
+ scss-to-css
32
32
  ```
33
33
 
34
+ **💡 Note:** Pass `-n` or `--dry-run` to only see what files will be processed.
35
+
34
36
  Sample output:
35
37
 
36
38
  <img src="https://github.com/adamlui/js-utils/blob/main/scss-to-css/media/images/sample-output.png">
@@ -42,7 +44,7 @@ Sample output:
42
44
  To specify **input/output** paths:
43
45
 
44
46
  ```
45
- scsstocss [input_path] [output_path]
47
+ scss-to-css [input_path] [output_path]
46
48
  ```
47
49
 
48
50
  - `[input_path]`: Path to SCSS file or directory containing SCSS files to be compiled, relative to the current working directory.
@@ -52,7 +54,7 @@ scsstocss [input_path] [output_path]
52
54
 
53
55
  #
54
56
 
55
- To use as a **package script**, edit your project's `package.json` like this:
57
+ To use as a **package script**, in your project's `package.json`:
56
58
 
57
59
  ```json
58
60
  "scripts": {
@@ -60,7 +62,7 @@ To use as a **package script**, edit your project's `package.json` like this:
60
62
  },
61
63
  ```
62
64
 
63
- Replace `<scss-to-css-cmd>` with `scsstocss` + optional args. Then, `npm run build:css` can be used to run the command.
65
+ Replace `<scss-to-css-cmd>` with `scss-to-css` + optional args. Then, `npm run build:css` can be used to run the command.
64
66
  <br><br>
65
67
 
66
68
  ## 📃 Example commands:
@@ -68,27 +70,35 @@ Replace `<scss-to-css-cmd>` with `scsstocss` + optional args. Then, `npm run bui
68
70
  - Compile all SCSS files in the **current directory** (outputs to `css/`):
69
71
 
70
72
  ```
71
- scsstocss
73
+ scss-to-css
72
74
  ```
73
75
 
74
76
  - Compile all SCSS files in a **specific directory** (outputs to `path/to/your/directory/css/`):
75
77
 
76
78
  ```
77
- scsstocss path/to/your/directory
79
+ scss-to-css path/to/your/directory
78
80
  ```
79
81
 
80
82
  - Compile a **specific file** (outputs to `path/to/your/css/file.min.css`):
81
83
 
82
84
  ```
83
- scsstocss path/to/your/file.scss
85
+ scss-to-css path/to/your/file.scss
84
86
  ```
85
87
 
86
88
  - Specify both **input and output** directories (outputs to `output_folder/`):
87
89
 
88
90
  ```
89
- scsstocss input_folder output_folder
91
+ scss-to-css input_folder output_folder
90
92
  ```
91
93
 
94
+ **💡 Note:** Output CSS is minified unless `-M` or `--no-minify` is passed.
95
+ <br><br>
96
+
97
+ ## 💖 Support
98
+
99
+ Please consider giving a ⭐ on [GitHub](https://github.com/adamlui/js-utils) if this helped you!
100
+ <br><br>
101
+
92
102
  ## 🏛️ MIT License
93
103
 
94
104
  **Copyright (c) 2024 [Adam Lui](https://github.com/adamlui)**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamlui/scss-to-css",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "Recursively compile all SCSS files into minified CSS",
5
5
  "author": {
6
6
  "name": "Adam Lui",
package/scss-to-css.js CHANGED
@@ -15,20 +15,23 @@ const nc = '\x1b[0m', // no color
15
15
  if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
16
16
 
17
17
  // Print help
18
- console.info(`\n${by}scsstocss [inputPath] [outputPath] [options]${nc}`);
18
+ console.info(`\n${by}scss-to-css [inputPath] [outputPath] [options]${nc}`);
19
19
  console.info('\nPath arguments:');
20
- printWrappedMsg(' [inputPath] '
20
+ printWrappedMsg(' [inputPath] '
21
21
  + 'Path to SCSS file or directory containing SCSS files to be compiled,'
22
22
  + ' relative to the current working directory.');
23
- printWrappedMsg(' [outputPath] '
23
+ printWrappedMsg(' [outputPath] '
24
24
  + 'Path to file or directory where CSS + sourcemap files will be stored,'
25
25
  + ' relative to original file location (if not provided, css/ is used).');
26
26
  console.info('\nConfig options:');
27
- printWrappedMsg(' -dd, --include-dotfolders Include dotfolders in file search.');
28
- printWrappedMsg(' -S, --disable-source-maps Prevent source maps from being generated.');
27
+ printWrappedMsg(' -n, --dry-run Don\'t actually compile the file(s),'
28
+ + ' just show if they will be processed.');
29
+ printWrappedMsg(' -dd, --include-dotfolders Include dotfolders in file search.');
30
+ printWrappedMsg(' -S, --disable-source-maps Prevent source maps from being generated.');
31
+ printWrappedMsg(' -M, --no-minify Disable minification of output CSS.');
29
32
  console.info('\nInfo commands:');
30
- printWrappedMsg(' -h, --help Display this help screen.');
31
- printWrappedMsg(' -v, --version Show version number.');
33
+ printWrappedMsg(' -h, --help Display this help screen.');
34
+ printWrappedMsg(' -v, --version Show version number.');
32
35
 
33
36
  // Show VERSION number if -v or --version passed
34
37
  } else if (process.argv.some(arg => /^--?ve?r?s?i?o?n?$/.test(arg))) {
@@ -48,16 +51,20 @@ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
48
51
  if (inputArg && !fs.existsSync(inputPath)) {
49
52
  console.error(`\n${br}Error: First arg must be an existing file or directory.`
50
53
  + `\n${ inputPath } does not exist.${nc}`
51
- + '\n\nExample valid command: \n>> scsstocss . output.min.css');
54
+ + `\n\n${bg}Example valid command: \n>> scss-to-css . output.min.css${nc}`
55
+ + `\n\n${by}For all command options: \n>> scss-to-css --help${nc}`);
52
56
  process.exit(1);
53
57
  }
54
58
 
55
59
  // Load flag settings
56
60
  const config = {
61
+ dryRun: process.argv.some(arg => /^--?(?:n|dry-?run)$/.test(arg)),
57
62
  includeDotFolders: process.argv.some(arg =>
58
63
  /^--?(?:dd|(?:include-)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?)$/.test(arg)),
59
64
  disableSourceMaps: process.argv.some(arg =>
60
- /^--?(?:S|(?:exclude|disable)-so?u?rce?-?maps?)$/.test(arg))
65
+ /^--?(?:S|(?:exclude|disable)-so?u?rce?-?maps?)$/.test(arg)),
66
+ noMinify: process.argv.some(arg =>
67
+ /^--?(?:M|(?:disable|no)-minif(?:y|ication))$/.test(arg))
61
68
  };
62
69
 
63
70
  // Recursively find all eligible SCSS files or arg-passed file
@@ -75,48 +82,59 @@ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
75
82
  });
76
83
  })(inputPath);
77
84
 
78
- // Compile SCSS files to CSS
79
- let cssGenCnt = 0, srcMapGenCnt = 0;
80
- console.log(''); // line break before first log
81
- scssFiles.forEach(scssPath => {
82
- console.info(`Compiling ${ scssPath }...`);
83
- try { // to compile it
84
- const outputDir = path.join(
85
- path.dirname(scssPath), // path of file to be minified
86
- /(?:src|s[ac]ss)$/.test(path.dirname(scssPath)) ? '../css' // + ../css/ if in *(src|sass|scss)/
87
- : outputArg.endsWith('.css') ? path.dirname(outputArg) // or path from file output arg
88
- : outputArg || 'css' // or path from folder output arg or css/ if no output arg passed
89
- );
90
- const outputFilename = (
91
- outputArg.endsWith('.css') && inputArg.endsWith('.scss')
92
- ? path.basename(outputArg).replace(/(\.min)?\.css$/, '')
93
- : path.basename(scssPath, '.scss')
94
- ) + '.min.css';
95
- const outputPath = path.join(outputDir, outputFilename),
96
- compileResult = sass.compile(scssPath, { style: 'compressed', sourceMap: !config.disableSourceMaps });
97
- if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
98
- fs.writeFileSync(outputPath, compileResult.css, 'utf8'); cssGenCnt++;
99
- if (!config.disableSourceMaps) {
100
- fs.writeFileSync(outputPath + '.map', JSON.stringify(compileResult.sourceMap), 'utf8');
101
- srcMapGenCnt++;
85
+ if (scssFiles.length === 0) { // print nothing found
86
+ console.info(`\n${by}No SCSS files found.${nc}`);
87
+
88
+ } else if (config.dryRun) { // print files to be processed
89
+ console.info(`\n${by}SCSS files to be compiled:${nc}`);
90
+ scssFiles.forEach(file => console.info(file));
91
+
92
+ } else { // actually compile SCSS files
93
+
94
+ let cssGenCnt = 0, srcMapGenCnt = 0;
95
+ console.log(''); // line break before first log
96
+ scssFiles.forEach(scssPath => {
97
+ console.info(`Compiling ${ scssPath }...`);
98
+ try { // to compile it
99
+ const outputDir = path.join(
100
+ path.dirname(scssPath), // path of file to be minified
101
+ /(?:src|s[ac]ss)$/.test(path.dirname(scssPath)) ? '../css' // + ../css/ if in *(src|sass|scss)/
102
+ : outputArg.endsWith('.css') ? path.dirname(outputArg) // or path from file output arg
103
+ : outputArg || 'css' // or path from folder output arg or css/ if no output arg passed
104
+ );
105
+ const outputFilename = (
106
+ outputArg.endsWith('.css') && inputArg.endsWith('.scss')
107
+ ? path.basename(outputArg).replace(/(\.min)?\.css$/, '')
108
+ : path.basename(scssPath, '.scss')
109
+ ) + '.min.css';
110
+ const outputPath = path.join(outputDir, outputFilename),
111
+ compileResult = sass.compile(scssPath, {
112
+ style: config.noMinify ? 'expanded' : 'compressed',
113
+ sourceMap: !config.disableSourceMaps });
114
+ if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
115
+ fs.writeFileSync(outputPath, compileResult.css, 'utf8'); cssGenCnt++;
116
+ if (!config.disableSourceMaps) {
117
+ fs.writeFileSync(outputPath + '.map', JSON.stringify(compileResult.sourceMap), 'utf8');
118
+ srcMapGenCnt++;
119
+ }
120
+ } catch (err) {
121
+ console.error(`${br}Error compiling ${ scssPath }: ${ err.message }${nc}`);
102
122
  }
103
- } catch (err) {
104
- console.error(`${br}Error compiling ${ scssPath }: ${ err.message }${nc}`);
105
- }
106
- });
123
+ });
107
124
 
108
- // Print final summary
109
- if (cssGenCnt) {
110
- console.info(`\n${bg}Compilation complete!${nc}`);
111
- console.info(`${ cssGenCnt } CSS file${ cssGenCnt > 1 ? 's' : '' }`
112
- + ( srcMapGenCnt ? ` + ${ srcMapGenCnt } source map${ srcMapGenCnt > 1 ? 's' : '' }` : '' )
113
- + ' generated.');
114
- } else console.info(`${by}No SCSS files found.${nc}`);
125
+ // Print final summary
126
+ if (cssGenCnt) {
127
+ console.info(`\n${bg}Compilation complete!${nc}`);
128
+ console.info(`${ cssGenCnt } CSS file${ cssGenCnt > 1 ? 's' : '' }`
129
+ + ( srcMapGenCnt ? ` + ${ srcMapGenCnt } source map${ srcMapGenCnt > 1 ? 's' : '' }` : '' )
130
+ + ' generated.');
131
+ } else console.info(`${by}No SCSS files processed successfully.${nc}`);
132
+ }
115
133
  }
116
134
 
117
- function printWrappedMsg(msg) { // truncates msg, indents 2nd+ lines
135
+ function printWrappedMsg(msg) { // wraps msg, indents 2nd+ lines
118
136
  const terminalWidth = process.stdout.columns || 80,
119
- indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
137
+ indentation = 30, lines = [], words = msg.match(/\S+|\s+/g);
120
138
 
121
139
  // Split msg into lines of appropriate lengths
122
140
  let currentLine = '';