diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ee160a5..36c3953 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,7 +3,7 @@ name: main on: [push] jobs: - test: + test-and-build: runs-on: ubuntu-latest steps: @@ -30,3 +30,17 @@ jobs: - run: pnpm lint - run: pnpm test + + - run: pnpm build + + - run: npm pack + working-directory: dist + + - name: npm publish + run: | + echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc + mv dist/*.tgz pack.tgz + npm publish pack.tgz + # if: success() && github.ref == 'refs/heads/master' + env: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/solve-daily.yml b/.github/workflows/solve-daily.yml index 2b70004..e84b9b5 100644 --- a/.github/workflows/solve-daily.yml +++ b/.github/workflows/solve-daily.yml @@ -13,32 +13,18 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: pnpm/action-setup@v2 - with: - version: 6 - - uses: actions/setup-node@v2 - with: - node-version: 16 - cache: pnpm + - run: npm install platane-wordle-bot - - run: pnpm install --frozen-lockfile - - - name: cache word list - uses: actions/cache@v2 - with: - path: node_modules/wordList-cache.txt - key: wordList-cache - restore-keys: wordList-cache - - - run: pnpm twitter-refresh + - run: npm run twitter-refresh + working-directory: node_modules/platane-wordle-bot env: GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }} TWITTER_CLIENT_ID: ${{ secrets.TWITTER_CLIENT_ID }} TWITTER_CLIENT_SECRET: ${{ secrets.TWITTER_CLIENT_SECRET }} TWITTER_REFRESH_TOKEN: ${{ secrets.TWITTER_REFRESH_TOKEN }} - - run: pnpm start + - run: npm run solve + working-directory: node_modules/platane-wordle-bot env: GITHUB_REPOSITORY: ${{ github.repository }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 1dcef2d..d14eab8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +dist node_modules .env \ No newline at end of file diff --git a/package.json b/package.json index 2c780ed..4ad02c4 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@types/jest": "27.4.1", "@types/node": "16.11.11", "@types/node-fetch": "2.5.11", + "@vercel/ncc": "0.33.3", "jest": "27.5.1", "prettier": "2.6.0", "sucrase": "3.20.3", @@ -31,6 +32,7 @@ } }, "scripts": { + "build": "ncc build src/twitter/auth.ts -o dist/twitter-auth && ncc build src/twitter/refresh.ts -o dist/twitter-refresh && ncc build src/index.ts -o dist/solve -e puppeteer -e puppeteer-screen-recorder && node -e \"const pkg=require('./package.json');console.log(JSON.stringify({name:'platane-wordle-bot',version:pkg.version+'-'+process.env.GITHUB_RUN_NUMBER,dependencies:{puppeteer:pkg.dependencies.puppeteer,'puppeteer-screen-recorder':pkg.dependencies['puppeteer-screen-recorder']},scripts:{solve:'node solve/index.js','twitter-refresh':'node twitter-refresh/index.js','twitter-auth':'node twitter-auth/index.js'}}))\" > dist/package.json ", "twitter-auth": "sucrase-node src/twitter/auth.ts", "twitter-refresh": "sucrase-node src/twitter/refresh.ts", "start": "sucrase-node src/index.ts", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e48a849..7b9d6a4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,6 +6,7 @@ specifiers: '@types/jest': 27.4.1 '@types/node': 16.11.11 '@types/node-fetch': 2.5.11 + '@vercel/ncc': 0.33.3 dotenv: 16.0.0 github-secret-dotenv: 1.1.4 jest: 27.5.1 @@ -33,6 +34,7 @@ devDependencies: '@types/jest': 27.4.1 '@types/node': 16.11.11 '@types/node-fetch': 2.5.11 + '@vercel/ncc': 0.33.3 jest: 27.5.1 prettier: 2.6.0 sucrase: 3.20.3 @@ -908,6 +910,11 @@ packages: dev: false optional: true + /@vercel/ncc/0.33.3: + resolution: {integrity: sha512-JGZ11QV+/ZcfudW2Cz2JVp54/pJNXbsuWRgSh2ZmmZdQBKXqBtIGrwI1Wyx8nlbzAiEFe7FHi4K1zX4//jxTnQ==} + hasBin: true + dev: true + /abab/2.0.5: resolution: {integrity: sha512-9IK9EadsbHo6jLWIpxpR6pL0sazTXV6+SQv25ZB+F7Bj9mJNaOc4nCRabwd5M/JwmUa8idz6Eci6eKfJryPs6Q==} dev: true diff --git a/src/twitter/auth.ts b/src/twitter/auth.ts index 85a361f..364ed4b 100644 --- a/src/twitter/auth.ts +++ b/src/twitter/auth.ts @@ -1,11 +1,10 @@ import * as http from "http"; -import * as path from "path"; import type { Socket } from "net"; import TwitterApi from "twitter-api-v2"; import "dotenv/config"; import { writeDotEnv } from "./writeDotEnv"; import { createSecretUpdater } from "github-secret-dotenv/lib/github"; -import { readPackageJson } from "github-secret-dotenv/lib/readPackageJson"; +import { getRepository } from "./getRepository"; const CALLBACK_URL = "http://localhost:4194/callback"; @@ -74,7 +73,7 @@ const CALLBACK_URL = "http://localhost:4194/callback"; console.log(data, { expiresIn }); } - writeDotEnv(path.join(__dirname, "../../.env"), { + writeDotEnv(".env", { TWITTER_ACCESS_TOKEN: accessToken, TWITTER_REFRESH_TOKEN: refreshToken!, }); @@ -82,7 +81,7 @@ const CALLBACK_URL = "http://localhost:4194/callback"; console.log("written to .env"); const updateSecret = createSecretUpdater({ - ...readPackageJson(path.join(__dirname, "../.."))!, + ...getRepository()!, githubAccessToken: process.env.GITHUB_ACCESS_TOKEN ?? process.env.GITHUB_TOKEN!, }); diff --git a/src/twitter/getRepository.ts b/src/twitter/getRepository.ts new file mode 100644 index 0000000..c2067c3 --- /dev/null +++ b/src/twitter/getRepository.ts @@ -0,0 +1,12 @@ +import { readPackageJson } from "github-secret-dotenv/lib/readPackageJson"; + +const readRepository = () => { + const [owner, repo] = (process.env.GITHUB_REPOSITORY ?? "").split("/"); + if (!repo) return undefined; + return { owner, repo }; +}; + +export const getRepository = () => ({ + ...readRepository(), + ...readPackageJson()!, +}); diff --git a/src/twitter/refresh.ts b/src/twitter/refresh.ts index 7c47d0d..6317469 100644 --- a/src/twitter/refresh.ts +++ b/src/twitter/refresh.ts @@ -1,8 +1,7 @@ -import * as path from "path"; import TwitterApi from "twitter-api-v2"; import { writeDotEnv } from "./writeDotEnv"; import { createSecretUpdater } from "github-secret-dotenv/lib/github"; -import { readPackageJson } from "github-secret-dotenv/lib/readPackageJson"; +import { getRepository } from "./getRepository"; import "dotenv/config"; /** @@ -29,7 +28,7 @@ import "dotenv/config"; console.log(data, { expiresIn }); } - writeDotEnv(path.join(__dirname, "../../.env"), { + writeDotEnv(".env", { TWITTER_ACCESS_TOKEN: accessToken, TWITTER_REFRESH_TOKEN: refreshToken!, }); @@ -37,7 +36,7 @@ import "dotenv/config"; console.log("written to .env"); const updateSecret = createSecretUpdater({ - ...readPackageJson(path.join(__dirname, "../.."))!, + ...getRepository()!, githubAccessToken: process.env.GITHUB_ACCESS_TOKEN ?? process.env.GITHUB_TOKEN!, });