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

Variable sync plugin with extracted sync providers #3127

Draft
wants to merge 12 commits into
base: feat/3000-extract-sync-providers
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type GitSingleFileObject = Record<string, (

export type GitMultiFileObject = AnyTokenSet<false> | ThemeObjectsList | RemoteTokenStorageMetadata;

export abstract class GitTokenStorage extends RemoteTokenStorage<GitStorageSaveOptions, GitStorageSaveOption> {
export abstract class GitFileStorage extends RemoteTokenStorage<GitStorageSaveOptions, GitStorageSaveOption> {
protected secret: string;

protected owner: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import compact from 'just-compact';
import { Octokit } from '@octokit/rest';
import { RemoteTokenstorageErrorMessage, RemoteTokenStorageFile, RemoteTokenStorageMetadata } from './RemoteTokenStorage';
import IsJSONString from '@/utils/isJSONString';
import { AnyTokenSet } from '@/types/tokens';
import { ThemeObjectsList } from '@/types';
import OctokitCommitMultipleFiles from 'octokit-commit-multiple-files';
import octokitCommitMultipleFilesFallback from 'octokit-commit-multiple-files/create-or-update-files';

import type { RemoteTokenstorageErrorMessage, RemoteTokenStorageFile, RemoteTokenStorageMetadata } from '../types';
import { isJSONString, joinPath } from '../utils';
import {
GitMultiFileObject, GitSingleFileObject, GitTokenStorage,
} from './GitTokenStorage';
import { SystemFilenames } from '@/constants/SystemFilenames';
import { ErrorMessages } from '@/constants/ErrorMessages';
import { joinPath } from '@/utils/string';
AnyTokenSet, ThemeObjectsList, GitMultiFileObject, GitSingleFileObject,
} from '../types';
import { SystemFilenames, ErrorMessages } from '../constants';
import { GitTokenStorage } from './GitTokenStorage';

type ExtendedOctokitClient = Omit<Octokit, 'repos'> & {
repos: Octokit['repos'] & {
Expand Down Expand Up @@ -43,7 +43,9 @@ const octokitClientDefaultHeaders = {

};

export class GithubTokenStorage extends GitTokenStorage {
const commitMultipleFiles = octokitClient => async (params) => octokitCommitMultipleFilesFallback(octokitClient, params);

export class GithubFileStorage extends GitFileStorage {
private octokitClient: ExtendedOctokitClient;

constructor(
Expand All @@ -58,7 +60,7 @@ export class GithubTokenStorage extends GitTokenStorage {
};

// eslint-disable-next-line
const ExtendedOctokitConstructor = Octokit.plugin(require('octokit-commit-multiple-files'));
const ExtendedOctokitConstructor = Octokit.plugin(OctokitCommitMultipleFiles);
this.octokitClient = new ExtendedOctokitConstructor({
auth: this.secret,
baseUrl: this.baseUrl || undefined,
Expand Down Expand Up @@ -236,7 +238,7 @@ export class GithubTokenStorage extends GitTokenStorage {
}
} else if (response.data) {
const data = response.data as unknown as string;
if (IsJSONString(data)) {
if (isJSONString(data)) {
const parsed = JSON.parse(data) as GitSingleFileObject;
return [
{
Expand Down Expand Up @@ -275,7 +277,9 @@ export class GithubTokenStorage extends GitTokenStorage {
}

public async createOrUpdate(changeset: Record<string, string>, message: string, branch: string, shouldCreateBranch?: boolean, filesToDelete?: string[], ignoreDeletionFailures?: boolean): Promise<boolean> {
const response = await this.octokitClient.repos.createOrUpdateFiles({
const createOrUpdateFiles = this.octokitClient.repos.createOrUpdateFiles || commitMultipleFiles(this.octokitClient);

const response = await createOrUpdateFiles({
branch,
owner: this.owner,
repo: this.repository,
Expand Down
Empty file.
201 changes: 201 additions & 0 deletions packages/sync-providers/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// @ts-ignore This is due to weird @types/node issues based on hoistng
require('dotenv').config();

/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/en/configuration.html
*/

export default {
// All imported modules in your tests should be mocked automatically
// automock: false,

// Stop running tests after `n` failures
// bail: 0,

// The directory where Jest should store its cached dependency information
// cacheDirectory: "/tmp/jest_rs",

// Automatically clear mock calls and instances between every test
clearMocks: true,

// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,

// The directory where Jest should output its coverage files
coverageDirectory: 'coverage',

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],

// Indicates which provider should be used to instrument code for coverage
// coverageProvider: "babel",

// A list of reporter names that Jest uses when writing coverage reports
coverageReporters: [
'json',
'text',
'lcov',
'clover',
],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,

// A path to a custom dependency extractor
// dependencyExtractor: undefined,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,

// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,

// A set of global variables that need to be available in all test environments
// globals: {
// "ts-jest": {
// babelConfig: true,
// },
// },

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",

// An array of directory names to be searched recursively up from the requiring module's location
moduleDirectories: ['node_modules', 'src'],

// An array of file extensions your modules use
// moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/tests/__mocks__/fileMock.js',
'\\.(css)$': '<rootDir>/tests/__mocks__/styleMock.js',
'^@/(.*)$': '<rootDir>/src/$1',
'^@/types/(.*)$': '<rootDir>/src/types/$1',
},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],

// Activates notifications for test results
// notify: false,

// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
// preset: 'ts-jest',

// Run tests from one or more projects
// projects: undefined,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,

// Automatically reset mock state between every test
// resetMocks: false,

// Reset the module registry before running each individual test
// resetModules: false,

// A path to a custom resolver
// resolver: undefined,

// Automatically restore mock state between every test
// restoreMocks: false,

// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,

// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",

// The paths to modules that run some code to configure or set up the testing environment before each test
setupFiles: [
'./tests/__mocks__/textEncoderDecoderMock.js',
'./tests/__mocks__/octokitRestMock.js',
'./tests/__mocks__/figmaMock.js',
'./tests/__mocks__/mixpanelMock.js',
'./tests/__mocks__/supernovaMock.js',
'./tests/__mocks__/domRect.js',
'./tests/__mocks__/i18nMock.js',
],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],

// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],

// The test environment that will be used for testing
testEnvironment: 'jsdom',

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},

// Adds a location field to test results
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
// testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'],
testMatch: ['**/?(*.)+(spec|test).[tj]s?(x)'],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
testPathIgnorePatterns: ['/node_modules/', '<rootDir>/cypress/', '<rootDir>/dist/', '<rootDir>/token-transformer/'],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],

// This option allows the use of a custom results processor
// testResultsProcessor: undefined,

// This option allows use of a custom test runner
// testRunner: "jasmine2",

// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
// testURL: "http://localhost",

// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",

// A map from regular expressions to paths to transformers
// transform: {
// "node_modules/(react-dnd|dnd-core|@react-dnd)/.+\\.(j|t)sx?$": "ts-jest",
// "^.+\\.js$": "babel-jest",
// },
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
transformIgnorePatterns: ['/node_modules/(?!(@figma-plugin|react-dnd|@react-dnd|react-colorful|dnd-core|react-dnd-html5-backend|culori|dot-prop)/)', '\\.pnp\\.[^\\/]+$'],

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: undefined,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],

// Whether to use watchman for file crawling
// watchman: true,
};
37 changes: 33 additions & 4 deletions packages/sync-providers/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
{
"name": "sync-providers",
"version": "1.0.0",
"name": "@tokens-studio/sync-providers",
"private": true,
"version": "0.0.1",
"description": "A collection of helper functions for syncing data between different providers",
"main": "index.js",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": "./dist/index.cjs"
},
"./types": {
"import": {
"types": "./dist/types/index.d.ts",
"default": "./dist/types/index.js"
},
"require": "./dist/types/index.cjs"
},
"./schemas": {
"import": {
"types": "./dist/storage/schemas/index.d.ts",
"default": "./dist/storage/schemas/index.js"
},
"require": "./dist/storage/schemas/index.cjs"
}
},
"scripts": {
"build": "tsup",
"lint": "eslint . --quiet --fix",
"lint:nofix": "eslint ."
},
Expand All @@ -24,6 +51,7 @@
},
"devDependencies": {
"@figma/plugin-typings": "^1.99.0",
"@types/jest": "^29.5.12",
"@types/node": "^22.5.2",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
Expand All @@ -35,6 +63,7 @@
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-react": "^7.27.1",
"eslint-plugin-react-hooks": "^4.4.0",
"eslint-plugin-validate-jsx-nesting": "^0.1.1"
"eslint-plugin-validate-jsx-nesting": "^0.1.1",
"tsup": "^8.2.4"
}
}
4 changes: 4 additions & 0 deletions packages/sync-providers/src/constants/BoxShadowTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum BoxShadowTypes {
DROP_SHADOW = 'dropShadow',
INNER_SHADOW = 'innerShadow',
}
5 changes: 5 additions & 0 deletions packages/sync-providers/src/constants/EditTokenFormStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum EditTokenFormStatus {
CREATE = 'create',
EDIT = 'edit',
DUPLICATE = 'duplicate',
}
37 changes: 37 additions & 0 deletions packages/sync-providers/src/hooks/useSyncProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useEffect, useState } from 'react';

import { SyncProvider, SyncState } from '@/providers/provider';

// [WIP] - Unused for now

export function useSyncProvider(syncProvider: SyncProvider) {
const [syncState, setSyncState] = useState(syncProvider.getState());
const [data, setData] = useState<any>(null);
const [error, setError] = useState<any>(null);

useEffect(() => {
const handleStateChange = (state: SyncState) => {
setSyncState(state);
};

const handleError = (err: any) => {
setError(err);
};

const handlePullSuccess = (pulledData: any) => {
setData(pulledData);
};

syncProvider.on('stateChange', handleStateChange);
syncProvider.on('error', handleError);
syncProvider.on('pullSuccess', handlePullSuccess);

return () => {
syncProvider.on('stateChange', () => {});
syncProvider.on('error', () => {});
syncProvider.on('pullSuccess', () => {});
};
}, [syncProvider]);

return { syncState, data, error };
}
7 changes: 7 additions & 0 deletions packages/sync-providers/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { GithubTokenStorage } from './storage/GithubTokenStorage';

export { GithubTokenStorage };

import { SyncProvider } from './syncProvider';

export { SyncProvider };
Loading
Loading