@adamlui/scss-to-css 1.2.0 → 1.3.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.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  ### Recursively compile all SCSS files into minified CSS.
4
4
 
5
5
  <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.2.0-fc7811.svg?logo=npm&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.0-fc7811.svg?logo=npm&logoColor=white&labelColor=464646&style=for-the-badge"></a>
7
7
 
8
8
  <img height=8px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/media/images/separators/aqua.png">
9
9
 
@@ -33,20 +33,20 @@ Sample output:
33
33
 
34
34
  <img src="https://github.com/adamlui/js-utils/blob/main/scss-to-css/media/images/sample-output.png">
35
35
 
36
- **💡 Note:** Source maps are also generated.
36
+ **💡 Note:** Source maps are also generated by default. To disable, pass `--disable-source-maps`.
37
37
 
38
38
  #
39
39
 
40
- To specify **input/output** directories:
40
+ To specify **input/output** paths:
41
41
 
42
42
  ```
43
43
  scss-to-css <input_path> <output_path>
44
44
  ```
45
45
 
46
- - `<input_path>`: Path to directory containing SCSS files to be compiled, relative to the current working directory.
47
- - `<output_path>`: Path to directory where CSS/sourcemap files will be stored, relative to original file location. (If not provided, `css/` is used.)
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.)
48
48
 
49
- **💡 Note:** The paths can be either folders or specific files. If folders are passed, files will be processed recursively.
49
+ **💡 Note:** If folders are passed, files will be processed recursively. To include dotfolders, pass `--include-dotfolders`.
50
50
 
51
51
  #
52
52
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamlui/scss-to-css",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Recursively compile all SCSS files into minified CSS",
5
5
  "author": {
6
6
  "name": "Adam Lui",
@@ -38,6 +38,10 @@
38
38
  "url": "https://github.com/adamlui/js-utils/issues"
39
39
  },
40
40
  "dependencies": {
41
- "sass": "^1.70.0"
41
+ "sass": "^1.71.0"
42
+ },
43
+ "funding": {
44
+ "type": "github",
45
+ "url": "https://github.com/sponsors/adamlui"
42
46
  }
43
47
  }
package/scss-to-css.js CHANGED
@@ -11,18 +11,27 @@ 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
- // Clean leading slashes from args to avoid parsing system root
15
- const inputArg = process.argv[2] ? process.argv[2].replace(/^\/*/, '') : '',
16
- outputArg = process.argv[3] ? process.argv[3].replace(/^\/*/, '') : '';
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
+ );
17
20
 
18
21
  // Validate input arg (output arg can be anything)
19
- if (process.argv[2] && !fs.existsSync(inputArg)) {
20
- console.error(`\n${br}Error: First arg must be an existing file or path.${nc}`
22
+ if (inputArg && !fs.existsSync(inputArg)) {
23
+ console.error(`\n${br}Error: First arg must be an existing file or directory.${nc}`
21
24
  + '\nExample valid command: \n>> scss-to-css . output.min.css');
22
25
  process.exit(1);
23
26
  }
24
27
 
25
- // Recursively find all SCSS files or arg-passed file
28
+ // Store flag settings
29
+ const config = {
30
+ includeDotFolders: process.argv.some(arg => /--?include-dot-?folders?/.test(arg)),
31
+ disableSourceMaps: process.argv.some(arg => /--?(exclude|disable)-source-?maps?/.test(arg))
32
+ };
33
+
34
+ // Recursively find all eligible SCSS files or arg-passed file
26
35
  const inputPath = path.resolve(process.cwd(), inputArg),
27
36
  scssFiles = [];
28
37
  if (inputArg.endsWith('.scss')) scssFiles.push(inputPath);
@@ -30,45 +39,48 @@ else (function findSCSSfiles(dir) {
30
39
  const files = fs.readdirSync(dir);
31
40
  files.forEach(file => {
32
41
  const filePath = path.resolve(dir, file);
33
- if (fs.statSync(filePath).isDirectory())
34
- findSCSSfiles(filePath); // recursively find SCSS
35
- else if (/\.scss$/.test(file)) // SCSS file found
36
- scssFiles.push(filePath); // store it for minification
42
+ if (fs.statSync(filePath).isDirectory() &&
43
+ (config.includeDotFolders || !file.startsWith('.')))
44
+ findSCSSfiles(filePath); // recursively find SCSS in eligible dir
45
+ else if (file.endsWith('.scss')) // SCSS file found
46
+ scssFiles.push(filePath); // store it for compilation
37
47
  });
38
48
  })(inputPath);
39
49
 
40
50
  // Compile SCSS files to CSS
41
- let generatedCnt = 0;
51
+ let cssGenCnt = 0, srcMapGenCnt = 0;
42
52
  console.log(''); // line break before first log
43
53
  scssFiles.forEach(scssPath => {
54
+ console.info(`Compiling ${ scssPath }...`);
44
55
  try { // to compile it
45
56
  const outputDir = path.join(
46
57
  path.dirname(scssPath), // path of file to be minified
47
58
  /(src|s[ac]ss)$/.test(path.dirname(scssPath)) ? '../css' // + ../css/ if in *(src|sass|scss)/
48
59
  : outputArg.endsWith('.css') ? path.dirname(outputArg) // or path from file output arg
49
- : outputArg || 'css' // or path from path output arg or css/ from no arg
60
+ : outputArg || 'css' // or path from folder output arg or css/ if no output arg passed
50
61
  );
51
62
  const outputFilename = (
52
63
  outputArg.endsWith('.css') && inputArg.endsWith('.scss')
53
64
  ? path.basename(outputArg).replace(/(\.min)?\.css$/, '')
54
65
  : path.basename(scssPath, '.scss')
55
66
  ) + '.min.css';
56
- const outputPath = path.join(outputDir, outputFilename);
57
- console.info(`Compiling ${ scssPath }...`);
58
- const compileResult = sass.compile(scssPath, { style: 'compressed', sourceMap: true });
67
+ const outputPath = path.join(outputDir, outputFilename),
68
+ compileResult = sass.compile(scssPath, { style: 'compressed', sourceMap: !config.disableSourceMaps });
59
69
  if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
60
- fs.writeFileSync(outputPath, compileResult.css, 'utf8');
61
- fs.writeFileSync(outputPath + '.map', JSON.stringify(compileResult.sourceMap), 'utf8');
62
- generatedCnt+= 2;
70
+ fs.writeFileSync(outputPath, compileResult.css, 'utf8'); cssGenCnt++;
71
+ if (!config.disableSourceMaps) {
72
+ fs.writeFileSync(outputPath + '.map', JSON.stringify(compileResult.sourceMap), 'utf8');
73
+ srcMapGenCnt++;
74
+ }
63
75
  } catch (err) {
64
76
  console.error(`${br}Error compiling ${ scssPath }: ${ err.message }${nc}`);
65
77
  }
66
78
  });
67
79
 
68
80
  // Print final summary
69
- if (generatedCnt) {
70
- const isPlural = generatedCnt/2 > 1;
81
+ if (cssGenCnt) {
71
82
  console.info(`\n${bg}Compilation complete!${nc}`);
72
- console.info(`\n${ generatedCnt/2 } CSS file${ isPlural ? 's' : '' }`
73
- + ` + ${ generatedCnt/2 } source map${ isPlural ? 's' : '' } generated.`);
74
- } else console.info(`\n${by}No SCSS files found.${nc}`);
83
+ console.info(`${ cssGenCnt } CSS file${ cssGenCnt > 1 ? 's' : '' }`
84
+ + ( srcMapGenCnt ? ` + ${ srcMapGenCnt } source map${ srcMapGenCnt > 1 ? 's' : '' }` : '' )
85
+ + ' generated.');
86
+ } else console.info(`${by}No SCSS files found.${nc}`);