Skip to content

Commit

Permalink
refactor(*): 用 lerna 转 teambition-sdk 为多包单项目(monorepos)
Browse files Browse the repository at this point in the history
主要包为:

- teambition-sdk-core:包含较基础的代码
- teambition-sdk-request:包含接口代码,期待能够更快地相应应用需求,独
立更新

将使用 lift 的接口也包含在 fetches 包内

...并将相关 helper 函数和类型定义移到 fetches 包内。

save

sdk-request: 添加扩充 SDK 和 SDKFetch 对象的机制

sdk-request: 令现有测试跑起来

初步实现在跟目录上跑全局测试的脚本

...待完成:完善 teambition-sdk-request 的测试。

基本完成 teambition-sdk-request 相关测试,同时令mock成为packaege之一

基本完成对测试的分离和整理,并令 fixture 成为一个私有包

修复 core 和 reuqest 包的 watch 脚本,这两个包都可以执行 yarn watch

避免 watch 生成的 background process 在退出测试后继续跑

...相关命令:

 `npm run watch_test & ts-node ../../tools/tasks/test.ts $!`

即:令 watch_test 作为 background process 跑,及时将 test 目录下的代码
更新编译到 spec-js 目录,同时有 test.ts 的 nodejs 进程作为 foreground
process 在跑,及时在最新的 spec-js 目录上运行测试代码。当用户输入
Ctrl+C,即:SIGINT 信号,作为 foreground process 跑的 test.ts 会被终结。
而作为 background process 跑的 watch_test 通过 process.kill($!) 也终结
掉。注:$! 符号记录最近创建的 background process 的 pid。

修复跑全局测试的脚本,在根目录跑 yarn test,会跑所有测试

...包括每个 package 内的测试,以及根目录下 test/ 里的测试。

调整个别测试
  • Loading branch information
chuan6 committed Aug 21, 2017
1 parent c67e577 commit c253d5c
Show file tree
Hide file tree
Showing 151 changed files with 2,047 additions and 483 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ pids

# Coverage directory used by tools like istanbul
coverage
packages/*/coverage/
.nyc_output

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
packages/*/build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
packages/*/node_modules/

.tmp

Expand All @@ -29,8 +32,12 @@ node_modules
.idea/

typings
packages/*/typings
.tsdrc
packages/*/typings
jsconfig.json
coverage
dist
packages/*/dist
spec-js
packages/*/spec-js
2 changes: 2 additions & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
registry "https://registry.npmjs.org"

workspaces-experimental true
6 changes: 6 additions & 0 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"lerna": "2.0.0",
"version": "independent",
"npmClient": "yarn",
"useWorkSpaces": true
}
33 changes: 11 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
{
"private": true,
"name": "teambition-sdk",
"version": "0.8.17",
"description": "Front-End SDK for Teambition",
"main": "./index.js",
"typings": "./index.d.ts",
"workspaces": [
"packages/*"
],
"scripts": {
"build_all": "rm -rf dist && mkdir dist && mkdir dist/bundle && npm run build_es6 && npm run build_cjs && npm run build_bundle && npm run build_mock && npm run build_mock_cjs && npm run build_test && npm run build_socket",
"build_bundle": "rm -rf dist/bundle/tbsdk.umd.js && ts-node ./tools/tasks/bundle.sdk.ts",
"build_cjs": "rm -rf dist/cjs && tsc src/index.ts -m commonjs --outDir dist/cjs --sourcemap --inlineSources --target ES5 -d --diagnostics --pretty --strict --noUnusedLocals --noUnusedParameters --experimentalDecorators --suppressImplicitAnyIndexErrors --moduleResolution node --importHelpers --noEmitHelpers --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom",
"build_es6": "rm -rf dist/es6 && tsc src/index.ts -m es2015 --outDir dist/es6 --sourcemap --inlineSources --target ES2015 -d --diagnostics --pretty --strict --noUnusedLocals --noUnusedParameters --experimentalDecorators --suppressImplicitAnyIndexErrors --moduleResolution node --importHelpers --noEmitHelpers --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom",
"build_mock": "rm -rf dist/mock-es6 && tsc mock/index.ts -m es2015 --outDir dist/mock-es6 --sourcemap --inlineSources --target ES5 -d --diagnostics --pretty --strict --noUnusedLocals --noUnusedParameters --experimentalDecorators --suppressImplicitAnyIndexErrors --moduleResolution node --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom && ts-node ./tools/tasks/bundle.mock.ts",
"build_mock_cjs": "rm -rf dist/mock-cjs && tsc mock/index.ts -m commonjs --outDir dist/mock-cjs --sourcemap --inlineSources --target ES5 -d --diagnostics --pretty --strict --noUnusedLocals --noUnusedParameters --experimentalDecorators --suppressImplicitAnyIndexErrors --moduleResolution node --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom",
"build_socket": "rm -rf dist/bundle/tbsdk.socket.js && tsc ./src/SocketApp.ts ./src/teambition.ts -m commonjs --outDir dist/socket --sourcemap --inlineSources --target ES5 -d --diagnostics --pretty --experimentalDecorators --suppressImplicitAnyIndexErrors --moduleResolution node --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom && ts-node ./tools/tasks/bundle.socket.ts",
"build_test": "rm -rf spec-js && tsc test/app.ts -m commonjs --sourcemap --inlineSources --outDir spec-js --target ES2015 --diagnostics --pretty --experimentalDecorators --suppressImplicitAnyIndexErrors --types \"node,chai,sinon,sinon-chai\" --moduleResolution node",
"check_circular_dependencies": "madge ./dist/cjs --circular",
"copy_files": "cp README.md package.json ./dist/cjs/",
"cover": "npm run build_test && rm -rf ./coverage && nyc --reporter=html --reporter=lcov --exclude=node_modules --exclude=spec-js/test --exclude=spec-js/mock --exclude=spec-js/src/sockets/SocketClient.js tman --mocha spec-js/test/app.js",
"lint": "tslint ./src/**/*.ts ./mock/**/*.ts ./test/*.ts ./test/apis/**/*.ts ./test/mock/**/*.ts ./test/utils/**/*.ts",
"publish_sdk": "npm run build_all && npm run copy_files && npm publish ./dist/cjs",
"publish_all": "npm run publish_sdk && ts-node ./tools/tasks/publish.ts && cp -r ./dist/mock-cjs/** ./.tmp/mock && cp -r ./dist/socket/** ./.tmp/socket && npm publish .tmp/mock/ && npm publish .tmp/socket/",
"test": "npm run lint && npm run cover",
"version": "ts-node tools/tasks/version.ts && git add .",
"watch": "npm run watch_test & ts-node ./tools/tasks/test.ts",
"watch_cjs": "tsc src/index.ts -m commonjs --outDir dist/cjs --sourcemap --inlineSources --target ES5 -d --diagnostics --pretty --experimentalDecorators --strict --noUnusedLocals --noUnusedParameters --suppressImplicitAnyIndexErrors --moduleResolution node --skipLibCheck -w --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom",
"watch_test": "tsc test/app.ts -m commonjs --outDir spec-js --sourcemap --inlineSources --target ES2015 --diagnostics --pretty --experimentalDecorators --suppressImplicitAnyIndexErrors --types \"node,chai,sinon,sinon-chai\" --moduleResolution node --skipLibCheck -w"
"build_test": "rm -rf spec-js && tsc --project test",
"test": "lerna run test && npm run build_test && tman --mocha spec-js/app.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -61,6 +46,7 @@
"google-closure-compiler-js": "^20170521.0.0",
"isomorphic-fetch": "^2.2.1",
"jsonrpc-lite": "^1.2.3",
"lerna": "^2.0.0",
"madge": "^1.6.0",
"moment": "^2.18.1",
"node-watch": "^0.5.4",
Expand All @@ -75,7 +61,10 @@
"sinon": "^2.3.8",
"sinon-chai": "^2.11.0",
"snapper-consumer": "^1.3.6",
"teambition-sdk-mock": "^0.6.8",
"teambition-sdk-core": "^0.8.13",
"teambition-sdk-fixture": "^0.8.13",
"teambition-sdk-mock": "^0.8.13",
"teambition-sdk-request": "^0.8.13",
"tman": "^1.7.1",
"ts-node": "^3.2.0",
"tslib": "^1.7.1",
Expand Down
87 changes: 87 additions & 0 deletions packages/teambition-sdk-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"name": "teambition-sdk-core",
"version": "0.8.13",
"description": "Front-End SDK (Core) for Teambition",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/index.d.ts",
"scripts": {
"build_all": "rm -rf dist && mkdir dist && mkdir dist/bundle && npm run build_es6 && npm run build_cjs && npm run build_bundle && npm run build_mock && npm run build_mock_cjs && npm run build_test && npm run build_socket",
"build_bundle": "rm -rf dist/bundle/tbsdk.umd.js && ts-node ./tools/tasks/bundle.sdk.ts",
"build_cjs": "rm -rf dist/cjs && tsc src/index.ts -m commonjs --outDir dist/cjs --sourcemap --inlineSources --target ES5 -d --diagnostics --pretty --strict --noUnusedLocals --noUnusedParameters --experimentalDecorators --suppressImplicitAnyIndexErrors --moduleResolution node --importHelpers --noEmitHelpers --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom",
"build_es6": "rm -rf dist/es6 && tsc src/index.ts -m es2015 --outDir dist/es6 --sourcemap --inlineSources --target ES2015 -d --diagnostics --pretty --strict --noUnusedLocals --noUnusedParameters --experimentalDecorators --suppressImplicitAnyIndexErrors --moduleResolution node --importHelpers --noEmitHelpers --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom",
"build_mock": "rm -rf dist/mock-es6 && tsc mock/index.ts -m es2015 --outDir dist/mock-es6 --sourcemap --inlineSources --target ES5 -d --diagnostics --pretty --strict --noUnusedLocals --noUnusedParameters --experimentalDecorators --suppressImplicitAnyIndexErrors --moduleResolution node --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom && ts-node ./tools/tasks/bundle.mock.ts",
"build_mock_cjs": "rm -rf dist/mock-cjs && tsc mock/index.ts -m commonjs --outDir dist/mock-cjs --sourcemap --inlineSources --target ES5 -d --diagnostics --pretty --strict --noUnusedLocals --noUnusedParameters --experimentalDecorators --suppressImplicitAnyIndexErrors --moduleResolution node --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom",
"build_socket": "rm -rf dist/bundle/tbsdk.socket.js && tsc ./src/SocketApp.ts ./src/teambition.ts -m commonjs --outDir dist/socket --sourcemap --inlineSources --target ES5 -d --diagnostics --pretty --experimentalDecorators --suppressImplicitAnyIndexErrors --moduleResolution node --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom && ts-node ./tools/tasks/bundle.socket.ts",
"build_test": "rm -rf spec-js && tsc --project test",
"check_circular_dependencies": "madge ./dist/cjs --circular",
"copy_files": "cp README.md package.json ./dist/cjs/",
"cover": "npm run build_test && rm -rf ./coverage && nyc --reporter=html --reporter=lcov --exclude=node_modules --exclude=spec-js/test --exclude=spec-js/mock --exclude=spec-js/src/sockets/SocketClient.js tman --mocha spec-js/test/app.js",
"lint": "tslint ./src/**/*.ts ./mock/**/*.ts ./test/*.ts ./test/apis/**/*.ts ./test/mock/**/*.ts ./test/utils/**/*.ts",
"publish_sdk": "npm run build_all && npm run copy_files && npm publish ./dist/cjs",
"publish_all": "npm run publish_sdk && ts-node ./tools/tasks/publish.ts && cp -r ./dist/mock-cjs/** ./.tmp/mock && cp -r ./dist/socket/** ./.tmp/socket && npm publish .tmp/mock/ && npm publish .tmp/socket/",
"test": "npm run lint && npm run cover",
"version": "ts-node tools/tasks/version.ts && git add .",
"watch": "npm run watch_test & ts-node ../../tools/tasks/test.ts $!",
"watch_cjs": "tsc src/index.ts -m commonjs --outDir dist/cjs --sourcemap --inlineSources --target ES5 -d --diagnostics --pretty --experimentalDecorators --strict --noUnusedLocals --noUnusedParameters --suppressImplicitAnyIndexErrors --moduleResolution node --skipLibCheck -w --lib es5,es2015.iterable,es2015.collection,es2015.promise,es2015.core,dom",
"watch_test": "tsc --project test --watch --skipLibCheck"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/teambition/teambition-sdk.git"
},
"keywords": [
"teambition",
"sdk",
"teambition-sdk"
],
"author": "[email protected]",
"license": "MIT",
"bugs": {
"url": "https://github.com/teambition/teambition-sdk/issues"
},
"homepage": "https://github.com/teambition/teambition-sdk#readme",
"peerDependencies": {
"reactivedb": "~0.9.9",
"rrule": "2.2.0",
"rxjs": "^5.2.0",
"snapper-consumer": "^1.3.6",
"tslib": "^1.6.0"
},
"devDependencies": {
"@types/chai": "^4.0.1",
"@types/lodash": "^4.14.64",
"@types/lovefield": "^2.0.32",
"@types/node": "^8.0.14",
"@types/sinon": "^2.3.3",
"@types/sinon-chai": "^2.7.28",
"chai": "^4.1.0",
"coveralls": "^2.13.1",
"engine.io-client": "^3.1.0",
"fetch-mock": "^5.10.0",
"google-closure-compiler-js": "^20170521.0.0",
"isomorphic-fetch": "^2.2.1",
"jsonrpc-lite": "^1.2.3",
"madge": "^1.6.0",
"moment": "^2.18.1",
"node-watch": "^0.5.4",
"nyc": "^11.0.1",
"reactivedb": "~0.9.9",
"rollup": "^0.42.0",
"rollup-plugin-alias": "^1.3.1",
"rollup-plugin-commonjs": "^8.0.2",
"rollup-plugin-node-resolve": "^3.0.0",
"rrule": "2.2.0",
"rxjs": "^5.4.2",
"sinon": "^2.3.8",
"sinon-chai": "^2.11.0",
"snapper-consumer": "^1.3.6",
"teambition-sdk-fixture": "^0.8.13",
"teambition-sdk-mock": "^0.8.13",
"tman": "^1.7.1",
"ts-node": "^3.2.0",
"tslib": "^1.7.1",
"tslint": "^5.5.0",
"tslint-eslint-rules": "^4.1.1",
"typescript": "^2.4.2"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/index.ts → packages/teambition-sdk-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/// <reference path="./teambition.ts" />
import 'tslib'

import { forEach, clone, uuid, concat, dropEle } from './utils/index'
import { forEach, clone, uuid, concat, dropEle, pagination, capitalizeFirstLetter } from './utils/index'

export const Utils = { forEach, clone, uuid, concat, dropEle }
export const Utils = { forEach, clone, uuid, concat, dropEle, pagination, capitalizeFirstLetter }
export { PagingQuery, UrlPagingQuery } from './utils/internalTypes'
export { eventParser } from './sockets/EventParser'

// export apis
import './sockets/SocketClient'
import './apis'

import './schemas'
export * from './schemas'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions packages/teambition-sdk-core/test/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'
import 'isomorphic-fetch'

process.env.NODE_ENV = 'production'

export { run, setExit, reset, mocha } from 'tman'
export * from './utils/utils'
export * from './utils/httpErrorSpec'

export * from './mock/MockSpec'

import './sockets'
import './net'
20 changes: 20 additions & 0 deletions packages/teambition-sdk-core/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'
import { Database, DataStoreType } from 'reactivedb'
import { testable } from '../src/testable'
import { SDK } from '../src/index'

import './SDKFetch.spec'

testable.UseXMLHTTPRequest = false

export function createSdk() {
const sdk = new SDK()

const database = new Database(DataStoreType.MEMORY, false, 'teambition-sdk', 1)
sdk.initReactiveDB(database)

return sdk
}

export * from '../src/index'
export * from '../src/utils/index'
45 changes: 45 additions & 0 deletions packages/teambition-sdk-core/test/mock/MockFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Backend } from 'teambition-sdk-mock'
import { SDKFetch } from '../index'

function throwIfSlashPath(path: string) {
if (path.charAt(0) === '/') {
throw new Error(`There shouldn't be a slash before path (${path})`)
}
}

export class MockFetch extends SDKFetch {
private httpBackend = new Backend
private _apiHost = new SDKFetch().getAPIHost()

mockGet(path: string, query?: any) {
throwIfSlashPath(path)
return {
mockResponse: this.httpBackend.whenGET(`${this._apiHost}/${path}`, query),
request: this.get(path, query)
}
}

mockPut(path: string, body?: any) {
throwIfSlashPath(path)
return {
mockResponse: this.httpBackend.whenPUT(`${this._apiHost}/${path}`, body),
request: this.put(path, body)
}
}

mockPost(path: string, body?: any) {
throwIfSlashPath(path)
return {
mockResponse: this.httpBackend.whenPOST(`${this._apiHost}/${path}`, body),
request: this.post(path, body)
}
}

mockDelete(path: string) {
throwIfSlashPath(path)
return {
mockResponse: this.httpBackend.whenDELETE(`${this._apiHost}/${path}`),
request: this.delete(path)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as chai from 'chai'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import { Backend, parseObject, reParseQuery } from '../index'
import { Backend, parseObject, reParseQuery } from 'teambition-sdk-mock'
import { describe, it, beforeEach } from 'tman'

const expect = chai.expect
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import './asyncLoadRDB'
import './net'
import './http'
23 changes: 13 additions & 10 deletions test/net/net.ts → packages/teambition-sdk-core/test/net/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { Database, DataStoreType } from 'reactivedb'
import { expect, use } from 'chai'
import { spy } from 'sinon'
import * as SinonChai from 'sinon-chai'
import { Backend } from 'teambition-sdk-mock'
import '../../src/schemas'
import { schemas, CacheStrategy } from '../../src/SDK'
import { Net, Backend, SDKFetch, forEach, uuid, Http, EventSchema, } from '..'
import { Net, SDKFetch, forEach, uuid, Http, EventSchema, } from '..'
import { ApiResult } from '../../src/Net/Net'
import { normalEvent, projectEvents } from '../fixtures/events.fixture'
import { EventsFixture } from 'teambition-sdk-fixture'

const { normalEvent, projectEvents } = EventsFixture

use(SinonChai)

Expand All @@ -18,10 +21,13 @@ describe('Net test', () => {
let database: Database
let version = 1
let subscription: Subscription | undefined
let spyFetch: sinon.SinonSpy

const sdkFetch = new SDKFetch()
const apiHost = sdkFetch.getAPIHost()
const path = 'test'
const http = new Http(`${apiHost}/${path}`)

beforeEach(() => {
httpBackend = new Backend()
net = new Net(schemas)
Expand All @@ -35,6 +41,7 @@ describe('Net test', () => {

afterEach(function* () {
httpBackend.restore()
spyFetch && spyFetch.restore()
if (subscription instanceof Subscription) {
subscription.unsubscribe()
}
Expand Down Expand Up @@ -212,7 +219,7 @@ describe('Net test', () => {
httpBackend.whenGET(`${apiHost}/${path}`)
.respond(projectEvents)

const spyFetch = spy(sdkFetch, 'get')
spyFetch = spy(sdkFetch, 'get')

const newLocation = 'new_event_location'

Expand Down Expand Up @@ -256,15 +263,13 @@ describe('Net test', () => {
expect(spyFetch.callCount).to.equal(2)
expect(events.length).to.equal(projectEvents.length + 1)
})

spyFetch.restore()
})

it('should handle empty Array', function* () {
it.skip('should handle empty Array', function* () {
httpBackend.whenGET(`${apiHost}/${path}`)
.respond([])

const spyFetch = spy(sdkFetch, 'get')
spyFetch = spy(sdkFetch, 'get')

const newLocation = 'new_event_location'

Expand Down Expand Up @@ -308,8 +313,6 @@ describe('Net test', () => {
expect(spyFetch.callCount).to.equal(2)
expect(events.length).to.equal(1)
})

spyFetch.restore()
})

it('should get result from cached Response and consumed by `values`', function* () {
Expand Down Expand Up @@ -444,7 +447,7 @@ describe('Net test', () => {
expect(r).to.deep.equal(projectEvents)
})

const spyFetch = spy(sdkFetch, 'get')
spyFetch = spy(sdkFetch, 'get')

const newLocation = 'new_event_location'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { eventParser, RequestEvent } from '../index'
import { RequestEvent } from 'teambition-sdk-mock'
import { describe, beforeEach, it } from 'tman'
import { expect } from 'chai'

import { eventParser } from '../index'

describe('eventParser', () => {

let event: RequestEvent
Expand Down
File renamed without changes.
Loading

0 comments on commit c253d5c

Please sign in to comment.