Link to reading, editing and saving INI files in node js - node.js

I tried searching for an entire day. Either i am blind or there is no specific answer to my question. How do i do it? I'm developing a steam bot in node js and i want to load accounts - username, pass etc.. of couple of accounts from .ini file. After a long time searching i only found how to read from an .ini file using node-ini module.
var ini = require('node-ini');
ini.parse('./config.ini', function(err,data){
console.log( data.Admin.pass ); //working.
data.Admin.pass = '1136'; //not working how to change value in ini.
data.Admin.H = 'test'; //not working as well.
});

This library (https://github.com/npm/ini) is a good option to do what you are looking to achieve.
Checkout this example from the docs:
var fs = require('fs')
, ini = require('ini')
var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'))
config.scope = 'local'
config.database.database = 'use_another_database'
config.paths.default.tmpdir = '/tmp'
delete config.paths.default.datadir
config.paths.default.array.push('fourth value')
fs.writeFileSync('./config_modified.ini', ini.stringify(config, { section: 'section' }))

Related

Encoding issue with reading markdown files on Github

When I load a markdown file from GitHub I am running into a lot of errors. I think I am not using the right encoding for GitHub files through Octokit. Any suggestions on how to fix my Buffer code in Node.js?
Is base64 and then to ascii correct for Github content? It works fine when loading it directly in my project without GitHub. I have a feeling GitHub stores their files in a different format but can't find docs on it.
const repos = await octokit.repos.getContents({
owner: 'owner-hidden',
repo: 'repo-hidden'
path: '/dinner.md
});
// repo loads with data.content just fine
const bufferedData = Buffer.from(repos.data.content, 'base64').toString('ascii');
const ymlData = YAML.parse(bufferedData); ## issue with reading this
The error is below, but the error doesn't necessarily matter because it works when I load it directly in my project there are no errors.
YAMLException: the stream contains non-printable characters at line 36, column 126:
... auteLed spinach and ratatouille
^
Loading the markdown file directly in my project there was no errors:
const fs = require('fs');
const path2 = require('path');
const file = path2.resolve(__dirname, '/dinner.md');
const content = fs.readFileSync(file);
const bufferedData = Buffer.from(content).toString('ascii');
console.log({bufferedData});
As one of the members of Octokit replied to me on my Github issue, I don't need to encode with ascii, I should be using uft8 as shown here:
- const bufferedData = Buffer.from(repos.data.content, 'base64').toString('ascii')
- const bufferedData = Buffer.from(repos.data.content, 'base64').toString()
buffer.toString() defaults to utf8 which is what I want.

NodeJS Read File Name is Japanese Characters

I have a file name is momo1.json. I can read them in Nodejs
var fs = require('fs');
var jsonWelcome = fs.readFileSync(WELCOME_JSON, UTF8_FORMAT);
var dataWelcome = JSON.parse(jsonWelcome);
But now i change file name is ももたろう.json. I cant read file anymore, tell my why and solutions i can read file name by japanese charaters
Try to use require() instead:
var dataWelcome = require("./ももたろう.json");
//output the data to see if it works
console.log(JSON.stringify(dataWelcome));
Live Demo

Extract a specific folder from a zip file using node.js

I have a zip file with the following structure:
download.zip\Temp\abc.txt
download.zip\Temp\Foo\abc2.txt
I want to extract the content under Temp in download.zip to a directory say D:\work_del.
This directory after extraction of zip should have abc.txt and Foo\abc2.txt
I am using adm-zip module of node but that doesn't seem to help. (Below code for reference).
var zip = require('adm-zip');
var file = new zip("D:\\Work\\download.zip");
file.extractEntryTo("Temp", 'D:\\Work_delete', false, true);
Any pointers to get the above the scenario working in node.js?
var zip = require('adm-zip');
var file = new zip("D:\\Work\\download.zip");
file.extractEntryTo("Temp/abc.txt", 'D:\\Work_delete', false, true);
The thing that I noticed is that if you specify the path as Temp\\1.txt it doesn't work. So try to avoid backslashes as forward slashes work perfectly fine in Windows with Node.js.
var zip = require('adm-zip');
var file = new zip("C:/Users/harslo/Desktop/node/Download.zip");
file.extractEntryTo("Temp/abc.txt", 'C:/Users/harslo/Desktop/node/Work_delete', false, true);
If you want to extract all the files inside of a folder use FolderName/ as described in adm-zip docs docs.
PS - ADM-ZIP extractEntryTo doesn't seem to be working with zips created with Windows Inbuilt "Send to ZIP".
var zip = require('adm-zip');
var file = new zip("D:/Work/download.zip");
file.extractEntryTo("Temp/", "D:/Work_delete", false, true);

How to read and parse gitconfig file with node.js?

[user]
name = Alvin J. Alexander
email = [omitted]
[merge]
tool = vimdiff
This is what ~/.gitconfig file looks like. I've never encountered such data objects before. Does this format have a name like json files? Or is this a custom format?
My goal is to extract data from this file to fill out a package.json template. I want to research this format to better understand how to parse it. Do parsing functions already exist for this?
For Reference
This is a template for how to parse it:
(requires iniparser module to be installed)
var iniparser = require('iniparser');
var fs = require('fs');
var home_dir = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
console.log (home_dir);
var config_file = home_dir+'/.gitconfig';
var exists = fs.existsSync(config_file);
if (exists) {
console.log("Getting some information from the git configuration...");
var config = iniparser.parseSync(config_file);
console.log(config);
return config;
}
else {
console.log("Git configuration file does not exist...");
return {};
};
This file is a ini file. You can try this parser, but any node-ini parser should do the job :).

Connect and Express utils

I'm new in the world of Node.js
According to this topic: What is Node.js' Connect, Express and “middleware”?
I learned that Connect was part of Express
I dug a little in the code, and I found two very interesting files :
./myProject/node_modules/express/lib/utils.js
and better :
./myProject/node_modules/express/node_modules/connect/lib/utils.js
These two files are full of useful functions and I was wondering how to invoke them correctly.
As far, in the ./myProject/app.js, that's what I do:
var express = require('express')
, resource = require('express-resource')
, mongoose = require('mongoose')
, expresstUtils =
require('./node_modules/express/lib/utils.js');
, connectUtils =
require('./node_modules/express/node_modules/connect/lib/utils.js');
But I found it a little clumsy, and what about my others files?
e.g., here is one of my routes:
myResources = app.resource(
'myresources',
require('./routes/myresources.js'));
and here is the content of myresources.js:
exports.index = function(req, res)
{
res.render('./myresources.jade', { title: 'My Resources' });
};
exports.show = function(req, res)
{
fonction resourceIsWellFormatted(param)
{
// Here is some code to determine whether the resource requested
// match with the required format or not
// return true if the format is ok
// return false if not
}
if (resourceIsWellFormatted(req.params['myresources']))
{
// render the resource
}
else
{
res.send(400); // HEY! what about the nice Connect.badRequest in its utils.js?
}
};
As you can see in the comment after the res.send(400), I ask myself if it is possible to use the badRequest function which is in the utils.js file of the Connect module.
What about the nice md5 function in the same file?
Do I have to place this hugly call at the start of my myresources.js to use them?:
var connectUtils =
require('../node_modules/express/node_modules/connect/lib/utils.js');
or, is there a more elegant solution (even for the app.js)?
Thank you in advance for your help!
the only more elegant way i came up with is (assuming express is inside your root "node_modules" folder):
require("express/node_modules/connect/lib/utils");
the node installation is on windows, node version 0.8.2
and a bit of extra information:
this way you don't need to know where you are in the path and be forced to use relative paths (./ or ../), this can be done on any file nesting level.
i put all my custom modules inside the root "node_modules" folder (i named my folder "custom_modules") and call them this way at any level of nesting:
require("custom_modules/mymodule/something")
If you want to access connect directly, I suggest you install connect as a dependency of your project, along with express. Then you can var utils = require('connect').utils.

Resources