├── .gitattributes ├── renovate.json ├── rollup.config.js ├── .eslintrc.cjs ├── .github └── workflows │ ├── nodejs-test.yml │ └── release-please.yml ├── CHANGELOG.md ├── .gitignore ├── package.json ├── src ├── tweet.js └── bin.js ├── tests ├── bin.test.js └── tweet.test.js ├── README.md └── LICENSE /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf 2 | 3 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base", 4 | ":rebaseStalePrs" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | export default [ 2 | { 3 | input: "src/bin.js", 4 | output: [ 5 | { 6 | file: "dist/bin.cjs.js", 7 | format: "cjs", 8 | banner: "#!/usr/bin/env node\n" 9 | }, 10 | { 11 | file: "dist/bin.js", 12 | format: "esm", 13 | banner: "#!/usr/bin/env node\n" 14 | } 15 | ] 16 | } 17 | ]; 18 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "env": { 3 | "es6": true, 4 | "node": true 5 | }, 6 | "extends": "eslint:recommended", 7 | "parserOptions": { 8 | "ecmaVersion": 2019, 9 | "sourceType": "module" 10 | }, 11 | "rules": { 12 | "indent": [ 13 | "error", 14 | 4 15 | ], 16 | "linebreak-style": [ 17 | "error", 18 | "unix" 19 | ], 20 | "quotes": [ 21 | "error", 22 | "double" 23 | ], 24 | "semi": [ 25 | "error", 26 | "always" 27 | ] 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /.github/workflows/nodejs-test.yml: -------------------------------------------------------------------------------- 1 | name: Node CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ${{ matrix.os }} 9 | 10 | strategy: 11 | matrix: 12 | os: [windows-latest, macOS-latest, ubuntu-latest] 13 | node: [18.x, 19.x] 14 | 15 | steps: 16 | - uses: actions/checkout@v3 17 | - name: Use Node.js ${{ matrix.node-version }} 18 | uses: actions/setup-node@v3 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - name: npm install, build, and test 22 | run: | 23 | npm install 24 | npm run build --if-present 25 | npm run lint 26 | npm test 27 | env: 28 | CI: true 29 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [1.0.1](https://github.com/humanwhocodes/tweet/compare/v1.0.0...v1.0.1) (2023-04-15) 4 | 5 | 6 | ### Bug Fixes 7 | 8 | * Publish package ([70df3e7](https://github.com/humanwhocodes/tweet/commit/70df3e73dc512294bdd95be10d483e6326308566)) 9 | 10 | ## [1.0.0](https://github.com/humanwhocodes/tweet/compare/v0.2.4...v1.0.0) (2023-04-15) 11 | 12 | 13 | ### ⚠ BREAKING CHANGES 14 | 15 | * Switch to v2 Twitter API by default 16 | 17 | ### Features 18 | 19 | * Switch to twitter-api-v2 package ([db56ef3](https://github.com/humanwhocodes/tweet/commit/db56ef38928e96cded00ebf985d4d9e37d455546)) 20 | * Switch to v2 Twitter API by default ([88e4095](https://github.com/humanwhocodes/tweet/commit/88e4095a08c983a4b54967f28882cdfc773abc09)) 21 | 22 | 23 | ### Bug Fixes 24 | 25 | * **deps:** update dependency dotenv to v16 ([#24](https://github.com/humanwhocodes/tweet/issues/24)) ([b88e67d](https://github.com/humanwhocodes/tweet/commit/b88e67d6b0a83f9f390043d3e78fb054d3d95671)) 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | # distribution files 64 | dist 65 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@humanwhocodes/tweet", 3 | "version": "1.0.1", 4 | "description": "A CLI for tweeting out messages", 5 | "type": "module", 6 | "bin": "dist/bin.js", 7 | "files": [ 8 | "dist", 9 | "LICENSE" 10 | ], 11 | "publishConfig": { 12 | "access": "public" 13 | }, 14 | "engines": { 15 | "node": ">=18" 16 | }, 17 | "gitHooks": { 18 | "pre-commit": "lint-staged" 19 | }, 20 | "lint-staged": { 21 | "*.js": [ 22 | "eslint --fix" 23 | ] 24 | }, 25 | "scripts": { 26 | "build": "rollup -c", 27 | "prepublishOnly": "npm run build", 28 | "lint": "eslint src/ tests/", 29 | "test": "mocha tests/ --recursive" 30 | }, 31 | "repository": { 32 | "type": "git", 33 | "url": "git+https://github.com/humanwhocodes/tweet.git" 34 | }, 35 | "bugs": { 36 | "url": "https://github.com/humanwhocodes/tweet/issues" 37 | }, 38 | "homepage": "https://github.com/humanwhocodes/tweet#readme", 39 | "keywords": [ 40 | "Twitter", 41 | "Tweet", 42 | "JavaScript" 43 | ], 44 | "funding": { 45 | "type": "github", 46 | "url": "https://github.com/sponsors/nzakas" 47 | }, 48 | "author": "Nicholas C. Zaks", 49 | "license": "Apache-2.0", 50 | "devDependencies": { 51 | "chai": "^4.3.7", 52 | "eslint": "8.38.0", 53 | "lint-staged": "^13.2.1", 54 | "mocha": "^10.2.0", 55 | "nock": "13.3.0", 56 | "rollup": "3.20.2", 57 | "yorkie": "^2.0.0" 58 | }, 59 | "dependencies": { 60 | "@humanwhocodes/env": "^2.2.2", 61 | "dotenv": "^16.0.3", 62 | "twitter-api-v2": "^1.14.2" 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/tweet.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Main functionality for tweeting. 3 | * @author Nicholas C. Zakas 4 | */ 5 | 6 | //----------------------------------------------------------------------------- 7 | // Imports 8 | //----------------------------------------------------------------------------- 9 | 10 | import { Env } from "@humanwhocodes/env"; 11 | import { TwitterApi } from "twitter-api-v2"; 12 | 13 | //----------------------------------------------------------------------------- 14 | // Data 15 | //----------------------------------------------------------------------------- 16 | 17 | const validAPIVersions = new Set(["v1", "v2"]); 18 | 19 | //----------------------------------------------------------------------------- 20 | // Exports 21 | //----------------------------------------------------------------------------- 22 | 23 | export async function tweet(message, options = {}) { 24 | 25 | if (!message) { 26 | throw new Error("Missing message to tweet."); 27 | } 28 | 29 | const env = new Env(options); 30 | const version = env.get("TWITTER_API_VERSION", "v2"); 31 | 32 | if (!validAPIVersions.has(version)) { 33 | throw new TypeError(`Invalid API version: ${ version }. Must be one of ${[...validAPIVersions]}.`); 34 | } 35 | 36 | const { 37 | TWITTER_ACCESS_TOKEN_KEY, 38 | TWITTER_ACCESS_TOKEN_SECRET, 39 | TWITTER_CONSUMER_KEY, 40 | TWITTER_CONSUMER_SECRET 41 | } = env.required; 42 | 43 | const client = new TwitterApi({ 44 | appKey: TWITTER_CONSUMER_KEY, 45 | appSecret: TWITTER_CONSUMER_SECRET, 46 | accessToken: TWITTER_ACCESS_TOKEN_KEY, 47 | accessSecret: TWITTER_ACCESS_TOKEN_SECRET 48 | }); 49 | 50 | return client[version].tweet(message); 51 | } 52 | -------------------------------------------------------------------------------- /.github/workflows/release-please.yml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - main 5 | name: release-please 6 | jobs: 7 | release-please: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: GoogleCloudPlatform/release-please-action@v3 11 | id: release 12 | with: 13 | release-type: node 14 | package-name: "@humanwhocodes/tweet" 15 | # The logic below handles the npm publication: 16 | - uses: actions/checkout@v3 17 | # these if statements ensure that a publication only occurs when 18 | # a new release is created: 19 | if: ${{ steps.release.outputs.release_created }} 20 | - uses: actions/setup-node@v3 21 | with: 22 | node-version: 18 23 | registry-url: 'https://registry.npmjs.org' 24 | if: ${{ steps.release.outputs.release_created }} 25 | - run: npm ci 26 | if: ${{ steps.release.outputs.release_created }} 27 | - run: npm publish 28 | env: 29 | NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} 30 | if: ${{ steps.release.outputs.release_created }} 31 | 32 | # Tweets out release announcement 33 | - run: 'npx @humanwhocodes/tweet "${{ github.event.repository.full_name }} v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }}.${{ steps.release.outputs.patch }} has been released!\n\n${{ github.event.repository.html_url }}/releases/tag/${{ steps.release.outputs.tag }}"' 34 | if: ${{ steps.release.outputs.release_created }} 35 | env: 36 | TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }} 37 | TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }} 38 | TWITTER_ACCESS_TOKEN_KEY: ${{ secrets.TWITTER_ACCESS_TOKEN_KEY }} 39 | TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} 40 | 41 | # Toots out release announcement 42 | - run: 'npx @humanwhocodes/toot "${{ github.event.repository.full_name }} v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }}.${{ steps.release.outputs.patch }} has been released!\n\n${{ github.event.repository.html_url }}/releases/tag/${{ steps.release.outputs.tag }}"' 43 | if: ${{ steps.release.outputs.release_created }} 44 | env: 45 | MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }} 46 | MASTODON_HOST: ${{ secrets.MASTODON_HOST }} 47 | -------------------------------------------------------------------------------- /src/bin.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview A CLI for tweeting out updates. 3 | * @author Nicholas C. Zakas 4 | */ 5 | 6 | /* eslint-disable no-console */ 7 | 8 | //----------------------------------------------------------------------------- 9 | // Imports 10 | //----------------------------------------------------------------------------- 11 | 12 | import { tweet } from "./tweet.js"; 13 | import dotenv from "dotenv"; 14 | 15 | //----------------------------------------------------------------------------- 16 | // Setup 17 | //----------------------------------------------------------------------------- 18 | 19 | if (process.argv.length < 3) { 20 | console.error("Usage: tweet \"Message to tweet.\""); 21 | console.error("Missing message to tweet."); 22 | process.exit(1); 23 | } 24 | 25 | if (process.env.TWEET_DOTENV === "1") { 26 | dotenv.config(); 27 | } 28 | 29 | /* 30 | * Command line arguments will escape \n as \\n, which isn't what we want. 31 | * Remove the extra escapes so newlines can be entered on the command line. 32 | */ 33 | const message = process.argv[2].replace(/\\n/g, "\n"); 34 | 35 | const environmentVariables = [ 36 | "TWITTER_ACCESS_TOKEN_KEY", 37 | "TWITTER_ACCESS_TOKEN_SECRET", 38 | "TWITTER_CONSUMER_KEY", 39 | "TWITTER_CONSUMER_SECRET" 40 | ]; 41 | 42 | //----------------------------------------------------------------------------- 43 | // Main 44 | //----------------------------------------------------------------------------- 45 | 46 | tweet(message, process.env) 47 | .then(response => console.log(JSON.stringify(response, null, 2))) 48 | .catch(error => { 49 | if (error.message) { 50 | console.error(error.message); 51 | console.dir(error); 52 | } else { 53 | 54 | console.dir(error); 55 | 56 | if (Array.isArray(error)) { // v1.1 57 | const firstError = error[0]; 58 | 59 | if (firstError.code === 215) { 60 | console.error(` 61 | This error is likely caused by invalid authentication information. Please check 62 | that you have configured your environment variables with the correct values. 63 | Here are the lengths of the environment variables provided for reference:\n`); 64 | 65 | for (const environmentVariable of environmentVariables) { 66 | console.error(environmentVariable, process.env[environmentVariable].length); 67 | } 68 | 69 | } 70 | } 71 | 72 | } 73 | process.exit(1); 74 | }); 75 | -------------------------------------------------------------------------------- /tests/bin.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Tests for the Env class. 3 | */ 4 | /*global describe, it*/ 5 | 6 | //----------------------------------------------------------------------------- 7 | // Requirements 8 | //----------------------------------------------------------------------------- 9 | 10 | import { execSync } from "child_process"; 11 | import { expect } from "chai"; 12 | 13 | //----------------------------------------------------------------------------- 14 | // Helpers 15 | //----------------------------------------------------------------------------- 16 | 17 | const command = "node src/bin.js"; 18 | 19 | const envKeys = [ 20 | "TWITTER_ACCESS_TOKEN_KEY", 21 | "TWITTER_ACCESS_TOKEN_SECRET", 22 | "TWITTER_CONSUMER_KEY", 23 | "TWITTER_CONSUMER_SECRET" 24 | ]; 25 | 26 | function exec(command, env) { 27 | return execSync(command, { 28 | env: { 29 | ...process.env, 30 | ...env 31 | }, 32 | stdio: ["ignore", "pipe", "pipe"] 33 | }); 34 | } 35 | 36 | 37 | //----------------------------------------------------------------------------- 38 | // Tests 39 | //----------------------------------------------------------------------------- 40 | 41 | describe("bin", () => { 42 | describe("Errors", () => { 43 | 44 | it("should error when environment variables are missing", () => { 45 | 46 | expect(() => { 47 | exec(`${command} "hi"`); 48 | }).to.throw(new RegExp(envKeys[0])); 49 | 50 | }); 51 | 52 | it("should error when only one environment variable is present", () => { 53 | 54 | expect(() => { 55 | exec(`${command} "hi"`, { [envKeys[0]]: "foo" }); 56 | }).to.throw(new RegExp(envKeys[1])); 57 | 58 | }); 59 | 60 | it("should error when only two environment variables are present", () => { 61 | 62 | expect(() => { 63 | exec(`${command} "hi"`, { 64 | [envKeys[0]]: "foo", 65 | [envKeys[1]]: "bar" 66 | }); 67 | }).to.throw(new RegExp(envKeys[2])); 68 | 69 | }); 70 | 71 | it("should error when only three environment variables are present", () => { 72 | 73 | expect(() => { 74 | exec(`${ command } "hi"`, { 75 | [envKeys[0]]: "foo", 76 | [envKeys[1]]: "bar", 77 | [envKeys[2]]: "baz" 78 | }); 79 | }).to.throw(new RegExp(envKeys[3])); 80 | 81 | }); 82 | 83 | it("should error when there is no message to tweet", () => { 84 | 85 | expect(() => { 86 | exec(command, { 87 | [envKeys[0]]: "foo", 88 | [envKeys[1]]: "bar", 89 | [envKeys[2]]: "baz", 90 | [envKeys[3]]: "bar" 91 | }); 92 | }).to.throw(/Missing message to tweet/); 93 | 94 | }); 95 | 96 | }); 97 | 98 | }); 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tweet CLI (Deprecated) 2 | 3 | > [!IMPORTANT] 4 | > This project is no longer being maintained. Please use [Crosspost](https://github.com/humanwhocodes.com/crosspost) instead. 5 | 6 | ## Description 7 | 8 | A simple CLI for sending tweets. This is intended for use in CI systems such as GitHub actions in order to enable to Twitter notifications of important events. 9 | 10 | ## Usage 11 | 12 | You must have Node.js to use this package. 13 | 14 | To start, you must have a registered [Twitter application](https://developer.twitter.com/apps). 15 | 16 | Next, define four environment variables: 17 | 18 | * `TWITTER_ACCESS_TOKEN_KEY` - your access token 19 | * `TWITTER_ACCESS_TOKEN_SECRET` - your access token secret 20 | * `TWITTER_CONSUMER_KEY` - your consumer API key 21 | * `TWITTER_CONSUMER_SECRET` - your consumer API secret 22 | 23 | The CLI will not work without these environment variables. All of these values come from your Twitter application. 24 | 25 | Then, you can run the CLI and pass a message on the command line using `npx`: 26 | 27 | ``` 28 | $ npx @humanwhocodes/tweet "Hello from the command line!" 29 | ``` 30 | 31 | If successful, the CLI will output the response from Twitter. 32 | 33 | ### Setting Twitter API version 34 | 35 | By default, Tweet uses v2 of the Twitter API. If you'd like to use v1.1 of the Twitter API instead, set the `TWITTER_API_VERSION` environment variable to `v1`. 36 | 37 | ### Testing with dotenv 38 | 39 | If you'd like to test with [`dotenv`](https://npmjs.com/package/dotenv), define an additional environment variable `TWEET_DOTENV=1` before executing the CLI. This will cause a local `.env` file to be read before executing. 40 | 41 | ### Using in a GitHub Workflow 42 | 43 | Be sure to set up [GitHub secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets) for each environment variable. Then, you can configure a job like this: 44 | 45 | ```yaml 46 | jobs: 47 | tweet: 48 | name: Tweet Something 49 | runs-on: ubuntu-latest 50 | steps: 51 | - uses: actions/setup-node@v3 52 | with: 53 | node-version: 18 54 | - run: 'npx @humanwhocodes/tweet "Your tweet text"' 55 | env: 56 | TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }} 57 | TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }} 58 | TWITTER_ACCESS_TOKEN_KEY: ${{ secrets.TWITTER_ACCESS_TOKEN_KEY }} 59 | TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} 60 | ``` 61 | 62 | ### Developer Setup 63 | 64 | 1. Ensure you have [Node.js](https://nodejs.org) 12+ installed 65 | 2. Fork and clone this repository 66 | 3. Run `npm install` 67 | 4. Run `npm test` to run tests 68 | 69 | ## Troubleshooting 70 | 71 | **Console says "Required environment variable 'TWITTER_ACCESS_TOKEN_KEY' is an empty string."** 72 | 73 | You haven't setup the correct environment variables for Tweet CLI. Double check that you don't have any misspellings in your environment variable settings. 74 | 75 | **GitHub Actions console says "Required environment variable 'TWITTER_ACCESS_TOKEN_KEY' is an empty string."** 76 | 77 | You're probably trying to use the Tweet CLI during the `pull_request` event. This won't work because secrets are not available when a pull request is from a fork. Try using the `pull_request_target` event instead. ([More information](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target)) 78 | 79 | ## License and Copyright 80 | 81 | This code is licensed under the Apache 2.0 License (see LICENSE for details). 82 | 83 | Copyright Human Who Codes LLC. All rights reserved. 84 | -------------------------------------------------------------------------------- /tests/tweet.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Tests for the tweet() function. 3 | */ 4 | /*global describe, it*/ 5 | 6 | //----------------------------------------------------------------------------- 7 | // Requirements 8 | //----------------------------------------------------------------------------- 9 | 10 | import { tweet } from "../src/tweet.js"; 11 | import { expect } from "chai"; 12 | import nock from "nock"; 13 | 14 | //----------------------------------------------------------------------------- 15 | // Helpers 16 | //----------------------------------------------------------------------------- 17 | 18 | const envKeys = [ 19 | "TWITTER_ACCESS_TOKEN_KEY", 20 | "TWITTER_ACCESS_TOKEN_SECRET", 21 | "TWITTER_CONSUMER_KEY", 22 | "TWITTER_CONSUMER_SECRET" 23 | ]; 24 | 25 | const message = "Tweet!"; 26 | 27 | //----------------------------------------------------------------------------- 28 | // Tests 29 | //----------------------------------------------------------------------------- 30 | 31 | describe("Tweet", () => { 32 | describe("Errors", () => { 33 | 34 | it("should error when environment variables are missing", (done) => { 35 | 36 | tweet(message, {}).catch(ex => { 37 | expect(ex.message).to.match(new RegExp(envKeys[0])); 38 | }).then(done); 39 | 40 | }); 41 | 42 | it("should error when only one environment variable is present", (done) => { 43 | 44 | tweet(message, { 45 | [envKeys[0]]: "foo", 46 | }).catch(ex => { 47 | expect(ex.message).to.match(new RegExp(envKeys[1])); 48 | }).then(done); 49 | 50 | }); 51 | 52 | it("should error when only two environment variables are present", (done) => { 53 | 54 | tweet(message, { 55 | [envKeys[0]]: "foo", 56 | [envKeys[1]]: "bar" 57 | }).catch(ex => { 58 | expect(ex.message).to.match(new RegExp(envKeys[2])); 59 | }).then(done); 60 | 61 | }); 62 | 63 | it("should error when only three environment variables are present", (done) => { 64 | 65 | tweet(message, { 66 | [envKeys[0]]: "foo", 67 | [envKeys[1]]: "bar", 68 | [envKeys[2]]: "baz" 69 | }).catch(ex => { 70 | expect(ex.message).to.match(new RegExp(envKeys[3])); 71 | }).then(done); 72 | 73 | }); 74 | 75 | it("should error when an invalid API version is set", (done) => { 76 | 77 | tweet(message, { 78 | [envKeys[0]]: "foo", 79 | [envKeys[1]]: "bar", 80 | [envKeys[2]]: "baz", 81 | [envKeys[3]]: "bar", 82 | TWITTER_API_VERSION: "foo" 83 | }).catch(ex => { 84 | expect(ex.message).to.match(/Invalid API version: foo/); 85 | }).then(done); 86 | 87 | }); 88 | 89 | it("should error when there is no message to tweet", (done) => { 90 | 91 | tweet(undefined).catch(ex => { 92 | expect(ex.message).to.match(/Missing message to tweet/); 93 | }).then(done); 94 | 95 | }); 96 | 97 | }); 98 | 99 | it("v1.1: should send a tweet when there's a message and environment variables", done => { 100 | 101 | nock("https://api.twitter.com", { 102 | reqheaders: { 103 | authorization: /OAuth oauth_consumer_key="baz"/ 104 | } 105 | }).post( 106 | "/1.1/statuses/update.json" 107 | ).reply(200, { result: "Success!" }); 108 | 109 | tweet("Tweet!", { 110 | [envKeys[0]]: "foo", 111 | [envKeys[1]]: "bar", 112 | [envKeys[2]]: "baz", 113 | [envKeys[3]]: "bar", 114 | TWITTER_API_VERSION: "v1" 115 | }).then(response => { 116 | expect(response.result).to.equal("Success!"); 117 | }).catch(ex => { 118 | console.error(ex); 119 | throw ex; 120 | 121 | }).then(done); 122 | 123 | }); 124 | 125 | it("v2: should send a tweet when there's a message and environment variables", done => { 126 | 127 | nock("https://api.twitter.com", { 128 | reqheaders: { 129 | authorization: (/OAuth oauth_consumer_key="baz"/) 130 | } 131 | }).post( 132 | "/2/tweets" 133 | ).reply(200, { result: "Success!" }); 134 | 135 | tweet("Tweet!", { 136 | [envKeys[0]]: "foo", 137 | [envKeys[1]]: "bar", 138 | [envKeys[2]]: "baz", 139 | [envKeys[3]]: "bar", 140 | TWITTER_API_VERSION: "v2" 141 | }).then(response => { 142 | expect(response.result).to.equal("Success!"); 143 | }).catch(ex => { 144 | console.error(ex); 145 | done(ex); 146 | }).then(done); 147 | 148 | }); 149 | 150 | }); 151 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------