Skip to content

Commit a25f5e7

Browse files
committed
beginnings of pbxProject interface
1 parent 3db8635 commit a25f5e7

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

lib/parseJob.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// parsing is slow and blocking right now
2+
// so we do it in a separate process
3+
var fs = require('fs'),
4+
parser = require('./parser/pbxproj'),
5+
path = process.argv[2],
6+
fileContents, obj;
7+
8+
try {
9+
fileContents = fs.readFileSync(path, 'utf-8'),
10+
obj = parser.parse(fileContents)
11+
process.send(obj)
12+
process.exit()
13+
} catch (e) {
14+
process.send(e)
15+
process.exit(1)
16+
}

lib/pbxProject.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var util = require('util'),
2+
EventEmitter = require('events').EventEmitter,
3+
path = require('path'),
4+
fork = require('child_process').fork
5+
6+
function pbxProject(filename) {
7+
this.filepath = path.resolve(filename)
8+
}
9+
10+
util.inherits(pbxProject, EventEmitter)
11+
12+
pbxProject.prototype.parse = function (cb) {
13+
var worker = fork(__dirname + '/parseJob.js', [this.filepath])
14+
15+
worker.on('message', function (msg) {
16+
if (msg.code) {
17+
this.emit('error', msg);
18+
} else {
19+
this.hash = msg;
20+
this.emit('end', null, msg)
21+
}
22+
}.bind(this));
23+
24+
if (cb)
25+
this.on('end', cb);
26+
27+
return this;
28+
}
29+
30+
module.exports = pbxProject;

test/pbxProject.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var pbx = require('../lib/pbxProject')
2+
3+
exports['parse function'] = {
4+
'should emit an "end" event': function (test) {
5+
var myProj = new pbx('test/parser/projects/hash.pbxproj');
6+
7+
myProj.parse().on('end', function (err, projHash) {
8+
test.done();
9+
})
10+
},
11+
'should take the end callback as a parameter': function (test) {
12+
var myProj = new pbx('test/parser/projects/hash.pbxproj');
13+
14+
myProj.parse(function (err, projHash) {
15+
test.done();
16+
})
17+
},
18+
'should allow evented error handling': function (test) {
19+
var myProj = new pbx('NotARealPath.pbxproj');
20+
21+
myProj.parse().on('error', function (err) {
22+
test.equal(typeof err, "object");
23+
test.done();
24+
})
25+
},
26+
'should pass the hash object to the callback function': function (test) {
27+
var myProj = new pbx('test/parser/projects/hash.pbxproj');
28+
29+
myProj.parse(function (err, projHash) {
30+
test.ok(projHash);
31+
test.done();
32+
})
33+
},
34+
'should attach the hash object to the pbx object': function (test) {
35+
var myProj = new pbx('test/parser/projects/hash.pbxproj');
36+
37+
myProj.parse(function (err, projHash) {
38+
test.ok(myProj.hash);
39+
test.done();
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)