Skip to content

Commit

Permalink
make eslint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Jun 16, 2019
1 parent 7ce0ded commit bb17136
Show file tree
Hide file tree
Showing 20 changed files with 286 additions and 289 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,8 @@

const rules = {
}

module.exports = {
extends: '@chatie',
rules,
}
32 changes: 16 additions & 16 deletions examples/demo.ts
Expand Up @@ -33,23 +33,23 @@ const puppet = new PuppetPuppeteer()
*
*/
puppet
.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)

/**
*
* 3. Start the bot!
*
*/
puppet.start()
.catch(async e => {
console.error('Bot start() fail:', e)
await puppet.stop()
process.exit(-1)
})
.catch(async e => {
console.error('Bot start() fail:', e)
await puppet.stop()
process.exit(-1)
})

/**
*
Expand All @@ -71,16 +71,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 (contactId: string) {
console.log(`${contactId} login`)
puppet.messageSendText({ contactId, }, 'Wechaty login').catch(console.error)
console.info(`${contactId} login`)
puppet.messageSendText({ contactId }, 'Wechaty login').catch(console.error)
}

function onLogout (contactId: string) {
console.log(`${contactId} logouted`)
console.info(`${contactId} logouted`)
}

function onError (e: string) {
Expand All @@ -100,7 +100,7 @@ function onError (e: string) {
*/
async function onMessage (messageId: string) {
const payload = await puppet.messagePayload(messageId)
console.log(JSON.stringify(payload))
console.info(JSON.stringify(payload))
}

/**
Expand All @@ -114,4 +114,4 @@ Puppet Version: ${puppet.version()}
Please wait... I'm trying to login in...
`
console.log(welcome)
console.info(welcome)
22 changes: 3 additions & 19 deletions package.json
Expand Up @@ -14,7 +14,7 @@
"lint": "npm run lint:es && npm run lint:ts && npm run lint:md",
"lint:es": "eslint \"{bin,examples,scripts,src,tests}/**/*.js\" --ignore-pattern=\"tests/fixtures/**\"",
"lint:md": "markdownlint README.md",
"lint:ts": "tslint --project tsconfig.json && tsc --noEmit",
"lint:ts": "eslint '{bin,examples,scripts,src,tests}/**/*.ts' --ignore-pattern=tests/fixtures && tsc --noEmit",
"start": "ts-node examples/demo.ts",
"test": "npm run lint && npm run test:unit:retry",
"test:pack": "bash -x scripts/npm-pack-testing.sh",
Expand Down Expand Up @@ -44,11 +44,9 @@
"@chatie/semver": "^0.4.7",
"@chatie/tsconfig": "^0.6.1",
"@types/bl": "^2.1.0",
"@types/blue-tape": "^0.1.31",
"@types/lru-cache": "^5.1.0",
"@types/md5": "^2.1.32",
"@types/mime": "^2.0.0",
"@types/node": "^12.0.0",
"@types/normalize-package-data": "^2.4.0",
"@types/promise-retry": "^1.1.2",
"@types/puppeteer": "^1.11.1",
Expand All @@ -57,32 +55,16 @@
"@types/read-pkg-up": "^3.0.1",
"@types/request": "^2.47.1",
"@types/retry": "0.12.0",
"@types/semver": "^6.0.0",
"@types/sinon": "^7.0.2",
"@types/xml2js": "^0.4.3",
"ajv": "^6.5.2",
"babel-eslint": "^10.0.0",
"blue-tape": "^1.0.0",
"eslint": "^5.0.1",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.0.0",
"eslint-plugin-standard": "^4.0.0",
"file-box": "^0.8.24",
"git-scripts": "^0.2.1",
"markdownlint-cli": "^0.17.0",
"memory-card": "^0.6.2",
"normalize-package-data": "^2.4.0",
"pkg-jq": "^0.2.2",
"pkg-up": "^3.1.0",
"semver": "^6.0.0",
"shx": "^0.3.1",
"sinon": "^7.2.2",
"ts-node": "^8.0.2",
"tslint": "^5.11.0",
"tslint-config-standard": "^8.0.0",
"typescript": "^3.0.1",
"wechaty-puppet": "^0.15.12"
},
"peerDependencies": {
Expand All @@ -91,6 +73,7 @@
},
"homepage": "https://github.com/Chatie/wechaty-puppet-puppeteer#readme",
"dependencies": {
"@chatie/eslint-config": "^0.6.10",
"@chatie/git-scripts": "^0.2.4",
"bl": "^3.0.0",
"brolog": "^1.6.5",
Expand All @@ -106,6 +89,7 @@
"rx-queue": "^0.8.1",
"rxjs": "^6.2.1",
"state-switch": "^0.6.2",
"tstest": "^0.4.2",
"watchdog": "^0.8.15",
"xml2js": "^0.4.19"
},
Expand Down
16 changes: 8 additions & 8 deletions scripts/retry-unit-tests.ts
Expand Up @@ -14,15 +14,15 @@ import { spawn } from 'child_process'
const MAX_RETRY_NUM = 3

async function main (): Promise<number> {
console.log('Safe Test: starting...')
console.info('Safe Test: starting...')

let round = 0
let succ = false
do {
console.log(`Safe Test: running for round #${round}`)
console.info(`Safe Test: running for round #${round}`)
succ = await unitTest()
if (succ) { // success!
console.log(`Safe Test: successed at round #${round}!`)
console.info(`Safe Test: successed at round #${round}!`)
return 0
}
} while (round++ < MAX_RETRY_NUM)
Expand Down Expand Up @@ -50,8 +50,8 @@ async function unitTest () {
}

main()
.then(process.exit)
.catch((e) => {
console.error(e)
process.exit(1)
})
.then(process.exit)
.catch((e) => {
console.error(e)
process.exit(1)
})
2 changes: 2 additions & 0 deletions src/bridge.spec.ts
Expand Up @@ -63,6 +63,7 @@ test('PuppetPuppeteerBridge', async (t) => {
}
})

/* eslint indent: off */
test('preHtmlToXml()', async (t) => {
const BLOCKED_HTML_ZH = [
'<pre style="word-wrap: break-word; white-space: pre-wrap;">',
Expand Down Expand Up @@ -204,6 +205,7 @@ test('WechatyBro.ding()', async t => {
t.pass('should init Bridge')

const retDing = await bridge.evaluate(() => {
// eslint-disable-next-line
return WechatyBro.ding()
}) as string

Expand Down
58 changes: 29 additions & 29 deletions src/bridge.ts
Expand Up @@ -67,6 +67,7 @@ export interface BridgeOptions {
}

export class Bridge extends EventEmitter {

private browser : undefined | Browser
private page : undefined | Page
private state : StateSwitch
Expand Down Expand Up @@ -119,7 +120,7 @@ export class Bridge extends EventEmitter {
public async initBrowser (): Promise<Browser> {
log.verbose('PuppetPuppeteerBridge', 'initBrowser()')

const headless = this.options.head ? false : true
const headless = !(this.options.head)
const browser = await launch({
args: [
'--audio-output-channels=0',
Expand All @@ -144,7 +145,7 @@ export class Bridge extends EventEmitter {

public async onDialog (dialog: Dialog) {
log.warn('PuppetPuppeteerBridge', 'onDialog() page.on(dialog) type:%s message:%s',
dialog.type, dialog.message())
dialog.type, dialog.message())
try {
// XXX: Which ONE is better?
await dialog.accept()
Expand Down Expand Up @@ -253,14 +254,14 @@ export class Bridge extends EventEmitter {

try {
const sourceCode = fs.readFileSync(WECHATY_BRO_JS_FILE)
.toString()
.toString()

let retObj = await page.evaluate(sourceCode) as InjectResult

if (retObj && /^(2|3)/.test(retObj.code.toString())) {
// HTTP Code 2XX & 3XX
log.silly('PuppetPuppeteerBridge', 'inject() eval(Wechaty) return code[%d] message[%s]',
retObj.code, retObj.message)
retObj.code, retObj.message)
} else { // HTTP Code 4XX & 5XX
throw new Error('execute injectio error: ' + retObj.code + ', ' + retObj.message)
}
Expand All @@ -269,7 +270,7 @@ export class Bridge extends EventEmitter {
if (retObj && /^(2|3)/.test(retObj.code.toString())) {
// HTTP Code 2XX & 3XX
log.silly('PuppetPuppeteerBridge', 'inject() Wechaty.init() return code[%d] message[%s]',
retObj.code, retObj.message)
retObj.code, retObj.message)
} else { // HTTP Code 4XX & 5XX
throw new Error('execute proxyWechaty(init) error: ' + retObj.code + ', ' + retObj.message)
}
Expand Down Expand Up @@ -553,9 +554,9 @@ export class Bridge extends EventEmitter {
try {
return await retry(async (retryException, attempt) => {
log.silly('PuppetPuppeteerBridge', 'getMessage(%s) retry attempt %d',
id,
attempt,
)
id,
attempt,
)
try {
const rawPayload = await this.proxyWechaty('getMessage', id)

Expand All @@ -579,9 +580,9 @@ export class Bridge extends EventEmitter {
try {
return await retry(async (retryException, attempt) => {
log.silly('PuppetPuppeteerBridge', 'getContact(%s) retry attempt %d',
id,
attempt,
)
id,
attempt,
)
try {
const rawPayload = await this.proxyWechaty('getContact', id)

Expand All @@ -599,7 +600,6 @@ export class Bridge extends EventEmitter {
log.error('PuppetPuppeteerBridge', 'promiseRetry() getContact() finally FAIL: %s', e.message)
throw e
}
/////////////////////////////////
}

public async getBaseRequest (): Promise<string> {
Expand Down Expand Up @@ -688,11 +688,11 @@ export class Bridge extends EventEmitter {
...args : any[]
): Promise<any> {
log.silly('PuppetPuppeteerBridge', 'proxyWechaty(%s%s)',
wechatyFunc,
args.length === 0
? ''
: ', ' + args.join(', '),
)
wechatyFunc,
args.length === 0
? ''
: ', ' + args.join(', '),
)

if (!this.page) {
throw new Error('no page')
Expand Down Expand Up @@ -745,13 +745,13 @@ export class Bridge extends EventEmitter {
log.verbose('PuppetPuppeteerBridge', 'ding(%s)', data || '')

this.proxyWechaty('ding', data)
.then(dongData => {
this.emit('dong', dongData)
})
.catch(e => {
log.error('PuppetPuppeteerBridge', 'ding(%s) exception: %s', data, e.message)
this.emit('error', e)
})
.then(dongData => {
this.emit('dong', dongData)
})
.catch(e => {
log.error('PuppetPuppeteerBridge', 'ding(%s) exception: %s', data, e.message)
this.emit('error', e)
})
}

public preHtmlToXml (text: string): string {
Expand Down Expand Up @@ -785,7 +785,7 @@ export class Bridge extends EventEmitter {

const textSnip = text.substr(0, 50).replace(/\n/, '')
log.verbose('PuppetPuppeteerBridge', 'testBlockedMessage(%s)',
textSnip)
textSnip)

interface BlockedMessage {
error?: {
Expand Down Expand Up @@ -903,8 +903,8 @@ export class Bridge extends EventEmitter {
// TODO: use page.$x() (with puppeteer v1.1 or above) to replace DIY version of listXpath() instead.
// See: https://github.com/GoogleChrome/puppeteer/blob/v1.1.0/docs/api.md#pagexexpression

const XPATH_SELECTOR =
`//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']`
const XPATH_SELECTOR
= `//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']`

try {
// const [button] = await listXpath(page, XPATH_SELECTOR)
Expand Down Expand Up @@ -958,7 +958,7 @@ export class Bridge extends EventEmitter {
log.error('PuppetPuppeteerBridge', 'cookies(%s) reject: %s', cookieList, e)
this.emit('error', e)
}
return
// RETURN
} else {
// FIXME: puppeteer typing bug
cookieList = await this.page.cookies() as any as Cookie[]
Expand Down Expand Up @@ -1015,7 +1015,6 @@ export class Bridge extends EventEmitter {
}

await this.page.reload()
return
}

public async evaluate (fn: () => any, ...args: any[]): Promise<any> {
Expand All @@ -1033,6 +1032,7 @@ export class Bridge extends EventEmitter {
return null
}
}

}

export {
Expand Down
5 changes: 1 addition & 4 deletions src/config.ts
@@ -1,8 +1,5 @@
// tslint:disable:no-reference

/// <reference path="./typings.d.ts" />

import promiseRetry = require('promise-retry')
import { OperationOptions } from 'retry'

import {
Expand All @@ -14,7 +11,7 @@ import qrImage from 'qr-image'

import { VERSION } from './version'

// export const log = new Brolog()
import promiseRetry = require('promise-retry')

export async function retry<T> (
retryableFn: (
Expand Down
2 changes: 1 addition & 1 deletion src/firer.spec.ts
Expand Up @@ -197,7 +197,7 @@ test('parseRoomTopic()', async (t) => {
contentList.forEach(([content, changer, topic]) => {
result = (firer as any).parseRoomTopic(content)
t.ok(result, 'should check topic right for content: ' + content)
t.is(topic , result[0], 'should get right topic')
t.is(topic, result[0], 'should get right topic')
t.is(changer, result[1], 'should get right changer')
})

Expand Down

0 comments on commit bb17136

Please sign in to comment.