diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 066eaed..e7544b5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,11 +11,5 @@ jobs: runs-on: ubuntu-18.04 steps: - uses: actions/checkout@v1 - - name: Install dependencies + - name: CI run: yarn ci - - - name: ESLint - run: yarn lint - - - name: Tests - run: yarn test diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 728ea04..bd4e121 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,12 +16,8 @@ jobs: uses: actions/setup-node@v1 with: node-version: 12 - - name: Install dependencies + - name: CI run: yarn ci - - name: ESLint - run: yarn lint - - name: Tests - run: yarn test - name: Release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 0e98c09..0000000 --- a/.npmignore +++ /dev/null @@ -1,97 +0,0 @@ -# JS -node_modules -yarn.lock - -# Project files -CONTRIBUTING.md -CODE_OF_CONDUCT.md -README.md - -# Config files -.babelrc -babel.config.js -.editorconfig -.eslintrc.json -.prettierrc -.flowconfig -.watchmanconfig -jsconfig.json -.npmrc -.gitattributes -.circleci -*.coverage.json -.opensource -.circleci -.eslintignore -codecov.yml - -# Tests -__tests__/ -jest.config.js -jest.setup.js -coverage - -# Example -examples/ - -# Android -android/*/build/ -android/gradlew -android/build -android/gradlew.bat -android/gradle/ -android/com_crashlytics_export_strings.xml -android/local.properties -android/.gradle/ -android/.signing/ -android/.idea/gradle.xml -android/.idea/libraries/ -android/.idea/workspace.xml -android/.idea/tasks.xml -android/.idea/.name -android/.idea/compiler.xml -android/.idea/copyright/profiles_settings.xml -android/.idea/encodings.xml -android/.idea/misc.xml -android/.idea/modules.xml -android/.idea/scopes/scope_settings.xml -android/.idea/vcs.xml -android/*.iml -android/.settings - -# iOS -ios/*.xcodeproj/xcuserdata -*.pbxuser -*.mode1v3 -*.mode2v3 -*.perspectivev3 -*.xcuserstate -*.xcworkspace/ -xcuserdata/ -ios/build/ -ios/Pods/ -Podfile -Podfile.lock - -# Misc -.DS_Store -.DS_Store? -*.DS_Store -coverage.android.json -coverage.ios.json -coverage -npm-debug.log -.github -._* -.Spotlight-V100 -.Trashes -ehthumbs.db -Thumbs.dbandroid/gradle -docs -.idea -tests/ -__tests__/ -bin/test.js -codorials -.vscode -.nyc_output diff --git a/.releaserc b/.releaserc index 425d489..ed885ac 100644 --- a/.releaserc +++ b/.releaserc @@ -14,7 +14,7 @@ }], "@semantic-release/npm", ["@semantic-release/git", { - "assets": ["CHANGELOG.md", "package.json"], + "assets": ["CHANGELOG.md", "package.json", "src/index.d.ts", "src/RNBackgroundActionsModule.d.ts"], "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" }] ] diff --git a/__mocks__/RNBackgroundActionsModule.js b/__mocks__/RNBackgroundActionsModule.js deleted file mode 100644 index db26e67..0000000 --- a/__mocks__/RNBackgroundActionsModule.js +++ /dev/null @@ -1,4 +0,0 @@ -export default { - start: jest.fn(), - stop: jest.fn(), -}; diff --git a/__tests__/index.test.js b/__tests__/index.test.js index 67a4cf3..ade9672 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -1,6 +1,6 @@ import { Platform, AppRegistry } from 'react-native'; -import BackgroundActions from '../index'; -import RNBackgroundActionsModule from '../RNBackgroundActionsModule'; +import BackgroundActions from '../src/index'; +import RNBackgroundActionsModule from '../src/RNBackgroundActionsModule'; // Flush promises const flushPromises = () => new Promise(setImmediate); diff --git a/declaration.tsconfig.json b/declaration.tsconfig.json new file mode 100644 index 0000000..356f57e --- /dev/null +++ b/declaration.tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "declaration": true, + "noEmit": false, + "emitDeclarationOnly": true + }, + "exclude": ["__tests__", "__mocks__"] +} diff --git a/jest.setup.js b/jest.setup.js index 00663a8..b2b9f7f 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -1 +1,4 @@ -jest.mock('./RNBackgroundActionsModule'); +jest.mock('./src/RNBackgroundActionsModule.js', () => ({ + start: jest.fn(), + stop: jest.fn(), +})); diff --git a/jsconfig.json b/jsconfig.json deleted file mode 100644 index abc807c..0000000 --- a/jsconfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "compilerOptions": { - "checkJs": true, - }, - "exclude": ["node_modules", "coverage", ".github", "examples", "ios", "android"], -} diff --git a/package.json b/package.json index b31ea1e..6f7e3b4 100644 --- a/package.json +++ b/package.json @@ -10,12 +10,27 @@ "android", "ios" ], - "main": "index.js", + "files": [ + "/android/src/", + "/android/build.gradle", + "/ios", + "!Podfile*", + "/windows", + "/src", + "/lib", + "/*.podspec", + "/jest" + ], + "types": "src/index.d.ts", + "main": "src/index.js", "scripts": { - "ci": "yarn install --frozen-lockfile", + "ci": "yarn install --frozen-lockfile && yarn lint && yarn declaration:build && yarn checkjs && yarn test", "lint": "eslint .", + "checkjs": "tsc", "eslint:fix": "yarn eslint --fix", "test": "jest ./__tests__/", + "declaration:build": "tsc -p ./declaration.tsconfig.json", + "prepublishOnly": "yarn declaration:build && yarn checkjs", "pods": "cd ios && pod install" }, "repository": { @@ -38,6 +53,7 @@ "@semantic-release/github": "^7.0.0", "@semantic-release/npm": "^7.0.0", "@types/jest": "^25.1.3", + "@types/react-native": "^0.61.17", "babel-jest": "^24.9.0", "eslint": "^6.6.0", "eslint-config-prettier": "^6.5.0", @@ -48,7 +64,8 @@ "prettier": "^1.18.2", "react": "16.9.0", "react-native": "^0.61.4", - "semantic-release": "^17.0.1" + "semantic-release": "^17.0.1", + "typescript": "^3.8.2" }, "dependencies": {} } diff --git a/src/RNBackgroundActionsModule.d.ts b/src/RNBackgroundActionsModule.d.ts new file mode 100644 index 0000000..ae7a70f --- /dev/null +++ b/src/RNBackgroundActionsModule.d.ts @@ -0,0 +1,2 @@ +export default RNBackgroundActions; +declare const RNBackgroundActions: any; diff --git a/RNBackgroundActionsModule.js b/src/RNBackgroundActionsModule.js similarity index 100% rename from RNBackgroundActionsModule.js rename to src/RNBackgroundActionsModule.js diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..bbfdb95 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,69 @@ +export default backgroundServer; +export type BackgroundTaskOptions = { + taskName: string; + taskTitle: string; + taskDesc: string; + taskIcon: { + name: string; + type: string; + package?: string | undefined; + }; + color?: string | undefined; + parameters?: any; +}; +declare const backgroundServer: BackgroundServer; +/** + * @typedef {{taskName: string, + * taskTitle: string, + * taskDesc: string, + * taskIcon: {name: string, type: string, package?: string}, + * color?: string, + * parameters?: any}} BackgroundTaskOptions + */ +declare class BackgroundServer { + _runnedTasks: number; + _stopTask: () => void; + _isRunning: boolean; + /** + * Returns if the current background task is running. + * + * It returns `true` if `start()` has been called and the task has not finished. + * + * It returns `false` if `stop()` has been called, **even if the task has not finished**. + */ + isRunning(): boolean; + /** + * @param {(taskData: any) => Promise} task + * @param {BackgroundTaskOptions} options + * @returns {Promise} + */ + async start(task: (taskData: any) => Promise, options: { + taskName: string; + taskTitle: string; + taskDesc: string; + taskIcon: { + name: string; + type: string; + package?: string | undefined; + }; + color?: string | undefined; + parameters?: any; + }): Promise; + /** + * @private + * @param {(taskData: any) => Promise} task + * @param {any} [parameters] + */ + private _generateTask; + /** + * @private + * @param {BackgroundTaskOptions} options + */ + private _normalizeOptions; + /** + * Stops the background task. + * + * @returns {Promise} + */ + async stop(): Promise; +} diff --git a/index.js b/src/index.js similarity index 94% rename from index.js rename to src/index.js index d0498b4..64b4a50 100644 --- a/index.js +++ b/src/index.js @@ -30,6 +30,7 @@ class BackgroundServer { /** * @param {(taskData: any) => Promise} task * @param {BackgroundTaskOptions} options + * @returns {Promise} */ async start(task, options) { this._runnedTasks++; @@ -46,6 +47,7 @@ class BackgroundServer { } /** + * @private * @param {(taskData: any) => Promise} task * @param {any} [parameters] */ @@ -60,6 +62,7 @@ class BackgroundServer { } /** + * @private * @param {BackgroundTaskOptions} options */ _normalizeOptions(options) { @@ -72,6 +75,11 @@ class BackgroundServer { }; } + /** + * Stops the background task. + * + * @returns {Promise} + */ async stop() { this._stopTask(); await RNBackgroundActions.stop(); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..63d3595 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "allowJs": true, + "checkJs": true, + "noEmit": true, + "strict": true, + "skipLibCheck": true, + }, + "include": ["src/index.js", "__tests__/", "__mocks__/"], +} diff --git a/yarn.lock b/yarn.lock index 242d8be..39f8078 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1271,6 +1271,26 @@ resolved "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA== +"@types/prop-types@*": + version "15.7.3" + resolved "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7" + integrity sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw== + +"@types/react-native@^0.61.17": + version "0.61.17" + resolved "https://registry.npmjs.org/@types/react-native/-/react-native-0.61.17.tgz#5994af207c2339d498ebd8bd427c68074ce9e744" + integrity sha512-12qKZz/ob56lglVRAIDgqDchcyO0g5lZtrjxCTQDNJv8HFHZMcZx78+/CjKsu3LGs6KbajkZ1+0htqiaxr5JVA== + dependencies: + "@types/react" "*" + +"@types/react@*": + version "16.9.22" + resolved "https://registry.npmjs.org/@types/react/-/react-16.9.22.tgz#f0288c92d94e93c4b43e3f5633edf788b2c040ae" + integrity sha512-7OSt4EGiLvy0h5R7X+r0c7S739TCU/LvWbkNOrm10lUwNHe7XPz5OLhLOSZeCkqO9JSCly1NkYJ7ODTUqVnHJQ== + dependencies: + "@types/prop-types" "*" + csstype "^2.2.0" + "@types/retry@^0.12.0": version "0.12.0" resolved "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d" @@ -2634,6 +2654,11 @@ cssstyle@^1.0.0: dependencies: cssom "0.3.x" +csstype@^2.2.0: + version "2.6.9" + resolved "https://registry.npmjs.org/csstype/-/csstype-2.6.9.tgz#05141d0cd557a56b8891394c1911c40c8a98d098" + integrity sha512-xz39Sb4+OaTsULgUERcCk+TJj8ylkL4aSVDQiX/ksxbELSqwkgt4d4RD7fovIdgJGSuNYqwZEiVjYY5l0ask+Q== + currently-unhandled@^0.4.1: version "0.4.1" resolved "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea" @@ -8803,6 +8828,11 @@ typedarray@^0.0.6: resolved "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= +typescript@^3.8.2: + version "3.8.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.2.tgz#91d6868aaead7da74f493c553aeff76c0c0b1d5a" + integrity sha512-EgOVgL/4xfVrCMbhYKUQTdF37SQn4Iw73H5BgCrF1Abdun7Kwy/QZsE/ssAy0y4LxBbvua3PIbFsbRczWWnDdQ== + ua-parser-js@^0.7.18: version "0.7.21" resolved "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.21.tgz#853cf9ce93f642f67174273cc34565ae6f308777"