replace-js-file
v1.0.2
Published
A simple utility to quickly replace the contents of text blocks in Json format within JavaScript files.
Downloads
4
Maintainers
Readme
Replace Json in file
A simple utility to quickly replace the contents of text blocks in Json format within JavaScript files.
Index
Installation
# Using npm, installing to local project
npm i --save replace-js-file
# Using npm, installing globally for global cli usage
npm i -g replace-js-file
# Using yarn
yarn add replace-js-file
Basic usage
//Load the library
const replace = require('replace-js-file');
var path = require("path");
var file = path.join(__dirname,'<FILE_PATH>');
var find = {
"email": {
"type": "String",
"unique": true,
"required": true
},
"password": "String"
};
var input = {
"email": {"type":"String","unique":true,"required": true},
"password": "String",
"usergroup": {
"type": "ObjectId",
"ref": "group",
"required": true
}
};
replace(file,find,input,function(err,file_path){
console.log("path: ",file_path);
});
Asynchronous replacement with promises
replace(file,find,input)
.then(response => {
console.log(response.message);
}, err => {
console.log(err.message);
});
Replacement and copy to another file
var file_output = path.join(__dirname,'<FILE_PATH>');
replace(file,find,input,file_output,function(err,file_path){
console.log("path: ",file_path);
});
Asynchronous replacement with promises and copy to another file
var file_output = path.join(__dirname,'<FILE_PATH>');
replace(file,find,input,file_output)
.then(response => {
console.log(response.message);
}, err => {
console.log(err.message);
});
Return Values
When it is a promise, it returns an object with the answer, in it there are two variables, success and message.
success Initiates that everything is successful and message details the result of the operation.
When callback is used, two parameters are returned, the first is an error and the second the path of the created or modified file.
License
(MIT License)
Copyright 2019, Fabian Vargas, co-founder at Donec Lab