mtcute/scripts/publish.js
Alina Tumanova f5976a2d74
ESM + end-to-end tests (#11)
* feat: moved tl-runtime to esm and native ArrayBuffers

* feat: migration to esm

* fix(core): web-related fixes

* test: finally, some good fucking e2e

* chore: fixed linters etc

* ci: added e2e to ci

* build(tl): fixed gen-code on node 20

* fix: codegen Uint8Array, not Buffer

never `git reset --hard` kids

* build: only do type-aware linting for `packages/*`

* build: ignore no-unresolved in ci for e2e

* fix: node 16 doesn't have subtle crypto apparently?

* fix(tests): use Uint8Array

for gods sake please can i just merge this already

* ci: don't parallel tasks in ci

because machines are utter garbage and it may just randomly break

* ci: pass secrets to e2e tests

* ci: separate cli command for ci

apparently im retarded

* fix: run codegen in e2e

im actually retarded

* ci: more fixes for e2e

* ci: debugging stuff

* ci: still debugging

* ci: hopefully fix ci???
2023-10-16 19:23:53 +03:00

111 lines
3 KiB
JavaScript

const fs = require('fs')
const path = require('path')
const cp = require('child_process')
// const REGISTRY = 'https://registry.npmjs.org'
const REGISTRY = process.env.REGISTRY || 'https://npm.tei.su/'
exports.REGISTRY = REGISTRY
async function checkVersion(name, version, retry = 0) {
return fetch(`${REGISTRY}@mtcute/${name}/${version}`)
.then((r) => r.status === 200)
.catch((err) => {
if (retry >= 5) throw err
// for whatever reason this request sometimes fails with ECONNRESET
// no idea why, probably some issue in orbstack networking
console.log('[i] Error checking version:')
console.log(err)
return new Promise((resolve) => setTimeout(resolve, 1000)).then(() =>
checkVersion(name, version, retry + 1),
)
})
}
async function publishSinglePackage(name) {
let packageDir = path.join(__dirname, '../packages', name)
console.log('[i] Building %s', name)
// run build script
cp.execSync('pnpm run build', {
cwd: packageDir,
stdio: 'inherit',
})
console.log('[i] Publishing %s', name)
if (process.env.E2E) {
const version = require(path.join(packageDir, 'dist/package.json')).version
const exists = await checkVersion(name, version)
if (exists) {
console.log('[i] %s already exists, unpublishing..', name)
cp.execSync(`npm unpublish --registry ${REGISTRY} --force @mtcute/${name}`, {
cwd: path.join(packageDir, 'dist'),
stdio: 'inherit',
})
}
}
// publish to npm
cp.execSync(`npm publish --registry ${REGISTRY} --force -q`, {
cwd: path.join(packageDir, 'dist'),
stdio: 'inherit',
})
}
const LOCAL = ['crypto']
function listPackages() {
const packages = []
for (const f of fs.readdirSync(path.join(__dirname, '../packages'))) {
if (LOCAL.indexOf(f) > -1) continue
if (f[0] === '.') continue
packages.push(f)
}
return packages
}
exports.listPackages = listPackages
async function main(arg = process.argv[2]) {
if (!arg) {
console.log('Usage: publish.js <package name | all | updated>')
process.exit(0)
}
console.log('[i] Using registry %s', REGISTRY)
if (arg === 'all' || arg === 'updated') {
for (const pkg of listPackages()) {
if (arg === 'updated') {
const pkgVersion = require(`../packages/${pkg}/package.json`).version
const published = await checkVersion(pkg, pkgVersion)
if (published) {
console.log('[i] %s is up to date', pkg)
continue
}
}
await publishSinglePackage(pkg)
}
} else {
await publishSinglePackage(arg)
}
}
exports.main = main
if (require.main === module) {
main().catch((e) => {
console.error(e)
process.exit(1)
})
}