diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 263feadf..01ee50c2 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -75,3 +75,11 @@ jobs: API_ID: ${{ secrets.TELEGRAM_API_ID }} API_HASH: ${{ secrets.TELEGRAM_API_HASH }} run: cd e2e && ./cli.sh ci + - name: Publish to canary NPM + if: github.repository == 'mtcute/mtcute' # do not run on forks + continue-on-error: true + env: + NPM_TOKEN: ${{ secrets.CANARY_NPM_TOKEN }} + E2E: 1 + REGISTRY: 'https://npm.tei.su' + run: cd e2e && node publish-canary.js diff --git a/e2e/publish-canary.js b/e2e/publish-canary.js new file mode 100644 index 00000000..e4de2a7a --- /dev/null +++ b/e2e/publish-canary.js @@ -0,0 +1,51 @@ +// this scripts publishes our e2e-tested builds to canary npm +// at this point, we should have all our packages installed in node_modules +// so it should be safe to just cd into them and run `npm publish` on them + +const fs = require('fs') +const path = require('path') +const { execSync } = require('child_process') + +const { REGISTRY } = require('../scripts/publish.js') +const { getCurrentCommit } = require('../scripts/git-utils.js') + +// setup token +const { NPM_TOKEN } = process.env + +if (!NPM_TOKEN) { + console.error('NPM_TOKEN is not set!') + process.exit(1) +} + +execSync(`npm config set //${REGISTRY.replace(/^https?:\/\//, '')}/:_authToken ${NPM_TOKEN}`, { stdio: 'inherit' }) + +const nodeModulesDir = path.join(__dirname, 'node_modules') +const mtcuteDir = path.join(nodeModulesDir, '@mtcute') + +const commit = getCurrentCommit().slice(0, 7) + +for (const pkg of fs.readdirSync(mtcuteDir)) { + const pkgJsonPath = path.join(mtcuteDir, pkg, 'package.json') + const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8')) + const version = `${pkgJson.version}-git.${commit}` + + fs.writeFileSync( + pkgJsonPath, + JSON.stringify( + { + ...pkgJson, + version, + }, + null, + 4, + ), + ) + + execSync(`npm publish --registry ${REGISTRY} -q --tag canary`, { + cwd: path.join(mtcuteDir, pkg), + stdio: 'inherit', + }) + + // restore package.json just in case + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4)) +} diff --git a/scripts/git-utils.js b/scripts/git-utils.js index a82b8115..3b902ee4 100644 --- a/scripts/git-utils.js +++ b/scripts/git-utils.js @@ -1,5 +1,9 @@ const cp = require('child_process') +function getCurrentCommit() { + return cp.execSync('git rev-parse HEAD', { encoding: 'utf8', stdio: 'pipe' }).trim() +} + function getLatestTag() { try { const res = cp.execSync('git describe --abbrev=0 --tags', { encoding: 'utf8', stdio: 'pipe' }).trim() @@ -64,4 +68,5 @@ module.exports = { findChangedFilesSince, getCommitsSince, parseConventionalCommit, + getCurrentCommit, }