Skip to content

Commit

Permalink
fix(sdk): correct way to retrieve client/sdk version (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 authored Feb 3, 2023
1 parent 2f5f60e commit 381eff4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
7 changes: 4 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
"bin": {
"sentio": "./lib/cli.js"
},
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"module": "./lib/index.js",
"types": "module",
"exports": {
".": "./lib/index.js"
},
"files": [
"{lib,src,templates}",
"!{lib,src}/tests",
Expand Down
14 changes: 1 addition & 13 deletions packages/cli/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,7 @@ import path from 'path'
import fs from 'fs'
import { exec } from 'child_process'
import * as process from 'process'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)

function getPackageRoot(pkgId: string): string {
const m = require.resolve(pkgId)

let dir = path.dirname(m)
while (!fs.existsSync(path.join(dir, 'package.json'))) {
dir = path.dirname(dir)
}

return dir
}
import { getPackageRoot } from './utils.js'

export async function buildProcessor(onlyGen: boolean) {
if (!onlyGen) {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
21 changes: 18 additions & 3 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import fs from 'fs-extra'
import path from 'path'
import { createRequire } from 'module'
import url from 'url'

const require = createRequire(import.meta.url)
const PACKAGE_JSON = 'package.json'

export function getPackageRoot(pkgId: string): string {
const m = require.resolve(pkgId)

let dir = path.dirname(m)
while (!fs.existsSync(path.join(dir, PACKAGE_JSON))) {
dir = path.dirname(dir)
}
return dir
}

export function getCliVersion() {
const packageJsonPath = path.resolve(__dirname, '../package.json')
const packageJsonPath = url.fileURLToPath(new URL('../' + PACKAGE_JSON, import.meta.url))
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf-8')
const packageJson = JSON.parse(packageJsonContent)

Expand All @@ -11,8 +26,8 @@ export function getCliVersion() {

export function getSdkVersion() {
try {
const packageJsonPath = require.resolve('@sentio/sdk/package.json')
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf-8')
const packageJsonPath = getPackageRoot('@sentio/sdk')
const packageJsonContent = fs.readFileSync(path.join(packageJsonPath, PACKAGE_JSON), 'utf-8')
const packageJson = JSON.parse(packageJsonContent)
return packageJson.version
} catch (e) {
Expand Down

0 comments on commit 381eff4

Please sign in to comment.