A simple utility to quickly replace the contents of text blocks in Json format within JavaScript files.
# 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
//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);
});
replace(file,find,input)
.then(response => {
console.log(response.message);
}, err => {
console.log(err.message);
});
var file_output = path.join(__dirname,'<FILE_PATH>');
replace(file,find,input,file_output,function(err,file_path){
console.log("path: ",file_path);
});
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);
});
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.
(MIT License)
Copyright 2019, Fabian Vargas, co-founder at Donec Lab