From 618dd1e3be1cf933f8c9103fba2dd59e326d4bef Mon Sep 17 00:00:00 2001 From: jorenbroekema Date: Thu, 23 Nov 2023 10:03:20 +0100 Subject: [PATCH] chore: add changesets --- .changeset/gorgeous-lamps-chew.md | 5 ++ .changeset/light-games-occur.md | 5 ++ .changeset/unlucky-cameras-draw.md | 67 +++++++++++++++++++ __tests__/__helpers.js | 15 +++++ __tests__/__setup.js | 13 +--- .../common/formatHelpers/fileHeader.test.js | 6 ++ lib/StyleDictionary.js | 4 +- lib/transform/config.js | 14 +--- mocha-hooks.mjs | 11 +-- 9 files changed, 107 insertions(+), 33 deletions(-) create mode 100644 .changeset/gorgeous-lamps-chew.md create mode 100644 .changeset/light-games-occur.md create mode 100644 .changeset/unlucky-cameras-draw.md diff --git a/.changeset/gorgeous-lamps-chew.md b/.changeset/gorgeous-lamps-chew.md new file mode 100644 index 000000000..49efca435 --- /dev/null +++ b/.changeset/gorgeous-lamps-chew.md @@ -0,0 +1,5 @@ +--- +'style-dictionary': minor +--- + +options.log to be respected in all error logging, including platform specific logs. diff --git a/.changeset/light-games-occur.md b/.changeset/light-games-occur.md new file mode 100644 index 000000000..3b2f03743 --- /dev/null +++ b/.changeset/light-games-occur.md @@ -0,0 +1,5 @@ +--- +'style-dictionary': major +--- + +BREAKING: StyleDictionaryInstance.properties & .allProperties have been removed. They were deprecated in v3 in favor of .tokens and .allTokens. diff --git a/.changeset/unlucky-cameras-draw.md b/.changeset/unlucky-cameras-draw.md new file mode 100644 index 000000000..a26cf1bb2 --- /dev/null +++ b/.changeset/unlucky-cameras-draw.md @@ -0,0 +1,67 @@ +--- +'style-dictionary': major +--- + +BREAKING: StyleDictionary to be initialized with a new API and have async methods. Use: + +```js +import StyleDictionary from 'style-dictionary'; + +/** + * old: + * const sd = StyleDictionary.extend({ source: ['tokens.json'], platforms: {} }); + * sd.buildAllPlatforms(); + */ +const sd = new StyleDictionary({ source: ['tokens.json'], platforms: {} }); +await sd.buildAllPlatforms(); +``` + +You can still extend a dictionary instance with an extended config, but `.extend()` is only used for this, it is no longer used to initialize the first instance: + +```js +import StyleDictionary from 'style-dictionary'; + +const sd = new StyleDictionary({ source: ['tokens.json'], platforms: {} }); +const extended = await sd.extend({ + fileHeader: { + myFileHeader: (defaultMessage) => { + return [...defaultMessage, 'hello, world!']; + } + } +}); +``` + +To ensure your initialized StyleDictionary instance is fully ready and has imported all your tokens, you can await `hasInitialized`: + +```js +import StyleDictionary from 'style-dictionary'; + +const sd = new StyleDictionary({ source: ['tokens.json'], platforms: {} }); +await sd.hasInitialized; +console.log(sd.allTokens); +``` + +For error handling and testing purposes, you can also manually initialize the style-dictionary config: + +```js +import StyleDictionary from 'style-dictionary'; + +const sd = new StyleDictionary({ source: ['tokens.js'], platforms: {} }, { init: false }); +try { + await sd.init(); +} catch(e) { + // handle error, e.g. when tokens.js file has syntax errors and cannot be imported +} +console.log(sd.allTokens); +``` + +The main reason for an initialize step after class instantiation is that async constructors are not a thing in JavaScript, and if you return a promise from a constructor to "hack it", TypeScript will eventually trip over it. + +Due to being able to dynamically (asynchronously) import ES Modules rather than synchronously require CommonJS modules, we had to make the APIs asynchronous, so the following methods are now async: + +- extend +- exportPlatform +- buildAllPlatforms & buildPlatform +- cleanAllPlatforms & cleanPlatform + +In a future release, most other methods will be made async or support async as well, such as parsers, transforms, formats etc. diff --git a/__tests__/__helpers.js b/__tests__/__helpers.js index 5b026dd0c..c437c852c 100644 --- a/__tests__/__helpers.js +++ b/__tests__/__helpers.js @@ -50,3 +50,18 @@ export const fileExists = (filePath, _fs = fs) => { export const dirExists = (dirPath, _fs = fs) => { return _fs.existsSync(dirPath); }; + +export function fixDate() { + const constantDate = new Date('2000-01-01'); + // eslint-disable-next-line no-undef + const __global = typeof window === 'object' ? window : globalThis; + + // eslint-disable-next-line no-undef + __global.Date = function () { + return constantDate; + }; + // eslint-disable-next-line no-undef + __global.Date.now = function () { + return constantDate; + }; +} diff --git a/__tests__/__setup.js b/__tests__/__setup.js index d9a4dabd2..c9cf0c48f 100644 --- a/__tests__/__setup.js +++ b/__tests__/__setup.js @@ -3,16 +3,7 @@ import chaiAsPromised from '@esm-bundle/chai-as-promised'; import path from '@bundled-es-modules/path-browserify'; import { fs } from 'style-dictionary/fs'; import { chaiWtrSnapshot } from '../snapshot-plugin/chai-wtr-snapshot.js'; - -const constantDate = new Date('2000-01-01'); -// eslint-disable-next-line no-undef -window.Date = function () { - return constantDate; -}; -// eslint-disable-next-line no-undef -window.Date.now = function () { - return constantDate; -}; +import { fixDate } from './__helpers.js'; /** * We have a bunch of files that we use a mock data for our tests @@ -24,6 +15,8 @@ window.Date.now = function () { * which needs to be initialized client-side */ +fixDate(); + function ensureDirectoryExistence(filePath) { const dirname = path.dirname(filePath); if (fs.existsSync(dirname)) { diff --git a/__tests__/common/formatHelpers/fileHeader.test.js b/__tests__/common/formatHelpers/fileHeader.test.js index f421918cd..3c38098b3 100644 --- a/__tests__/common/formatHelpers/fileHeader.test.js +++ b/__tests__/common/formatHelpers/fileHeader.test.js @@ -11,6 +11,7 @@ * and limitations under the License. */ import { expect } from 'chai'; +import { fixDate } from '../../__helpers.js'; import fileHeader from '../../../lib/common/formatHelpers/fileHeader.js'; const defaultLine1 = `Do not edit directly`; @@ -18,6 +19,11 @@ const defaultLine2 = `Generated on Sat, 01 Jan 2000 00:00:00 GMT`; describe('common', () => { describe('formatHelpers', () => { + beforeEach(() => { + // reset Date again, for some reasons these tests are flaky otherwise in the pipelines + fixDate(); + }); + describe('fileHeader', () => { it(`should default to /**/ comment style`, () => { const comment = fileHeader({}); diff --git a/lib/StyleDictionary.js b/lib/StyleDictionary.js index cd9777af9..e613ad6ca 100644 --- a/lib/StyleDictionary.js +++ b/lib/StyleDictionary.js @@ -363,7 +363,7 @@ export default class StyleDictionary { console.log('\n' + platform); - if (!this.options || !(platform in (this.options.platforms || {}))) { + if (!this.options || !this.options?.platforms[platform]) { throw new Error(`Platform "${platform}" does not exist`); } @@ -402,7 +402,7 @@ export default class StyleDictionary { console.log('\n' + platform); - if (!this.options || !(platform in (this.options.platforms || {}))) { + if (!this.options || !this.options?.platforms[platform]) { throw new Error('Platform ' + platform + " doesn't exist"); } diff --git a/lib/transform/config.js b/lib/transform/config.js index 619afa8a4..8ed4265cd 100644 --- a/lib/transform/config.js +++ b/lib/transform/config.js @@ -30,7 +30,7 @@ const MISSING_TRANSFORM_ERRORS = GroupMessages.GROUP.MissingRegisterTransformErr */ export default function transformConfig(platformConfig, dictionary, platformName) { const to_ret = clone(platformConfig); - to_ret.log = dictionary.log; + to_ret.log = platformConfig.log ?? dictionary.log; // The platform can define either a transformGroup or an array // of transforms. If given a transformGroup that doesn't exist, @@ -131,17 +131,7 @@ None of ${transform_warnings} match the name of a registered transform. } } - if (file.template) { - if (dictionary.format[file.template]) { - GroupMessages.add( - TEMPLATE_DEPRECATION_WARNINGS, - `${file.destination} (template: ${file.template})`, - ); - ext.format = dictionary.format[file.template]; - } else { - throw new Error("Can't find template: " + file.template); - } - } else if (file.format) { + if (file.format) { if (dictionary.format[file.format]) { ext.format = dictionary.format[file.format]; } else { diff --git a/mocha-hooks.mjs b/mocha-hooks.mjs index 1e63082d8..af08f0efb 100644 --- a/mocha-hooks.mjs +++ b/mocha-hooks.mjs @@ -1,16 +1,9 @@ import { use } from 'chai'; import chaiAsPromised from '@esm-bundle/chai-as-promised'; import { chaiWtrSnapshot } from './snapshot-plugin/chai-wtr-snapshot.js'; +import { fixDate } from './__tests__/__helpers.js'; -const constantDate = new Date('2000-01-01'); -// eslint-disable-next-line no-undef -globalThis.Date = function () { - return constantDate; -}; -// eslint-disable-next-line no-undef -globalThis.Date.now = function () { - return constantDate; -}; +fixDate(); export const mochaHooks = { beforeAll(done) {