Skip to content

Commit 02cf5d5

Browse files
authored
fix: replace "\n" to support Windows for tokens output (#8352)
**Related Issue:** #8350 ## Summary Fixes an issue that was causing problems in Windows by handing off control of formatting to the experts and by experts I mean Prettier.
1 parent 743f294 commit 02cf5d5

File tree

10 files changed

+577
-522
lines changed

10 files changed

+577
-522
lines changed

package-lock.json

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@types/jest-axe": "3.5.9",
5050
"@types/lodash-es": "4.17.12",
5151
"@types/node": "^18.0.0",
52+
"@types/prettier": "2.7.2",
5253
"@types/react": "^16.7.6",
5354
"@types/react-dom": "^16.0.9",
5455
"@types/semver": "7.5.6",

packages/calcite-components/src/components/modal/modal.scss

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import "~@esri/calcite-design-tokens/dist/scss/core";
2+
13
/**
24
* CSS Custom Properties
35
*
@@ -21,7 +23,7 @@
2123
transition: visibility 0ms linear var(--calcite-internal-animation-timing-slow),
2224
opacity var(--calcite-internal-animation-timing-slow) $easing-function;
2325
// the modal should always use a dark scrim, regardless of light / dark mode - matches value in global.scss
24-
--calcite-modal-scrim-background-internal: #{rgba($calcite-color-neutral-blk-240, 0.85)};
26+
--calcite-modal-scrim-background-internal: #{rgba($calcite-color-neutral-blk-240, $calcite-opacity-85)};
2527
}
2628

2729
.content-top[hidden],

packages/calcite-design-tokens/support/tests/__snapshots__/index.spec.ts.snap

+505-496
Large diffs are not rendered by default.

packages/calcite-design-tokens/support/token-transformer/styleDictionary/formatter/css.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import sd, { Core as StyleDictionary } from "style-dictionary";
2+
import * as prettier from "prettier";
23

34
import { formatTokens } from "./utils/formatTokens.js";
45
import { formatExtraOutput } from "./utils/formatExtraOutput.js";
56
import { CalledFormatterFunction, FormatterConfig } from "../../../types/styleDictionary/formatterArguments.js";
7+
import { EOL } from "os";
68

79
export const formatCssPlatform: CalledFormatterFunction = (args) => {
810
const { file, dictionary } = args;
@@ -12,7 +14,7 @@ export const formatCssPlatform: CalledFormatterFunction = (args) => {
1214
if (Object.keys(extraOutput).length > 0) {
1315
formatExtraOutput(extraOutput, { ...args.options, header, buildPath: args.platform.buildPath });
1416
}
15-
return header + `:root {\n\t${tokens.join("\n\t")}\n}\n`;
17+
return prettier.format(header + `:root {${tokens.join(EOL)}}`, { parser: "css" });
1618
};
1719

1820
export const registerFormatterCss = (sd: StyleDictionary): void => {

packages/calcite-design-tokens/support/token-transformer/styleDictionary/formatter/docs.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Core as StyleDictionary } from "style-dictionary";
2+
import * as prettier from "prettier";
3+
24
import { CalledFormatterFunction, FormatterConfig } from "../../../types/styleDictionary/formatterArguments";
35

46
export const formatDocsPlatform: CalledFormatterFunction = (args) => {
@@ -15,7 +17,7 @@ export const formatDocsPlatform: CalledFormatterFunction = (args) => {
1517
output.tokens[token.type].push(token);
1618
}
1719

18-
return JSON.stringify(output, null, 2);
20+
return prettier.format(JSON.stringify(output, null, 2), { parser: "json" });
1921
};
2022

2123
export const registerFormatterDocs = (sd: StyleDictionary): void => {

packages/calcite-design-tokens/support/token-transformer/styleDictionary/formatter/javascript.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import styleDictionary, { Core as StyleDictionary } from "style-dictionary";
2+
import * as prettier from "prettier";
3+
24
import { CalledFormatterFunction, FormatterConfig } from "../../../types/styleDictionary/formatterArguments";
35

46
export const formatJsPlatform: CalledFormatterFunction = (args) => {
5-
return (
7+
return prettier.format(
68
styleDictionary.formatHelpers.fileHeader({ file: args.file }) +
7-
"export default " +
8-
JSON.stringify(args.dictionary.properties, null, 2) +
9-
";\n"
9+
"export default " +
10+
JSON.stringify(args.dictionary.properties, null, 2) +
11+
";",
12+
{ parser: "babel" }
1013
);
1114
};
1215

packages/calcite-design-tokens/support/token-transformer/styleDictionary/formatter/scss.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import sd, { Core as StyleDictionary } from "style-dictionary";
22

33
import { formatTokens } from "./utils/formatTokens.js";
44
import { formatExtraOutput } from "./utils/formatExtraOutput.js";
5+
import * as prettier from "prettier";
6+
57
import { CalledFormatterFunction, FormatterConfig } from "../../../types/styleDictionary/formatterArguments.js";
8+
import { EOL } from "os";
69

710
export const formatScssPlatform: CalledFormatterFunction = (args) => {
811
const { file, dictionary } = args;
@@ -12,7 +15,7 @@ export const formatScssPlatform: CalledFormatterFunction = (args) => {
1215
if (Object.keys(extraOutput).length > 0) {
1316
formatExtraOutput(extraOutput, { ...args.options, header, buildPath: args.platform.buildPath });
1417
}
15-
return header + tokens.join("\n");
18+
return prettier.format(header + tokens.join(EOL), { parser: "scss" });
1619
};
1720

1821
export const registerFormatterScss = (sd: StyleDictionary): void => {

packages/calcite-design-tokens/support/token-transformer/styleDictionary/formatter/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ export const FormattingRules: Partial<Record<PlatformUnion | "default", Formatte
3535
commentStyle: "long",
3636
indentation: " ",
3737
separator: ":",
38-
join: "\n\t",
38+
join: "",
3939
suffix: ";",
4040
},
4141
sass: {
4242
prefix: "$",
4343
commentStyle: "short",
4444
indentation: "",
4545
separator: ":",
46-
join: "\n",
46+
join: "",
4747
suffix: ";",
4848
},
4949
scss: {
5050
prefix: "$",
5151
commentStyle: "short",
5252
indentation: "",
5353
separator: ":",
54-
join: "\n",
54+
join: "",
5555
suffix: ";",
5656
},
5757
};

packages/calcite-design-tokens/support/token-transformer/styleDictionary/formatter/utils/formatExtraOutput.ts

+41-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { writeFileSync } from "fs";
22
import { resolve } from "path";
3+
import * as prettier from "prettier";
4+
35
import { Platform } from "../../../../types/platform.js";
46
import { Options } from "../../../../types/styleDictionary/options.js";
57
import { DeepKeyTokenMap } from "../../../../types/tokenStudio/designTokenTypes.js";
@@ -14,6 +16,8 @@ export function formatExtraOutput(
1416
const ensureIfArray = (x: any) => (Array.isArray(x) ? (x as any[]) : x);
1517

1618
if (index) {
19+
let parser;
20+
// Set output
1721
switch (args.platform) {
1822
case "css":
1923
case "scss":
@@ -29,14 +33,14 @@ export function formatExtraOutput(
2933
const classes = index.class
3034
? index.class.map((cls) => {
3135
const c = ensureIfArray(outputObject[`${cls[1]}.${args.platform}`]);
32-
return cls && Array.isArray(c) ? `.${cls[0]} {\n\t${c.join("\n\t")}\n}` : "";
36+
return cls && Array.isArray(c) ? `.${cls[0]} {${c.join("")}}` : "";
3337
})
3438
: [];
3539
const mixins = index.mixin
3640
? index.mixin.map(([mixinName, output]) => {
3741
const m = ensureIfArray(outputObject[`${output}.${args.platform}`]);
3842
return Array.isArray(m)
39-
? `@mixin ${mixinName} {\n\t${m.map((o) => `${o}`.replaceAll("$", "--")).join("\n\t")}\n}`
43+
? `@mixin ${mixinName} {${m.map((o) => `${o}`.replaceAll("$", "--")).join("")}}`
4044
: "";
4145
})
4246
: [];
@@ -45,9 +49,8 @@ export function formatExtraOutput(
4549
const m = ensureIfArray(outputObject[`${output}.${args.platform}`]);
4650
const cssProps = m.map((o) => `${o}`.replaceAll("$", "--"));
4751
return Array.isArray(m)
48-
? `${
49-
output === "light" ? `:root {\n\t${cssProps.join("\n\t")}\n}\n` : ""
50-
}@media (${mediaSchemed}) {\n\t.calcite-mode-auto {\n\t\t${cssProps.join("\n\t\t")}\n\t}\n}`
52+
? `${output === "light" ? `:root {${cssProps.join("")}}` : ""
53+
}@media (${mediaSchemed}) {.calcite-mode-auto {${cssProps.join("")}}}`
5154
: "";
5255
})
5356
: [];
@@ -72,32 +75,55 @@ export function formatExtraOutput(
7275
break;
7376
}
7477

75-
writeFileSync(resolve(args.buildPath, index.name), `${args.header}${outputFiles[index.name].join("\n\n")}\n`);
78+
// Set Parser
79+
switch (args.platform) {
80+
case "css":
81+
case "scss":
82+
parser = args.platform;
83+
break;
84+
case "sass":
85+
parser = "scss";
86+
case "es6":
87+
case "js":
88+
parser = "babel";
89+
break;
90+
case "docs":
91+
parser = "json";
92+
default:
93+
break;
94+
}
95+
96+
writeFileSync(
97+
resolve(args.buildPath, index.name),
98+
prettier.format(`${args.header}${outputFiles[index.name].join(" ")}`, { parser })
99+
);
76100
}
77101

78102
Object.entries(outputObject).forEach(([fileName, outputList]) => {
79103
const absoluteFilePath = resolve(args.buildPath, `${fileName}`);
80104
switch (args.platform) {
81105
case Platform.CSS:
82106
if (typeof outputList[0] === "string" && outputList[0].slice(0, 2) === "--") {
83-
writeFileSync(absoluteFilePath, `${args.header}:root{\n\t${outputList.join("\n\t")}\n}\n`);
107+
writeFileSync(
108+
absoluteFilePath,
109+
prettier.format(`${args.header}:root{${outputList.join("")}}`, { parser: "css" })
110+
);
84111
} else {
85-
writeFileSync(absoluteFilePath, `${args.header}${outputList.join("\n\n")}\n`);
112+
writeFileSync(absoluteFilePath, prettier.format(`${args.header}${outputList.join("")}`, { parser: "css" }));
86113
}
87114
break;
88115
case Platform.SCSS:
89116
case Platform.SASS:
90-
if (typeof outputList[0] === "string" && outputList[0][0] === "$") {
91-
writeFileSync(absoluteFilePath, `${args.header}${outputList.join("\n")}\n`);
92-
} else {
93-
writeFileSync(absoluteFilePath, `${args.header}${outputList.join("\n\n")}\n`);
94-
}
117+
writeFileSync(absoluteFilePath, prettier.format(`${args.header}${outputList.join("")}`, { parser: "scss" }));
95118
break;
96119
case Platform.JS:
97-
writeFileSync(absoluteFilePath, args.header + "export default " + outputList[0] + "\n");
120+
writeFileSync(
121+
absoluteFilePath,
122+
prettier.format(args.header + "export default " + outputList[0] + "", { parser: "babel" })
123+
);
98124
break;
99125
case Platform.DOCS:
100-
writeFileSync(absoluteFilePath, outputList[0]);
126+
writeFileSync(absoluteFilePath, prettier.format(outputList[0].join(""), { parser: "json" }));
101127
break;
102128
default:
103129
break;

0 commit comments

Comments
 (0)