@adamlui/scss-to-css 1.3.1 → 1.4.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 +9 -8
  2. package/package.json +1 -1
  3. package/scss-to-css.js +119 -68
package/README.md CHANGED
@@ -2,10 +2,11 @@
2
2
 
3
3
  ### Recursively compile all SCSS files into minified CSS.
4
4
 
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>
5
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>
6
- <a href="https://www.npmjs.com/package/@adamlui/scss-to-css"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.3.1-fc7811.svg?logo=npm&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.0-fc7811.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
7
8
 
8
- <img height=8px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/media/images/separators/aqua.png">
9
+ <img height=8px width="100%" src="https://github.com/adamlui/js-utils/blob/main/docs/images/aqua-separator.png">
9
10
 
10
11
  ## ⚡ Installation
11
12
 
@@ -33,20 +34,20 @@ Sample output:
33
34
 
34
35
  <img src="https://github.com/adamlui/js-utils/blob/main/scss-to-css/media/images/sample-output.png">
35
36
 
36
- **💡 Note:** Source maps are also generated by default unless `--disable-source-maps` is passed.
37
+ **💡 Note:** Source maps are also generated by default unless `-S` or `--disable-source-maps` is passed.
37
38
 
38
39
  #
39
40
 
40
41
  To specify **input/output** paths:
41
42
 
42
43
  ```
43
- scss-to-css <input_path> <output_path>
44
+ scss-to-css [input_path] [output_path]
44
45
  ```
45
46
 
46
- - `<input_path>`: Path to SCSS file or directory containing SCSS files to be compiled, relative to the current working directory.
47
- - `<output_path>`: Path to file or directory where CSS + sourcemap files will be stored, relative to original file location (if not provided, `css/` is used).
47
+ - `[input_path]`: Path to SCSS file or directory containing SCSS files to be compiled, relative to the current working directory.
48
+ - `[output_path]`: Path to file or directory where CSS + sourcemap files will be stored, relative to original file location (if not provided, `css/` is used).
48
49
 
49
- **💡 Note:** If folders are passed, files will be processed recursively. To include dotfolders, pass `--include-dotfolders`.
50
+ **💡 Note:** If folders are passed, files will be processed recursively. To include dotfolders, pass `-dd` or `--include-dotfolders`.
50
51
 
51
52
  #
52
53
 
@@ -111,7 +112,7 @@ SOFTWARE.
111
112
 
112
113
  <br>
113
114
 
114
- <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/media/images/separators/aqua.png">
115
+ <img height=6px width="100%" src="https://github.com/adamlui/js-utils/blob/main/docs/images/aqua-separator.png">
115
116
 
116
117
  <a href="https://github.com/adamlui/js-utils">**Home**</a> /
117
118
  <a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamlui/scss-to-css",
3
- "version": "1.3.1",
3
+ "version": "1.4.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
@@ -11,77 +11,128 @@ const nc = '\x1b[0m', // no color
11
11
  by = '\x1b[1;33m', // bright yellow
12
12
  bg = '\x1b[1;92m'; // bright green
13
13
 
14
- // Init I/O args
15
- const [inputArg = '', outputArg = ''] = ( // default to empty strings for error-less handling
16
- process.argv.slice(2) // exclude executable and script path
17
- .filter(arg => !arg.startsWith('-')) // exclude flags
18
- .map(arg => arg.replace(/^\/*/, '')) // clean leading slashes to avoid parsing system root
19
- );
14
+ // Show help screen if -h or --help passed
15
+ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
20
16
 
21
- // Validate input arg (output arg can be anything)
22
- const inputPath = path.resolve(__dirname, inputArg);
23
- if (inputArg && !fs.existsSync(inputPath)) {
24
- console.error(`\n${br}Error: First arg must be an existing file or directory.`
25
- + `\n${ inputPath } does not exist.${nc}`
26
- + '\n\nExample valid command: \n>> scss-to-css . output.min.css');
27
- process.exit(1);
28
- }
17
+ // Print help
18
+ console.info(`\n${by}scss-to-css [inputPath] [outputPath] [options]${nc}`);
19
+ console.info('\nPath arguments:');
20
+ printWrappedMsg(' [input_path] '
21
+ + 'Path to SCSS file or directory containing SCSS files to be compiled,'
22
+ + ' relative to the current working directory.');
23
+ printWrappedMsg(' [output_path] '
24
+ + 'Path to file or directory where CSS + sourcemap files will be stored,'
25
+ + ' relative to original file location (if not provided, css/ is used).');
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.');
29
+ console.info('\nInfo commands:');
30
+ printWrappedMsg(' -h, --help Display this help screen.');
31
+ printWrappedMsg(' -v, --version Show version number.');
29
32
 
30
- // Store flag settings
31
- const config = {
32
- includeDotFolders: process.argv.some(arg => /--?include-dot-?folders?/.test(arg)),
33
- disableSourceMaps: process.argv.some(arg => /--?(exclude|disable)-source-?maps?/.test(arg))
34
- };
33
+ function printWrappedMsg(msg) { // indents 2nd+ lines
34
+ const terminalWidth = process.stdout.columns || 80,
35
+ indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
35
36
 
36
- // Recursively find all eligible SCSS files or arg-passed file
37
- const scssFiles = [];
38
- if (inputArg.endsWith('.scss')) scssFiles.push(inputPath);
39
- else (function findSCSSfiles(dir) {
40
- const files = fs.readdirSync(dir);
41
- files.forEach(file => {
42
- const filePath = path.resolve(dir, file);
43
- if (fs.statSync(filePath).isDirectory() &&
44
- (config.includeDotFolders || !file.startsWith('.')))
45
- findSCSSfiles(filePath); // recursively find SCSS in eligible dir
46
- else if (file.endsWith('.scss')) // SCSS file found
47
- scssFiles.push(filePath); // store it for compilation
48
- });
49
- })(inputPath);
37
+ // Split msg into lines of appropriate lengths
38
+ let currentLine = '';
39
+ words.forEach(word => {
40
+ const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
41
+ if (currentLine.length + word.length > lineLength) { // cap/store it
42
+ lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
43
+ currentLine = '';
44
+ }
45
+ currentLine += word;
46
+ });
47
+ lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
50
48
 
51
- // Compile SCSS files to CSS
52
- let cssGenCnt = 0, srcMapGenCnt = 0;
53
- console.log(''); // line break before first log
54
- scssFiles.forEach(scssPath => {
55
- console.info(`Compiling ${ scssPath }...`);
56
- try { // to compile it
57
- const outputDir = path.join(
58
- path.dirname(scssPath), // path of file to be minified
59
- /(src|s[ac]ss)$/.test(path.dirname(scssPath)) ? '../css' // + ../css/ if in *(src|sass|scss)/
60
- : outputArg.endsWith('.css') ? path.dirname(outputArg) // or path from file output arg
61
- : outputArg || 'css' // or path from folder output arg or css/ if no output arg passed
62
- );
63
- const outputFilename = (
64
- outputArg.endsWith('.css') && inputArg.endsWith('.scss')
65
- ? path.basename(outputArg).replace(/(\.min)?\.css$/, '')
66
- : path.basename(scssPath, '.scss')
67
- ) + '.min.css';
68
- const outputPath = path.join(outputDir, outputFilename),
69
- compileResult = sass.compile(scssPath, { style: 'compressed', sourceMap: !config.disableSourceMaps });
70
- if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
71
- fs.writeFileSync(outputPath, compileResult.css, 'utf8'); cssGenCnt++;
72
- if (!config.disableSourceMaps) {
73
- fs.writeFileSync(outputPath + '.map', JSON.stringify(compileResult.sourceMap), 'utf8');
74
- srcMapGenCnt++;
75
- }
76
- } catch (err) {
77
- console.error(`${br}Error compiling ${ scssPath }: ${ err.message }${nc}`);
49
+ // Print formatted msg
50
+ lines.forEach((line, index) => {
51
+ if (index === 0) console.info(line); // print 1st line unindented
52
+ else console.info(' '.repeat(indentation) + line); // print subsequent lines indented
53
+ });
54
+ }
55
+
56
+ // Show version number if -v or --version passed
57
+ } else if (process.argv.some(arg => /^--?v(?:er(?:s(?:ion)?)?)?$/.test(arg))) {
58
+ console.info('v' + require('./package.json').version);
59
+
60
+ } else { // run main routine
61
+
62
+ // Init I/O args
63
+ const [inputArg = '', outputArg = ''] = ( // default to empty strings for error-less handling
64
+ process.argv.slice(2) // exclude executable and script paths
65
+ .filter(arg => !arg.startsWith('-')) // exclude flags
66
+ .map(arg => arg.replace(/^\/*/, '')) // clean leading slashes to avoid parsing system root
67
+ );
68
+
69
+ // Validate input arg (output arg can be anything)
70
+ const inputPath = path.resolve(process.cwd(), inputArg);
71
+ if (inputArg && !fs.existsSync(inputPath)) {
72
+ console.error(`\n${br}Error: First arg must be an existing file or directory.`
73
+ + `\n${ inputPath } does not exist.${nc}`
74
+ + '\n\nExample valid command: \n>> scss-to-css . output.min.css');
75
+ process.exit(1);
78
76
  }
79
- });
80
77
 
81
- // Print final summary
82
- if (cssGenCnt) {
83
- console.info(`\n${bg}Compilation complete!${nc}`);
84
- console.info(`${ cssGenCnt } CSS file${ cssGenCnt > 1 ? 's' : '' }`
85
- + ( srcMapGenCnt ? ` + ${ srcMapGenCnt } source map${ srcMapGenCnt > 1 ? 's' : '' }` : '' )
86
- + ' generated.');
87
- } else console.info(`${by}No SCSS files found.${nc}`);
78
+ // Load flag settings
79
+ const config = {
80
+ includeDotFolders: process.argv.some(arg =>
81
+ /^--?(?:dd|(?:include-)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?)$/.test(arg)),
82
+ disableSourceMaps: process.argv.some(arg =>
83
+ /^--?(?:S|(?:exclude|disable)-so?u?rce?-?maps?)$/.test(arg))
84
+ };
85
+
86
+ // Recursively find all eligible SCSS files or arg-passed file
87
+ const scssFiles = [];
88
+ if (inputArg.endsWith('.scss')) scssFiles.push(inputPath);
89
+ else (function findSCSSfiles(dir) {
90
+ const files = fs.readdirSync(dir);
91
+ files.forEach(file => {
92
+ const filePath = path.resolve(dir, file);
93
+ if (fs.statSync(filePath).isDirectory() &&
94
+ (config.includeDotFolders || !file.startsWith('.')))
95
+ findSCSSfiles(filePath); // recursively find SCSS in eligible dir
96
+ else if (file.endsWith('.scss')) // SCSS file found
97
+ scssFiles.push(filePath); // store it for compilation
98
+ });
99
+ })(inputPath);
100
+
101
+ // Compile SCSS files to CSS
102
+ let cssGenCnt = 0, srcMapGenCnt = 0;
103
+ console.log(''); // line break before first log
104
+ scssFiles.forEach(scssPath => {
105
+ console.info(`Compiling ${ scssPath }...`);
106
+ try { // to compile it
107
+ const outputDir = path.join(
108
+ path.dirname(scssPath), // path of file to be minified
109
+ /(?:src|s[ac]ss)$/.test(path.dirname(scssPath)) ? '../css' // + ../css/ if in *(src|sass|scss)/
110
+ : outputArg.endsWith('.css') ? path.dirname(outputArg) // or path from file output arg
111
+ : outputArg || 'css' // or path from folder output arg or css/ if no output arg passed
112
+ );
113
+ const outputFilename = (
114
+ outputArg.endsWith('.css') && inputArg.endsWith('.scss')
115
+ ? path.basename(outputArg).replace(/(\.min)?\.css$/, '')
116
+ : path.basename(scssPath, '.scss')
117
+ ) + '.min.css';
118
+ const outputPath = path.join(outputDir, outputFilename),
119
+ compileResult = sass.compile(scssPath, { style: 'compressed', sourceMap: !config.disableSourceMaps });
120
+ if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
121
+ fs.writeFileSync(outputPath, compileResult.css, 'utf8'); cssGenCnt++;
122
+ if (!config.disableSourceMaps) {
123
+ fs.writeFileSync(outputPath + '.map', JSON.stringify(compileResult.sourceMap), 'utf8');
124
+ srcMapGenCnt++;
125
+ }
126
+ } catch (err) {
127
+ console.error(`${br}Error compiling ${ scssPath }: ${ err.message }${nc}`);
128
+ }
129
+ });
130
+
131
+ // Print final summary
132
+ if (cssGenCnt) {
133
+ console.info(`\n${bg}Compilation complete!${nc}`);
134
+ console.info(`${ cssGenCnt } CSS file${ cssGenCnt > 1 ? 's' : '' }`
135
+ + ( srcMapGenCnt ? ` + ${ srcMapGenCnt } source map${ srcMapGenCnt > 1 ? 's' : '' }` : '' )
136
+ + ' generated.');
137
+ } else console.info(`${by}No SCSS files found.${nc}`);
138
+ }