@@ -9,6 +9,13 @@ const CompileState = require('./object_factory').CompileState;
99const PatternGraphDot = require ( './pattern_graph_dot' ) ;
1010const PatternRegistry = require ( './pattern_registry' ) ;
1111
12+ /**
13+ * The most recent version of the pattern graph. This is used to rebuild the graph when
14+ * the version of a serialized graph does not match the current version.
15+ * @type {number }
16+ */
17+ const PATTERN_GRAPH_VERSION = 1 ;
18+
1219/**
1320 * Wrapper around a graph library to build a dependency graph of patterns.
1421 * Each node in the graph will maintain a {@link CompileState}. This allows finding all
@@ -21,13 +28,14 @@ const PatternRegistry = require('./pattern_registry');
2128 *
2229 * @param {Graph } graph The graphlib graph object
2330 * @param {int } timestamp The unix timestamp
31+ * @param {int } version The graph version.
2432 *
2533 * @returns {{PatternGraph: PatternGraph} }
2634
2735 * @see PatternGraph#fromJson
2836 * @see <a href="https://github.com/pattern-lab/patternlab-node/issues/540">#540</a>
2937 */
30- const PatternGraph = function ( graph , timestamp ) {
38+ const PatternGraph = function ( graph , timestamp , version ) {
3139
3240 this . graph = graph || new Graph ( {
3341 directed : true
@@ -38,6 +46,7 @@ const PatternGraph = function (graph, timestamp) {
3846 // The idea here is to make a pattern known to the graph as soon as it exists
3947 this . patterns = new PatternRegistry ( ) ;
4048 this . timestamp = timestamp || new Date ( ) . getTime ( ) ;
49+ this . version = version || PATTERN_GRAPH_VERSION ;
4150} ;
4251
4352// shorthand. Use relPath as it is always unique, even with subPatternType
@@ -53,7 +62,7 @@ PatternGraph.prototype = {
5362 clone : function ( ) {
5463 const json = graphlib . json . write ( this . graph ) ;
5564 const graph = graphlib . json . read ( json ) ;
56- return new PatternGraph ( graph , this . timestamp ) ;
65+ return new PatternGraph ( graph , this . timestamp , this . version ) ;
5766 } ,
5867
5968 /**
@@ -267,6 +276,7 @@ PatternGraph.prototype = {
267276 */
268277 toJson : function ( ) {
269278 return {
279+ version : this . version ,
270280 timestamp : this . timestamp ,
271281 graph : graphlib . json . write ( this . graph )
272282 } ;
@@ -277,26 +287,56 @@ PatternGraph.prototype = {
277287 */
278288 nodes : function ( ) {
279289 return this . graph . nodes ( ) ;
290+ } ,
291+
292+ /**
293+ * Updates the version to the most recent one
294+ */
295+ upgradeVersion : function ( ) {
296+ this . version = PATTERN_GRAPH_VERSION ;
280297 }
281298} ;
282299
283300/**
284301 * Creates an empty graph with a unix timestamp of 0 as last compilation date.
285- *
302+ * @param { int } [version=PATTERN_GRAPH_VERSION]
286303 * @return {PatternGraph }
287304 */
288- PatternGraph . empty = function ( ) {
289- return new PatternGraph ( null , 0 ) ;
305+ PatternGraph . empty = function ( version ) {
306+ return new PatternGraph ( null , 0 , version || PATTERN_GRAPH_VERSION ) ;
307+ } ;
308+
309+ /**
310+ * Checks if the version of
311+ * @param {PatternGraph|Object } graphOrJson
312+ * @return {boolean }
313+ */
314+ PatternGraph . checkVersion = function ( graphOrJson ) {
315+ return graphOrJson . version === PATTERN_GRAPH_VERSION ;
290316} ;
291317
318+ /**
319+ * Error that is thrown if the given version does not match the current graph version.
320+ *
321+ * @param oldVersion
322+ * @constructor
323+ */
324+ function VersionMismatch ( oldVersion ) {
325+ this . message = `Version of graph on disk ${ oldVersion } != current version ${ PATTERN_GRAPH_VERSION } . Please clean your patterns output directory.` ;
326+ this . name = "VersionMismatch" ;
327+ }
328+
292329/**
293330 * Parse the graph from a JSON object.
294331 * @param {object } o The JSON object to read from
295332 * @return {PatternGraph }
296333 */
297334PatternGraph . fromJson = function ( o ) {
335+ if ( ! PatternGraph . checkVersion ( o ) ) {
336+ throw new VersionMismatch ( o . version ) ;
337+ }
298338 const graph = graphlib . json . read ( o . graph ) ;
299- return new PatternGraph ( graph , o . timestamp ) ;
339+ return new PatternGraph ( graph , o . timestamp , o . version ) ;
300340} ;
301341
302342/**
@@ -328,6 +368,9 @@ PatternGraph.loadFromFile = function (patternlab, file) {
328368 }
329369
330370 const obj = fs . readJSONSync ( jsonGraphFile ) ;
371+ if ( ! PatternGraph . checkVersion ( obj ) ) {
372+ return PatternGraph . empty ( obj . version )
373+ }
331374 return this . fromJson ( obj ) ;
332375} ;
333376
@@ -356,5 +399,6 @@ PatternGraph.exportToDot = function (patternlab, file) {
356399} ;
357400
358401module . exports = {
359- PatternGraph : PatternGraph
402+ PatternGraph : PatternGraph ,
403+ PATTERN_GRAPH_VERSION : PATTERN_GRAPH_VERSION
360404} ;
0 commit comments