@@ -20,10 +20,23 @@ const path = require("path");
20
20
const fg = require ( "fast-glob" ) ;
21
21
22
22
const { processCSS } = require ( "../../tasks/component-builder.js" ) ;
23
- const { copy , fetchContent } = require ( "../../tasks/utilities.js" ) ;
23
+ const { fetchContent } = require ( "../../tasks/utilities.js" ) ;
24
24
25
25
require ( "colors" ) ;
26
26
27
+ /**
28
+ * Create a tagline for the CSS file based on the package.json data
29
+ * @param {Object } [packageJson={}]
30
+ * @param {string } packageJson.name
31
+ * @param {string } packageJson.version
32
+ * @returns
33
+ */
34
+ function generateTagline ( { name, version } = { } ) {
35
+ if ( ! name ) return "" ;
36
+ if ( ! version ) return `/* ${ name } */\n\n` ;
37
+ return `/* ${ name } @v${ version } */\n\n` ;
38
+ }
39
+
27
40
/**
28
41
* The builder for the main entry point
29
42
* @param {object } config
@@ -32,19 +45,21 @@ require("colors");
32
45
* @returns Promise<void>
33
46
*/
34
47
async function index ( inputGlob , outputPath , { cwd = process . cwd ( ) , clean = false } = { } ) {
35
- // Create an index.css asset for each component
36
- if ( clean && fs . existsSync ( outputPath ) ) {
37
- await fsp . unlink ( outputPath ) ;
38
- }
39
-
40
48
// Read in the package version from the package.json file
41
49
const packageJson = await fsp . readFile ( path . join ( cwd , "package.json" ) , "utf-8" ) . then ( JSON . parse ) ;
42
50
43
51
const inputs = await fg ( inputGlob , { cwd } ) ;
44
52
const contents = inputs . map ( input => `@import "${ input } ";` ) . join ( "\n" ) ;
45
53
if ( ! contents ) return ;
46
54
47
- return processCSS ( contents , undefined , outputPath , { cwd, clean, configPath : cwd , map : false , resolveImports : true , customTagline : `/* Token version: v${ packageJson . version } */\n\n` } ) ;
55
+ return processCSS ( contents , undefined , outputPath , {
56
+ cwd,
57
+ clean,
58
+ configPath : cwd ,
59
+ map : false ,
60
+ resolveImports : true ,
61
+ customTagline : generateTagline ( packageJson ) ,
62
+ } ) ;
48
63
}
49
64
50
65
/**
@@ -53,7 +68,7 @@ async function index(inputGlob, outputPath, { cwd = process.cwd(), clean = false
53
68
* @param {string } [config.cwd=process.cwd()] - Current working directory for the component
54
69
* @returns {Promise<string[]> }
55
70
*/
56
- async function appendCustomOverrides ( { cwd = process . cwd ( ) } = { } ) {
71
+ async function appendCustomOverrides ( { cwd = process . cwd ( ) , packageJson = { } } = { } ) {
57
72
const promises = [ ] ;
58
73
59
74
// Add custom/*-vars.css to the end of the dist/css/*-vars.css files and run through postcss before writing back to the dist/css/*-vars.css file
@@ -75,6 +90,7 @@ async function appendCustomOverrides({ cwd = process.cwd() } = {}) {
75
90
processCSS ( combinedContent [ 0 ] . content , path . join ( cwd , "dist" , "css" , file ) , path . join ( cwd , "dist" , "css" , file ) , {
76
91
cwd,
77
92
configPath : cwd ,
93
+ customTagline : generateTagline ( packageJson ) ,
78
94
} )
79
95
) ;
80
96
}
@@ -94,25 +110,27 @@ async function main({
94
110
cwd = process . cwd ( ) ,
95
111
clean,
96
112
} = { } ) {
97
- if ( typeof clean === "undefined" ) {
98
- clean = process . env . NODE_ENV === "production" ;
99
- }
100
-
101
113
const key = `[build] ${ "@spectrum-css/tokens" . cyan } index` ;
102
114
console . time ( key ) ;
103
115
104
116
const compiledOutputPath = path . join ( cwd , "dist" ) ;
105
117
118
+ // Ensure the dist directory exists
119
+ if ( ! fs . existsSync ( compiledOutputPath ) ) {
120
+ fs . mkdirSync ( compiledOutputPath ) ;
121
+ }
122
+
123
+ // Read in the package version from the package.json file
124
+ const packageJson = await fsp . readFile ( path . join ( cwd , "package.json" ) , "utf-8" ) . then ( JSON . parse ) ;
125
+
106
126
// Wait for all the custom files to be processed
107
- return appendCustomOverrides ( { cwd } ) . then ( async ( r ) =>
127
+ return appendCustomOverrides ( { packageJson , cwd } ) . then ( async ( r ) =>
108
128
Promise . all ( [
109
129
index (
110
130
[ "dist/css/*-vars.css" ] ,
111
131
path . join ( compiledOutputPath , "css" , "index.css" ) ,
112
- { cwd, clean }
113
- ) . then ( ( reports ) =>
114
- copy ( path . join ( compiledOutputPath , "css" , "index.css" ) , path . join ( cwd , "dist" , "index.css" ) , { cwd, isDeprecated : false } )
115
- . then ( ( reps ) => [ reports , reps ] ) )
132
+ { cwd, clean, packageJson }
133
+ )
116
134
] ) . then ( ( reports ) => {
117
135
const logs = [ reports , r ] . flat ( Infinity ) . filter ( Boolean ) ;
118
136
0 commit comments