From 2d5c10852db6283668fc781dfc77566c20df7330 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Sun, 29 Oct 2017 10:15:38 +0800 Subject: [PATCH] use read-pkg-up module to get version from package.json --- index.ts | 3 +-- package.json | 4 +++- src/config.ts | 23 ++++++++--------------- src/wechaty.ts | 5 +++-- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/index.ts b/index.ts index f9e12d017..117135958 100644 --- a/index.ts +++ b/index.ts @@ -2,6 +2,7 @@ export { config, log, Sayable, + VERSION, } from './src/config' export { Contact } from './src/contact' @@ -24,8 +25,6 @@ export { PuppetWeb } from './src/puppet-web/' export { Room } from './src/room' export { Misc } from './src/misc' -export const VERSION = require('./package.json').version - import Wechaty from './src/wechaty' export { Wechaty } export default Wechaty diff --git a/package.json b/package.json index 2302c0074..75db13e5a 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ }, "scripts": { "ts-node": "ts-node", - "dist": "npm run clean && tsc && jq \"del (.files)\" < package.json > dist/package.json && shx cp src/puppet-web/*.js dist/src/puppet-web/", + "dist": "npm run clean && tsc && shx cp src/puppet-web/*.js dist/src/puppet-web/", "doc": "npm run dist && echo '# Wechaty v'$(jq -r .version package.json)' Documentation\n' > docs/index.md && jsdoc2md dist/src/{wechaty,room,contact,friend-request,message}.js dist/src/puppet-web/friend-request.js>> docs/index.md", "coverage": "nyc report --reporter=text-lcov | coveralls", "changelog": "github_changelog_generator -u chatie -p wechaty && sed -i'.bak' /greenkeeper/d CHANGELOG.md && ts-node script/sort-contributiveness.ts < CHANGELOG.md > CHANGELOG.new.md 2>/dev/null && cat CHANGELOG.md >> CHANGELOG.new.md && mv CHANGELOG.new.md CHANGELOG.md", @@ -96,6 +96,7 @@ "mime": "^2.0.3", "puppeteer": "^0.12.0", "raven": "^2.2.1", + "read-pkg-up": "^2.0.0", "request": "^2.83.0", "retry-promise": "^1.0.0", "rxjs": "^5.4.3", @@ -113,6 +114,7 @@ "@types/mime": "^2.0.0", "@types/puppeteer": "^0.12.0", "@types/raven": "^2.1.2", + "@types/read-pkg-up": "^2.0.0", "@types/request": "^2.0.4", "@types/sinon": "^2.3.5", "@types/xml2js": "^0.4.0", diff --git a/src/config.ts b/src/config.ts index 5bf2d6b37..650f6b785 100644 --- a/src/config.ts +++ b/src/config.ts @@ -20,6 +20,11 @@ import * as fs from 'fs' import * as os from 'os' import * as path from 'path' +import * as readPkgUp from 'read-pkg-up' + +const pkg = readPkgUp.sync().pkg +export const VERSION = pkg.version + /** * Raven.io */ @@ -31,7 +36,7 @@ Raven process.env.NODE_ENV === 'production' && 'https://f6770399ee65459a82af82650231b22c:d8d11b283deb441e807079b8bb2c45cd@sentry.io/179672', { - release: require('../package.json').version, + release: VERSION, tags: { git_commit: 'c0deb10c4', platform: !!process.env['WECHATY_DOCKER'] @@ -113,14 +118,13 @@ export interface ConfigSetting { puppetInstance(instance: Puppet): void puppetInstance(instance?: Puppet | null): Puppet | void, - gitVersion(): string | null, - npmVersion(): string, + gitRevision(): string | null, docker: boolean, } /* tslint:disable:variable-name */ /* tslint:disable:no-var-requires */ -export const config: ConfigSetting = require('../package.json').wechaty +export const config: ConfigSetting = pkg.wechaty /** * 1. ENVIRONMENT VARIABLES + PACKAGES.JSON (default) @@ -236,19 +240,8 @@ function gitVersion(): string | null { } } -function npmVersion(): string { - try { - return require('../package.json').version - } catch (e) { - log.error('Wechaty', 'npmVersion() exception %s', e.message) - Raven.captureException(e) - return '0.0.0' - } -} - Object.assign(config, { gitVersion, - npmVersion, puppetInstance, }) diff --git a/src/wechaty.ts b/src/wechaty.ts index c1a6c5e6d..e50a66387 100644 --- a/src/wechaty.ts +++ b/src/wechaty.ts @@ -32,6 +32,7 @@ import { Raven, Sayable, log, + VERSION, } from './config' import Contact from './contact' @@ -153,12 +154,12 @@ export class Wechaty extends EventEmitter implements Sayable { */ public static version(forceNpm = false): string { if (!forceNpm) { - const revision = config.gitVersion() + const revision = config.gitRevision() if (revision) { return `#git[${revision}]` } } - return config.npmVersion() + return VERSION } /** -- GitLab