Skip to content

Commit

Permalink
fix(repo): Remove formatting from token build
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivanpj committed Nov 13, 2022
1 parent 3adf326 commit 070133f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 47 deletions.
2 changes: 1 addition & 1 deletion tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { createGlobPatternsForDependencies } = require("@nrwl/react/tailwind");
const { join } = require("path");
const extend = require("dist/design-system/tokens/js/theme");
const extend = require("./dist/design-system/tokens/js/theme");

/** @type {import('tailwindcss').Config} */
module.exports = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import { ConsoleLogger } from "@open-system/core-typescript-utilities";
import deepmerge from 'deepmerge';
import * as _ from 'lodash';
import os from 'os';
import prettier from 'prettier';
import { IToken, PartialRecord, TokensType } from '../../types';
import { LibsType } from '../global-libs';
import { ColorsFormat, FormatName, TailwindTokenClass, TailwindType } from './to-tailwind.type';
import * as TokensClass from './tokens';
import deepmerge from "deepmerge";
import * as _ from "lodash";
import os from "os";
import { IToken, PartialRecord, TokensType } from "../../types";
import { LibsType } from "../global-libs";
import {
ColorsFormat,
FormatName,
TailwindTokenClass,
TailwindType,
} from "./to-tailwind.type";
import * as TokensClass from "./tokens";

export type OutputDataType = string;
export type InputDataType = Array<
Pick<IToken, 'id' | 'name' | 'value' | 'type'> & Record<string, any>
Pick<IToken, "id" | "name" | "value" | "type"> & Record<string, any>
>;
export type FormatTokenType = Partial<{
colorFormat: {
format: ColorsFormat;
};
fontSizeFormat: {
unit: 'px' | 'rem';
unit: "px" | "rem";
};
}>;
export type OptionsType =
| Partial<{
formatName: FormatName;
formatTokens: FormatTokenType;
formatConfig: Partial<{
module: 'es6' | 'commonjs';
module: "es6" | "commonjs";
objectName: string;
endOfLine: 'auto' | 'lf' | 'crlf' | 'cr';
endOfLine: "auto" | "lf" | "crlf" | "cr";
tabWidth: number;
useTabs: boolean;
singleQuote: boolean;
Expand All @@ -53,18 +57,20 @@ class ToTailwind {
styles: Partial<Record<TailwindType, any>> = {};
constructor(tokens: InputDataType, options: OptionsType) {
this.options = options;
this.objectName = options?.formatConfig?.objectName ?? 'theme';
this.objectName = options?.formatConfig?.objectName ?? "theme";
this.exportDefault = options?.formatConfig?.exportDefault ?? true;
this.module = options?.formatConfig?.module ?? 'es6';
this.module = options?.formatConfig?.module ?? "es6";
this.tokens = tokens;
this.tokensGroupedByType = _.groupBy(tokens, 'type');
this.tokensGroupedByType = _.groupBy(tokens, "type");
this.styles = {};
}
exec() {
const tokenTypes = Object.keys(this.tokensGroupedByType) as Array<TokensType>;
const tokenTypes = Object.keys(
this.tokensGroupedByType
) as Array<TokensType>;
this.styles = tokenTypes.reduce(
(acc, tokenType) => ({ ...acc, ...this.setGlobal(tokenType) }),
{},
{}
);
return this.finalize(JSON.stringify(this.styles));
}
Expand All @@ -73,45 +79,44 @@ class ToTailwind {
const TokenHandler = getClassByType(tokenType);
if (!TokenHandler) return {};

const tokenByType = this.tokensGroupedByType[tokenType].reduce((acc, token) => {
const instance = new TokenHandler(token);
const tailwindTokens = instance.generate(this.options, this.tokens);
return deepmerge(acc, tailwindTokens);
}, {});
const tokenByType = this.tokensGroupedByType[tokenType].reduce(
(acc, token) => {
const instance = new TokenHandler(token);
const tailwindTokens = instance.generate(this.options, this.tokens);
return deepmerge(acc, tailwindTokens);
},
{}
);

return TokenHandler.afterGenerate ? TokenHandler.afterGenerate(tokenByType) : tokenByType;
return TokenHandler.afterGenerate
? TokenHandler.afterGenerate(tokenByType)
: tokenByType;
}

finalize(result: string) {
return prettier.format(
(() => {
if (this.module === 'es6' && this.exportDefault)
return `const ${this.objectName} = ${result} ${`;${os.EOL + os.EOL}export default ${
this.objectName
};`}`;
else if (this.module === 'es6' && !this.exportDefault)
return `export const ${this.objectName} = ${result};`;
else if (this.module === 'commonjs' && this.exportDefault)
return `const ${this.objectName} = ${result}; ${`${os.EOL + os.EOL}module.exports = ${
this.objectName
};`}`;
else
return `const ${this.objectName} = ${result}; ${`${os.EOL + os.EOL}module.exports = {${
this.objectName
}};`}`;
})(),
{
...this.options?.formatConfig,
parser: 'babel',
},
);
return (() => {
if (this.module === "es6" && this.exportDefault)
return `const ${this.objectName} = ${result} ${`;${
os.EOL + os.EOL
}export default ${this.objectName};`}`;
else if (this.module === "es6" && !this.exportDefault)
return `export const ${this.objectName} = ${result};`;
else if (this.module === "commonjs" && this.exportDefault)
return `const ${this.objectName} = ${result}; ${`${
os.EOL + os.EOL
}module.exports = ${this.objectName};`}`;
else
return `const ${this.objectName} = ${result}; ${`${
os.EOL + os.EOL
}module.exports = {${this.objectName}};`}`;
})();
}
}

export default async function (
tokens: InputDataType,
options: OptionsType,
{ _ }: Pick<LibsType, '_'>,
{ _ }: Pick<LibsType, "_">
): Promise<OutputDataType> {
try {
const parserInstance = new ToTailwind(tokens, options);
Expand Down

0 comments on commit 070133f

Please sign in to comment.