Skip to content

Commit

Permalink
chore: add changesets
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Nov 23, 2023
1 parent fd8c3b5 commit a4542f4
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-lamps-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': minor
---

options.log to be respected in all error logging, including platform specific logs.
5 changes: 5 additions & 0 deletions .changeset/light-games-occur.md
Original file line number Diff line number Diff line change
@@ -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.
67 changes: 67 additions & 0 deletions .changeset/unlucky-cameras-draw.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions __tests__/__helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
13 changes: 3 additions & 10 deletions __tests__/__setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand Down
6 changes: 6 additions & 0 deletions __tests__/common/formatHelpers/fileHeader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
* 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`;
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({});
Expand Down
4 changes: 2 additions & 2 deletions lib/StyleDictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}

Expand Down Expand Up @@ -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");
}

Expand Down
14 changes: 2 additions & 12 deletions lib/transform/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 2 additions & 9 deletions mocha-hooks.mjs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions web-test-runner.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export default {
report: true,
reportDir: 'coverage',
threshold: {
statements: 98.9,
branches: 99.89,
functions: 96.53,
lines: 98.9,
statements: 98,
branches: 99,
functions: 96,
lines: 98,
},
},
browsers: [
Expand Down

0 comments on commit a4542f4

Please sign in to comment.