I'm getting into TypeScript as a way to write simple utility programs, meant to be run on the mand line. There are a bunch of basic questions I have, maybe someone can give me pointers.
- I am assuming that I will run the app by a mand like:
$ node myapp.js
- If I want to run it by
$ myapp.js
I need a shebang line in the js, but I can't see how to have that flow from the .ts file, so I have to post edit the js file, right? - How do I just open a text file from a local path within TypeScript? Where do I look for the right incantation?
- And once I have the text file open I have to process it as a cvs. I found a library called node-cvs but I am not sure how to connect the dots.
I'm getting into TypeScript as a way to write simple utility programs, meant to be run on the mand line. There are a bunch of basic questions I have, maybe someone can give me pointers.
- I am assuming that I will run the app by a mand like:
$ node myapp.js
- If I want to run it by
$ myapp.js
I need a shebang line in the js, but I can't see how to have that flow from the .ts file, so I have to post edit the js file, right? - How do I just open a text file from a local path within TypeScript? Where do I look for the right incantation?
- And once I have the text file open I have to process it as a cvs. I found a library called node-cvs but I am not sure how to connect the dots.
- 1 It should be CSV (Comma Separated Values) not CVS in the title. – Meryan Commented Dec 7, 2021 at 16:30
2 Answers
Reset to default 6
- I am assuming that I will run the app by a mand like:
$ node myapp.js
Yes. TypeScript piles to JavaScript.
- If I want to run it by
$ myapp.js
I need a shebang line in the js, but I can't see how to have that flow from the .ts file, so I have to post edit the js file, right?
No. Take a look at my workaround : https://github./npm/npm/issues/6674#issuement-108132800
- How do I just open a text file from a local path within typescript? Where do I look for the right incantation?
fs.readFile
in node. You can use it in typescript (and even provide type safety using node.d.ts
: https://github./borisyankov/DefinitelyTyped/blob/master/node/node.d.ts
- And once I have the text file open I have to process it as a cvs. I found a library called node-cvs but I am not sure how to connect the dots.
You can just do require('node-cvs')
same as javascript
You would need to do something like:
/// <reference path="node.d.ts" />
/// <reference path="a-type-definition-for-node-csv.d.ts" />
import fs = require('fs');
import parse = require('node-csv');
var parser = parse({delimiter: ','}, function(err, data) {
console.log(data);
});
fs.createReadStream(__dirname+'/fs_read.csv').pipe(parser);