forked from carbon-design-system/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
197 lines (176 loc) · 4.72 KB
/
build.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Copyright IBM Corp. 2015, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable no-console */
'use strict';
require('core-js/features/array/flat-map');
const { reporter } = require('@carbon/cli-reporter');
const { types: t, generate } = require('@carbon/scss-generator');
const fs = require('fs-extra');
const path = require('path');
const {
container,
iconSize,
spacing,
fluidSpacing,
sizes,
layout,
} = require('../lib');
async function build() {
reporter.info('Building scss files for layout...');
const SCSS_DIR = path.resolve(__dirname, '../scss/generated');
const files = [
{
filepath: path.join(SCSS_DIR, '_container.scss'),
builder() {
return buildModulesTokenFile(container, 'container');
},
},
{
filepath: path.join(SCSS_DIR, '_size.scss'),
builder() {
const FILE_BANNER =
t.Comment(` Code generated by @carbon/layout. DO NOT EDIT.
Copyright IBM Corp. 2018, 2023
This source code is licensed under the Apache-2.0 license found in the
LICENSE file in the root directory of this source tree.
`);
const convert = {
XSmall: 'xs',
Small: 'sm',
Medium: 'md',
Large: 'lg',
XLarge: 'xl',
'2XLarge': '2xl',
};
const comment = t.Comment(`/ @type Number
/ @access public
/ @group @carbon/layout`);
return t.StyleSheet([
FILE_BANNER,
t.Newline(),
comment,
...Object.entries(sizes).map(([name, value]) => {
return t.Assignment({
id: t.Identifier(`size-${convert[name]}`),
init: t.SassValue(value),
});
}),
]);
},
},
{
filepath: path.join(SCSS_DIR, '_icon-size.scss'),
builder() {
return buildModulesTokenFile(iconSize, 'icon-size');
},
},
{
filepath: path.join(SCSS_DIR, '_spacing.scss'),
builder() {
return buildModulesTokenFile(spacing, 'spacing', '');
},
},
{
filepath: path.join(SCSS_DIR, '_fluid-spacing.scss'),
builder() {
return buildModulesTokenFile(fluidSpacing, 'fluid-spacing', '');
},
},
{
filepath: path.join(SCSS_DIR, '_layout.scss'),
builder() {
return buildModulesTokenFile(layout, 'layout');
},
},
];
await fs.ensureDir(SCSS_DIR);
for (const { filepath, builder } of files) {
const { code } = generate(builder());
await fs.writeFile(filepath, await code);
}
reporter.success('Done! 🎉');
}
/**
* Build a Sass Module token stylesheet for a given token scale and group. This will help
* generate the initial collection of tokens and a list of all tokens. In
* addition, it will generate aliases for these tokens.
*
* @param {Array} tokenScale
* @param {string} group
* @returns {StyleSheet}
*/
function buildModulesTokenFile(tokenScale, group) {
const FILE_BANNER = t.Comment(` Code generated by @carbon/layout. DO NOT EDIT.
Copyright IBM Corp. 2018, 2023
This source code is licensed under the Apache-2.0 license found in the
LICENSE file in the root directory of this source tree.
`);
const values = tokenScale.map((value, index) => {
const name = formatStep(`${group}`, index + 1);
const shorthand = formatStep(group, index + 1);
const id = t.Identifier(name);
return [
name,
shorthand,
id,
t.Assignment({
id,
init: t.SassValue(value),
default: true,
}),
];
});
const variables = values.flatMap(([_name, _shorthand, _id, assignment]) => {
const comment = t.Comment(`/ @type Number
/ @access public
/ @group @carbon/layout`);
return [comment, assignment, t.Newline()];
});
const map = [
t.Comment(`/ @type Map
/ @access public
/ @group @carbon/layout`),
t.Assignment({
id: t.Identifier(group),
init: t.SassMap({
properties: values.map(([name, _shorthand, id]) => {
return t.SassMapProperty({
key: id,
value: t.SassValue(`$${name}`),
});
}),
}),
}),
];
return t.StyleSheet([
FILE_BANNER,
t.Newline(),
...variables,
...map,
t.Newline(),
]);
}
/**
* Format the given step for a token name. Most often, this is to pad a `0` for
* numbers that are less than 10. For example, instead of spacing-1 we would
* want spacing-01
*
* @param {string} name
* @param {number} index
* @returns {string}
*/
function formatStep(name, index) {
let step = index;
if (step < 10) {
step = '0' + step;
}
return `${name}-${step}`;
}
build().catch((error) => {
console.log(error);
process.exit(1);
});