I have a .plist file on my mac that I would like to read. I can easily open it using Xcode but would like to do it using NodeJS.
I found plist package and I'm trying to use it like this:
import fs from 'fs';
import plist from 'plist';
fs.readFile('/my/path/here.plist', 'utf-8', (err, data) => {
console.log('#data', data);
const obj = plist.parse(data);
console.log('#obj', obj);
});
What I'm getting as data is looking like this (a part from that file)
dataYdisplayasZfile-label]file-mod-date[arrangement_preferreditemsize��- O(book(0$Usersnyc Downloads �i(� +� HXhA���L� file:///Macintosh
plist package is throwing errors as I guess it expects string in XML format.
How can I read a .plist file like this?
Ok, found the solution. Guess posting to SO gives me more luck in googling...
macOS .plist files are mostly in binary. There is a command plutil that allows you to convert to binary to xml, so to do this in node you have to:
import { exec } from 'child_process';
const command = 'plutil -convert xml1 ./path/to/binary/file.plist';
exec(command, callback); // Here you gonna convert this file to plain `xml`.
Then you just have to repeat my steps from above - read the file using fs.readFile and then convert it using plist package so you get a nice JS object.
Hope this helps someone!
I’d recommend using an appropriate library such as bplist-parser (read-only) or simple-plist (read-write, wraps bplist-parser and bplist-creator, also handles plain text plist) instead of using the child_process module. Don’t forget that exec needs special care (e.g. arguments escaping) and it’s easy to shoot yourself in the foot using it!
import { parseFile } from 'bplist-parser';
parseFile('./path/to/binary/file.plist', callback);
Related
I'm currently using "require(./file.json)" because I can't get anything else to work.
So I want to be able to reload config file so I can disable / enable some commands in my bot without having to restart it.
Any Help or Suggestion will be appreciated!
If JSON doesn't work then any other file that can be used as a "config/db" is fine!
Thank you!
To read a JSON file, you need to use the built-in fs module.
const fs = require('fs');
const myFile = JSON.parse(fs.readFileSync('file.json'));
//edit myFile however you want
fs.writeFileSync('file.json', JSON.stringify(myFile, null, 4));
use fs to read file contents, instead of include or require, When you make include, it will read once in run-time only.
Also you can use require inside functions, which makes read in every function call. This is not pretty (In my opinion), But it works.
I want to open a binary file, or at least when I try to open this with the vscode editor, is say that, can't be opened, because is a binary file.
Can someone explain to me what I can do in order to open this type of files and read the content?
About the .nii file format. is a NIFTI1 and used on medical visualization like MRI.
What I trying to do is to read this file at the lowest level and then make some computations.
I will like to use Node.js for this, not any Python or C++.
More details about the file format can be found here.
https://nifti.nimh.nih.gov/
I don't know about how VScode handle binary file but for exemple with Atom (or with another text editor like vi), you can open and view the content of a binary file. This is not very usefull however as the content is not particularly human readable, except maybe some metadata at the top of the file.
$ vim yourniifile.nii
Anyway, it's all depends on what you want to do with that file, which "computation" you're planned to apply to it, and how you will use it after that.
Luckily, there are some npm packages that can help you with the task of reading and processing that kind of file, like nifti-reader-js or nifti-js, for exemple:
const fs = require('fs');
const niftijs = require('nifti-js');
let rawData = fs.readFileSync('yourniifile.nii');
let data = niftijs.parse(rawData);
console.log(data);
I am trying to copy the definitions portion of a yaml file into a js doc for a codegen project. I tried using regular expressions (which worked great for copying methods out of swagger generated js files) but apparently regexp does not handle info from yaml files very well. I managed to print MOST of what I want to the commandline through console.log. There are a few arrays that just say [Object], which is problematic. I would like to have their full contents printed. HOWEVER, this is not the main problem. When I try to write this output to a file instead of the console...it just says
"[object Object]
[object Object]"
for my 2 definitions. Has anyone done anything like this before? Here is a snippet of my code and what the console output looks like vs the two lines above TIA!
var doc = yaml.safeLoad(fs.readFileSync('path to my file\swagger.yaml', 'utf8'));
for(var d in doc['definitions']){
logit(doc['definitions'][d]); //logit write to consle and a file
}
safeLoad suggests you are using the js-yaml library. It also provides the safeDump and dump methods.
yamlDef= yaml.safeDump(doc['definitions'][d]);
logit(yamlDef);
to convert YAML to JSON:
var json = JSON.stringify(yamlDef);
I am using rn-nodeify for enabling the use of pdf2json in React Native. pdf2json uses fs for loading files using the method readFileSync. I am getting this error when I try to use the library:
fs.readFileSync is not a function. (In 'fs.readFileSync(_basePath + fieldName, 'utf8')', 'fs.readFileSync' is undefined)
I cannot find any support for this issue. Any pointers appreciated.
Edit: Please note that I am not trying to run this in a browser. This pertains to react-native i.e. it runs on a device, and the code should have access to the file system.
FWIW, I came across this problem while writing tests in ES6/latest JS - fixed this by changing the import from:
import { fs } from 'fs';
to
import fs from 'fs';
Notice the unnecessary {} - fs is the default export from the fs node module and should be imported in the latter way
For a better explanation on default & named exports please see this SO discussion
i am using pdfkit for convert for create PDF file and it's work fine with this code.
var fs = require('fs');
var PDFDocument = require('pdfkit');
pdf.text('{{ }} text ');
pdf.pipe(
fs.createWriteStream('./data/file1.pdf')
)
.on('finish', function () {
console.log('PDF closed');
});
// Close PDF and write file.
pdf.end();
this code are work pretty but i want to convert chines text and some special character too with a-z text for example i want to make pdf of
pdf.text('{{ }} text 漢字 昨夜のコンサートは最高でした');
this is not give me proper output .
Unfortunately PDFKit doesn't seem to support converting documents to PDF:
The documentation states nothing about converting. This module seems to be purely for creating the documents from scratch.
You'll have to find a module or create something that takes an MS Doc and converts it to text, store this in your node app, then pass it through to PDFKit via the methods shown in the documentation.
Mammoth seems to do this.
There's a bunch of docx modules that might do an even better job.
Hope this helps!