-
Notifications
You must be signed in to change notification settings - Fork 10
/
build-css.js
71 lines (62 loc) · 2.02 KB
/
build-css.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import * as fs from "fs"
import { execa } from "execa"
import path from "path"
/**
* The directory where the components are located.
* @type {string}
*/
const componentsDir = "components/packages"
/**
* The output directory for compiled CSS.
* @type {string}
*/
const outputDir = "dist/packages"
/**
* Get the list of component names from the components directory.
* @type {string[]}
*/
const componentNames = fs.readdirSync(componentsDir)
/**
* Compile Sass to CSS using the `sass` command.
* @param {string} sassFilePath - The path to the Sass file.
* @param {string} cssFilePath - The path to the output CSS file.
* @returns {Promise<void>} A promise that resolves when Sass compilation is complete.
*/
const compileSass = async (sassFilePath, cssFilePath) => {
try {
await execa("sass", [sassFilePath, cssFilePath, "--no-source-map"], {
stdio: "inherit",
})
console.log(`Successfully compiled Sass: ${sassFilePath}`)
} catch (err) {
console.error(`Error compiling Sass: ${sassFilePath}`)
console.error(err)
}
}
/**
* Compile Sass for all components.
* @returns {Promise<void>} A promise that resolves when all components are compiled.
*/
const compileComponents = async () => {
const compileTasks = componentNames.map(async (componentName) => {
const componentDir = path.join(componentsDir, componentName)
const sassFilePath = path.join(componentDir, "styles", "_index.scss")
const outputStyleDir = path.join(outputDir, componentName, "style")
const cssFilePath = path.join(outputStyleDir, "index.css")
if (fs.existsSync(sassFilePath)) {
// Create the output directory
fs.mkdirSync(outputStyleDir, { recursive: true })
await compileSass(sassFilePath, cssFilePath)
} else {
console.warn(`Sass file not found: ${sassFilePath}`)
}
})
try {
await Promise.all(compileTasks)
console.log("All components compiled successfully.")
} catch (err) {
console.error("Error compiling components:")
console.error(err)
}
}
compileComponents()