Skip to content

Commit

Permalink
Merge pull request #1 from dora-js/upgrade-node-isimulator
Browse files Browse the repository at this point in the history
refactor to node-isimulator@1.0.x, update usage and test cases
  • Loading branch information
soda-x committed Jul 1, 2016
2 parents 6fb61ec + ea0d76f commit 84a9566
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 149 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"parser": "babel-eslint",
"extends": "eslint-config-airbnb/base",
"rules": {
"no-console": [0]
Expand Down
22 changes: 15 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
language: node_js

node_js:
- "4"
- "5"

language:
- objective-c
osx_image: xcode7.3
env:
- DEVICE=9.2
install:
- rm -rf ~/.nvm
- git clone https://github.com/creationix/nvm.git ~/.nvm
- source ~/.nvm/nvm.sh
- nvm install v6.2.2
- node --version
- npm install
script:
- npm run test
after_success:
- npm run coveralls
- npm run coveralls
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

---

## 0.2.0

upgrade: node-isimulator to 1.0.x
brand new style

## 0.1.4

fix: npmjs bad layout
Expand Down
38 changes: 20 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,48 @@
"main": "./lib",
"scripts": {
"build": "rm -rf lib && babel src --out-dir lib",
"test": "babel-node $(npm bin)/babel-istanbul cover $(npm bin)/_mocha --no-timeouts",
"test": "babel-node $(npm bin)/babel-istanbul cover $(npm bin)/_mocha -- --no-timeouts",
"lint": "eslint --ext .js src test/*-test.js",
"coveralls": "cat ./coverage/lcov.info | coveralls"
},
"pre-commit": [
"lint"
],
"dependencies": {
"co": "^4.6.0",
"node-isimulator": "~0.1.7"
"lodash.isequal": "^4.2.0",
"node-isimulator": "^1.0.2"
},
"devDependencies": {
"babel-cli": "~6.2.0",
"babel-core": "~6.1.21",
"babel-istanbul": "^0.5.9",
"babel-plugin-add-module-exports": "~0.1.1",
"babel-preset-es2015": "~6.1.18",
"babel-preset-stage-0": "~6.1.18",
"coveralls": "^2.11.6",
"babel-cli": "^6.10.1",
"babel-core": "^6.10.4",
"babel-eslint": "^6.1.0",
"babel-istanbul": "^0.11.0",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-plugin-transform-runtime": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"babel-runtime": "^6.9.2",
"coveralls": "^2.11.9",
"dora": "*",
"dora-plugin-config-manager": "~0.2.2",
"eslint": "~1.10.3",
"eslint-config-airbnb": "~5.0.0",
"expect": "^1.14.0",
"mocha": "~2.4.5",
"pre-commit": "~1.1.2",
"sinon": "^1.17.3"
"eslint": "^2.7.0",
"eslint-config-airbnb": "^6.2.0",
"expect.js": "^0.3.1",
"mocha": "^2.5.3"
},
"babel": {
"presets": [
"es2015",
"stage-0"
],
"plugins": [
"add-module-exports"
"add-module-exports",
"transform-runtime"
]
},
"files": [
"lib",
"package.json",
"README.md"
]
}
}
52 changes: 10 additions & 42 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,30 @@
import { type } from 'os';
import Simulator from 'node-isimulator';
import co from 'co';
import EventEmitter from 'events';
const eventEmitter = new EventEmitter();

import start from 'node-isimulator';
import isEqual from 'lodash.isequal';
import mergeCfg from './mergeCfg';

let lastOpts = {};
let simOpts = {};
let sim = {};

eventEmitter.on('initializationSimOpts', () => {
if (type() === 'Darwin') {
sim = new Simulator(lastOpts);
co(sim.start(lastOpts.scheme));
}
});

eventEmitter.on('changeSimOptsNeedReopen', () => {
if (type() === 'Darwin') {
co(sim.killAll());
}
eventEmitter.emit('initializationSimOpts');
});

eventEmitter.on('changeSimOptsDoNotNeedReopen', () => {
if (type() === 'Darwin') {
sim.scheme = lastOpts.scheme;
co(sim.start(lastOpts.scheme));
}
});

export default {
'server.before'() {
const { query, get, set } = this;
set('simulatorEmitter', eventEmitter);
async 'server.before'() {
const { query, get } = this;

simOpts = get('_global_simOpts') || {};
lastOpts = mergeCfg(query, simOpts);

eventEmitter.emit('initializationSimOpts');

const configManagerEmitter = get('configManagerEmitter');
if (!configManagerEmitter) {
return;
}

configManagerEmitter.on('_global_simOpts', () => {
configManagerEmitter.on('_global_simOpts', async () => {
simOpts = get('_global_simOpts');
const changeFinalOpts = mergeCfg(query, simOpts);
if (changeFinalOpts.application !== lastOpts.application
|| changeFinalOpts.device !== lastOpts.device
|| changeFinalOpts.os !== lastOpts.os) {
lastOpts = changeFinalOpts;
eventEmitter.emit('changeSimOptsNeedReopen');
} else if (changeFinalOpts.scheme !== lastOpts.scheme) {
lastOpts = changeFinalOpts;
eventEmitter.emit('changeSimOptsDoNotNeedReopen');
if (!isEqual(lastOpts, changeFinalOpts)) {
await start(changeFinalOpts);
}
});

await start(lastOpts);
},
};
9 changes: 4 additions & 5 deletions src/mergeCfg.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
function mergeCfg(query, globalSimOpts) {
const options = {
prefix: 'antm',
application: 'mobilesafari',
device: 'iPhone-6',
os: '',
appPath: '',
bundleId: '',
device: 'iPhone 6',
downloadURL: '',
prefix: 'antm',
scheme: 'http://127.0.0.1',
verbose: false,
sdk: '',
};
const finalOpts = { ...options, ...query, ...globalSimOpts };

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/mobile.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var simOpts = { scheme: 'http://m.baidu.com' }; module.exports.simOpts = simOpts;
var simOpts = { prefix:'doraPlugin', scheme: 'http://m.baidu.com' }; module.exports.simOpts = simOpts;
133 changes: 72 additions & 61 deletions test/index-test.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,88 @@
import { join } from 'path';
import { writeFileSync } from 'fs';

import sinon from 'sinon';
import expect from 'expect';
import expect from 'expect.js';
import dora from 'dora';
import { simUtil } from 'node-isimulator';

const oldCwd = process.cwd();
const fixtures = join(oldCwd, 'test/fixtures');
let simulatorEmitter = {};
const spyInitial = sinon.spy();
const spyReOpen = sinon.spy();
const spyJustOpen = sinon.spy();

describe('index', function sim() {
this.timeout(25000);
before(done => {
process.chdir(fixtures);
dora({
port: 12346,
plugins: [
'config-manager?path=./mobile.config.js|simOpts',
'../../src/index?{scheme:"http://127.0.0.1:12345"}',
{
'server.after': function after() {
const { get } = this;
simulatorEmitter = get('simulatorEmitter');
simulatorEmitter.on('initializationSimOpts', spyInitial);
simulatorEmitter.on('changeSimOptsNeedReopen', spyReOpen);
simulatorEmitter.on('changeSimOptsDoNotNeedReopen', spyJustOpen);
},
},
],
cwd: fixtures,
verbose: true,
});
setTimeout(done, 15000);
describe('index', () => {
const simOpts = {
sdk: '',
udid: '',
};
before(async done => {
try {
simOpts.sdk = await simUtil.getLatestSDK();
process.chdir(fixtures);
dora({
port: 12346,
plugins: [
'config-manager?path=./mobile.config.js|simOpts',
'../../src/index',
],
cwd: fixtures,
verbose: true,
}, done);
} catch (e) {
done(e);
}
});

after(done => {
writeFileSync(
join(fixtures, './mobile.config.js'),
`var simOpts = { scheme: 'http://m.baidu.com' }; module.exports.simOpts = simOpts;`,
'utf-8'
);
process.chdir(oldCwd);
done();
afterEach(async done => {
try {
await simUtil.killAllSimulators();
await simUtil.deleteDevice(simOpts.udid);
simOpts.udid = '';
if (simOpts.rewrite) {
writeFileSync(
join(fixtures, './mobile.config.js'),
"var simOpts = { prefix:'doraPlugin', scheme: 'http://m.baidu.com' }; " +
'module.exports.simOpts = simOpts;',
'utf-8'
);
simOpts.rewrite = false;
}
done();
} catch (e) {
done(e);
}
});

it('changeSimOptsDoNotNeedReopen should called', done => {
writeFileSync(
join(fixtures, './mobile.config.js'),
`var simOpts = { scheme: 'http://m.taobao.com' }; module.exports.simOpts = simOpts;`,
'utf-8'
);

setTimeout(() => {
expect(spyReOpen.called).toEqual(false);
expect(spyJustOpen.called).toEqual(true);
it('should create a simulator named doraPluginSim--iPhone-6--${latestsdk}', async done => {
try {
const normalizeSDK = simOpts.sdk.replace(/\./, '-');
const simName = `doraPluginsim--iPhone-6--${normalizeSDK}`;
const sim = await simUtil.getUdidBySimName(simName);
expect(sim).to.not.be.empty();
simOpts.udid = sim[0];
done();
}, 10000);
} catch (e) {
done(e);
}
});

it('changeSimOptsNeedReopen should called', done => {
writeFileSync(
join(fixtures, './mobile.config.js'),
`var simOpts = { device: 'iPhone-5s', scheme: 'https://www.npmjs.com/package/dora-plugin-simulator' }; module.exports.simOpts = simOpts;`,
'utf-8'
);

setTimeout(() => {
expect(spyReOpen.called).toEqual(true);
expect(spyInitial.called).toEqual(true);
done();
}, 15000);
it('change mobile.config.js should create doraPluginSim--iPhone-5s--${latestsdk}', done => {
try {
writeFileSync(
join(fixtures, './mobile.config.js'),
"var simOpts = { prefix:'doraPlugin', device: 'iPhone 5s' }; " +
'module.exports.simOpts = simOpts;',
'utf-8'
);
setTimeout(async () => {
const normalizeSDK = simOpts.sdk.replace(/\./, '-');
const simName = `doraPluginsim--iPhone-5s--${normalizeSDK}`;
const sim = await simUtil.getUdidBySimName(simName);
console.log(sim)
expect(sim).to.not.be.empty();
simOpts.udid = sim[0];
simOpts.rewrite = true;
done();
}, 2000);
} catch (e) {
done(e);
}
});
});
Loading

0 comments on commit 84a9566

Please sign in to comment.