Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File IO? #79

Closed
fluffynuts opened this issue May 19, 2020 · 20 comments
Closed

File IO? #79

fluffynuts opened this issue May 19, 2020 · 20 comments

Comments

@fluffynuts
Copy link

Hi

I see that support for "fs" is still on the way, so just asking out of curiosity -- is there currently any other way to do file IO?

@adrien-thierry
Copy link
Contributor

Hi @fluffynuts ,
Yes, fs is not finished yet, but you can access file by adding a module with native c++ call with ffi

You can check the example/test.js file that call windows popup for example

I will update this issue when fs is updated

@adrien-thierry
Copy link
Contributor

@fluffynuts I added the fs-std module for simple fs support, it will be improved soon

see 94689cf

@adrien-thierry adrien-thierry self-assigned this May 20, 2020
@raguay
Copy link

raguay commented Jun 30, 2020

That helps, but I still can't compile my simple script:

var fs = require("fs-std");   // use with nectar.js
//var fs = require("fs");     // use with node.js
var process = require("process");

var inputFile = fs.readFileSync(process.argv[2]);
var output = '{"items": [';
var first = true;
inputFile = inputFile.toString();
inputFile = inputFile.split('\n');
for(var i = 0; i < inputFile.length; i++) {
  if(inputFile[i] !== "") {
    if(!first) {
      output += ",";
    } else {
      first = false;
    }
    var subitem = {
          uid: inputFile[i],
          type: "",
          title: inputFile[i],
          subtitle: process.argv[3],
          arg: inputFile[i],
          autocomplete: inputFile[i],
          valid: true,
          icon: {
              type: "fileicon",
              path: "~/Desktop"
          }
      };
    output += JSON.stringify(subitem);
  }
}

console.log(output + "]}");

It processes a list of names in a file and presents it to an Alfred Script filter. The resulting JSON file is large, so a compiled version would be preferable.

@adrien-thierry
Copy link
Contributor

@raguay i will check it

@raguay
Copy link

raguay commented Jun 30, 2020

@raguay i will check it

Thanks. Looking forward to a solution. It'll make this a great way to build cli utilities. I've been using Haskel, but I'm constantly having issues with linked libraries.

@adrien-thierry
Copy link
Contributor

@raguay I added an example in the examples folder named read_file_to_json.js

I Updated NectarJS to make it working, should work with version v0.6.68

@raguay
Copy link

raguay commented Jul 1, 2020 via email

@raguay
Copy link

raguay commented Jul 1, 2020

I just optimized the code some more and it will compile, but not run? It looks like you can't push items onto an array? What would be the recommened way? The new code is:

var fs = require("fs-std");
var process = require("process");

var inputFile = fs.readFileSync(process.argv[2]);
var output = {};
output.items = [];
inputFile = inputFile.toString().split("\n");
for(var i = 0; i < inputFile.length; i++) {
  if(inputFile[i] != "") {
    var subitem = {
          uid: inputFile[i],
          type: "",
          title: inputFile[i],
          subtitle: process.argv[3],
          arg: inputFile[i],
          autocomplete: inputFile[i],
          valid: true,
          icon: {
              type: "fileicon",
              path: "~/Desktop"
          }
      };
    output.items.push(subitem);
  }
}

console.log(JSON.stringify(output));

By doing the JSON.stringify once, I'm figuring it would save a lot of time!

@adrien-thierry
Copy link
Contributor

@raguay the array.push function does not exist at the moment, I will add it

@adrien-thierry
Copy link
Contributor

@raguay it should be okay, i added array.push method, could you try again ?

@raguay
Copy link

raguay commented Jul 2, 2020 via email

@raguay
Copy link

raguay commented Jul 2, 2020 via email

@adrien-thierry
Copy link
Contributor

@raguay : Chaining functions is not working yet, i add it to the roadmap,

You can make your script working by removing the toString() call on inputFile :

inputFile = inputFile.split("\n");

@raguay
Copy link

raguay commented Jul 2, 2020 via email

@adrien-thierry
Copy link
Contributor

@raguay function chaining is now available,
i'm correcting a bug in the object construction (subobject keys are undefined)

@adrien-thierry
Copy link
Contributor

The sub object bug is corrected, your code is working good now

@raguay
Copy link

raguay commented Jul 3, 2020 via email

@adrien-thierry
Copy link
Contributor

Did you try to compile with the --preset speed flag ?

@raguay
Copy link

raguay commented Jul 3, 2020 via email

@raguay
Copy link

raguay commented Jul 3, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants