Comparing version 0.11.2 to 0.11.3
0.11.3 / 2016-02-15 | ||
=================== | ||
* cleaning up stringified tree | ||
0.11.2 / 2016-02-11 | ||
@@ -3,0 +8,0 @@ =================== |
@@ -6,3 +6,2 @@ | ||
let extension = require('file-extension'); | ||
let iso = require('regex-iso-date')(); | ||
let path = require('path'); | ||
@@ -173,17 +172,8 @@ | ||
toJSON() { | ||
return this.clone(null); | ||
let clone = this.clone(null); | ||
delete clone.tree; | ||
return clone; | ||
} | ||
/** | ||
* Serializes the file into a plain JSON string for writing to storage. | ||
* (probably disk) | ||
* | ||
* @param {Number} space The JSON.stringify space parameter. | ||
* @return {String} | ||
*/ | ||
toString(space) { | ||
return JSON.stringify(this, null, space); | ||
} | ||
/** | ||
* Used to parse a string value into a usable file. | ||
@@ -196,9 +186,8 @@ * | ||
*/ | ||
static fromString(input, tree) { | ||
debug('creating file from string'); | ||
let o = JSON.parse(input, reviver); | ||
let file = new File(o.path, null, o.entry); | ||
Object.assign(file, o); | ||
static fromObject(input, tree) { | ||
debug('creating file instance from a plain object'); | ||
let file = new File(input.path, null, input.entry); | ||
Object.assign(file, input); | ||
file.tree = tree; | ||
debug('done creating file from string'); | ||
debug('done creating file instance'); | ||
return file; | ||
@@ -211,21 +200,1 @@ } | ||
module.exports = File; | ||
/** | ||
* JSON.parse reviver param for restoring buffers and dates to file objects. | ||
* | ||
* @param {String} key See JSON.parse reviver documentation | ||
* @param {String} value See JSON.parse reviver documentation | ||
* @return {Mixed} | ||
*/ | ||
function reviver(key, value) { | ||
if (value && value.type === 'Buffer') { | ||
return new Buffer(value.data); | ||
} | ||
if (typeof value === 'string' && iso.test(value)) { | ||
return new Date(value); | ||
} | ||
return value; | ||
} |
@@ -9,2 +9,3 @@ | ||
let File = require('./file'); | ||
let iso = require('regex-iso-date')(); | ||
let Graph = require('graph.js/dist/graph.js'); | ||
@@ -366,13 +367,6 @@ let path = require('path'); | ||
toJSON() { | ||
let vertices = []; | ||
for (let vertex of this.graph.vertices()) { | ||
vertices.push([ vertex[0], vertex[1].toString() ]); | ||
} | ||
let edges = []; | ||
for (let edge of this.graph.edges()) { | ||
edges.push([ edge[0], edge[1] ]); | ||
} | ||
return { vertices: vertices, edges: edges }; | ||
return { | ||
files: this.getFiles({ objects: true }), | ||
dependencies: Array.from(this.graph.edges()).map(e => e.slice(0, 2)) | ||
}; | ||
} | ||
@@ -402,10 +396,12 @@ | ||
let tree = new Tree(); | ||
let parsed = JSON.parse(input); | ||
let parsed = JSON.parse(input, reviver); | ||
parsed.vertices.forEach(v => { | ||
debug('file from cache', v[0]); | ||
tree.graph.addNewVertex(v[0], File.fromString(v[1], tree)); | ||
parsed.files.forEach(o => { | ||
let file = File.fromObject(o, tree); | ||
debug('file from cache', relative(file.path)); | ||
tree.graph.addNewVertex(file.path, file); | ||
}); | ||
parsed.edges.forEach(e => { | ||
debug('dependency from cache: %s', e.join(' ')); | ||
parsed.dependencies.forEach(e => { | ||
debug('dependency from cache: %s', e.map(v => relative(v)).join(' ')); | ||
tree.graph.addNewEdge(e[0], e[1]); | ||
@@ -437,1 +433,20 @@ }); | ||
} | ||
/** | ||
* JSON.parse reviver param for restoring buffers and dates to file objects. | ||
* | ||
* @param {String} key See JSON.parse reviver documentation | ||
* @param {String} value See JSON.parse reviver documentation | ||
* @return {Mixed} | ||
*/ | ||
function reviver(key, value) { | ||
if (value && value.type === 'Buffer') { | ||
return new Buffer(value.data); | ||
} | ||
if (typeof value === 'string' && iso.test(value)) { | ||
return new Date(value); | ||
} | ||
return value; | ||
} |
{ | ||
"name": "mako-tree", | ||
"version": "0.11.2", | ||
"version": "0.11.3", | ||
"main": "./lib/tree", | ||
@@ -17,9 +17,9 @@ "description": "The build tree structure used internally by mako", | ||
"devDependencies": { | ||
"@dominicbarnes/eslint-config": "^0.2.1", | ||
"@dominicbarnes/eslint-config-node": "^0.2.1", | ||
"@dominicbarnes/eslint-config-test": "^0.1.1", | ||
"@dominicbarnes/eslint-config": "^1.0.0", | ||
"@dominicbarnes/eslint-config-node": "^1.0.1", | ||
"@dominicbarnes/eslint-config-test": "^1.0.0", | ||
"chai": "^3.2.0", | ||
"chai-as-promised": "^5.1.0", | ||
"eslint": "^1.3.1", | ||
"eslint-plugin-mocha": "^1.1.0", | ||
"eslint": "^2.1.0", | ||
"eslint-plugin-mocha": "^2.0.0", | ||
"eslint-plugin-require-path-exists": "^1.0.15", | ||
@@ -26,0 +26,0 @@ "mocha": "^2.3.0" |
@@ -288,25 +288,11 @@ | ||
let a = tree.addFile('a.txt', true); | ||
assert.isNull(a.toJSON().tree); | ||
assert.isUndefined(a.toJSON().tree); | ||
}); | ||
}); | ||
describe('#toString([space])', function () { | ||
it('should completely stringify to JSON', function () { | ||
let file = new File('a.txt', null, true); | ||
assert.deepEqual(JSON.parse(file.toString()), { | ||
path: 'a.txt', | ||
type: 'txt', | ||
entry: true, | ||
analyzing: false, | ||
analyzed: false, | ||
tree: null | ||
}); | ||
}); | ||
}); | ||
describe('.fromString(input, tree)', function () { | ||
describe('.fromObject(input, tree)', function () { | ||
it('should parse a JSON string into a file instance', function () { | ||
let file = new File('a.txt', null, true); | ||
let actual = File.fromString(file.toString()); | ||
let actual = File.fromObject(file.toJSON()); | ||
assert.instanceOf(actual, File); | ||
@@ -325,3 +311,3 @@ assert.strictEqual(actual.path, 'a.txt'); | ||
let actual = File.fromString(file.toString()); | ||
let actual = File.fromObject(file.toJSON()); | ||
assert.instanceOf(actual.modified, Date); | ||
@@ -335,3 +321,3 @@ assert.strictEqual(actual.modified.getTime(), now.getTime()); | ||
let actual = File.fromString(file.toString()); | ||
let actual = File.fromObject(file.toJSON()); | ||
assert.isTrue(Buffer.isBuffer(actual.contents)); | ||
@@ -345,3 +331,3 @@ assert.strictEqual(actual.contents.toString(), 'hello world'); | ||
let actual = File.fromString(file.toString(), tree); | ||
let actual = File.fromObject(file.toJSON(), tree); | ||
assert.strictEqual(actual.tree, tree); | ||
@@ -348,0 +334,0 @@ }); |
@@ -676,7 +676,4 @@ | ||
assert.deepEqual(tree.toJSON(), { | ||
vertices: [ | ||
[ 'a', a.toString() ], | ||
[ 'b', b.toString() ] | ||
], | ||
edges: [ | ||
files: [ a, b ], | ||
dependencies: [ | ||
[ 'b', 'a' ] | ||
@@ -697,7 +694,4 @@ ] | ||
assert.strictEqual(tree.toString(), JSON.stringify({ | ||
vertices: [ | ||
[ 'a', a.toString() ], | ||
[ 'b', b.toString() ] | ||
], | ||
edges: [ | ||
files: [ a, b ], | ||
dependencies: [ | ||
[ 'b', 'a' ] | ||
@@ -704,0 +698,0 @@ ] |
63327
1441