@koobiq/tokens-builder 3.0.0-beta.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Positive Technologies
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Tokens Builder
2
+
3
+
package/build.js ADDED
@@ -0,0 +1,64 @@
1
+ const StyleDictionary = require('style-dictionary');
2
+ const getPlatformConfig = require('./configs');
3
+
4
+ // ==== Include custom transforms ====
5
+ require('./transforms/attribute/md-typography')(StyleDictionary);
6
+ require('./transforms/attribute/typography')(StyleDictionary);
7
+ require('./transforms/attribute/palette')(StyleDictionary);
8
+ require('./transforms/attribute/prefix')(StyleDictionary);
9
+ require('./transforms/attribute/size')(StyleDictionary);
10
+
11
+ // ==== Include custom filters ====
12
+ require('./filters/palette')(StyleDictionary);
13
+ require('./filters/color')(StyleDictionary);
14
+ require('./filters/size')(StyleDictionary);
15
+ require('./filters/typography')(StyleDictionary);
16
+ require('./filters/md-typography')(StyleDictionary);
17
+
18
+ // ==== Include custom transform groups ====
19
+ require('./transformGroups/scss')(StyleDictionary);
20
+ require('./transformGroups/css')(StyleDictionary);
21
+ require('./transformGroups/ts')(StyleDictionary);
22
+
23
+ // ==== Include custom formats ====
24
+ require('./formats/typography')(StyleDictionary);
25
+ require('./formats/palette')(StyleDictionary);
26
+
27
+ // ==== Run build ====
28
+ console.log('Build started...');
29
+ console.log('==============================================');
30
+
31
+
32
+ module.exports = (themeConfig) => {
33
+ StyleDictionary.registerFileHeader({
34
+ name: 'customHeader',
35
+ fileHeader: () => [`Do not edit directly`]
36
+ });
37
+ console.log('themeConfig: ', themeConfig);
38
+
39
+ if (!themeConfig || themeConfig.length === 0) {
40
+ console.error('Build Failed. Please set ThemeConfig, for example: ', {
41
+ name: 'default-theme',
42
+ buildPath: [
43
+ `design-tokens/tokens/properties/**/*.json5`,
44
+ `design-tokens/tokens/components/**/*.json5`
45
+ ],
46
+ outputPath: 'design-tokens/'
47
+ }
48
+ )
49
+ process.exit(0);
50
+ }
51
+
52
+ themeConfig.map((platform) => {
53
+ // APPLY THE CONFIGURATION
54
+ // Very important: the registration of custom transforms
55
+ // needs to be done _before_ applying the configuration
56
+ const StyleDictionaryExtended = StyleDictionary.extend(getPlatformConfig(platform));
57
+
58
+ // FINALLY, BUILD ALL THE PLATFORMS
59
+ StyleDictionaryExtended.buildAllPlatforms();
60
+ });
61
+
62
+ console.log('\n==============================================');
63
+ console.log('\nBuild completed!');
64
+ }
package/configs/css.js ADDED
@@ -0,0 +1,11 @@
1
+ module.exports = {
2
+ css: {
3
+ transformGroup: 'kbq/css',
4
+ files: [{
5
+ destination: 'css-tokens.css',
6
+ format: 'css/variables',
7
+ filter: 'size',
8
+ prefix: 'kbq'
9
+ }]
10
+ }
11
+ };
@@ -0,0 +1,56 @@
1
+ const { join } = require('path');
2
+ const scssConfig = require('./scss');
3
+ const jsConfig = require('./js');
4
+ const cssConfig = require('./css');
5
+
6
+ const filterObj = {
7
+ options: {
8
+ showFileHeader: true,
9
+ fileHeader: 'customHeader'
10
+ }
11
+ };
12
+
13
+ function filterOptions(platforms) {
14
+ const platformObj = {};
15
+
16
+ platforms.map(p => Object.assign(platformObj, p));
17
+
18
+ Object.keys(platformObj).forEach((p) => {
19
+ platformObj[p].files = platformObj[p].files.map(f => ({
20
+ ...f,
21
+ ...filterObj,
22
+ options: {
23
+ ...f.options ?? {},
24
+ ...filterObj.options
25
+ }
26
+ }));
27
+ });
28
+
29
+ return platformObj;
30
+ }
31
+
32
+ function getSources(theme) {
33
+ return theme.buildPath.map(pathName => {
34
+ if (pathName.startsWith('@')) {
35
+ return join('node_modules', pathName);
36
+ }
37
+
38
+ return pathName;
39
+ });
40
+ }
41
+
42
+ function getConfigs(theme) {
43
+ scssConfig.scss.buildPath = theme.outputPath;
44
+ jsConfig.js.buildPath = theme.outputPath;
45
+ cssConfig.css.buildPath = theme.outputPath;
46
+
47
+ return filterOptions([scssConfig, jsConfig, cssConfig]);
48
+ }
49
+
50
+ module.exports = (theme) => {
51
+
52
+ return {
53
+ source: [...getSources(theme)],
54
+ platforms: getConfigs(theme)
55
+ };
56
+ };
package/configs/js.js ADDED
@@ -0,0 +1,16 @@
1
+ module.exports = {
2
+ js: {
3
+ transformGroup: 'kbq/ts',
4
+ files: [
5
+ {
6
+ destination: 'index.js',
7
+ format: 'javascript/es6'
8
+ },
9
+ {
10
+ destination: 'index.d.ts',
11
+ format: 'typescript/es6-declarations'
12
+ }
13
+ ]
14
+ }
15
+ };
16
+
@@ -0,0 +1,29 @@
1
+ module.exports = {
2
+ scss: {
3
+ transformGroup: 'kbq/scss',
4
+ files: [
5
+ {
6
+ destination: '_variables.scss',
7
+ format: 'scss/variables',
8
+ filter: 'color'
9
+ },
10
+ {
11
+ destination: '_palette.scss',
12
+ format: 'kbq-scss/palette',
13
+ filter: 'palette'
14
+ },
15
+ {
16
+ destination: '_typography.scss',
17
+ format: 'kbq-scss/typography',
18
+ mapName: 'koobiq',
19
+ filter: 'typography'
20
+ },
21
+ {
22
+ destination: '_md-typography.scss',
23
+ format: 'kbq-scss/typography',
24
+ mapName: 'md-typography',
25
+ filter: 'md-typography'
26
+ }
27
+ ]
28
+ }
29
+ };
@@ -0,0 +1,9 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerFilter({
4
+ name: 'color',
5
+ matcher: (prop) => !prop.attributes.palette &&
6
+ !prop.attributes.typography &&
7
+ !prop.attributes['md-typography']
8
+ })
9
+ }
@@ -0,0 +1,7 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerFilter({
4
+ name: 'md-typography',
5
+ matcher: (prop) => prop.attributes['md-typography']
6
+ })
7
+ }
@@ -0,0 +1,7 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerFilter({
4
+ name: 'palette',
5
+ matcher: (prop) => prop.attributes.palette
6
+ })
7
+ }
@@ -0,0 +1,7 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerFilter({
4
+ name: 'size',
5
+ matcher: (prop) => prop.attributes.size
6
+ })
7
+ }
@@ -0,0 +1,7 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerFilter({
4
+ name: 'typography',
5
+ matcher: (prop) => prop.attributes.typography
6
+ })
7
+ }
@@ -0,0 +1,25 @@
1
+
2
+ module.exports = (StyleDictionary) => {
3
+ StyleDictionary.registerFormat({
4
+ name: 'kbq-scss/palette',
5
+ formatter: function (dictionary) {
6
+ return dictionary.allProperties
7
+ .map((prop) => `\$${prop.name}: ${getMapFromObj(prop.value)};\n`)
8
+ .join('\n');
9
+ }
10
+ })
11
+ };
12
+
13
+ function getMapFromObj(object) {
14
+ const result = Object.keys(object)
15
+ .map((key) => {
16
+ if (key === 'contrast') {
17
+ return `${key}: ${getMapFromObj(object[key])}`
18
+ }
19
+
20
+ return `${key}: ${object[key].value},\n`
21
+ })
22
+ .join('');
23
+
24
+ return `(\n${result}\n)`
25
+ }
@@ -0,0 +1,48 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerFormat({
4
+ name: 'kbq-scss/typography',
5
+ formatter: function(dictionary) {
6
+ let string;
7
+
8
+ string = dictionary.allProperties
9
+ .map((prop) => {
10
+ let output = '';
11
+
12
+ const value = prop.attributes.category === 'asset' ? `"${ prop.value }"` : prop.value;
13
+
14
+ output += `$${ prop.name }: ${ value };`
15
+
16
+ if (prop.comment) {
17
+ output += ` // ${ prop.comment }`;
18
+ }
19
+
20
+ return output + '\n';
21
+ })
22
+ .join('');
23
+
24
+ return string + `\n$${this.mapName || 'tokens' }: ${processJsonNode(dictionary.properties, 0)};\n`;
25
+ }
26
+ })
27
+ };
28
+
29
+
30
+ function processJsonNode(obj, depth) {
31
+ let output = '';
32
+
33
+ if (obj.hasOwnProperty('value')) {
34
+ output += `$${obj.name}`;
35
+ } else {
36
+ output += '(\n'
37
+ output += Object.keys(obj).map((newKey) => {
38
+ const newProp = obj[newKey];
39
+ const indent = ' '.repeat(depth+1);
40
+
41
+ return `${indent}'${newKey}': ${processJsonNode(newProp, depth + 1)}`;
42
+ }).join(',\n');
43
+
44
+ output += '\n' + ' '.repeat(depth) + ')';
45
+ }
46
+
47
+ return output;
48
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@koobiq/tokens-builder",
3
+ "description": "Design tokens",
4
+ "author": "",
5
+ "license": "MIT",
6
+ "keywords": [
7
+ "tokens",
8
+ "design",
9
+ "system"
10
+ ],
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "exports": {
15
+ "./build": {
16
+ "default": "./build.js"
17
+ },
18
+ "./*": {
19
+ "default": "./*"
20
+ }
21
+ },
22
+ "devDependencies": {
23
+ "style-dictionary": "3.7.1"
24
+ },
25
+ "version": "3.0.0-beta.0"
26
+ }
@@ -0,0 +1,13 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerTransformGroup({
4
+ name: 'kbq/css',
5
+ transforms: [
6
+ 'attribute/cti',
7
+ 'kbq-attribute/size',
8
+ 'name/cti/kebab',
9
+ 'color/css',
10
+ 'kbq/prefix'
11
+ ]
12
+ });
13
+ };
@@ -0,0 +1,13 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerTransformGroup({
4
+ name: 'kbq/scss',
5
+ transforms: [
6
+ 'attribute/cti',
7
+ 'kbq-attribute/palette',
8
+ 'kbq-attribute/typography',
9
+ 'kbq-attribute/md-typography',
10
+ 'name/cti/kebab'
11
+ ]
12
+ });
13
+ }
@@ -0,0 +1,11 @@
1
+ module.exports = (StyleDictionary) => {
2
+ StyleDictionary.registerTransformGroup({
3
+ name: 'kbq/ts',
4
+ transforms: [
5
+ 'attribute/cti',
6
+ 'name/cti/pascal',
7
+ 'size/px',
8
+ 'color/hex'
9
+ ]
10
+ });
11
+ }
@@ -0,0 +1,9 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerTransform({
4
+ name: 'kbq-attribute/md-typography',
5
+ type: 'attribute',
6
+ matcher: (prop) => prop.attributes.category === 'md-typography',
7
+ transformer: () => ({ 'md-typography': true })
8
+ });
9
+ };
@@ -0,0 +1,9 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerTransform({
4
+ name: 'kbq-attribute/palette',
5
+ type: 'attribute',
6
+ matcher: (prop) => prop.name === 'palette',
7
+ transformer: () => ({ palette: true })
8
+ });
9
+ };
@@ -0,0 +1,11 @@
1
+ module.exports = (StyleDictionary) => {
2
+ StyleDictionary.registerTransform({
3
+ name: 'kbq/prefix',
4
+ type: 'attribute',
5
+ transformer: (prop, options) => {
6
+ const prefix = options.files[0].prefix ? `${options.files[0].prefix}-` : '';
7
+
8
+ prop.name = `${prefix}${prop.name}`;
9
+ }
10
+ });
11
+ };
@@ -0,0 +1,8 @@
1
+ module.exports = (StyleDictionary) => {
2
+ StyleDictionary.registerTransform({
3
+ name: 'kbq-attribute/size',
4
+ type: 'attribute',
5
+ matcher: (prop) => prop.attributes.type === 'size',
6
+ transformer: () => ({ size: true })
7
+ });
8
+ };
@@ -0,0 +1,9 @@
1
+ module.exports = (StyleDictionary) => {
2
+
3
+ StyleDictionary.registerTransform({
4
+ name: 'kbq-attribute/typography',
5
+ type: 'attribute',
6
+ matcher: (prop) => prop.attributes.category === 'typography',
7
+ transformer: () => ({ typography: true })
8
+ });
9
+ };