diff --git a/.gitignore b/.gitignore index e2ce3c0d18..b4b5f0d9e4 100644 --- a/.gitignore +++ b/.gitignore @@ -67,6 +67,8 @@ typings/ # Jetbrains IDEA .idea +# Imports of the themes generated by the script generateThemesImports.js +themeImports.js dist build diff --git a/package.json b/package.json index d15606cec2..961b390cb9 100644 --- a/package.json +++ b/package.json @@ -2,16 +2,16 @@ "scripts": { "commit": "git-cz", "clean": "lerna clean -y && lerna run prebuild --scope=@gympass/yoga-doc", - "test": "NODE_ENV=test jest --config=./jest/config/config.js", + "test": "yarn generateThemeImports && NODE_ENV=test jest --config=./jest/config/config.js", "pretest": "lerna run prebuild --ignore=@gympass/yoga-doc", - "test:native": "NODE_ENV=test jest --config=./jest/config/native.js", - "test:helpers": "NODE_ENV=test jest --config=./jest/config/helpers.js", - "test:system": "NODE_ENV=test jest --config=./jest/config/system.js", - "test:web": "NODE_ENV=test jest --config=./jest/config/web.js", + "test:native": "yarn generateThemeImports && NODE_ENV=test jest --config=./jest/config/native.js", + "test:helpers": "yarn generateThemeImports && NODE_ENV=test jest --config=./jest/config/helpers.js", + "test:system": "yarn generateThemeImports && NODE_ENV=test jest --config=./jest/config/system.js", + "test:web": "yarn generateThemeImports && NODE_ENV=test jest --config=./jest/config/web.js", "prettier": "prettier", - "dev:web": "lerna run dev --scope=@gympass/yoga-doc --stream", - "dev:native:android": "lerna run android --scope=@gympass/yoga-labnative --stream", - "dev:native:ios": "lerna run ios --scope=@gympass/yoga-labnative --stream", + "dev:web": "yarn generateThemeImports && lerna run dev --scope=@gympass/yoga-doc --stream", + "dev:native:android": "yarn generateThemeImports && lerna run android --scope=@gympass/yoga-labnative --stream", + "dev:native:ios": "yarn generateThemeImports && lerna run ios --scope=@gympass/yoga-labnative --stream", "lint": "eslint --ext js,jsx,android.jsx,ios.jsx", "lint:yoga": "NODE_ENV=test yarn lint packages/yoga", "lint:doc": "NODE_ENV=test yarn lint packages/doc", @@ -29,7 +29,8 @@ "build:packages": "lerna run build --ignore=@gympass/yoga-doc --stream", "build:system": "lerna run build --scope=@gympass/yoga-system --stream", "build": "lerna run build --stream --parallel", - "new": "node ./scripts/cli/index.js" + "new": "node ./scripts/cli/index.js && yarn generateThemeImports", + "generateThemeImports": "node ./scripts/generateThemesImports.js > ./packages/yoga/src/Theme/theme/themeImports.js" }, "private": true, "devDependencies": { @@ -56,6 +57,7 @@ "eslint-plugin-import": "^2.26.0", "eslint-plugin-jsx-a11y": "^6.5.1", "eslint-plugin-prettier": "4.0.0", + "fs": "^0.0.1-security", "fs-extra": "^8.1.0", "git-last-commit": "^1.0.0", "husky": "3.0.5", @@ -65,6 +67,7 @@ "lerna": "^5.0.0", "lint-staged": "9.2.5", "metro-react-native-babel-preset": "^0.67.0", + "path": "^0.12.7", "prettier": "2.6.2", "react-dom": "^18.0.0", "tsup": "^7.1.0", diff --git a/packages/yoga/package.json b/packages/yoga/package.json index 2c948a1ab5..97944ba280 100644 --- a/packages/yoga/package.json +++ b/packages/yoga/package.json @@ -11,9 +11,10 @@ "build:web": "tsup src src/**/web !src/Fonts !**/*.test.jsx.snap !**/*.test.tsx.snap --outDir dist/cjs --format=cjs", "build:esm": "tsup src src/**/web !src/Fonts !**/*.test.jsx.snap !**/*.test.tsx.snap --format=esm --legacy-output", "build:native": "tsup src src/**/native !src/Fonts !**/*.test.jsx.snap !**/*.test.tsx.snap --outDir dist/cjs --format=cjs", - "prebuild": "rm -rf ./dist", + "prebuild": "rm -rf ./dist && yarn generateThemeImports", "postbuild": "cp -r ./src/Fonts ./dist/fonts", - "prepublishOnly": "node ../../scripts/prepublish.js --rn" + "prepublishOnly": "node ../../scripts/prepublish.js --rn", + "generateThemeImports": "node ../../scripts/generateThemesImports.js > ./src/Theme/theme/themeImports.js" }, "keywords": [ "Gympass", diff --git a/packages/yoga/src/Theme/theme/componentThemes.js b/packages/yoga/src/Theme/theme/componentThemes.js deleted file mode 100644 index e8755fc55e..0000000000 --- a/packages/yoga/src/Theme/theme/componentThemes.js +++ /dev/null @@ -1,83 +0,0 @@ -import Accordion from '../../Accordion/Accordion.theme.js'; -import ActionRequirement from '../../ActionRequirement/ActionRequirement.theme.js'; -import AutoComplete from '../../AutoComplete/AutoComplete.theme.js'; -import Avatar from '../../Avatar/Avatar.theme.js'; -import Banner from '../../Banner/Banner.theme.js'; -import BottomSheet from '../../BottomSheet/BottomSheet.theme.js'; -import Button from '../../Button/Button.theme.js'; -import Card from '../../Card/Card.theme.js'; -import CardNative from '../../Card/Card.native.theme.js'; -import CardWeb from '../../Card/Card.web.theme.js'; -import Checkbox from '../../Checkbox/Checkbox.theme.js'; -import CheckboxSwitch from '../../Checkbox/CheckboxSwitch.theme.js'; -import Datepicker from '../../Datepicker/Datepicker.theme.js'; -import Dialog from '../../Dialog/Dialog.theme.js'; -import Divider from '../../Divider/Divider.theme.js'; -import Drawer from '../../Drawer/Drawer.theme.js'; -import Dropdown from '../../Dropdown/Dropdown.theme.js'; -import Feedback from '../../Feedback/Feedback.theme.js'; -import Grid from '../../Grid/Grid.theme.js'; -import Header from '../../Header/Header.theme.js'; -import Heading from '../../Heading/Heading.theme.js'; -import Icon from '../../Icon/Icon.theme.js'; -import Input from '../../Input/Input.theme.js'; -import List from '../../List/List.theme.js'; -import Menu from '../../Menu/Menu.theme.js'; -import Popover from '../../Popover/Popover.theme.js'; -import Progress from '../../Progress/Progress.theme.js'; -import RadioGroup from '../../RadioGroup/RadioGroup.theme.js'; -import Rating from '../../Rating/Rating.theme.js'; -import Result from '../../Result/Result.theme.js'; -import Skeleton from '../../Skeleton/Skeleton.theme.js'; -import Slider from '../../Slider/Slider.theme.js'; -import Snackbar from '../../Snackbar/Snackbar.theme.js'; -import Spinner from '../../Spinner/Spinner.theme.js'; -import Stepper from '../../Stepper/Stepper.theme.js'; -import Tag from '../../Tag/Tag.theme.js'; -import Text from '../../Text/Text.theme.js'; -import TextArea from '../../TextArea/TextArea.theme.js'; -import NavigationMenu from '../../NavigationMenu/NavigationMenu.theme'; - -const componentThemes = { - Accordion, - ActionRequirement, - AutoComplete, - Avatar, - Banner, - BottomSheet, - Button, - Card, - CardNative, - CardWeb, - Checkbox, - CheckboxSwitch, - Datepicker, - Dialog, - Divider, - Drawer, - Dropdown, - Feedback, - Grid, - Header, - Heading, - Icon, - Input, - List, - Menu, - Popover, - Progress, - RadioGroup, - Rating, - Result, - Skeleton, - Slider, - Snackbar, - Spinner, - Stepper, - Tag, - Text, - TextArea, - NavigationMenu, -}; - -export default componentThemes; diff --git a/packages/yoga/src/Theme/theme/theme.js b/packages/yoga/src/Theme/theme/theme.js index 6303337edf..e4b7d208c2 100644 --- a/packages/yoga/src/Theme/theme/theme.js +++ b/packages/yoga/src/Theme/theme/theme.js @@ -1,6 +1,6 @@ /* eslint-disable prefer-destructuring */ import { merge } from '@gympass/yoga-common'; -import componentThemes from './componentThemes'; +import componentThemes from './themeImports'; const getComponentThemes = tokens => { const { colors, baseFont, baseFontSize } = tokens; diff --git a/scripts/generateThemesImports.js b/scripts/generateThemesImports.js new file mode 100644 index 0000000000..67be21dc75 --- /dev/null +++ b/scripts/generateThemesImports.js @@ -0,0 +1,57 @@ +/* eslint-disable no-console */ +const fs = require('fs'); +const path = require('path'); + +const themeFiles = []; +const yogaPath = path.join(__dirname, '../packages/yoga/src'); +const removeExt = /\.theme\.(js|ts)$/; + +function findThemeFiles(directory) { + const files = fs.readdirSync(directory); + + files.forEach(file => { + const filePath = path.join(directory, file); + const stats = fs.statSync(filePath); + + if (stats.isDirectory()) { + findThemeFiles(filePath); + } + + if (file.endsWith('.theme.js') || file.endsWith('.theme.ts')) { + themeFiles.push(filePath); + } + }); +} + +function toCamelCase(name) { + return name.replace(/[-_.](.)/g, (_, afterDot) => afterDot.toUpperCase()); +} + +function adjustPath(name) { + return name.replace('packages/yoga/src', '..'); +} + +findThemeFiles(yogaPath); + +const importStatements = themeFiles.map(filePath => { + const componentName = toCamelCase( + path.basename(filePath).replace(removeExt, ''), + ); + const componentPath = adjustPath(path.relative(__dirname, filePath)); + + return `import ${componentName} from '${componentPath}';`; +}); + +const exportStatements = themeFiles.map(filePath => { + const componentName = toCamelCase( + path.basename(filePath).replace(removeExt, ''), + ); + + return ` ${componentName},`; +}); + +console.log(importStatements.join('\n')); +console.log('\nconst componentThemes = {'); +console.log(exportStatements.join('\n')); +console.log('};'); +console.log('\nexport default componentThemes;'); diff --git a/yarn.lock b/yarn.lock index f996385652..c4c5967ceb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12745,6 +12745,11 @@ fs.realpath@^1.0.0: resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== +fs@^0.0.1-security: + version "0.0.1-security" + resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4" + integrity sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w== + fsevents@^1.2.7: version "1.2.13" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-1.2.13.tgz#f325cb0455592428bcf11b383370ef70e3bfcc38" @@ -20439,6 +20444,14 @@ path-type@^4.0.0: resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== +path@^0.12.7: + version "0.12.7" + resolved "https://registry.yarnpkg.com/path/-/path-0.12.7.tgz#d4dc2a506c4ce2197eb481ebfcd5b36c0140b10f" + integrity sha512-aXXC6s+1w7otVF9UletFkFcDsJeO7lSZBPUQhtb5O0xJe8LtYhj/GxldoL09bBj9+ZmE2hNoHqQSFMN5fikh4Q== + dependencies: + process "^0.11.1" + util "^0.10.3" + pbkdf2@^3.0.3: version "3.1.2" resolved "https://registry.yarnpkg.com/pbkdf2/-/pbkdf2-3.1.2.tgz#dd822aa0887580e52f1a039dc3eda108efae3075" @@ -21149,7 +21162,7 @@ process-nextick-args@~2.0.0: resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2" integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag== -process@^0.11.10: +process@^0.11.1, process@^0.11.10: version "0.11.10" resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182" integrity sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A== @@ -25487,6 +25500,13 @@ util@0.10.3: dependencies: inherits "2.0.1" +util@^0.10.3: + version "0.10.4" + resolved "https://registry.yarnpkg.com/util/-/util-0.10.4.tgz#3aa0125bfe668a4672de58857d3ace27ecb76901" + integrity sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A== + dependencies: + inherits "2.0.3" + util@^0.11.0: version "0.11.1" resolved "https://registry.yarnpkg.com/util/-/util-0.11.1.tgz#3236733720ec64bb27f6e26f421aaa2e1b588d61"