Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate JavaScript tokens #790

Merged
merged 6 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .theo/formats/stories.mdx.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const groupBy = require('lodash/groupBy');
const camelCase = require('lodash/camelCase');
const kebabCase = require('lodash/kebabCase');
const startCase = require('lodash/startCase');
const { basename, extname } = require('path');
Expand Down Expand Up @@ -129,6 +130,10 @@ function mdxStoriesFormat(result) {
const slug = basename(filename, extname(filename));
const title = startCase(slug);
const props = result.get('props').toJS();
const firstProp = props[0];
firstProp.sassName = `$${kebabCase(firstProp.name)}`;
firstProp.jsName = camelCase(firstProp.name);
firstProp.jsonValue = JSON.stringify(firstProp.value);
const categories = groupBy(props, 'category');
const mdxCategories = Object.keys(categories).map((category) =>
categoryToMdx(category, categories[category])
Expand All @@ -141,8 +146,12 @@ import { Meta, ColorPalette, ColorItem } from '@storybook/addon-docs/blocks';
# ${title}

\`\`\`scss
@use 'path/to/${filename}';
$example: ${slug}.$${kebabCase(props[0].name)}; // => ${props[0].value}
@use "../../design-tokens/${filename}";
$example: ${slug}.${firstProp.sassName}; // => ${firstProp.value}
\`\`\`
\`\`\`javascript
import { ${firstProp.jsName} } from '../../design-tokens/generated/${slug}.js';
console.log(${firstProp.jsName}); // => ${firstProp.jsonValue}
\`\`\`

${mdxCategories.join('\n\n')}`.trim();
Expand Down
2 changes: 1 addition & 1 deletion gulpfile.js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

module.exports = {
watchPreprocess: require('./tasks/watch-preprocess'),
theoToMDX: require('./tasks/theo-to-mdx'),
theo: require('./tasks/theo'),
svgToTwig: require('./tasks/svg-to-twig'),
buildSass: require('./tasks/build-sass'),
buildJS: require('./tasks/build-scripts').buildJS,
Expand Down
25 changes: 0 additions & 25 deletions gulpfile.js/tasks/theo-to-mdx.js

This file was deleted.

48 changes: 48 additions & 0 deletions gulpfile.js/tasks/theo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { src, dest, parallel } = require('gulp');
const gulpTheo = require('gulp-theo');
// Requiring this file applies our Theo customizations to gulp-theo
require('../../.theo');
const prettier = require('gulp-prettier');
const rename = require('gulp-rename');

// Ignore any files with leading underscores
const srcGlob = ['src/design-tokens/*.yml', '!src/design-tokens/_*'];
const destGlob = 'src/design-tokens/generated';
const transform = {
type: 'web',
};

function theoToJs() {
return src(srcGlob)
.pipe(
gulpTheo({
transform,
format: {
type: 'module.js',
},
})
)
.pipe(prettier())
.pipe(
rename((path) => {
path.basename = path.basename.replace('.module', '');
})
)
.pipe(dest(destGlob));
}

function theoToMdx() {
return src(srcGlob)
.pipe(
gulpTheo({
transform,
format: {
type: 'stories.mdx',
},
})
)
.pipe(prettier())
.pipe(dest(destGlob));
}

module.exports = parallel(theoToJs, theoToMdx);
4 changes: 2 additions & 2 deletions gulpfile.js/tasks/watch-preprocess.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { watch } = require('gulp');
const svgToTwig = require('./svg-to-twig');
const theoToMdx = require('./theo-to-mdx');
const theo = require('./theo');

module.exports = function () {
watch('src/assets/**/*.svg', svgToTwig);
watch('src/design-tokens/**/*.yml', theoToMdx);
watch('src/design-tokens/**/*.yml', theo);
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@
"build-storybook": "npm run preprocess && build-storybook -s static",
"preprocess": "run-p preprocess:*",
"preprocess:svg-to-twig": "gulp svgToTwig",
"preprocess:theo-to-mdx": "gulp theoToMDX",
"preprocess:theo": "gulp theo",
"watch-preprocess": "gulp watchPreprocess",
"build:js": "gulp buildJS",
"build:types": "npm run type && gulp buildTypes",
"build:sass": "gulp buildSass",
"build": "npm run clean && run-p build:*",
"build": "run-s clean preprocess && run-p build:*",
"clean": "eliminate dist",
"type": "tsc",
"type:watch": "tsc --watch",
Expand Down
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
"skipLibCheck": true
},
"include": ["src", "cypress"],
"exclude": ["src/prototypes", "src/vendor/prism/demo/samples"]
"exclude": [
"src/prototypes",
"src/vendor/prism/demo/samples",
"src/design-tokens/generated"
]
}