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

Adds Tailwind as an export option #1

Open
wants to merge 69 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
bf9d364
/feat Scaffolds of command for exporting a tailwind config file.
andrewfulton Feb 12, 2022
d318046
/feat Adds a translation of figma2theme tokens to a workable tailwind…
andrewfulton Feb 12, 2022
06c7727
Update dependencies
spykr Feb 16, 2022
85ed22f
Merge pull request #288 from PortableStudios/update-dependencies
spykr Feb 16, 2022
f125fd1
Bump version to 0.6.5
spykr Feb 16, 2022
16d3566
Bump follow-redirects from 1.14.5 to 1.14.8
dependabot[bot] Feb 16, 2022
2e2134d
Merge pull request #287 from PortableStudios/dependabot/npm_and_yarn/…
spykr Feb 16, 2022
42f11a5
chore: update dependencies
spykr Mar 1, 2022
afbcb1c
chore: bump version to `0.6.6`
spykr Mar 1, 2022
243c238
chore: update dependencies
spykr Apr 14, 2022
9f0ecfa
Merge pull request #301 from PortableStudios/update-dependencies
spykr Apr 14, 2022
8c283a6
chore: bump version to `0.6.7`
spykr Apr 14, 2022
48fff2d
Bump minimist from 1.2.5 to 1.2.6
dependabot[bot] Apr 14, 2022
95f2ba6
Merge pull request #299 from PortableStudios/dependabot/npm_and_yarn/…
spykr Apr 14, 2022
7bb7673
chore: update dependencies
spykr Jun 12, 2022
625f3cc
chore: update readme
spykr Jun 12, 2022
dce2e1f
chore: bump version to `0.6.8`
spykr Jun 12, 2022
5f7e7fb
Bump @emotion/react from 11.9.0 to 11.9.3
dependabot[bot] Jun 13, 2022
39b63e9
Merge pull request #322 from PortableStudios/dependabot/npm_and_yarn/…
spykr Jun 13, 2022
fbe947e
Bump @emotion/styled from 11.8.1 to 11.9.3
dependabot[bot] Jun 13, 2022
04b2b0f
Merge pull request #323 from PortableStudios/dependabot/npm_and_yarn/…
spykr Jun 13, 2022
94323fa
chore: update dependencies
spykr Jun 14, 2022
737875b
chore: bump version to `0.6.9`
spykr Jun 14, 2022
b6afed7
chore: update `storybook` to use `webpack` v5
spykr Jun 14, 2022
2bf280a
Bump terser from 4.8.0 to 4.8.1
dependabot[bot] Jul 20, 2022
d117c3e
fix: ignore text styles that begin with `_`
spykr Aug 8, 2022
6b7807d
feat: import all colours from figma file not just ones starting with …
spykr Aug 8, 2022
d97c29d
fix: when exporting chakra text styles replace hardcoded font familie…
spykr Aug 8, 2022
1d409e1
feat: add the ability to define font fallbacks in `.figma2themerc`
spykr Aug 8, 2022
f6baa07
update tests based on change to colour import
spykr Aug 8, 2022
25ef2a0
chore: bump version to `0.7.0`
spykr Aug 8, 2022
cbf9aba
✨ feat: adding preinstall script
EllyLoel Oct 6, 2022
e76a133
✨ feat: adding check for global config file
EllyLoel Oct 6, 2022
f1b251e
chore: update dependencies
spykr Oct 26, 2022
9534b3c
Merge pull request #342 from PortableStudios/update-dependencies
spykr Oct 26, 2022
85977a4
chore: add storybook testing to ci
spykr Oct 26, 2022
8408ae3
Merge pull request #334 from PortableStudios/dependabot/npm_and_yarn/…
spykr Oct 26, 2022
2d4f5e6
Merge pull request #343 from PortableStudios/add-storybook-tests
spykr Oct 26, 2022
e5a2984
chore: bump version to `0.7.1`
spykr Oct 26, 2022
4c78a07
✨ feat: switching from color-convert to colorjs.io and adding style-d…
EllyLoel Nov 7, 2022
673289e
✨ feat: switch to using colorjs.io
EllyLoel Nov 7, 2022
ae8f0d0
✨ feat: updating tokens to match design token format module spec
EllyLoel Nov 7, 2022
fdb08f8
✨ feat: adding css generator
EllyLoel Nov 7, 2022
276af31
Merge branch 'develop' into feature/design-token-format
EllyLoel Nov 7, 2022
80db483
Merge branch 'develop' into feature/export-css-custom-properties
EllyLoel Nov 7, 2022
4b4ae11
🐛 fix: rolling back changes related to PR #340
EllyLoel Nov 7, 2022
4c8d4a7
Merge branch 'feature/export-css-custom-properties' of github.com:Por…
EllyLoel Nov 7, 2022
46d4248
🐛 fix: rolling back changes related to PR #340
EllyLoel Nov 7, 2022
3c32526
🐛 fix: removing ts-ignore and unneeded type check
EllyLoel Nov 15, 2022
45c5329
✂️ refactor: refactoring reshapeDesignTokens function and adding comm…
EllyLoel Nov 15, 2022
4ffb2d8
🐛 fix: removing ts-ignore and unneeded type check
EllyLoel Nov 15, 2022
31db34e
✂️ refactor: refactoring reshapeDesignTokens function and adding comm…
EllyLoel Nov 15, 2022
c22827a
🧹 chore: updating gitignore file
EllyLoel Nov 15, 2022
85811cd
🐛 fix: removing .figma2themerc
EllyLoel Nov 15, 2022
1bc2069
feat: update token types to match new structure
spykr Dec 5, 2022
45fac7e
Merge branch 'feature/design-token-format' into feature/export-css-cu…
spykr Dec 5, 2022
f612883
Merge branch 'feature/design-token-format' into feature/export-css-cu…
EllyLoel Dec 5, 2022
c762aea
Merge branch 'feature/export-css-custom-properties' of github.com:Por…
EllyLoel Dec 5, 2022
9901026
Merge pull request #353 from PortableStudios/feature/export-css-custo…
EllyLoel Dec 6, 2022
07e6803
Merge pull request #356 from PortableStudios/chore/update-gitignore
EllyLoel Dec 6, 2022
e8f21b6
Merge pull request #352 from PortableStudios/feature/design-token-format
EllyLoel Dec 6, 2022
1026f6a
🧹 chore: bump version to 0.8.0
EllyLoel Dec 6, 2022
d1583f1
🧹 chore: adding myself as a contributor
EllyLoel Dec 6, 2022
5d58c8e
feat(UIKIT-17): update to support new page name "Icons & Media"
spykr Dec 15, 2022
3d3f8d5
feat: make the "sizes" page optional
spykr Dec 15, 2022
77922a8
Merge pull request #366 from PortableStudios/UIKIT-17-support-icons-a…
spykr Dec 15, 2022
9bf6ee2
Merge pull request #367 from PortableStudios/make-sizes-page-optional
spykr Dec 15, 2022
132ad39
chore: bump version to `0.8.1`
spykr Dec 15, 2022
2137b61
Merge branch 'develop' into export-tailwind-config
andrewfulton Sep 19, 2023
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
34 changes: 13 additions & 21 deletions src/export/export-chakra.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import fs from 'fs-extra';
import ejs from 'ejs';
import path from 'path';
import prettier from 'prettier';
import svgToJSX from 'svg-to-jsx';
import * as svgson from 'svgson';
import type { Data } from 'ejs';

import { version } from '../../package.json';
import type { OptimisedSVG, Icons, Tokens } from '../utils/types';

import { renderTemplate } from './templating';

const prettierConfigFile = path.resolve(__dirname, '../../.prettierrc');
const templateDir = path.resolve(__dirname, '../../templates');

Expand All @@ -19,18 +18,6 @@ const formatFileContents = async (contents: string) => {
});
};

// Render an EJS template with the given data, format it with Prettier and write the result to the output path
const renderTemplate = async (
templatePath: string,
outputPath: string,
data: Data
) => {
const contents = await ejs
.renderFile(templatePath, data)
.then((str) => formatFileContents(str));
return fs.outputFile(outputPath, contents);
};

type ChakraIcon = {
name: string;
viewBox: string;
Expand Down Expand Up @@ -162,12 +149,17 @@ export default async function exportChakraFromTokens(
// Render and save all the templates simultaneously
await Promise.all(
templates.map((template) => {
return renderTemplate(template.input, template.output, {
chakra,
version,
figmaFileKey,
versionDescription,
});
return renderTemplate(
template.input,
template.output,
{
chakra,
version,
figmaFileKey,
versionDescription,
},
formatFileContents
);
})
);
}
63 changes: 63 additions & 0 deletions src/export/export-tailwind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import fs from 'fs-extra';
import path from 'path';
import prettier from 'prettier';

import { version } from '../../package.json';
import { renderTemplate } from './templating';

import type { Tokens } from '../utils/types';

const prettierConfigFile = path.resolve(__dirname, '../../.prettierrc');
const templateDir = path.resolve(__dirname, '../../templates');

// Run Prettier on JS code using the config file
const formatFileContents = async (contents: string) => {
return prettier.resolveConfig(prettierConfigFile).then((options) => {
return prettier.format(contents, { ...options, parser: 'babel' });
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if there is a more correct parser option here but it din't like it when I left it out

});
};

export default async function exportJsonFromTokens(
tokens: Tokens,
outputDir: string,
figmaFileKey: string,
versionDescription: string
) {
await fs.mkdirs(outputDir);

const tailwind = {
content: [],
theme: {
extend: {},
},
plugins: [],
};

// render the main config
await renderTemplate(
`${templateDir}/tailwind.config.js.ejs`,
`${outputDir}/tailwind.config.js`,
{
tailwind,
version,
figmaFileKey,
versionDescription,
},
formatFileContents
);

// and the local edits file if it doesn't already exist.
if (!fs.existsSync(`${outputDir}/tailwind.config.local.js`)) {
await renderTemplate(
`${templateDir}/tailwind.config.local.js.ejs`,
`${outputDir}/tailwind.config.local.js`,
{
tailwind,
version,
figmaFileKey,
versionDescription,
},
formatFileContents
);
}
}
1 change: 1 addition & 0 deletions src/export/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as exportChakra } from './export-chakra';
export { default as exportJson } from './export-json';
export { default as exportTailwind } from './export-tailwind';
17 changes: 17 additions & 0 deletions src/export/templating.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fs from 'fs-extra';
import ejs from 'ejs';

import type { Data } from 'ejs';

// Render an EJS template with the given data, format it with Prettier and write the result to the output path
export const renderTemplate = async (
templatePath: string,
outputPath: string,
data: Data,
formatFileContents: (s: string) => Promise<string>
) => {
const contents = await ejs
.renderFile(templatePath, data)
.then((str) => formatFileContents(str));
return fs.outputFile(outputPath, contents);
};
21 changes: 20 additions & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {

import getConfig from './utils/config';
import { getFile, getVersions } from './api';
import { exportChakra, exportJson } from './export';
import { exportChakra, exportJson, exportTailwind } from './export';
import importTokensFromFigma from './import/import-figma';

import type { Tokens } from './utils/types';
Expand Down Expand Up @@ -163,3 +163,22 @@ export const generateJson = async (

return generator(exporter, apiKeyOverride, fileUrlOverride, latestChanges);
};

export const generateTailwind = async (
outputDir: string,
apiKeyOverride?: string,
fileUrlOverride?: string,
latestChanges?: boolean
) => {
// Generate a Tailwind config JS file using the tokens
const exporter: Exporter = async (tokens, fileKey, versionDescription) => {
const relativeDir = path.relative(process.cwd(), outputDir);
console.log(
`Exporting Tailwind config file to "${relativeDir}/tailwind.config.js"...`
.bold
);
await exportTailwind(tokens, outputDir, fileKey, versionDescription);
};

return generator(exporter, apiKeyOverride, fileUrlOverride, latestChanges);
};
22 changes: 21 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Command } from 'commander';
import path from 'path';

import { generateChakra, generateJson } from './generate';
import { generateChakra, generateJson, generateTailwind } from './generate';
import { version } from '../package.json';

const program = new Command();
Expand Down Expand Up @@ -52,6 +52,26 @@ program
});
});

program
.command('generate-tailwind')
.description('output a Tailwind config file')
.storeOptionsAsProperties(true)
.option('-o, --output <dir>', 'specify the output directory', './')
.option('--api-key <key>', 'specify the Figma API key')
.option('--file-url <url>', 'specify the URL of the Figma file')
.option(
'--latest-changes',
'use the most current, up-to-date version of the Figma file'
)
.action(async (cmd) => {
const { output, apiKey, fileUrl, latestChanges } = cmd.opts();
const outputDir = path.resolve(process.cwd(), output);
await generateTailwind(outputDir, apiKey, fileUrl, latestChanges).catch(
(e) => {
console.error(e);
}
);
});
program.parseAsync(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
Expand Down
8 changes: 8 additions & 0 deletions templates/tailwind.config.js.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// AUTO-GENERATED - File generated by figma2theme <%- version %>, manual edits may be overwritten
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear at the moment why format-on-save is indenting this file, but not the other.

// Figma file: https://www.figma.com/file/<%- figmaFileKey %>
// File version selected: <%- versionDescription %>
// Generated at: <%- new Date().toString() %>

const withLocalChanges = require('./tailwind.config.local');

module.exports = withLocalChanges(<%- JSON.stringify(tailwind) %>);
5 changes: 5 additions & 0 deletions templates/tailwind.config.local.js.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This file should contain any local edits that need to be made to the tailwind config
// eg any additional plugins or project specific settings.
// The figma2theme setup expects it, if you don't need it just leave it as is

module.exports = (tailwindConfig) => tailwindConfig;