diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000000000000000000000000000000000000..27bebd1191f44de7f77ae36e4ae172c1da3cec96 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,27 @@ +// { +// "rules": { +// "indent": [ +// "error", +// 4, +// { +// "CallExpression": { +// "arguments": "first" +// } +// } +// ] +// } +// } + +const rules = { + indent: ['error', 2, { + CallExpression: { + arguments: 'off', + }, + SwitchCase: 1, + }], +} + +module.exports = { + extends: '@chatie', + rules, +} diff --git a/.travis.yml b/.travis.yml index 2056f3c8cc0c08050d2b4fc3eb83b9dd721df5ca..a8a6b3168c4354ce4d1a61b6e3a59bb728efa477 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,7 +36,6 @@ script: - npm test # - if [ "$TRAVIS_OS_NAME" == 'linux' ]; then npm run coverage; fi - echo "Testing Finished" - - echo "TRAVIS_TEST_RESULT=$TRAVIS_TEST_RESULT" jobs: include: @@ -45,18 +44,18 @@ jobs: script: - echo "NPM Pack Testing Started ..." - npm version + - ./scripts/generate-version.sh - npm run test:pack - echo "NPM Pack Testing Finished." - - echo "TRAVIS_TEST_RESULT=$TRAVIS_TEST_RESULT" - stage: deploy script: - echo "NPM Deploying Started ..." - npm version - - if ./scripts/development-release.ts; then ./scripts/package-publish-config-tag-next.ts; fi + - ./scripts/generate-version.sh + - ./scripts/package-publish-config-tag.sh - npm run dist - echo "NPM Building Finished." - - echo "TRAVIS_TEST_RESULT=$TRAVIS_TEST_RESULT" deploy: provider: npm diff --git a/.vscode/settings.json b/.vscode/settings.json index 85e1f4c9462411a320d0477a2ad6cf3ddd30ffe0..5ba4c1699f53194627c7c4e6088893a304d77e83 100755 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -56,7 +56,7 @@ "alignment": { "operatorPadding": "right", "indentBase": "firstline", - "surroundSpace": {, + "surroundSpace": { "colon": [1, 1], // The first number specify how much space to add to the left, can be negative. The second number is how much space to the right, can be negative. "assignment": [1, 1], // The same as above. "arrow": [1, 1], // The same as above. @@ -66,4 +66,8 @@ }, "editor.formatOnSave": false, "python.pythonPath": "python3", + "eslint.validate": [ + "javascript", + "typescript", + ], } diff --git a/bin/io-client.ts b/bin/io-client.ts index e593dba4ed7dab48ded6d61362907fa508cecd22..a1279b01a3e7113868e50c2c831a5004e2f43f50 100644 --- a/bin/io-client.ts +++ b/bin/io-client.ts @@ -52,7 +52,7 @@ if (!token) { log.warn('Client', `set token to "${token}" for demo purpose`) } -console.log(welcome) +console.info(welcome) log.info('Client', 'Starting for WECHATY_TOKEN: %s', token) const client = new IoClient({ @@ -61,7 +61,7 @@ const client = new IoClient({ }) client.start() - .catch(onError.bind(client)) + .catch(onError.bind(client)) // client.initWeb() // .catch(onError.bind(client)) diff --git a/bin/puppet-install.ts b/bin/puppet-install.ts index a1915dbdb552ef0cbe758bfd30abfab483e1260f..23240ef20d9a97a86af0fba00f4709c9e34a5b5b 100755 --- a/bin/puppet-install.ts +++ b/bin/puppet-install.ts @@ -3,7 +3,7 @@ import { PuppetManager } from '../src/puppet-manager' PuppetManager.installAll() -.catch(e => { - console.error(e) - process.exit(1) -}) + .catch(e => { + console.error(e) + process.exit(1) + }) diff --git a/bin/version.ts b/bin/version.ts index 2656fa6880e7cbc5130ff47e27b23830283a42f0..6e610781fb64e5c6ee168948f03177b36e29deba 100644 --- a/bin/version.ts +++ b/bin/version.ts @@ -20,4 +20,4 @@ import { Wechaty } from '../src/wechaty' -console.log(Wechaty.version()) +console.info(Wechaty.version()) diff --git a/examples/ding-dong-bot.ts b/examples/ding-dong-bot.ts index 7c220f8811746fb8629e3331c294f761ca481be7..5e230bd8a9227c7e05e677257a11b28e3bdcad09 100644 --- a/examples/ding-dong-bot.ts +++ b/examples/ding-dong-bot.ts @@ -40,11 +40,11 @@ const bot = new Wechaty({ * */ bot -.on('logout', onLogout) -.on('login', onLogin) -.on('scan', onScan) -.on('error', onError) -.on('message', onMessage) + .on('logout', onLogout) + .on('login', onLogin) + .on('scan', onScan) + .on('error', onError) + .on('message', onMessage) /** * @@ -52,11 +52,11 @@ bot * */ bot.start() -.catch(async e => { - console.error('Bot start() fail:', e) - await bot.stop() - process.exit(-1) -}) + .catch(async e => { + console.error('Bot start() fail:', e) + await bot.stop() + process.exit(-1) + }) /** * @@ -80,16 +80,16 @@ function onScan (qrcode: string, status: number) { encodeURIComponent(qrcode), ].join('') - console.log(`[${status}] ${qrcodeImageUrl}\nScan QR Code above to log in: `) + console.info(`[${status}] ${qrcodeImageUrl}\nScan QR Code above to log in: `) } function onLogin (user: Contact) { - console.log(`${user.name()} login`) + console.info(`${user.name()} login`) bot.say('Wechaty login').catch(console.error) } function onLogout (user: Contact) { - console.log(`${user.name()} logouted`) + console.info(`${user.name()} logouted`) } function onError (e: Error) { @@ -108,10 +108,10 @@ function onError (e: Error) { * */ async function onMessage (msg: Message) { - console.log(msg.toString()) + console.info(msg.toString()) if (msg.age() > 60) { - console.log('Message discarded because its TOO OLD(than 1 minute)') + console.info('Message discarded because its TOO OLD(than 1 minute)') return } @@ -119,7 +119,7 @@ async function onMessage (msg: Message) { || !/^(ding|ping|bing|code)$/i.test(msg.text()) /*&& !msg.self()*/ ) { - console.log('Message discarded because it does not match ding/ping/bing/code') + console.info('Message discarded because it does not match ding/ping/bing/code') return } @@ -127,7 +127,7 @@ async function onMessage (msg: Message) { * 1. reply 'dong' */ await msg.say('dong') - console.log('REPLY: dong') + console.info('REPLY: dong') /** * 2. reply image(qrcode image) @@ -135,7 +135,7 @@ async function onMessage (msg: Message) { const fileBox = FileBox.fromUrl('https://chatie.io/wechaty/images/bot-qr-code.png') await msg.say(fileBox) - console.log('REPLY: %s', fileBox.toString()) + console.info('REPLY: %s', fileBox.toString()) /** * 3. reply 'scan now!' @@ -175,4 +175,4 @@ upgrade me to more superpowers! Please wait... I'm trying to login in... ` -console.log(welcome) +console.info(welcome) diff --git a/package.json b/package.json index 40f93e8d7a3780e593a4972904b045df13bd8b24..eed6126d752daddbcfdf9b0e1fae6af4762aa857 100644 --- a/package.json +++ b/package.json @@ -21,9 +21,10 @@ "changelog": "github_changelog_generator -u chatie -p wechaty && sed -i'.bak' /greenkeeper/d CHANGELOG.md && sed -i'.bak' '/An in-range update of/d' CHANGELOG.md && ts-node scripts/sort-contributiveness.ts < CHANGELOG.md > CHANGELOG.new.md 2>/dev/null && cat CHANGELOG.md >> CHANGELOG.new.md && mv CHANGELOG.new.md CHANGELOG.md", "doctor": "npm run check-node-version && ts-node bin/doctor", "check-node-version": "check-node-version --node \">= 10\"", - "lint": "npm run check-node-version && npm run lint:ts && npm run lint:sh", + "lint": "npm run check-node-version && npm run lint:es && npm run lint:ts && npm run lint:sh", + "lint:es": "eslint --ignore-pattern fixtures/ src/**/*.ts tests/**/*.ts scripts/**/*.ts examples/**/*.ts bin/**/*.ts", "lint:md": "markdownlint README.md", - "lint:ts": "tslint --project tsconfig.json && tsc --noEmit", + "lint:ts": "tsc --noEmit", "lint:sh": "bash -n bin/*.sh", "puppet-install": "ts-node bin/puppet-install.ts", "sloc": "sloc bin examples scripts src tests --details --format cli-table --keys total,source,comment && sloc bin examples scripts src tests", @@ -93,16 +94,16 @@ "qr-image": "^3.2.0", "raven": "^2.6.2", "read-pkg-up": "^6.0.0", + "semver": "^6.0.0", "state-switch": "^0.6.2", "watchdog": "^0.8.1", "wechaty-puppet": "^0.15.9", "ws": "^7.0.0" }, "devDependencies": { - "@babel/core": "^7.0.1", - "@babel/node": "^7.0.0", - "@babel/preset-env": "^7.0.0", - "@chatie/tsconfig": "^0.4.2", + "@chatie/eslint-config": "^0.6.5", + "@chatie/git-scripts": "^0.2.1", + "@chatie/tsconfig": "^0.4.5", "@types/blessed": "^0.1.10", "@types/blue-tape": "^0.1.0", "@types/cuid": "^1.3.0", @@ -120,7 +121,6 @@ "@types/semver": "^6.0.0", "@types/sinon": "^7.0.4", "@types/ws": "^6.0.0", - "@wwwouter/tslint-contrib": "^1.0.0", "apiai": "^4.0.0", "blessed": "^0.1.81", "blessed-contrib": "^4.8.5", @@ -130,7 +130,6 @@ "cross-env": "^5.1.6", "finis": "^0.4.1", "fluent-ffmpeg": "^2.1.0", - "git-scripts": "git+https://github.com/nkzawa/git-scripts.git", "gl-matrix": "^3.0.0", "glob": "^7.1.0", "is-pr": "^1.0.0", @@ -139,15 +138,11 @@ "nyc": "^14.0.0", "qrcode-terminal": "^0.12.0", "request": "^2.87.0", - "semver": "^6.0.0", "shx": "^0.3.0", "sinon": "^7.2.3", "sloc": "^0.2.0", - "ts-node": "^8.0.2", - "tslint": "^5.11.0", - "tslint-config-standard": "^8.0.0", + "tstest": "^0.2.4", "typedoc": "^0.14.0", - "typescript": "^3.4.5", "wechaty-puppet-mock": "^0.15.5" }, "files_comment__whitelist_npm_publish": "http://stackoverflow.com/a/8617868/1123955", @@ -162,17 +157,17 @@ "dist/src", "src" ], - "git": { - "scripts": { - "pre-push": "./scripts/pre-push.sh" - } - }, "publishConfig": { "access": "public", - "tag": "latest" + "tag": "next" }, "collective": { "type": "opencollective", "url": "https://opencollective.com/wechaty" + }, + "git": { + "scripts": { + "pre-push": "npx git-scripts-pre-push" + } } } diff --git a/scripts/generate-version.sh b/scripts/generate-version.sh new file mode 100755 index 0000000000000000000000000000000000000000..e8ef7f99e2701fdbe4c85af33dd63e7adb879548 --- /dev/null +++ b/scripts/generate-version.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -e + +SRC_VERSION_TS_FILE='src/version.ts' + +[ -f ${SRC_VERSION_TS_FILE} ] || { + echo ${SRC_VERSION_TS_FILE}" not found" + exit 1 +} + +VERSION=$(npx pkg-jq -r .version) + +cat <<_SRC_ > ${SRC_VERSION_TS_FILE} +/** + * This file was auto generated from scripts/generate-version.sh + */ +export const VERSION: string = '${VERSION}' +_SRC_ diff --git a/scripts/package-publish-config-tag.sh b/scripts/package-publish-config-tag.sh new file mode 100755 index 0000000000000000000000000000000000000000..01872833155dd13ea8413f16bbbfd001175e9654 --- /dev/null +++ b/scripts/package-publish-config-tag.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e + +VERSION=$(npx pkg-jq -r .version) + +if npx --package @chatie/semver semver-is-prod $VERSION; then + npx pkg-jq -i '.publishConfig.tag="latest"' + echo "production release: publicConfig.tag set to latest." +else + npx pkg-jq -i '.publishConfig.tag="next"' + echo 'development release: publicConfig.tag set to next.' +fi + diff --git a/scripts/update-license.ts b/scripts/update-license.ts index c86c746c00aa7fb3f4912710840c27e754225b9d..d1ee6ce8fcaeb2a1cce81c37ee0aa66138c6286c 100755 --- a/scripts/update-license.ts +++ b/scripts/update-license.ts @@ -1,5 +1,20 @@ #!/usr/bin/env ts-node +import { + createReadStream, + createWriteStream, + promises as fsPromises, + // link as linkCallback, + // unlink as unlinkCallback, +} from 'fs' +import { + Transform, + // TransformOptions, +} from 'stream' +import { promisify } from 'util' + +import * as globCallback from 'glob' + const LICENSE = `/** * Wechaty - https://github.com/chatie/wechaty * @@ -19,32 +34,16 @@ const LICENSE = `/** * */` -import { - createReadStream, - createWriteStream, - promises as fsPromises, - // link as linkCallback, - // unlink as unlinkCallback, -} from 'fs' -import { - Transform, - TransformOptions, -} from 'stream' -import { promisify } from 'util' - -import * as globCallback from 'glob' - class LicenseTransformer extends Transform { - private lineBuf = '' private lineNum = 0 private updating = false private updated = false - constructor (options?: TransformOptions) { - super(options) - } + // constructor (options?: TransformOptions) { + // super(options) + // } public _transform (chunk: any, _: string /* encoding: string */, done: () => void) { if (this.updated) { @@ -69,40 +68,40 @@ class LicenseTransformer extends Transform { const updatedLineList: string[] = [] buffer - .split(/\n/) - .forEach(line => { - if (this.lineNum === 0 && line.startsWith('#!')) { - updatedLineList.push(line) - } else if (this.updated) { - updatedLineList.push(line) - } else if (this.updating) { - if (/\*\//.test(line)) { - updatedLineList.push(line.replace(/.*\*\//, LICENSE)) - this.updating = false - this.updated = true - } else { - // drop the old comments - } - } else { // not updating and not updated. searching... - if (!line) { + .split(/\n/) + .forEach(line => { + if (this.lineNum === 0 && line.startsWith('#!')) { updatedLineList.push(line) - } else if (/\s*\/\*\*/.test(line)) { // comment start - if (/\*\//.test(line)) { // comment end at the same line with start - updatedLineList.push(line.replace(/\/\*\*.*\*\//, LICENSE)) - this.updated = true + } else if (this.updated) { + updatedLineList.push(line) + } else if (this.updating) { + if (/\*\//.test(line)) { + updatedLineList.push(line.replace(/.*\*\//, LICENSE)) + this.updating = false + this.updated = true } else { - this.updating = true + // drop the old comments + } + } else { // not updating and not updated. searching... + if (!line) { + updatedLineList.push(line) + } else if (/\s*\/\*\*/.test(line)) { // comment start + if (/\*\//.test(line)) { // comment end at the same line with start + updatedLineList.push(line.replace(/\/\*\*.*\*\//, LICENSE)) + this.updated = true + } else { + this.updating = true + } + } else { // not a comment. INSERT here + updatedLineList.push(LICENSE) + updatedLineList.push(line) + this.updated = true } - } else { // not a comment. INSERT here - updatedLineList.push(LICENSE) - updatedLineList.push(line) - this.updated = true } - } - this.lineBuf = line - this.lineNum++ - }) + this.lineBuf = line + this.lineNum++ + }) return updatedLineList.join('\n') } @@ -122,7 +121,7 @@ async function updateLicense (file: string): Promise { const writeStream = createWriteStream(tmpFile) const tranStream = new LicenseTransformer() - console.log(`Updating LICENSE for file ${file}...`) + console.info(`Updating LICENSE for file ${file}...`) await new Promise((resolve, reject) => { readStream .pipe(tranStream) @@ -152,8 +151,8 @@ async function main (): Promise { } main() -.then(process.exit) -.catch(e => { - console.error(e) - process.exit(1) -}) + .then(process.exit) + .catch(e => { + console.error(e) + process.exit(1) + }) diff --git a/src/config.ts b/src/config.ts index 0c08b2e66fd87cd865b7b5ceca462c2343afd5ca..9d746e8c0d477957884524ee4ec3b34226ed5c91 100644 --- a/src/config.ts +++ b/src/config.ts @@ -25,7 +25,6 @@ import path from 'path' import qrImage from 'qr-image' import Raven from 'raven' -import readPkgUp from 'read-pkg-up' import { log } from 'brolog' import { @@ -34,36 +33,34 @@ import { import { PuppetModuleName, -} from './puppet-config' +} from './puppet-config' +import { VERSION } from './version' // https://github.com/Microsoft/TypeScript/issues/14151#issuecomment-280812617 // if (!Symbol.asyncIterator) { // (Symbol as any).asyncIterator = Symbol.for('Symbol.asyncIterator') // } -const pkg = readPkgUp.sync({ cwd: __dirname })!.package -export const VERSION = pkg.version - /** * Raven.io */ Raven.disableConsoleAlerts() Raven -.config( - isProduction() - && 'https://f6770399ee65459a82af82650231b22c:d8d11b283deb441e807079b8bb2c45cd@sentry.io/179672', - { - release: VERSION, - tags: { - git_commit: '', - platform: process.env.WECHATY_DOCKER - ? 'docker' - : os.platform(), + .config( + isProduction() + && 'https://f6770399ee65459a82af82650231b22c:d8d11b283deb441e807079b8bb2c45cd@sentry.io/179672', + { + release: VERSION, + tags: { + git_commit: '', + platform: process.env.WECHATY_DOCKER + ? 'docker' + : os.platform(), + }, }, - }, -) -.install() + ) + .install() /* try { @@ -248,6 +245,7 @@ export function isProduction (): boolean { export { log, Raven, + VERSION, } export const config = new Config() diff --git a/src/version.spec.ts b/src/version.spec.ts new file mode 100755 index 0000000000000000000000000000000000000000..127e603c76dfb2214b053e216df336dd91009a16 --- /dev/null +++ b/src/version.spec.ts @@ -0,0 +1,10 @@ +#!/usr/bin/env ts-node + +// tslint:disable:no-shadowed-variable +import test from 'blue-tape' + +import { VERSION } from './version' + +test('Make sure the VERSION is fresh in source code', async (t) => { + t.equal(VERSION, '0.0.0', 'version should be 0.0.0 in source code, only updated before publish to NPM') +}) diff --git a/src/version.ts b/src/version.ts new file mode 100644 index 0000000000000000000000000000000000000000..9a99c04f5c0fb8fadcb950a4af07cf97b444bb6d --- /dev/null +++ b/src/version.ts @@ -0,0 +1,6 @@ +/** + * This file will be overwrite when we publish NPM module + * by scripts/generate_version.sh + */ + +export const VERSION = '0.0.0' diff --git a/tests/fixtures/smoke-testing.ts b/tests/fixtures/smoke-testing.ts index 653bb9fc6347708e2fe3f781365c6a04abfa9c8b..9710d7f54a0555e2461b6770c1218820c55b16ed 100644 --- a/tests/fixtures/smoke-testing.ts +++ b/tests/fixtures/smoke-testing.ts @@ -3,7 +3,10 @@ // tslint:disable:no-var-requires const isPR = require('is-pr') -import { Wechaty } from 'wechaty' +import { + Wechaty, + VERSION, +} from 'wechaty' function getBotList (): Wechaty[] { const botList = [ @@ -25,6 +28,10 @@ function getBotList (): Wechaty[] { } async function main () { + if (VERSION === '0.0.0') { + throw new Error('VERSION not set!') + } + const botList = getBotList() try { await Promise.all( @@ -46,8 +53,8 @@ async function main () { } main() -.then(process.exit) -.catch(e => { - console.error(e) - process.exit(1) -}) + .then(process.exit) + .catch(e => { + console.error(e) + process.exit(1) + })