@adamlui/scss-to-css 1.0.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 (2) hide show
  1. package/package.json +33 -0
  2. package/scss-to-css.js +63 -0
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@adamlui/scss-to-css",
3
+ "version": "1.0.0",
4
+ "description": "Compile all SCSS files into CSS recursively",
5
+ "author": "Adam Lui",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/adamlui/js-utils",
8
+ "main": "scss-to-css.js",
9
+ "bin": {
10
+ "scss-to-css": "scss-to-css.js"
11
+ },
12
+ "scripts": {
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/adamlui/js-utils.git"
18
+ },
19
+ "keywords": [
20
+ "scss",
21
+ "sass",
22
+ "css",
23
+ "stylesheets",
24
+ "compiler",
25
+ "minifier"
26
+ ],
27
+ "bugs": {
28
+ "url": "https://github.com/adamlui/js-utils/issues"
29
+ },
30
+ "dependencies": {
31
+ "sass": "^1.70.0"
32
+ }
33
+ }
package/scss-to-css.js ADDED
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env node
2
+
3
+ // Import libs
4
+ const { execSync } = require('child_process'),
5
+ fs = require('fs'),
6
+ path = require('path');
7
+
8
+ // Init UI colors
9
+ const nc = '\x1b[0m', // no color
10
+ br = '\x1b[1;91m', // bright red
11
+ bg = '\x1b[1;92m'; // bright green
12
+
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.`);
18
+ process.exit(1);
19
+ }
20
+
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
+ })();
49
+
50
+ // 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'
58
+ );
59
+ console.info(`\nCompiling ${ scssPath } to ${ outputPath }...`);
60
+ execSync(`sass --style compressed "${ scssPath }" "${ outputPath }"`);
61
+ console.info(`${bg}Compilation complete!`);
62
+ });
63
+ }