From d3580a4ae4f05ee5b15990b04d3e44894492806d Mon Sep 17 00:00:00 2001 From: b Date: Wed, 17 Jul 2024 14:16:08 +0000 Subject: [PATCH] prebuilt-tdlib: change the package structure to use optionalDependencies (this is untested for now) --- .github/workflows/prebuilt-tdlib.yml | 37 +++---- packages/prebuilt-tdlib/.gitignore | 1 - packages/prebuilt-tdlib/LICENSE | 21 ---- packages/prebuilt-tdlib/LICENSE.md | 55 +++++++++++ packages/prebuilt-tdlib/README.md | 31 ++++-- packages/prebuilt-tdlib/index.d.ts | 3 +- packages/prebuilt-tdlib/index.js | 58 +++++++---- packages/prebuilt-tdlib/package.json | 10 +- packages/prebuilt-tdlib/prebuild-list.js | 51 ++++++++++ .../prebuild-template/LICENSE.md | 55 +++++++++++ .../prebuild-template/README.md | 3 + .../prebuild-template/package.json | 28 ++++++ packages/prebuilt-tdlib/publish.js | 99 +++++++++++++------ tests/types/flow.js | 2 +- tests/types/ts.ts | 2 +- 15 files changed, 351 insertions(+), 105 deletions(-) delete mode 100644 packages/prebuilt-tdlib/LICENSE create mode 100644 packages/prebuilt-tdlib/LICENSE.md create mode 100644 packages/prebuilt-tdlib/prebuild-list.js create mode 100644 packages/prebuilt-tdlib/prebuild-template/LICENSE.md create mode 100644 packages/prebuilt-tdlib/prebuild-template/README.md create mode 100644 packages/prebuilt-tdlib/prebuild-template/package.json diff --git a/.github/workflows/prebuilt-tdlib.yml b/.github/workflows/prebuilt-tdlib.yml index d74c800b..cd870fca 100644 --- a/.github/workflows/prebuilt-tdlib.yml +++ b/.github/workflows/prebuilt-tdlib.yml @@ -19,7 +19,7 @@ on: default: 'latest' # NOTE: The ZLIB_USE_STATIC_LIBS option requires CMake >= 3.24 jobs: - build-linux-x64: + build-linux-x86_64: name: 'Build TDLib / Linux x86_64 glibc' runs-on: ubuntu-22.04 steps: @@ -33,9 +33,9 @@ jobs: ./build-linux.sh ${{ inputs.tdlib }} x86_64-linux-gnu.2.22 - uses: actions/upload-artifact@v4 with: - name: tdlib-linux-x64 + name: tdlib-linux-x86_64-glibc path: ${{ env.TO_UPLOAD }} - build-macos-x64: + build-macos-x86_64: name: 'Build TDLib / macOS x86_64' runs-on: macos-13 steps: @@ -45,10 +45,11 @@ jobs: nix_path: nixpkgs=channel:nixpkgs-unstable - name: Build TDLib run: | - cd packages/prebuilt-tdlib/ci && ./build-macos.sh ${{ inputs.tdlib }} + cd packages/prebuilt-tdlib/ci + ./build-macos.sh ${{ inputs.tdlib }} - uses: actions/upload-artifact@v4 with: - name: temp-macos-x64 + name: temp-macos-x86_64 path: ${{ env.TO_UPLOAD }} build-macos-arm64: name: 'Build TDLib / macOS arm64' @@ -60,14 +61,15 @@ jobs: nix_path: nixpkgs=channel:nixpkgs-unstable - name: Build TDLib run: | - cd packages/prebuilt-tdlib/ci && ./build-macos.sh ${{ inputs.tdlib }} + cd packages/prebuilt-tdlib/ci + ./build-macos.sh ${{ inputs.tdlib }} - uses: actions/upload-artifact@v4 with: name: temp-macos-arm64 path: ${{ env.TO_UPLOAD }} build-macos: name: Create universal macOS shared library - needs: [build-macos-x64, build-macos-arm64] + needs: [build-macos-x86_64, build-macos-arm64] runs-on: macos-13 steps: - uses: actions/download-artifact@v4 @@ -76,20 +78,16 @@ jobs: - name: Combine shared libraries run: | mkdir -p to-upload - lipo temp-macos-x64/libtdjson.dylib temp-macos-arm64/libtdjson.dylib \ + lipo temp-macos-x86_64/libtdjson.dylib temp-macos-arm64/libtdjson.dylib \ -output to-upload/libtdjson.dylib -create cd to-upload file libtdjson.dylib otool -L libtdjson.dylib - # echo "--x86_64--" >> info.txt - # cat ../temp-macos-x64/libtdjson.dylib >> info.txt - # echo "--arm64--" >> info.txt - # cat ../temp-macos-arm64/libtdjson.dylib >> info.txt - uses: actions/upload-artifact@v4 with: name: tdlib-macos path: to-upload - build-windows-x64: + build-windows-x86_64: name: 'Build TDLib / Windows x86_64' runs-on: windows-2019 steps: @@ -120,30 +118,28 @@ jobs: cmake --build . --target tdjson --config Release --parallel 4 cd .. cp build\Release\tdjson.dll to-upload\tdjson.dll - # It should be possible to print the dependencies using dumpbin.exe - vcpkg list | Select-String openssl,zlib > to-upload\info.txt - cat to-upload\info.txt + vcpkg list | Select-String openssl,zlib - uses: actions/upload-artifact@v4 with: - name: tdlib-windows-x64 + name: tdlib-windows-x86_64 path: to-upload test: name: 'Test / ${{ matrix.v.bin }} / ${{ matrix.v.os }}' - needs: [build-linux-x64, build-macos, build-windows-x64] + needs: [build-linux-x86_64, build-macos, build-windows-x86_64] runs-on: ${{ matrix.v.os }} strategy: fail-fast: false matrix: v: - os: ubuntu-latest - bin: tdlib-linux-x64 + bin: tdlib-linux-x86_64-glibc - os: macos-13 bin: tdlib-macos - os: macos-14 bin: tdlib-macos - os: windows-latest - bin: tdlib-windows-x64 + bin: tdlib-windows-x86_64 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 @@ -184,7 +180,6 @@ jobs: path: packages/prebuilt-tdlib/prebuilds - run: tree packages/prebuilt-tdlib - run: du -hsc packages/prebuilt-tdlib/prebuilds/* - - run: npm run test:integration env: PREBUILT_PATH: packages/prebuilt-tdlib - run: | diff --git a/packages/prebuilt-tdlib/.gitignore b/packages/prebuilt-tdlib/.gitignore index 50d12e54..d5c0b7f3 100644 --- a/packages/prebuilt-tdlib/.gitignore +++ b/packages/prebuilt-tdlib/.gitignore @@ -1,3 +1,2 @@ prebuilds/tdlib-* -prebuilds/*.json to-upload/ diff --git a/packages/prebuilt-tdlib/LICENSE b/packages/prebuilt-tdlib/LICENSE deleted file mode 100644 index 9332ada8..00000000 --- a/packages/prebuilt-tdlib/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2022-2024 https://github.com/Bannerets - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/packages/prebuilt-tdlib/LICENSE.md b/packages/prebuilt-tdlib/LICENSE.md new file mode 100644 index 00000000..c5402b95 --- /dev/null +++ b/packages/prebuilt-tdlib/LICENSE.md @@ -0,0 +1,55 @@ +# Blue Oak Model License + +Version 1.0.0 + +## Purpose + +This license gives everyone as much permission to work with +this software as possible, while protecting contributors +from liability. + +## Acceptance + +In order to receive this license, you must agree to its +rules. The rules of this license are both obligations +under that agreement and conditions to your license. +You must not do anything with this software that triggers +a rule that you cannot or will not follow. + +## Copyright + +Each contributor licenses you to do everything with this +software that would otherwise infringe that contributor's +copyright in it. + +## Notices + +You must ensure that everyone who gets a copy of +any part of this software from you, with or without +changes, also gets the text of this license or a link to +. + +## Excuse + +If anyone notifies you in writing that you have not +complied with [Notices](#notices), you can keep your +license by taking all practical steps to comply within 30 +days after the notice. If you do not do so, your license +ends immediately. + +## Patent + +Each contributor licenses you to do everything with this +software that would otherwise infringe any patent claims +they can license or become able to license. + +## Reliability + +No contributor can revoke this license. + +## No Liability + +***As far as the law allows, this software comes as is, +without any warranty or condition, and no contributor +will be liable to anyone for any damages related to this +software or this license, under any kind of legal claim.*** diff --git a/packages/prebuilt-tdlib/README.md b/packages/prebuilt-tdlib/README.md index 82285dca..cfd68098 100644 --- a/packages/prebuilt-tdlib/README.md +++ b/packages/prebuilt-tdlib/README.md @@ -1,13 +1,15 @@ # Prebuilt TDLib This package distributes pre-built [TDLib][] shared libraries through npm. -The libraries are built on GitHub Actions: [prebuilt-tdlib.yml][]. +The libraries are built on GitHub Actions ([prebuilt-tdlib.yml][]) and published +using [npm publish --provenance][provenance]. [TDLib]: https://github.com/tdlib/td [prebuilt-tdlib.yml]: ../../.github/workflows/prebuilt-tdlib.yml +[provenance]: https://docs.npmjs.com/generating-provenance-statements -The shared libraries are statically linked against OpenSSL and zlib to prevent -compatibility issues in Node.js. +The shared libraries are statically linked against OpenSSL and zlib, for one, to +prevent compatibility issues in Node.js. Supported systems: - Linux x86_64 (requires glibc >= 2.22) @@ -32,7 +34,8 @@ $ npm install prebuilt-tdlib@td-1.8.30 `$ npm info prebuilt-tdlib dist-tags` to get the list of available versions. The TDLib version is important: there is no backward compatibility and the -interface you use may significantly change after an update. +interface you use may significantly change after an update. It is, though, +recommended to use the latest TDLib version. ## Usage @@ -111,7 +114,6 @@ An incomplete list is available below (mostly exceptions or "notable" versions): | npm tag | notes | | ------- | ----- | | [![npm](https://img.shields.io/npm/v/prebuilt-tdlib/td-1.8.26.svg)](https://www.npmjs.com/package/prebuilt-tdlib/v/td-1.8.26) | [tdlib [b1b33cf42790ca10ef34abc2ac8828ae704f1f56](https://github.com/tdlib/td/commit/b1b33cf42790ca10ef34abc2ac8828ae704f1f56)] | -| [![npm](https://img.shields.io/npm/v/prebuilt-tdlib/td-1.8.19.svg)](https://www.npmjs.com/package/prebuilt-tdlib/v/td-1.8.19) | | | [![npm](https://img.shields.io/npm/v/prebuilt-tdlib/td-1.8.14.svg)](https://www.npmjs.com/package/prebuilt-tdlib/v/td-1.8.14) | [tdlib [66234ae2537a99ec0eaf7b0857245a6e5c2d2bc9](https://github.com/tdlib/td/commit/66234ae2537a99ec0eaf7b0857245a6e5c2d2bc9)] | | [![npm](https://img.shields.io/npm/v/prebuilt-tdlib/td-1.8.12.svg)](https://www.npmjs.com/package/prebuilt-tdlib/v/td-1.8.12) | [tdlib [70bee089d492437ce931aa78446d89af3da182fc](https://github.com/tdlib/td/commit/70bee089d492437ce931aa78446d89af3da182fc)] | | [![npm](https://img.shields.io/npm/v/prebuilt-tdlib/td-1.8.7.svg)](https://www.npmjs.com/package/prebuilt-tdlib/v/td-1.8.7) | [tdlib [de5379f00b6af7686f197037ca3b494e6277e523](https://github.com/tdlib/td/commit/de5379f00b6af7686f197037ca3b494e6277e523)] | @@ -123,10 +125,21 @@ Changes to the building process of `prebuilt-tdlib` are noted below. ### (unreleased) -The building process of Linux and macOS builds is significantly changed. - -- on macOS, TDLib is built using macOS SDK from nixpkgs, and the minimal - supported macOS version is now 10.12 instead of 10.14. +First published as ``. + +The building process is significantly changed in this update. + +- Changed the structure of the package: instead of packing all binaries into the + prebuilt-tdlib package, every binary is split into a separate package, and all + the packages are specified in `optionalDependencies` of `prebuilt-tdlib`. The + same approach is used by, e.g., esbuild and swc. This installs a binary for + the user's system only, allowing `prebuilt-tdlib` to potentially scale for + more architectures and libc variants. One downside is that `node_modules` + can't simply be copied to a different platform anymore. +- On macOS, TDLib is built using macOS SDK from nixpkgs, and the minimal + supported macOS version is now 10.12 instead of 10.14. The arm64 macOS + library is now tested in the CI using the macos-14 GitHub runner (and not + crosscompiled anymore). - On Linux, TDLib is now built using zig. The minimal glibc version is 2.22 instead of 2.17. diff --git a/packages/prebuilt-tdlib/index.d.ts b/packages/prebuilt-tdlib/index.d.ts index 269865f9..64f6d5bc 100644 --- a/packages/prebuilt-tdlib/index.d.ts +++ b/packages/prebuilt-tdlib/index.d.ts @@ -1,5 +1,6 @@ +/** Currently present for forward compatibility only. */ export type Options = { - libc: 'glibc' + readonly forceLibc?: 'glibc' | 'musl' } export declare function getTdjson(options?: Options): string diff --git a/packages/prebuilt-tdlib/index.js b/packages/prebuilt-tdlib/index.js index 29d266dd..74006825 100644 --- a/packages/prebuilt-tdlib/index.js +++ b/packages/prebuilt-tdlib/index.js @@ -1,31 +1,53 @@ // @flow const path = require('path') +const fs = require('fs') +const { prebuilds } = require('./prebuild-list') + +const SCOPE = '@prebuilt-tdlib' -// For now, the `Options` object is present for forward compatibility. -// TODO: Add an option like "fallback"? /*:: export type Options = { - // Can be 'glibc' | 'musl' in the future - libc: 'glibc' + +forceLibc?: 'glibc' | 'musl' } */ -function prebuild (pathcomps/*: string[] */) { - return path.resolve(__dirname, 'prebuilds', ...pathcomps) -} +let libcNameCache = null -// eslint-disable-next-line no-unused-vars -function getTdjson (options/*:: ?: Options */)/*: string */ { - const platform = process.platform + '-' + process.arch - switch (platform) { - case 'win32-x64': return prebuild(['tdlib-windows-x64', 'tdjson.dll']) - case 'darwin-x64': return prebuild(['tdlib-macos', 'libtdjson.dylib']) - case 'darwin-arm64': return prebuild(['tdlib-macos', 'libtdjson.dylib']) - case 'linux-x64': return prebuild(['tdlib-linux-x64', 'libtdjson.so']) - case 'android-x64': return prebuild(['tdlib-linux-x64', 'libtdjson.so']) - default: throw new Error(`The ${platform} platform is not supported`) +function detectLibc ()/*: 'glibc' | 'musl' */ { + // This function can return false results: it currently only checks for + // Alpine Linux as a heuristic (the same approach is used by node-gyp-build). + if (libcNameCache != null) return libcNameCache + let result = 'glibc' + try { + if (fs.existsSync('/etc/alpine-release')) + result = 'musl' + } catch (e) { + // Intentionally empty (defaults to 'glibc') } + libcNameCache = result + return result } -module.exports = { getTdjson } +exports.getTdjson = function getTdjson (options/*:: ?: Options */)/*: string */ { + let { platform, arch } = process + if (platform === 'android') platform = 'linux' + const libc = options?.forceLibc || (platform === 'linux' ? detectLibc() : null) + for (const prebuild of prebuilds) { + if (prebuild.requirements.os != null) + if (!prebuild.requirements.os.includes(platform)) continue + if (prebuild.requirements.cpu != null) + if (!prebuild.requirements.cpu.includes(arch)) continue + if (prebuild.libc != null && prebuild.libc !== libc) continue + // Found a prebuild for the current platform + const pkg = `${SCOPE}/${prebuild.packageName}/${prebuild.libfile}` + try { + return require.resolve(pkg) + } catch (e) { + throw new Error(`Could not load ${pkg} (are optionalDependencies installed?): ${e?.message}`) + } + } + let entirePlatform = `${platform}-${arch}` + if (libc != null) entirePlatform += '-' + libc + throw new Error(`The ${entirePlatform} platform is not supported`) +} diff --git a/packages/prebuilt-tdlib/package.json b/packages/prebuilt-tdlib/package.json index ef189ea6..d296d5e0 100644 --- a/packages/prebuilt-tdlib/package.json +++ b/packages/prebuilt-tdlib/package.json @@ -9,11 +9,13 @@ }, "files": [ "index.js", + "prebuild-list.js", "index.d.ts", - "prebuilds" + "LICENSE.md" ], + "optionalDependencies": {}, "author": "Bannerets ", - "license": "MIT", + "license": "BlueOak-1.0.0", "keywords": [ "telegram", "telegram-api", @@ -28,5 +30,9 @@ "url": "https://github.com/Bannerets/tdl/issues" }, "homepage": "https://github.com/Bannerets/tdl/tree/main/packages/prebuilt-tdlib#readme", + "tdlib": { + "commit": "unknown", + "version": "unknown" + }, "private": true } diff --git a/packages/prebuilt-tdlib/prebuild-list.js b/packages/prebuilt-tdlib/prebuild-list.js new file mode 100644 index 00000000..e71e8188 --- /dev/null +++ b/packages/prebuilt-tdlib/prebuild-list.js @@ -0,0 +1,51 @@ +// @flow + +/*:: +export type PrebuildInfo = { + packageName: string, + prebuildDir: string, + libfile: string, + descr: string, + requirements: { os?: string[], cpu?: string[] }, + libc?: 'glibc' | 'musl' +} +*/ + +const SHARED_LINUX = 'libtdjson.so' +const SHARED_MACOS = 'libtdjson.dylib' +const SHARED_WINDOWS = 'tdjson.dll' + +const prebuilds/*: PrebuildInfo[] */ = [ + { + packageName: 'win32-x64', + prebuildDir: 'tdlib-windows-x86_64', + libfile: SHARED_WINDOWS, + descr: 'Windows x86_64', + requirements: { + os: ['win32'], + cpu: ['x64'] + } + }, + { + packageName: 'darwin', + prebuildDir: 'tdlib-macos', + libfile: SHARED_MACOS, + descr: 'macOS (universal)', + requirements: { + os: ['darwin'] + } + }, + { + packageName: 'linux-x64-glibc', + prebuildDir: 'tdlib-linux-x86_64-glibc', + libfile: SHARED_LINUX, + descr: 'Linux x86_64 (glibc)', + requirements: { + os: ['linux'], + cpu: ['x64'] + }, + libc: 'glibc' + } +] + +exports.prebuilds = prebuilds diff --git a/packages/prebuilt-tdlib/prebuild-template/LICENSE.md b/packages/prebuilt-tdlib/prebuild-template/LICENSE.md new file mode 100644 index 00000000..c5402b95 --- /dev/null +++ b/packages/prebuilt-tdlib/prebuild-template/LICENSE.md @@ -0,0 +1,55 @@ +# Blue Oak Model License + +Version 1.0.0 + +## Purpose + +This license gives everyone as much permission to work with +this software as possible, while protecting contributors +from liability. + +## Acceptance + +In order to receive this license, you must agree to its +rules. The rules of this license are both obligations +under that agreement and conditions to your license. +You must not do anything with this software that triggers +a rule that you cannot or will not follow. + +## Copyright + +Each contributor licenses you to do everything with this +software that would otherwise infringe that contributor's +copyright in it. + +## Notices + +You must ensure that everyone who gets a copy of +any part of this software from you, with or without +changes, also gets the text of this license or a link to +. + +## Excuse + +If anyone notifies you in writing that you have not +complied with [Notices](#notices), you can keep your +license by taking all practical steps to comply within 30 +days after the notice. If you do not do so, your license +ends immediately. + +## Patent + +Each contributor licenses you to do everything with this +software that would otherwise infringe any patent claims +they can license or become able to license. + +## Reliability + +No contributor can revoke this license. + +## No Liability + +***As far as the law allows, this software comes as is, +without any warranty or condition, and no contributor +will be liable to anyone for any damages related to this +software or this license, under any kind of legal claim.*** diff --git a/packages/prebuilt-tdlib/prebuild-template/README.md b/packages/prebuilt-tdlib/prebuild-template/README.md new file mode 100644 index 00000000..f57bee37 --- /dev/null +++ b/packages/prebuilt-tdlib/prebuild-template/README.md @@ -0,0 +1,3 @@ +This distribues a pre-built TDLib shared library that is part of the [prebuilt-tdlib][] package. + +[prebuilt-tdlib]: https://npmjs.com/package/prebuilt-tdlib diff --git a/packages/prebuilt-tdlib/prebuild-template/package.json b/packages/prebuilt-tdlib/prebuild-template/package.json new file mode 100644 index 00000000..ee5f6743 --- /dev/null +++ b/packages/prebuilt-tdlib/prebuild-template/package.json @@ -0,0 +1,28 @@ +{ + "name": "@prebuilt-tdlib/dev", + "version": "0.0.0", + "description": "Pre-built TDLib shared library", + "scripts": { + "build": "" + }, + "files": [ + "LICENSE.md", + "README.md" + ], + "preferUnplugged": true, + "license": "BlueOak-1.0.0", + "keywords": [ + "tdlib", + "prebuilt-tdlib" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/Bannerets/tdl.git" + }, + "homepage": "https://github.com/Bannerets/tdl/tree/main/packages/prebuilt-tdlib#readme", + "tdlib": { + "commit": "unknown", + "version": "unknown" + }, + "private": true +} diff --git a/packages/prebuilt-tdlib/publish.js b/packages/prebuilt-tdlib/publish.js index f3b2d7e8..f2ef766e 100755 --- a/packages/prebuilt-tdlib/publish.js +++ b/packages/prebuilt-tdlib/publish.js @@ -4,8 +4,11 @@ const path = require('path') const fs = require('fs') const { execSync } = require('child_process') +const { prebuilds } = require('./prebuild-list') +/*:: import type { PrebuildInfo } from './prebuild-list' */ -const packageName = 'prebuilt-tdlib' +const MAIN_PACKAGE_NAME = 'prebuilt-tdlib' +const SCOPE = '@prebuilt-tdlib' const tdlibCommit = process.env.TDLIB_COMMIT_HASH let tdlibVersion = process.env.TDLIB_VERSION @@ -15,7 +18,7 @@ const patchVersion = process.argv[2] if (!tdlibCommit) throw new Error('Expected TDLIB_COMMIT_HASH') if (!tdlibVersion) throw new Error('Expected TDLIB_VERSION') if (!npmTag) throw new Error('Expected NPM_TAG') -if (!patchVersion) throw new Error('Expected the patch version') +if (!patchVersion) throw new Error('Expected the patch version argument') if (Number.isNaN(Number(patchVersion))) throw new Error(`Incorrect patch version: ${patchVersion}`) @@ -34,56 +37,92 @@ tdlibPatch = tdlibPatch.padStart(3, '0') const npmVersion = `0.${tdlibMajor}${tdlibMinor}${tdlibPatch}.${patchVersion}` -console.log(`Preparing to publish ${npmVersion}`) +const prebuildPackageJson = require('./prebuild-template/package.json') -const packageJson = require('./package.json') - -delete packageJson.private -packageJson.name = packageName -packageJson.version = npmVersion -packageJson.tdlib = { +delete prebuildPackageJson.private +prebuildPackageJson.version = npmVersion +prebuildPackageJson.tdlib = { commit: tdlibCommit, version: tdlibVersion } -const tdlibJson = - { commit: tdlibCommit, version: tdlibVersion, ref: tdlibCommit } +const prebuildTemplateDir = path.join(__dirname, 'prebuild-template') -fs.writeFileSync( - path.join(__dirname, 'prebuilds', 'tdlib.json'), - JSON.stringify(tdlibJson) + '\n' -) +function publishPrebuild (info/*: PrebuildInfo */) { + console.log(`Preparing to publish ${SCOPE}/${info.packageName}@${npmVersion}`) -fs.writeFileSync( - path.join(__dirname, 'package.json'), - JSON.stringify(packageJson, null, ' ') + '\n' -) + // Writing package.json + const pkg = { ...prebuildPackageJson } + pkg.name = SCOPE + '/' + info.packageName + pkg.description += ' for ' + info.descr + pkg.files = [info.libfile, 'LICENSE.md', 'README.md'] + for (const [k, v] of Object.entries(info.requirements)) + pkg[k] = v + fs.writeFileSync( + path.join(prebuildTemplateDir, 'package.json'), + JSON.stringify(pkg, null, ' ') + '\n' + ) -// Check + const lib = path.join(__dirname, 'prebuilds', info.prebuildDir, info.libfile) -function checkExists (pathparts/*: string[] */) { - const p = path.join(__dirname, 'prebuilds', ...pathparts) - if (fs.existsSync(p)) return - throw new Error(`'${p}' does not exist`) + if (!fs.existsSync(lib)) + throw new Error(`'${lib}' does not exist`) + + fs.copyFileSync(lib, path.join('.', info.libfile)) + + const files = fs.readdirSync(prebuildTemplateDir) + + if (files.length < 4 || !files.includes(info.libfile)) + throw new Error(`No shared library found (files: ${files.join()})`) + + execSync('npm publish --provenance --access public', { stdio: 'inherit' }) + + fs.rmSync(info.libfile) } -checkExists(['tdlib-linux-x64', 'libtdjson.so']) -checkExists(['tdlib-macos', 'libtdjson.dylib']) -checkExists(['tdlib-windows-x64', 'tdjson.dll']) +const prebuildCount = fs.readdirSync(path.join(__dirname, 'prebuilds')) + .filter(name => name.startsWith('tdlib-')) + .length -checkExists(['tdlib.json']) +if (prebuildCount < prebuilds.length) + throw new Error(`Expected ${prebuilds.length} prebuilds, found ${prebuildCount}`) // Publish const oldCwd = process.cwd() +process.chdir(prebuildTemplateDir) + +for (const prebuild of prebuilds) + publishPrebuild(prebuild) + +console.log(`Preparing to publish ${MAIN_PACKAGE_NAME}@${npmVersion}`) + +const mainPackageJson = require('./package.json') + +delete mainPackageJson.private +mainPackageJson.name = MAIN_PACKAGE_NAME +mainPackageJson.version = npmVersion +mainPackageJson.tdlib = { + commit: tdlibCommit, + version: tdlibVersion +} +mainPackageJson.optionalDependencies = prebuilds.reduce((obj, info) => { + return { ...obj, [SCOPE + '/' + info.packageName]: npmVersion } +}, {}) + +fs.writeFileSync( + path.join(__dirname, 'package.json'), + JSON.stringify(mainPackageJson, null, ' ') + '\n' +) + process.chdir(__dirname) const publishCommand = `npm publish --provenance --access public --tag ${npmTag}` const addTagCommand = - `npm dist-tag add ${packageName}@${npmVersion} td-${tdlibVersion}` + `npm dist-tag add ${MAIN_PACKAGE_NAME}@${npmVersion} td-${tdlibVersion}` execSync(publishCommand, { stdio: 'inherit' }) execSync(addTagCommand, { stdio: 'inherit' }) @@ -91,5 +130,5 @@ execSync(addTagCommand, { stdio: 'inherit' }) try { process.chdir(oldCwd) } catch (e) { - console.error(`Note: failed to chdir to ${oldCwd}: ${e && e.message}`) + console.error(`Note: failed to chdir to ${oldCwd}: ${e?.message}`) } diff --git a/tests/types/flow.js b/tests/types/flow.js index e0e17de7..98da9f41 100644 --- a/tests/types/flow.js +++ b/tests/types/flow.js @@ -14,7 +14,7 @@ tdl.configure({ tdjson: getTdjson() }) tdl.configure({ receiveTimeout: 10 }) -getTdjson({ libc: 'glibc' }) +getTdjson({ forceLibc: 'glibc' }) const client = tdl.createClient({ apiId: 2, diff --git a/tests/types/ts.ts b/tests/types/ts.ts index 589e8e91..4a2f719f 100644 --- a/tests/types/ts.ts +++ b/tests/types/ts.ts @@ -10,7 +10,7 @@ tdl.configure({ libdir: __dirname }) tdl.configure({ libdir: '' }) tdl.configure({ tdjson: getTdjson() }) -getTdjson({ libc: 'glibc' }) +getTdjson({ forceLibc: 'glibc' }) const client = tdl.createClient({ apiId: 2,