From 17df7eff7a946d63b52b17497724c6af6465cadb Mon Sep 17 00:00:00 2001 From: danieloprado Date: Mon, 10 Jul 2023 12:10:30 -0300 Subject: [PATCH] feat: initial --- .eslintrc | 3 + .github/CODEOWNERS | 1 + .github/workflows/publish.yml | 77 ++ .gitignore | 10 + .npmignore | 16 + .prettierrc.js | 3 + .vscode/settings.json | 58 ++ README.md | 17 + index.tsx | 5 + package.json | 48 + scripts/should-release.js | 52 + tsconfig.json | 34 + useBoolean/index.md | 20 + useBoolean/index.tsx | 15 + useCallbackGenerator/index.md | 51 + useCallbackGenerator/index.tsx | 46 + yarn.lock | 1723 ++++++++++++++++++++++++++++++++ 17 files changed, 2179 insertions(+) create mode 100644 .eslintrc create mode 100644 .github/CODEOWNERS create mode 100644 .github/workflows/publish.yml create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 .prettierrc.js create mode 100644 .vscode/settings.json create mode 100644 README.md create mode 100644 index.tsx create mode 100644 package.json create mode 100644 scripts/should-release.js create mode 100644 tsconfig.json create mode 100644 useBoolean/index.md create mode 100644 useBoolean/index.tsx create mode 100644 useCallbackGenerator/index.md create mode 100644 useCallbackGenerator/index.tsx create mode 100644 yarn.lock diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..1e118e1 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": ["@eduzz/eslint-config/react"] +} \ No newline at end of file diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..d6b9760 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @danieloprado @ffernandomoraes @JonathasRodrigues \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..bb38f18 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,77 @@ +name: Publish + +on: + push: + branches: + - master + +concurrency: + group: publish-${{ github.ref }} + cancel-in-progress: true + +jobs: + should-release: + name: Should Release + runs-on: ubuntu-latest + steps: + - name: Clone + uses: actions/checkout@v3 + - name: Node + uses: actions/setup-node@v3 + - name: Deps + run: npm i -g semver + - name: Run should-release + run: node ./scripts/should-release.js + - name: Current Version + id: current-version + run: echo 'CURRENT_VERSION='$(node -p -e "require('./package.json').version") >> $GITHUB_OUTPUT + - name: Check Skip Release + id: skip-release + run: echo 'SKIP='$(cat .skip-release) >> $GITHUB_OUTPUT + outputs: + SKIP: ${{ steps.skip-release.outputs.SKIP }} + CURRENT_VERSION: ${{ steps.current-version.outputs.CURRENT_VERSION }} + + publish: + needs: [should-release] + if: needs.should-release.outputs.SKIP != 'true' + name: Publish + runs-on: ubuntu-latest + steps: + - name: Clone + uses: actions/checkout@v3 + - name: Node + uses: actions/setup-node@v3 + - name: Cache Yarn + id: cache-nodemodules + uses: actions/cache@v3 + with: + path: node_modules + key: packages-${{ hashFiles('yarn.lock') }} + restore-keys: | + packages- + - name: Yarn + if: steps.cache-nodemodules.outputs.cache-hit != 'true' + run: yarn install --immutable + - name: Setup NPM Registry + run: echo '//registry.npmjs.org/:_authToken=${{ secrets.NPM_AUTH_TOKEN }}' > ~/.npmrc && npm config get registry && npm whoami + - name: Build + run: npm run build + - name: Publish + run: npm publish + - name: Check Skip Release + id: skip-release + run: echo 'SKIP='$(cat .skip-release) >> $GITHUB_OUTPUT + - name: Create tag + uses: actions/github-script@v3 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + github.git.createRef({ + owner: context.repo.owner, + repo: context.repo.repo, + ref: "refs/tags/v${{ needs.should-release.outputs.CURRENT_VERSION }}", + sha: context.sha + }) + outputs: + SKIP: ${{ steps.skip-release.outputs.SKIP }} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f1ea6f5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +node_modules +*.log +.DS_Store +.skip-release +*.js +*.d.ts +*.map + +!.prettierrc.js +!scripts/*.js \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..556808c --- /dev/null +++ b/.npmignore @@ -0,0 +1,16 @@ +.vscode +node_modules +.eslintignore +.eslintrc +.prettierrc +.prettierrc.js +tsconfig.json +/**/**/*.tsx +/**/**/*.ts +!/**/**/*.d.ts +.git +scripts +*.md +!README.md +.github +.vscode \ No newline at end of file diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..11dee39 --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,3 @@ +module.exports = { + ...require('@eduzz/eslint-config/.prettierrc') +}; \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6ed7570 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,58 @@ +{ + "[css]": { + "editor.formatOnPaste": true, + "editor.formatOnSave": true, + "editor.formatOnType": true + }, + "editor.codeActionsOnSave": { + "source.fixAll.eslint": true + }, + "editor.formatOnPaste": false, + "editor.formatOnSave": false, + "editor.formatOnType": false, + "editor.quickSuggestions": { + "strings": true + }, + "editor.rulers": [ + { + "color": "#fd3e3e78", + "column": 120 + } + ], + "eslint.validate": [ + "javascript", + "javascriptreact", + "typescript", + "typescriptreact" + ], + "files.associations": { + "*.css": "tailwindcss" + }, + "files.exclude": { + "**/*.lock.json": true, + "**/*?.d.ts": true, + "**/*?.js": { + "when": "$(basename).ts" + }, + "**/**?.js": { + "when": "$(basename).tsx" + }, + "**/*?.css.js": true, + "**/.DS_Store": true, + "**/*.map": true, + "**/.git": true, + "**/.gradle/**": true, + "**/.idea/**": true, + "**/.svn": true, + "**/.tmp": true, + "**/Pods/**": true, + "**/bin/**": true, + "**/build/**": true, + "**/dist/**": true, + "**/node_modules": true, + "**/obj/**": true, + "**/vendor/**": true, + ".docker-data": true, + "tsconfig.tsbuildinfo": true + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..90aaff4 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Eduzz Hooks + +[![version](https://img.shields.io/npm/v/@eduzz/ui-hooks)](https://www.npmjs.com/package/@eduzz/ui-hooks) +[![size](https://img.shields.io/bundlephobia/min/@eduzz/ui-hooks)](https://www.npmjs.com/package/@eduzz/ui-hooks) + +Hooks diversos para auxiliar no desenvolvimento. + +## Instalação + +```bash +yarn add @eduzz/ui-hooks +``` + +## Hooks + +- [useBoolean](https://github.com/eduzz/ui-hooks/blob/master/useBoolean/index.md). +- [useCallbackGenerator](https://github.com/eduzz/ui-hooks/blob/master/useCallbackGenerator/index.md). \ No newline at end of file diff --git a/index.tsx b/index.tsx new file mode 100644 index 0000000..4772a8e --- /dev/null +++ b/index.tsx @@ -0,0 +1,5 @@ +import useBooleanExport from './useBoolean'; +import useCallbackGeneratorExport from './useCallbackGenerator'; + +export const useBoolean = useBooleanExport; +export const useCallbackGenerator = useCallbackGeneratorExport; diff --git a/package.json b/package.json new file mode 100644 index 0000000..f38dcaa --- /dev/null +++ b/package.json @@ -0,0 +1,48 @@ +{ + "name": "@eduzz/ui-hooks", + "description": "Eduzz Hooks", + "version": "0.0.2", + "keywords": ["react-hooks", "hooks", "react", "eduzz"], + "main": "./index.js", + "types": "./index.d.ts", + "author": "Eduzz Team", + "license": "MIT", + "private": false, + "homepage": "https://github.com/eduzz/ui-hooks", + "repository": { + "type": "git", + "url": "https://github.com/eduzz/ui-hooks" + }, + "bugs": { + "url": "https://github.com/eduzz/ui-hooks/issues" + }, + "scripts": { + "clean": "git add . && git clean -fdx . -e node_modules", + "prebuild": "yarn clean && yarn lint", + "build": "yarn tsc -p tsconfig.json", + "lint": "eslint . --ext js,ts,tsx" + }, + "dependencies": { + "lodash-es": "^4" + }, + "devDependencies": { + "@eduzz/eslint-config": "^0.0.3", + "@types/lodash-es": "^4", + "@types/react-dom": "^18.2.6", + "@types/react": "^18.2.14", + "@typescript-eslint/eslint-plugin": "^5.61.0", + "@typescript-eslint/parser": "^5.61.0", + "eslint-config-prettier": "^8.8.0", + "eslint-plugin-eslint-plugin": "^5.1.0", + "eslint-plugin-import": "^2.27.5", + "eslint-plugin-prettier": "^4.2.1", + "eslint-plugin-react-hooks": "^4.6.0", + "eslint-plugin-react": "^7.32.2", + "eslint-plugin-unused-imports": "^2.0.0", + "eslint": "^8.44.0", + "prettier": "^2", + "react-dom": "^18.2.0", + "react": "^18.2.0", + "typescript": "^5.1.6" + } +} diff --git a/scripts/should-release.js b/scripts/should-release.js new file mode 100644 index 0000000..4214389 --- /dev/null +++ b/scripts/should-release.js @@ -0,0 +1,52 @@ +/* eslint-disable @typescript-eslint/no-require-imports */ +const childProccess = require('child_process'); + +const globalPackages = childProccess.execSync('npm root -g').toString().trim(); +const semver = require(`${globalPackages}/semver`); + +const package = require('../package.json'); + +async function init() { + if (!semver.valid(package.version)) { + throw new Error(`Invalid Version: ${package.version}`); + } + + console.log('NEW VERSION:' + package.version); + + const remoteVersion = await exec(`npm view ${package.name} version`); + package.remoteVersion = remoteVersion; + + console.log(`REMOTE VERSIONS: ${package.remoteVersion}`); + + const isGreater = semver.gt(package.version, package.remoteVersion); + + if (!isGreater) { + console.log('Version already published, skipping...'); + await exec('echo true > .skip-release'); + return; + } + + await exec('echo false > .skip-release'); +} + +function exec(command, live) { + return new Promise((resolve, reject) => { + const cmd = childProccess.exec(command, { env: { ...process.env } }, (err, stdout) => { + if (err) reject(err); + resolve(stdout?.trim()); + }); + + if (!live) return; + cmd.stdout.on('data', data => console.log(data.toString())); + }); +} + +init() + .then(() => { + console.log(`NEW VERSION CAN BE PUBLISHED: ${package.version}`); + process.exit(0); + }) + .catch(err => { + console.error(err); + process.exit(-1); + }); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..a3858f0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,34 @@ +{ + "compilerOptions": { + "strict": true, + "outDir": ".", + "declaration": true, + "declarationDir": ".", + "module": "es2020", + "target": "ES6", + "experimentalDecorators": true, + "lib": ["es2020", "dom"], + "sourceMap": false, + "jsx": "react-jsx", + "moduleResolution": "node", + "forceConsistentCasingInFileNames": true, + "allowSyntheticDefaultImports": false, + "noUnusedLocals": true, + "importHelpers": true, + "skipLibCheck": true, + "downlevelIteration": true, + "esModuleInterop": true, + "resolveJsonModule": true, + "isolatedModules": true + }, + "include": [ + "./**/*.tsx", + "./**/*.ts" + ], + "exclude": [ + "*.d.ts", + "./**/*.d.ts", + "dist", + "node_modules" + ] +} diff --git a/useBoolean/index.md b/useBoolean/index.md new file mode 100644 index 0000000..ffd2299 --- /dev/null +++ b/useBoolean/index.md @@ -0,0 +1,20 @@ +# useBoolean + +Esse é um hooks apenas para facilitar o uso de booleanos, útil quando deseja passar como callback de uma função + +## Como usar + +```tsx +const [value, toggleValue, toTrue, toFalse] = useBoolean(); +``` + +## Parâmetros e Retorno + +```tsx +/** + * Simplify the way to use a boolean state + * @param initial A boolen of a function that return a boolean + * @returns [value, toogleValue, toTrue, toFalse] + */ +export default function useBoolean(initial?: boolean | (() => boolean)): [boolean, () => void, () => void, () => void]; +``` diff --git a/useBoolean/index.tsx b/useBoolean/index.tsx new file mode 100644 index 0000000..8449fb9 --- /dev/null +++ b/useBoolean/index.tsx @@ -0,0 +1,15 @@ +import { useState, useCallback } from 'react'; + +/** + * Simplify the way to use a boolean state + * @param initial A boolen of a function that return a boolean + * @returns [value, toogleValue, toTrue, toFalse] + */ +export default function useBoolean(initial?: boolean | (() => boolean)): [boolean, () => void, () => void, () => void] { + const [value, setValue] = useState(initial ?? false); + const toogleValue = useCallback(() => setValue(value => !value), []); + const toTrue = useCallback(() => setValue(() => true), []); + const toFalse = useCallback(() => setValue(() => false), []); + + return [value, toogleValue, toTrue, toFalse]; +} diff --git a/useCallbackGenerator/index.md b/useCallbackGenerator/index.md new file mode 100644 index 0000000..5ec1fb0 --- /dev/null +++ b/useCallbackGenerator/index.md @@ -0,0 +1,51 @@ +# useCallbackGenerator + +Um gerador de callbacks que faz [Currying](https://rodrigorgs.github.io/aulas/mata56/aula14-currying)(apenas um nível) +dos argumentos passados, útil quando temos que uma mesma função mudando apenas alguns parâmetros. É necessário uma key, +para poder criar cache (useCallback padrão) dessa função. + +## Como usar + +```tsx +const navigateTo = useCallbackGenerator( + (screen: string) => navigation.navigate(screen, { invoice }), + [invoice, navigation] +); + +//... +