dimanche 4 mars 2018

ember-electron use fs in third party npm module

Currently I need a third party npm package in my ember-electron app, that requires acces to the file system (ngraph.offline.layout, which also has several dependencies.

It uses the following function:

function createLayout(graph, options) {


options = options || {};
  var iterations = typeof options.iterations === 'number' ? options.iterations : 500;
  var saveEach = typeof options.saveEach === 'number' ? options.saveEach : 5;
  var outDir = typeof options.outDir === 'string' ? options.outDir : './data';
  var is2d = options.is2d ? true : false;
  var coordinatesPerRecord = is2d ? 2 : 3;
  var intSize = 4;
  var layouter = is2d ? layout3d.get2dLayout : layout3d;
  var layout = layouter(graph);
  if (!fs.existsSync(outDir)) {
    mkdirp.sync(outDir);
  }
  var lastIteration = getLastIteration(outDir);

  return {
    run: run,
    lastIteration: getLastIteration
  };

In my ember controller I have imported it like that:

import createLayout from 'npm:ngraph.offline.layout';

and then

g=graphGenerator.wattsStrogatz(1000, 10, 0.50);
 var layout = createLayout(g);
 layout.run();

But I always get the following error : enter image description here

However when I do another request to the filesytem directly in my controller my own function works fine:

import toDot from 'npm:ngraph.todot';
var fs = requireNode('fs');
var stream = fs.createWriteStream('graphDot.txt',{
        flags:'a'
      });

toDot.write(g, function customWriter(line){
        stream.write(line + '\n');
      })
      stream.end();

What exactly is the difference? As the module performs an offline layout of a graph, it should be executed on the "server" and I was wondering if this is the correct approach, anyway.

Thanks for your help!




Aucun commentaire:

Enregistrer un commentaire