@adamlui/scss-to-css 1.0.1 → 1.1.1

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
@@ -1,81 +1,118 @@
1
- # { } scss-to-css.js
2
-
3
- <a href="#%EF%B8%8F-mit-license"><img height=25 src="https://img.shields.io/badge/License-MIT-fcde7b.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
4
-
5
- Recursively compile all SCSS files into minified CSS.
6
-
7
- <img height=10px width="100%" src="https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/aqua.png">
8
-
9
- ## ⚡ Installation
10
-
11
- - As a global utility:
12
-
13
- ```
14
- npm install -g @adamlui/scss-to-css
15
- ```
16
-
17
- - As a dev dependency, from your project root:
18
-
19
- ```
20
- npm install -D @adamlui/scss-to-css
21
- ```
22
-
23
- ## 💻 Usage
24
-
25
- ### Globally:
26
-
27
- Run the following command from a folder to recursively compile all SCSS:
28
-
29
- ```
30
- scss-to-css
31
- ```
32
-
33
- Sample output:
34
-
35
- ![](https://raw.githubusercontent.com/adamlui/js-utils/main/scss-to-css/media/images/sample-output.png)
36
-
37
- ### Dev dependency:
38
-
39
- 1. Add build script to project's `package.json`:
40
-
41
- ```json
42
- "scripts": {
43
- "build:css": "scss-to-css"
44
- },
45
- ```
46
-
47
- 2. Run script from a folder to recursively compile all SCSS:
48
-
49
- ```
50
- npm run build:css
51
- ```
52
-
53
- ## 🏛️ MIT License
54
-
55
- **Copyright (c) 2024 [Adam Lui](https://github.com/adamlui)**
56
-
57
- Permission is hereby granted, free of charge, to any person obtaining a copy
58
- of this software and associated documentation files (the "Software"), to deal
59
- in the Software without restriction, including without limitation the rights
60
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
61
- copies of the Software, and to permit persons to whom the Software is
62
- furnished to do so, subject to the following conditions:
63
-
64
- The above copyright notice and this permission notice shall be included in all
65
- copies or substantial portions of the Software.
66
-
67
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
68
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
69
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
70
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
71
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
72
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
73
- SOFTWARE.
74
-
75
- <br>
76
-
77
- <img height=6px width="100%" src="https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/aqua.png">
78
-
79
- <a href="https://github.com/adamlui">**Home**</a> /
80
- <a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
81
- <a href="#--scss-to-cssjs">Back to top ↑</a>
1
+ # { } scss-to-css.js
2
+
3
+ ### Recursively compile all SCSS files into minified CSS.
4
+
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.1.1-fc7811.svg?logo=npm&logoColor=white&labelColor=464646&style=for-the-badge"></a>
7
+
8
+ <img height=8px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/media/images/separators/aqua.png">
9
+
10
+ ## ⚡ Installation
11
+
12
+ As a **global utility**:
13
+
14
+ ```
15
+ npm install -g @adamlui/scss-to-css
16
+ ```
17
+
18
+ As a **dev dependency**, from your project root:
19
+
20
+ ```
21
+ npm install -D @adamlui/scss-to-css
22
+ ```
23
+
24
+ ## 💻 Usage
25
+
26
+ The basic **global command** is:
27
+
28
+ ```
29
+ scss-to-css
30
+ ```
31
+
32
+ Sample output:
33
+
34
+ <img src="https://github.com/adamlui/js-utils/blob/main/scss-to-css/media/images/sample-output.png">
35
+
36
+ **💡 Note:** Source maps are also generated.
37
+
38
+ #
39
+
40
+ To specify **input/output** directories:
41
+
42
+ ```
43
+ scss-to-css <input_path> <output_path>
44
+ ```
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, same location is used.)
48
+
49
+ **💡 Note:** The paths can be either folders or specific files. If folders are passed, files will be processed recursively.
50
+
51
+ #
52
+
53
+ To use as a **package script**, edit your project's `package.json` like this:
54
+
55
+ ```json
56
+ "scripts": {
57
+ "build:css": "<scss-to-css-cmd>"
58
+ },
59
+ ```
60
+
61
+ Replace `<scss-to-css-cmd>` with `scss-to-css` + optional args. Then, `npm run build:css` can be used to run the command.
62
+ <br><br>
63
+
64
+ ## 📃 Example commands:
65
+
66
+ - Compile all SCSS files in the **current directory** (outputs to same directory`):
67
+
68
+ ```
69
+ scss-to-css
70
+ ```
71
+
72
+ - Compile all SCSS files in a **specific directory** (outputs to `path/to/your/directory/`):
73
+
74
+ ```
75
+ scss-to-css path/to/your/directory
76
+ ```
77
+
78
+ - Compile a **specific file** (outputs to `path/to/your/minified/file.min.css`):
79
+
80
+ ```
81
+ scss-to-css path/to/your/file.scss
82
+ ```
83
+
84
+ - Specify both **input and output** directories (outputs to `output_folder/`):
85
+
86
+ ```
87
+ scss-to-css input_folder output_folder
88
+ ```
89
+
90
+ ## 🏛️ MIT License
91
+
92
+ **Copyright (c) 2024 [Adam Lui](https://github.com/adamlui)**
93
+
94
+ Permission is hereby granted, free of charge, to any person obtaining a copy
95
+ of this software and associated documentation files (the "Software"), to deal
96
+ in the Software without restriction, including without limitation the rights
97
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
98
+ copies of the Software, and to permit persons to whom the Software is
99
+ furnished to do so, subject to the following conditions:
100
+
101
+ The above copyright notice and this permission notice shall be included in all
102
+ copies or substantial portions of the Software.
103
+
104
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
105
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
106
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
107
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
108
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
109
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
110
+ SOFTWARE.
111
+
112
+ <br>
113
+
114
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/media/images/separators/aqua.png">
115
+
116
+ <a href="https://github.com/adamlui/js-utils">**Home**</a> /
117
+ <a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
118
+ <a href="#--scss-to-cssjs">Back to top ↑</a>
package/package.json CHANGED
@@ -1,37 +1,43 @@
1
- {
2
- "name": "@adamlui/scss-to-css",
3
- "version": "1.0.1",
4
- "description": "Recursively compile all SCSS files into minified CSS",
5
- "author": {
6
- "name": "Adam Lui",
7
- "email": "adam@kudoai.com",
8
- "url": "https://github.com/adamlui"
9
- },
10
- "homepage": "https://github.com/adamlui/js-utils",
11
- "license": "MIT",
12
- "main": "scss-to-css.js",
13
- "bin": {
14
- "scss-to-css": "scss-to-css.js"
15
- },
16
- "scripts": {
17
- "test": "echo \"Error: no test specified\" && exit 1"
18
- },
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/adamlui/js-utils.git"
22
- },
23
- "keywords": [
24
- "scss",
25
- "sass",
26
- "css",
27
- "stylesheets",
28
- "compiler",
29
- "minifier"
30
- ],
31
- "bugs": {
32
- "url": "https://github.com/adamlui/js-utils/issues"
33
- },
34
- "dependencies": {
35
- "sass": "^1.70.0"
36
- }
37
- }
1
+ {
2
+ "name": "@adamlui/scss-to-css",
3
+ "version": "1.1.1",
4
+ "description": "Recursively compile all SCSS files into minified CSS",
5
+ "author": {
6
+ "name": "Adam Lui",
7
+ "email": "adam@kudoai.com",
8
+ "url": "https://github.com/adamlui"
9
+ },
10
+ "homepage": "https://github.com/adamlui/js-utils",
11
+ "license": "MIT",
12
+ "main": "scss-to-css.js",
13
+ "bin": {
14
+ "scss-to-css": "scss-to-css.js"
15
+ },
16
+ "scripts": {
17
+ "test": "echo \"Error: no test specified\" && exit 1",
18
+ "bump:patch": "bash utils/bump.sh patch",
19
+ "bump:minor": "bash utils/bump.sh minor",
20
+ "bump:major": "bash utils/bump.sh major",
21
+ "publish:patch": "bash utils/bump.sh patch --publish",
22
+ "publish:minor": "bash utils/bump.sh minor --publish",
23
+ "publish:major": "bash utils/bump.sh major --publish"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/adamlui/js-utils.git"
28
+ },
29
+ "keywords": [
30
+ "scss",
31
+ "sass",
32
+ "css",
33
+ "stylesheets",
34
+ "compiler",
35
+ "minifier"
36
+ ],
37
+ "bugs": {
38
+ "url": "https://github.com/adamlui/js-utils/issues"
39
+ },
40
+ "dependencies": {
41
+ "sass": "^1.70.0"
42
+ }
43
+ }
package/scss-to-css.js CHANGED
@@ -1,63 +1,71 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // Import libs
4
- const { execSync } = require('child_process'),
5
- fs = require('fs'),
6
- path = require('path');
4
+ const fs = require('fs'),
5
+ path = require('path'),
6
+ sass = require('sass');
7
7
 
8
8
  // Init UI colors
9
9
  const nc = '\x1b[0m', // no color
10
10
  br = '\x1b[1;91m', // bright red
11
+ by = '\x1b[1;33m', // bright yellow
11
12
  bg = '\x1b[1;92m'; // bright green
12
13
 
13
- // Check for Sass
14
- try { execSync('sass --version'); }
15
- catch (err) {
16
- console.error(`\n${br}Error: Sass not installed. `
17
- + `\n${nc}Please run 'npm i @adamlui/scss-to-css -D' to ensure all dependencies are present.`);
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(/^\/*/, '') : '';
17
+
18
+ // 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}`
21
+ + '\nExample valid command: \n>> scss-to-css . output.min.css');
18
22
  process.exit(1);
19
23
  }
20
24
 
21
- // Locate first parent dir w/ package.json to indicate project root
22
- const userProjectDir = (() => {
23
- let currentDir = process.cwd();
24
- while (!fs.existsSync(path.join(currentDir, 'package.json'))) {
25
- const parentDir = path.dirname(currentDir);
26
- if (parentDir === currentDir) {
27
- console.error(`\n${br}Error: Could not find package.json in any parent directory.`
28
- + `\n${nc}Please add a manifest to your project to indicate a root.`);
29
- process.exit(1);
30
- }
31
- currentDir = parentDir;
32
- }
33
- return currentDir;
34
- })();
35
-
36
- // Recursively find all SCSS files in project directory
37
- const scssFiles = (() => {
38
- const fileList = [];
39
- const findSCSSfiles = dir => {
40
- const files = fs.readdirSync(dir);
41
- files.forEach(file => {
42
- const filePath = path.join(dir, file);
43
- if (fs.statSync(filePath).isDirectory()) findSCSSfiles(filePath);
44
- else if (file.endsWith('.scss')) fileList.push(filePath);
45
- });
46
- };
47
- findSCSSfiles(userProjectDir); return fileList;
48
- })();
25
+ // Recursively find all SCSS files or arg-passed file
26
+ const inputPath = path.resolve(process.cwd(), inputArg);
27
+ const scssFiles = inputArg.endsWith('.scss') ? [inputPath]
28
+ : (() => {
29
+ const fileList = [];
30
+ (function findSCSSfiles(dir) {
31
+ const files = fs.readdirSync(dir);
32
+ files.forEach(file => {
33
+ const filePath = path.resolve(dir, file);
34
+ if (fs.statSync(filePath).isDirectory())
35
+ findSCSSfiles(filePath); // recursively find SCSS
36
+ else if (/\.scss$/.test(file)) // SCSS file found
37
+ fileList.push(filePath); // store it for minification
38
+ });
39
+ })(inputPath); return fileList;
40
+ })();
49
41
 
50
42
  // Compile SCSS files to CSS
51
- if (scssFiles.length === 0) console.info('\nNo SCSS files found.');
52
- else {
53
- scssFiles.forEach(scssPath => {
54
- const inputDir = path.dirname(scssPath);
55
- const outputPath = path.join(inputDir,
56
- inputDir.includes('scss') ? '..' : '', 'css', // build to ../css/ if from scss/
57
- path.basename(scssPath, '.scss') + '.min.css'
43
+ let generatedCnt = 0;
44
+ console.log(''); // line break before first log
45
+ scssFiles.forEach(scssPath => {
46
+ try { // to compile it
47
+ const outputDir = path.join(
48
+ path.dirname(scssPath), // path of file to be minified
49
+ outputArg.endsWith('.css') ? path.dirname(outputArg) : outputArg, // path from output arg
50
+ path.dirname(scssPath).endsWith('scss') ? '../css' : '' // ../css/ if in scss/
58
51
  );
59
- console.info(`\nCompiling ${ scssPath } to ${ outputPath }...`);
60
- execSync(`sass --style compressed "${ scssPath }" "${ outputPath }"`);
61
- console.info(`${bg}Compilation complete!${nc}`);
62
- });
63
- }
52
+ const outputFilename = (
53
+ outputArg.endsWith('.css') && inputArg.endsWith('.scss')
54
+ ? path.basename(outputArg).replace(/(\.min)?\.css$/, '')
55
+ : path.basename(scssPath, '.scss')
56
+ ) + '.min.css';
57
+ const outputPath = path.join(outputDir, outputFilename);
58
+ console.info(`Compiling ${ scssPath }...`);
59
+ const compileResult = sass.compile(scssPath, { style: 'compressed', sourceMap: true });
60
+ if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
61
+ fs.writeFileSync(outputPath, compileResult.css, 'utf8');
62
+ fs.writeFileSync(outputPath + '.map', JSON.stringify(compileResult.sourceMap), 'utf8');
63
+ generatedCnt+= 2;
64
+ } catch (err) {
65
+ console.error(`${br}Error compiling ${ scssPath }: ${ err.message }${nc}`);
66
+ }
67
+ });
68
+
69
+ // Print final summary
70
+ if (generatedCnt) console.info(`\n${bg}Compilation complete!${nc}\n${ generatedCnt } files generated.`);
71
+ else console.info(`\n${by}No SCSS files found.${nc}`);
Binary file