@adamlui/scss-to-css 1.7.1 → 1.7.3
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 +37 -20
- package/dist/scss-to-css.min.js +25 -0
- package/docs/LICENSE.md +5 -1
- package/docs/SECURITY.md +5 -1
- package/package.json +13 -4
- package/scss-to-css.js +0 -250
package/README.md
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
|
-
<
|
|
2
|
-
<
|
|
3
|
-
<
|
|
4
|
-
<
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
</
|
|
1
|
+
<table align="center">
|
|
2
|
+
<td align="center" width=1000>
|
|
3
|
+
<h6>
|
|
4
|
+
<picture>
|
|
5
|
+
<source type="image/svg+xml" media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/white/icon32.svg">
|
|
6
|
+
<img height=14 src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/black/icon32.svg">
|
|
7
|
+
</picture>
|
|
8
|
+
English |
|
|
9
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/zh-cn#readme">简体中文</a> |
|
|
10
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/zh-tw#readme">繁體中文</a> |
|
|
11
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/hi#readme">हिंदी</a> |
|
|
12
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/bn#readme">বাংলা</a> |
|
|
13
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/mr#readme">मराठी</a> |
|
|
14
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/pa#readme">ਪੰਜਾਬੀ</a> |
|
|
15
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/de#readme">Deutsch</a> |
|
|
16
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/es#readme">Español</a> |
|
|
17
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/fr#readme">Français</a> |
|
|
18
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/it#readme">Italiano</a> |
|
|
19
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/nl#readme">Nederlands</a> |
|
|
20
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/scss-to-css/docs/pt#readme">Português</a>
|
|
21
|
+
</h6>
|
|
22
|
+
</td>
|
|
23
|
+
</table>
|
|
18
24
|
|
|
19
25
|
# { } scss-to-css
|
|
20
26
|
|
|
@@ -22,7 +28,7 @@
|
|
|
22
28
|
|
|
23
29
|
<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&color=af68ff&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
24
30
|
<a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
25
|
-
<a href="https://
|
|
31
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/scss-to-css-1.7.3"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.7.3-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
26
32
|
<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&color=blue&labelColor=464646"></a>
|
|
27
33
|
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:scss-to-css/scss-to-css.js"><img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Ascss-to-css%2Fscss-to-css.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
|
|
28
34
|
|
|
@@ -212,8 +218,19 @@ Searches for all SCSS files within the `searchDir` string passed (useful for dis
|
|
|
212
218
|
Options are boolean, passed as object properties. For example:
|
|
213
219
|
|
|
214
220
|
```js
|
|
215
|
-
//
|
|
216
|
-
scssToCSS.findSCSS(
|
|
221
|
+
// Search for SCSS files in exactly assets/scss:
|
|
222
|
+
const searchResults = scssToCSS.findSCSS('assets/scss', { recursive: false });
|
|
223
|
+
console.log(searchResults);
|
|
224
|
+
|
|
225
|
+
/* sample output:
|
|
226
|
+
|
|
227
|
+
Searching for SCSS files...
|
|
228
|
+
Search complete. 2 files found.
|
|
229
|
+
[
|
|
230
|
+
'E:\\js\\utils\\minify.js\\assets\\scss\\foo.scss',
|
|
231
|
+
'E:\\js\\utils\\minify.js\\assets\\scss\\bar.scss'
|
|
232
|
+
]
|
|
233
|
+
*/
|
|
217
234
|
```
|
|
218
235
|
|
|
219
236
|
Available parameters (and their default settings) are:
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const c=require("fs"),l=require("path"),t=require("sass");function a(s,r={}){var e={recursive:!0,verbose:!0,dotFolders:!1};if(r={...e,...r},!s)return console.error("findSCSS() » ERROR: Please supply a `searchDir` as 1st arg.");if("string"!=typeof s)return console.error("findSCSS() » ERROR: 1st arg `searchDir` must be a string.");var o=l.resolve(process.cwd(),s);if(!c.existsSync(o))return console.error("findSCSS() » ERROR: Arg `searchDir` must be an existing directory."+`
|
|
3
|
+
'${o}' does not exist.`);for(const t of Object.keys(r))if(!Object.prototype.hasOwnProperty.call(e,t)){if("isRecursing"!==t)return console.error(`findSCSS() » ERROR: \`${t}\` is an invalid option.`+`
|
|
4
|
+
findSCSS() » Valid options: [ ${Object.keys(e).join(", ")} ]`);if("boolean"!=typeof r[t])return console.error(`findSCSS() » ERROR: \`${t}\` option must be set to \`true\` or \`false\`.`)}const i=c.readdirSync(s),n=[];return r.verbose&&!r.isRecursing&&console.info("\nfindSCSS() » Searching for SCSS files..."),i.forEach(e=>{var o=l.resolve(s,e);c.statSync(o).isDirectory()&&"node_modules"!=e&&(r.dotFolders||!e.startsWith("."))&&r.recursive?n.push(...a(o,{...r,isRecursing:!0})):e.endsWith(".scss")&&n.push(o)}),!r.isRecursing&&r.verbose&&console.info("findSCSS() » Search complete. "+(0===n.length?"No":n.length)+` file${1<n.length?"s":""} found.`+("compile"!==a.caller.name?"\nfindSCSS() » Check returned object.":"")),r.isRecursing||0<n.length?n:[]}function s(e,s={}){var o={minify:!0,sourceMaps:!0,recursive:!0,verbose:!0,dotFolders:!1};if(s={...o,...s},"string"!=typeof e)return console.error("findSCSS() » ERROR: Arg `inputPath` must be a string.");if(e=l.resolve(process.cwd(),e),!c.existsSync(e))return console.error("findSCSS() » ERROR: Arg `inputPath` must be an existing directory or file."+`
|
|
5
|
+
findSCSS() » '${e}' does not exist.`);for(const n of Object.keys(s)){if(!Object.prototype.hasOwnProperty.call(o,n))return console.error(`compile() » ERROR: \`${n}\` is an invalid option.`+`
|
|
6
|
+
findSCSS() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof s[n])return console.error(`compile() » ERROR: \`${n}\` option must be set to \`true\` or \`false\`.`)}const r={style:s.minify?"compressed":"expanded",sourceMap:s.sourceMaps};if(c.existsSync(e)){if(!e.endsWith(".scss"))return a(e,{recursive:s.recursive,verbose:s.verbose,dotFolders:s.dotFolders})?.map(e=>{s.verbose&&console.info(`compile() » Compiling ${e}...`);try{var o=t.compile(e,r);return console.info("compile() » Compilation complete! Check returned object."),{code:o.css,srcMap:o.sourceMap,srcPath:e}}catch(e){return console.error(`
|
|
7
|
+
compile() » ERROR: ${e.message}
|
|
8
|
+
`),{error:e}}}).filter(e=>!e.error);s.verbose&&console.info(`compile() » Compiling ${e}...`);try{var i=t.compile(e,r);return console.info("compile() » Compilation complete! Check returned object."),{code:i.css,srcMap:i.sourceMap,srcPath:l.resolve(process.cwd(),e)}}catch(e){return console.error(`
|
|
9
|
+
compile() » ERROR: ${e.message}
|
|
10
|
+
`),{error:e}}}}if(require.main!==module)module.exports={compile:s,findSCSS:a};else{const i="[0m",n="[1;91m",f="[1;33m",e="[1;92m",u={},p={dryRun:/^--?(?:n|dry-?run)$/,includeDotFolders:/^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?=?(?:true|1)?)$/,noSourceMaps:/^--?(?:S|(?:exclude|disable|no)-?so?u?rce?-?maps?|so?u?rce?-?maps?=(?:false|0))$/,noRecursion:/^--?(?:R|(?:disable|no)-?recursi(?:on|ve)|recursi(?:on|ve)=(?:false|0))$/,noMinify:/^--?(?:M|(?:disable|no)-?minif(?:y|ication)|minif(?:y|ication)=(?:false|0))$/,quietMode:/^--?q(?:uiet)?(?:-?mode)?$/,help:/^--?h(?:elp)?$/,version:/^--?ve?r?s?i?o?n?$/};if(process.argv.forEach(o=>{var e;o.startsWith("-")&&((e=Object.keys(p).find(e=>p[e].test(o)))?u[e]=!0:(console.error(`
|
|
11
|
+
${n}ERROR: Arg [${o}] not recognized.`+i),console.info(`
|
|
12
|
+
${f}Valid arguments are below.`+i),r(["configOptions","infoCmds"]),process.exit(1)))}),process.argv.some(e=>p.help.test(e)))r();else if(process.argv.some(e=>p.version.test(e)))console.info("v"+require("./package.json").version);else{const[d="",m=""]=process.argv.slice(2).filter(e=>!e.startsWith("-")).map(e=>e.replace(/^\/*/,"")),S=l.resolve(process.cwd(),d),h=(d&&!c.existsSync(S)&&(console.error(`
|
|
13
|
+
${n}Error: First argument must be an existing file or directory.`+`
|
|
14
|
+
'${S}' does not exist.`+i+`
|
|
15
|
+
|
|
16
|
+
${e}Example valid command:
|
|
17
|
+
>> scss-to-css . output.min.css`+i+`
|
|
18
|
+
|
|
19
|
+
${f}For all command options:
|
|
20
|
+
>> scss-to-css --help`+i),process.exit(1)),d.endsWith(".scss")?[S]:a(S,{recursive:!u.noRecursion,verbose:!u.quietMode}));if(u.dryRun)0<h.length?(console.info(`
|
|
21
|
+
${f}SCSS files to be compiled:`+i),h.forEach(e=>console.info(e))):console.info(f+`
|
|
22
|
+
No SCSS files will be compiled.`+i);else{const g=[],v=h.map(e=>{var o=s(e,{minify:!u.noMinify,sourceMaps:!u.noSourceMaps,verbose:!u.quietMode});return o.error&&g.push(e),o}).filter(e=>!e.error);if(v?.forEach(({code:e,srcMap:o,srcPath:s})=>{var r=l.join(l.dirname(s),/(?:src|s[ac]ss)$/.test(l.dirname(s))?"../css":m.endsWith(".css")?l.dirname(m):m||"css"),s=(m.endsWith(".css")&&d.endsWith(".scss")?l.basename(m).replace(/(\.min)?\.css$/,""):l.basename(s,".scss"))+".min.css",s=l.join(r,s);c.existsSync(r)||c.mkdirSync(r,{recursive:!0}),c.writeFileSync(s,e,"utf8"),u.noSourceMaps||c.writeFileSync(s+".map",JSON.stringify(o),"utf8")}),0<v?.length){const y=1<v.length?"s":"";o(`
|
|
23
|
+
${e}Compilation complete!`+i),o(v.length+" CSS file"+y+(u.noSourceMaps?"":` + ${v.length} source map`+y)+" generated.")}else o(f+"No SCSS files processed."+i);0<g.length&&(o(`
|
|
24
|
+
`+n+g.length+" file"+(1<g.length?"s":"")+" failed to compile:"+i),g.forEach(e=>o(e)))}}function r(e=["cmdFormat","pathArgs","configOptions","infoCmds"]){const o={cmdFormat:[`
|
|
25
|
+
${f}scss-to-css [inputPath] [outputPath] [options]`+i],pathArgs:["\nPath arguments:"," [inputPath] Path to SCSS file or directory containing SCSS files to be compiled, relative to the current working directory."," [outputPath] Path to file or directory where CSS + sourcemap files will be stored, relative to original file location (if not provided, css/ is used)."],configOptions:["\nConfig options:"," -n, --dry-run Don't actually compile the file(s), just show if they will be processed."," -d, --include-dotfolders Include dotfolders in file search."," -S, --no-source-maps Prevent source maps from being generated."," -M, --no-minify Disable minification of output CSS."," -R, --no-recursion Disable recursive file searching."," -q, --quiet Suppress all logging except errors."],infoCmds:["\nInfo commands:"," -h, --help Display help screen."," -v, --version Show version number."]};e.forEach(e=>{o[e]?.forEach(e=>{{const r=process.stdout.columns||80,i=[],o=e.match(/\S+|\s+/g);let s="";o.forEach(e=>{var o=r-(0===i.length?0:30);s.length+e.length>o&&(i.push(0===i.length?s:s.trimStart()),s=""),s+=e}),i.push(0===i.length?s:s.trimStart()),i.forEach((e,o)=>console.info(0===o?e:" ".repeat(30)+e))}})})}function o(e){u.quietMode||console.info(e)}}
|
package/docs/LICENSE.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<div align="
|
|
1
|
+
<div align="center">
|
|
2
2
|
<h6>
|
|
3
3
|
<picture>
|
|
4
4
|
<source type="image/svg+xml" media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/white/icon32.svg">
|
|
@@ -11,7 +11,11 @@
|
|
|
11
11
|
<a href="bn/LICENSE.md">বাংলা</a> |
|
|
12
12
|
<a href="mr/LICENSE.md">मराठी</a> |
|
|
13
13
|
<a href="pa/LICENSE.md">ਪੰਜਾਬੀ</a> |
|
|
14
|
+
<a href="de/LICENSE.md">Deutsch</a> |
|
|
14
15
|
<a href="es/LICENSE.md">Español</a> |
|
|
16
|
+
<a href="fr/LICENSE.md">Français</a> |
|
|
17
|
+
<a href="it/LICENSE.md">Italiano</a> |
|
|
18
|
+
<a href="nl/LICENSE.md">Nederlands</a> |
|
|
15
19
|
<a href="pt/LICENSE.md">Português</a>
|
|
16
20
|
</h6>
|
|
17
21
|
</div>
|
package/docs/SECURITY.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<div align="
|
|
1
|
+
<div align="center">
|
|
2
2
|
<h6>
|
|
3
3
|
<picture>
|
|
4
4
|
<source type="image/svg+xml" media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/white/icon32.svg">
|
|
@@ -11,7 +11,11 @@
|
|
|
11
11
|
<a href="bn/SECURITY.md">বাংলা</a> |
|
|
12
12
|
<a href="mr/SECURITY.md">मराठी</a> |
|
|
13
13
|
<a href="pa/SECURITY.md">ਪੰਜਾਬੀ</a> |
|
|
14
|
+
<a href="de/SECURITY.md">Deutsch</a> |
|
|
14
15
|
<a href="es/SECURITY.md">Español</a> |
|
|
16
|
+
<a href="fr/SECURITY.md">Français</a> |
|
|
17
|
+
<a href="it/SECURITY.md">Italiano</a> |
|
|
18
|
+
<a href="nl/SECURITY.md">Nederlands</a> |
|
|
15
19
|
<a href="pt/SECURITY.md">Português</a>
|
|
16
20
|
</h6>
|
|
17
21
|
</div>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adamlui/scss-to-css",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.3",
|
|
4
4
|
"description": "Recursively compile all SCSS files into minified CSS",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
@@ -9,13 +9,19 @@
|
|
|
9
9
|
},
|
|
10
10
|
"homepage": "https://github.com/adamlui/js-utils",
|
|
11
11
|
"license": "MIT",
|
|
12
|
-
"main": "scss-to-css.js",
|
|
12
|
+
"main": "dist/scss-to-css.min.js",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"docs/",
|
|
16
|
+
"!docs/*/"
|
|
17
|
+
],
|
|
13
18
|
"bin": {
|
|
14
|
-
"scsstocss": "scss-to-css.js",
|
|
15
|
-
"scss-to-css": "scss-to-css.js"
|
|
19
|
+
"scsstocss": "dist/scss-to-css.min.js",
|
|
20
|
+
"scss-to-css": "dist/scss-to-css.min.js"
|
|
16
21
|
},
|
|
17
22
|
"scripts": {
|
|
18
23
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
24
|
+
"build": "minify-js scss-to-css.js dist",
|
|
19
25
|
"bump:patch": "bash utils/bump.sh patch",
|
|
20
26
|
"bump:minor": "bash utils/bump.sh minor",
|
|
21
27
|
"bump:major": "bash utils/bump.sh major",
|
|
@@ -46,5 +52,8 @@
|
|
|
46
52
|
"funding": {
|
|
47
53
|
"type": "github",
|
|
48
54
|
"url": "https://github.com/sponsors/adamlui"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@adamlui/minify.js": "^1.4.6"
|
|
49
58
|
}
|
|
50
59
|
}
|
package/scss-to-css.js
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
// Import LIBS
|
|
4
|
-
const fs = require('fs'),
|
|
5
|
-
path = require('path'),
|
|
6
|
-
sass = require('sass');
|
|
7
|
-
|
|
8
|
-
// Define MAIN functions
|
|
9
|
-
|
|
10
|
-
function findSCSS(searchDir, options = {}) {
|
|
11
|
-
|
|
12
|
-
// Init options
|
|
13
|
-
const defaultOptions = { recursive: true, verbose: true, dotFolders: false };
|
|
14
|
-
options = { ...defaultOptions, ...options };
|
|
15
|
-
|
|
16
|
-
// Search for SCSS
|
|
17
|
-
const dirFiles = fs.readdirSync(searchDir), scssFiles = [];
|
|
18
|
-
if (options.verbose && !options.isRecursing) console.info('\nSearching for SCSS files...');
|
|
19
|
-
dirFiles.forEach(file => {
|
|
20
|
-
const filePath = path.resolve(searchDir, file);
|
|
21
|
-
if (fs.statSync(filePath).isDirectory() && file != 'node_modules'
|
|
22
|
-
&& (options.dotFolders || !file.startsWith('.')) && options.recursive)
|
|
23
|
-
scssFiles.push( // recursively find SCSS in eligible dir
|
|
24
|
-
...findSCSS(filePath, { ...options, isRecursing: true }));
|
|
25
|
-
else if (file.endsWith('.scss')) // SCSS file found
|
|
26
|
-
scssFiles.push(filePath); // store it for compilation
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
// Log/return final result
|
|
30
|
-
if (!options.isRecursing && options.verbose) {
|
|
31
|
-
console.info('Search complete. '
|
|
32
|
-
+ ( scssFiles.length === 0 ? 'No' : scssFiles.length )
|
|
33
|
-
+ ` file${ scssFiles.length > 1 ? 's' : '' } found.`);
|
|
34
|
-
}
|
|
35
|
-
return options.isRecursing || scssFiles.length > 0 ? scssFiles : [];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function compile(inputPath, options = {}) {
|
|
39
|
-
|
|
40
|
-
// Init options
|
|
41
|
-
const defaultOptions = { minify: true, sourceMaps: true, recursive: true, verbose: true, dotFolders: false };
|
|
42
|
-
options = { ...defaultOptions, ...options };
|
|
43
|
-
|
|
44
|
-
// Validate inputPath
|
|
45
|
-
if (typeof inputPath !== 'string')
|
|
46
|
-
return console.error('ERROR:'
|
|
47
|
-
+ ' First argument must be a string representing a file/folder path.');
|
|
48
|
-
|
|
49
|
-
// Compile SCSS based on inputPath
|
|
50
|
-
const compileOptions = { style: options.minify ? 'compressed' : 'expanded', sourceMap: options.sourceMaps };
|
|
51
|
-
if (fs.existsSync(inputPath)) { // compile based on path arg
|
|
52
|
-
if (inputPath.endsWith('.scss')) { // file path passed
|
|
53
|
-
if (options.verbose) console.info(`Compiling ${ inputPath }...`);
|
|
54
|
-
try { // to compile file passed
|
|
55
|
-
const compileResult = sass.compile(inputPath, compileOptions);
|
|
56
|
-
return { code: compileResult.css, srcMap: compileResult.sourceMap, srcPath: inputPath };
|
|
57
|
-
} catch (err) { console.error(`\nERROR: ${ err.message }\n`); return { error: err }; }
|
|
58
|
-
} else { // dir path passed
|
|
59
|
-
return findSCSS(inputPath, { recursive: options.recursive, verbosity: options.verbose,
|
|
60
|
-
dotFolders: options.dotFolders
|
|
61
|
-
})?.map(scssPath => { // compile found SCSS files
|
|
62
|
-
if (options.verbose) console.info(`Compiling ${ scssPath }...`);
|
|
63
|
-
try { // to compile found file
|
|
64
|
-
const compileResult = sass.compile(scssPath, compileOptions);
|
|
65
|
-
return { code: compileResult.css, srcMap: compileResult.sourceMap, srcPath: scssPath };
|
|
66
|
-
} catch (err) { console.error(`\nERROR: ${ err.message }\n`); return { error: err }; }
|
|
67
|
-
}).filter(data => !data.error ); // filter out failed compilations
|
|
68
|
-
}
|
|
69
|
-
} else return console.error('First argument must be an existing file or directory.'
|
|
70
|
-
+ `\n'${ inputPath }' does not exist.`);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// EXPORT functions if script was required
|
|
74
|
-
if (require.main !== module) module.exports = { compile, findSCSS };
|
|
75
|
-
|
|
76
|
-
else { // run as CLI tool
|
|
77
|
-
|
|
78
|
-
// Init UI colors
|
|
79
|
-
const nc = '\x1b[0m', // no color
|
|
80
|
-
br = '\x1b[1;91m', // bright red
|
|
81
|
-
by = '\x1b[1;33m', // bright yellow
|
|
82
|
-
bg = '\x1b[1;92m'; // bright green
|
|
83
|
-
|
|
84
|
-
// Load FLAG settings
|
|
85
|
-
const config = {};
|
|
86
|
-
const argRegex = {
|
|
87
|
-
'dryRun': /^--?(?:n|dry-?run)$/,
|
|
88
|
-
'includeDotFolders': /^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?=?(?:true|1)?)$/,
|
|
89
|
-
'noSourceMaps': /^--?(?:S|(?:exclude|disable|no)-?so?u?rce?-?maps?|so?u?rce?-?maps?=(?:false|0))$/,
|
|
90
|
-
'noRecursion': /^--?(?:R|(?:disable|no)-?recursion|recursion=(?:false|0))$/,
|
|
91
|
-
'noMinify': /^--?(?:M|(?:disable|no)-?minif(?:y|ication)|minif(?:y|ication)=(?:false|0))$/,
|
|
92
|
-
'quietMode': /^--?q(?:uiet)?(?:-?mode)?$/,
|
|
93
|
-
'help': /^--?h(?:elp)?$/,
|
|
94
|
-
'version': /^--?ve?r?s?i?o?n?$/
|
|
95
|
-
};
|
|
96
|
-
process.argv.forEach(arg => {
|
|
97
|
-
if (!arg.startsWith('-')) return;
|
|
98
|
-
const matchedFlag = Object.keys(argRegex).find(flag => argRegex[flag].test(arg));
|
|
99
|
-
if (matchedFlag) config[matchedFlag] = true;
|
|
100
|
-
else {
|
|
101
|
-
console.error(`\n${br}ERROR: Arg [${ arg }] not recognized.${nc}`);
|
|
102
|
-
console.info(`\n${by}Valid arguments are below.${nc}`);
|
|
103
|
-
printHelpScreen(['configOptions', 'infoCmds']);
|
|
104
|
-
process.exit(1);
|
|
105
|
-
}});
|
|
106
|
-
|
|
107
|
-
// Show HELP screen if -h or --help passed
|
|
108
|
-
if (process.argv.some(arg => argRegex.help.test(arg))) printHelpScreen();
|
|
109
|
-
|
|
110
|
-
// Show VERSION number if -v or --version passed
|
|
111
|
-
else if (process.argv.some(arg => argRegex.version.test(arg)))
|
|
112
|
-
console.info('v' + require('./package.json').version);
|
|
113
|
-
|
|
114
|
-
else { // run MAIN routine
|
|
115
|
-
|
|
116
|
-
// Init I/O args
|
|
117
|
-
const [inputArg = '', outputArg = ''] = ( // default to empty strings for error-less handling
|
|
118
|
-
process.argv.slice(2) // exclude executable and script paths
|
|
119
|
-
.filter(arg => !arg.startsWith('-')) // exclude flags
|
|
120
|
-
.map(arg => arg.replace(/^\/*/, '')) // clean leading slashes to avoid parsing system root
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
// Validate input arg (output arg can be anything)
|
|
124
|
-
const inputPath = path.resolve(process.cwd(), inputArg);
|
|
125
|
-
if (inputArg && !fs.existsSync(inputPath)) {
|
|
126
|
-
console.error(`\n${br}Error: First argument must be an existing file or directory.`
|
|
127
|
-
+ `\n'${ inputPath }' does not exist.${nc}`
|
|
128
|
-
+ `\n\n${bg}Example valid command: \n>> scss-to-css . output.min.css${nc}`
|
|
129
|
-
+ `\n\n${by}For all command options: \n>> scss-to-css --help${nc}`);
|
|
130
|
-
process.exit(1);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Find all eligible JavaScript files or arg-passed file
|
|
134
|
-
const scssFiles = inputArg.endsWith('.scss') ? [inputPath]
|
|
135
|
-
: findSCSS(inputPath, { recursive: !config.noRecursion, verbose: !config.quietMode });
|
|
136
|
-
|
|
137
|
-
if (config.dryRun) { // -n or --dry-run passed
|
|
138
|
-
if (scssFiles.length > 0) { // print files to be processed
|
|
139
|
-
console.info(`\n${by}SCSS files to be compiled:${nc}`);
|
|
140
|
-
scssFiles.forEach(file => console.info(file));
|
|
141
|
-
} else console.info(`${by}\nNo SCSS files will be compiled.${nc}`);
|
|
142
|
-
|
|
143
|
-
} else { // actually compile SCSS files
|
|
144
|
-
|
|
145
|
-
// Build array of compilation data
|
|
146
|
-
const failedPaths = [];
|
|
147
|
-
const compileData = scssFiles.map(scssPath => {
|
|
148
|
-
const compileResult = compile(scssPath, {
|
|
149
|
-
minify: !config.noMinify, sourceMaps: !config.noSourceMaps, verbose: !config.quietMode });
|
|
150
|
-
if (compileResult.error) failedPaths.push(scssPath);
|
|
151
|
-
return compileResult;
|
|
152
|
-
}).filter(data => !data.error ); // filter out failed compilations
|
|
153
|
-
|
|
154
|
-
// Write array data to files
|
|
155
|
-
compileData?.forEach(({ code, srcMap, srcPath }) => {
|
|
156
|
-
const outputDir = path.join(
|
|
157
|
-
path.dirname(srcPath), // path of file to be minified
|
|
158
|
-
/(?:src|s[ac]ss)$/.test(path.dirname(srcPath)) ? '../css' // + ../css/ if in *(src|sass|scss)/
|
|
159
|
-
: outputArg.endsWith('.css') ? path.dirname(outputArg) // or path from file output arg
|
|
160
|
-
: outputArg || 'css' // or path from folder output arg or css/ if no output arg passed
|
|
161
|
-
);
|
|
162
|
-
const outputFilename = (
|
|
163
|
-
outputArg.endsWith('.css') && inputArg.endsWith('.scss')
|
|
164
|
-
? path.basename(outputArg).replace(/(\.min)?\.css$/, '')
|
|
165
|
-
: path.basename(srcPath, '.scss')
|
|
166
|
-
) + '.min.css';
|
|
167
|
-
const outputPath = path.join(outputDir, outputFilename);
|
|
168
|
-
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
|
|
169
|
-
fs.writeFileSync(outputPath, code, 'utf8');
|
|
170
|
-
if (!config.noSourceMaps) fs.writeFileSync(outputPath + '.map', JSON.stringify(srcMap), 'utf8');
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
// Print final summary
|
|
174
|
-
if (compileData?.length > 0) {
|
|
175
|
-
const cssCntSuffix = compileData.length > 1 ? 's' : '';
|
|
176
|
-
printIfNotQuiet(`\n${bg}Compilation complete!${nc}`);
|
|
177
|
-
printIfNotQuiet(`${ compileData.length } CSS file${ cssCntSuffix }`
|
|
178
|
-
+ ( !config.noSourceMaps ? ` + ${ compileData.length } source map${ cssCntSuffix }` : '' )
|
|
179
|
-
+ ' generated.');
|
|
180
|
-
} else printIfNotQuiet(`${by}No SCSS files processed.${nc}`);
|
|
181
|
-
if (failedPaths.length > 0) {
|
|
182
|
-
printIfNotQuiet(`\n${br}`
|
|
183
|
-
+ `${ failedPaths.length } file${ failedPaths.length > 1 ? 's' : '' }`
|
|
184
|
-
+ ` failed to compile:${nc}`);
|
|
185
|
-
failedPaths.forEach(path => printIfNotQuiet(path));
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// Define LOGGING functions
|
|
191
|
-
|
|
192
|
-
function printHelpMsg(msg) { // wrap msg + indent 2nd+ lines (for --help screen)
|
|
193
|
-
const terminalWidth = process.stdout.columns || 80,
|
|
194
|
-
indentation = 30, lines = [], words = msg.match(/\S+|\s+/g);
|
|
195
|
-
|
|
196
|
-
// Split msg into lines of appropriate lengths
|
|
197
|
-
let currentLine = '';
|
|
198
|
-
words.forEach(word => {
|
|
199
|
-
const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
|
|
200
|
-
if (currentLine.length + word.length > lineLength) { // cap/store it
|
|
201
|
-
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
202
|
-
currentLine = '';
|
|
203
|
-
}
|
|
204
|
-
currentLine += word;
|
|
205
|
-
});
|
|
206
|
-
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
207
|
-
|
|
208
|
-
// Print formatted msg
|
|
209
|
-
lines.forEach((line, index) => console.info(
|
|
210
|
-
index === 0 ? line // print 1st line unindented
|
|
211
|
-
: ' '.repeat(indentation) + line // print subsequent lines indented
|
|
212
|
-
));
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function printHelpScreen(includeSections = ['cmdFormat', 'pathArgs', 'configOptions', 'infoCmds']) {
|
|
216
|
-
const sections = {
|
|
217
|
-
'cmdFormat': [
|
|
218
|
-
`\n${by}scss-to-css [inputPath] [outputPath] [options]${nc}`
|
|
219
|
-
],
|
|
220
|
-
'pathArgs': [
|
|
221
|
-
'\nPath arguments:',
|
|
222
|
-
' [inputPath] '
|
|
223
|
-
+ 'Path to SCSS file or directory containing SCSS files to be compiled,'
|
|
224
|
-
+ ' relative to the current working directory.',
|
|
225
|
-
' [outputPath] '
|
|
226
|
-
+ 'Path to file or directory where CSS + sourcemap files will be stored,'
|
|
227
|
-
+ ' relative to original file location (if not provided, css/ is used).'
|
|
228
|
-
],
|
|
229
|
-
'configOptions': [
|
|
230
|
-
'\nConfig options:',
|
|
231
|
-
' -n, --dry-run Don\'t actually compile the file(s),'
|
|
232
|
-
+ ' just show if they will be processed.',
|
|
233
|
-
' -d, --include-dotfolders Include dotfolders in file search.',
|
|
234
|
-
' -S, --no-source-maps Prevent source maps from being generated.',
|
|
235
|
-
' -M, --no-minify Disable minification of output CSS.',
|
|
236
|
-
' -R, --no-recursion Disable recursive file searching.',
|
|
237
|
-
' -q, --quiet Suppress all logging except errors.'
|
|
238
|
-
],
|
|
239
|
-
'infoCmds': [
|
|
240
|
-
'\nInfo commands:',
|
|
241
|
-
' -h, --help Display help screen.',
|
|
242
|
-
' -v, --version Show version number.'
|
|
243
|
-
]
|
|
244
|
-
};
|
|
245
|
-
includeSections.forEach(section => { // print valid arg elems
|
|
246
|
-
if (sections[section]) sections[section].forEach(line => printHelpMsg(line)); });
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
function printIfNotQuiet(msg) { if (!config.quietMode) console.info(msg); }
|
|
250
|
-
}
|