I'm trying to do a simple Commit & Push to an existing repo using simple-git however I can't find
any example regarding this issue in the API in NPM (or Github) of simple-git.
I'm talking about this package : https://www.npmjs.com/package/simple-git
Consider the code :
const gitRepo = 'REPO-URL';
const tempFolder = '...';
// Simple Git
const simpleGit = require('simple-git')();
const options = ['--depth', '1'];
const callback = () => {
console.log('Done cloning!');
// Now change some code in the cloned code
// and commit && push
};
// Cloning ...
simpleGit.outputHandler((command, stdout, stderr) => {
stdout.pipe(process.stdout);
stderr.pipe(process.stderr)
stdout.on('data', (data) => {
// Print data
console.log(data.toString('utf8'));})
})
.clone(gitRepo, tempFolder, options, callback);
How can we commit and push using simple-git ?
Like #Lawrence Cherone said :
You can just use the basic commands as is.
This is how i used it in my project ( though i got a submodule in it where this example is changing its (git)working directory to content(submodule) first. After that i just commit with a message.
app.post("/gitCommit", async function(req, res) {
try {
await git.cwd({ path: 'content' }).commit(req.body.msg);
res.sendStatus(200)
} catch(err) {
console.log(err)
}
});
If you already have a working and initialised repo your in,
then you could just do the following:
await git.commit("your_message")
await git.push()
await git.push('origin', 'master')
You can leave out the 'await' part depending on your code running async.
Related
lately, I started working on a discord bot and added slash commands.
I noticed that I have a ping(replies with pong) command that I didn't create or I did and I can't get rid of it.
Here is my interactionHandler.js
const { REST } = require("#discordjs/rest");
const { Routes } = require("discord-api-types/v9");
module.exports = async (err, files, client) => {
if (err) return console.error(err);
client.interactionsArray = [];
files.forEach((file) => {
const interaction = require(`./../interactions/${file}`);
client.interactions.set(interaction.data.name, interaction);
client.interactionsArray.push(interaction.data.toJSON());
});
const rest = new REST({ version: "9" }).setToken(process.env.DISCORD_TOKEN);
(async () => {
try {
// console.log("Refreshing slash command list");
// const guildIds = await client.guilds.cache.map((guild) => guild.id);
// const clientId = await client.user.id;
// guildIds.forEach(async (guildId) => {
// await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
// body: client.interactionsArray,
// });
// });
await rest.put(
Routes.applicationGuildCommands("934895917453168653", "967384688069066852"),
{ body: client.interactionsArray},
);
console.log("Successfully refreshed slash command list");
} catch (error) {
console.error(error);
}
})();
};
Is there a way to delete the command because I cant find a way.
I was thinking of getting the ID of the command but I don't know how.
Thanks for all the helpers :)
Discord.js v13
These commands may not be refreshing because of 2 reasons:
These commands may be global commands to change that to refreshing global (instead of guild) commands replace Routes.applicationGuildCommands to Routes.applicationCommands
These commands may be from a different guild which in the case you should change the guild ID
To figure out the problem you should do client.application.commands.fetch() to get all your commands and then console.log() the result.
Example
//This will fetch all your slash commands
const commands = await client.application.commands.fetch();
//Returns a collection of application commands
console.log(commands);
Here is the error that it is returning
Here is a picture of the error i'm getting when I run npm install puppeter
I found some stuff online with permissions, but this is about node_package space. It is not disk space as I've looked over my disk storage availability and there's plenty. Working on an Apify SDK, and I'm following the documentation, but the console is returning a whole bunch of error messages.
Can someone please help?
const Apify = require('apify')
Apify.main(async () => {
const requestQueue = await Apify.openRequestQueue();
await requestQueue.addRequest({ url: 'https://www.iana.org/' });
const crawler = new Apify.PuppeteerCrawler({
requestQueue,
handlePageFunction: async ({ request, page }) => {
const title = await page.title();
console.log(`Title of ${request.url}: ${title}`);
await Apify.utils.enqueueLinks({
requestQueue,
page,
pseudoUrls: ['https://www.iana.org/[.*]'],
});
},
});
await crawler.run();
});
Environment
Operating System version: Mac OS 10.15.1
Firebase SDK version: 8.8.0
Firebase Product: database (auth, database, storage, etc)
Node.js version: 8.15.1
NPM version: 6.9.0
"firebase": "7.5.0",
"firebase-admin": "8.8.0",
"firebase-functions": "3.3.0"
Problem
When I call firebase deploy --only functions, it stops on step functions: preparing functions directory for uploading... and throws:
database: Firebase: Firebase service named 'database' already registered (app/duplicate-service)
This is the where the error is found:
at error (....../functions/node_modules/#firebase/app/dist/index.node.cjs.js:41:21)
Also I started getting: Error parsing triggers: The gRPC binary module was not installed. This may be fixed by running "npm rebuild"
Of course I have tried npm rebuild but without any luck...
Relevant Code
The only way I reference database is through:
// Init firebase access to DB
const firebase_admin = require("firebase-admin");
// Get a database reference
const firebase_db = firebase_admin.database();
I do this in multiple files tho.
After a little more digging, I managed to find out that it is caused by code in these two files:
// Init firebase access to DB
const firebase_admin = require("firebase-admin");
// Get a database reference to our blog
const firebase_db = firebase_admin.database();
const get_firebase_data = async (page_type, call_back) => {
const is_paypal_page = page_type === "paypal"
const is_admin_page = page_type === "admin"
const content = await firebase_db.ref("/content").once("value")
const footer_content = await firebase_db.ref("/footer_content").once("value")
const header_items = await firebase_db.ref("/header_items").once("value")
const translations = await firebase_db.ref("/translations").once("value")
const single_product_homepages = await firebase_db.ref("/single_product_homepages").once("value")
const couple_products_homepages = await firebase_db.ref("/couple_products_homepages").once("value")
const categories_of_product_variety = await firebase_db.ref("/categories_of_product_variety").once("value")
let products;
let shipping_options;
let admin;
if(is_paypal_page || is_admin_page) {
products = await firebase_db.ref("/products").once("value")
shipping_options = await firebase_db.ref("/shipping_options").once("value")
}
if(is_admin_page)
admin = await firebase_db.ref("/admin").once("value")
const final_object = {
...
}
call_back(null, final_object)
}
module.exports = (req,res) => {
get_firebase_data(req.params[0].replace("/", ""), (err,result) => {
if(err) res.send(err)
res.send(result)
})
}
and
const paypal = require('paypal-rest-sdk')
const { paypal_config } = require("../config")
const axios_instance = require("./inventorysource/axios_inventorysource_instance")
const create_order = require("./inventorysource/create_order")
paypal.configure(paypal_config)
const paymentPaypal = (paymentID, execute_payment_json, call_back) => {
paypal.payment.execute(paymentID, execute_payment_json, (error, paymentLog)=> {
if (error)
return call_back(error)
else {
call_back(null, paymentLog)
}
})
}
module.exports = (req, res) => {
const body = req.body
const execute_payment_json = { "payer_id": body.data.payerID }
const paymentID = body.data.paymentID
paymentPaypal(paymentID, execute_payment_json, (err, paypal_data) => {
if(err)
res.send(err)
else
create_order(
axios_instance(body.data.is_production),
body.data.is_production,
body.data.amount,
body.data.user_and_product_data,
res, paypal_data
)
})
}
However I dont import or require "firebase/database" anywhere in my code.
What I have tried
As answers to similar issues suggest, I have deleted node_modules and package-lock.json and tried re-installing with npm install multiple times.
I have also tried using different versions od Node and completely rebuilding my packages with npm rebuild.
Furthermore I have tried installing latest versions of the above mentioned firebase packages and then deleting and install npm all over again. Still with no luck...
I also tried commenting out parts of my code that deal with firebase but still, the error remains.
In order to be able to compare the pre and post-save version of a document, I am trying to lookup the document in a pre hook, and then use that to see what's changed in the doc in the post save hook.
But for some reason I'm getting a "Customer.findOne() is not a function" error. This doesn't make any sense to me because I've imported the model into this triggers file, and then, in my function I do this:
const Customer = require("../customer");
// Get a version of the document prior to changes
exports.preSave = async function(doc) {
console.log("preSave firing with doc._id", doc._id); // this ObjectId logs correctly
if (!doc) return;
this.preSaveDoc = await Customer.findOne({ _id: doc._id }).exec();
console.log("this.preSaveDoc: ", this.preSaveDoc);
};
Again, this code produces an error:
"Customer.findOne() is not a function"
FYI, the relevant code in my Customer model looks like this:
let Schema = mongoose
.Schema(CustomerSchema, {
timestamps: true
})
.pre("count", function(next) {
next();
})
.pre("save", function(next) {
const doc = this;
trigger.preSave(doc);
next();
})
.post("save", function(doc) {
trigger.postSave(doc);
})
.post("update", function(doc) {
trigger.postSave(doc);
})
.post("findOneAndUpdate", function(doc) {
trigger.postSave(doc);
});
module.exports = mongoose.model("Customer", Schema);
What am I missing here? Why would this code produce this error on a very standard MongoDB operation?
This problem has already been solved.
If your mongoDb version is 3.6 or more, you can use change streams
Change streams lets you know what changed in your document. A background process runs in mongoDb which notifies your code an event(CREATE, UPDATE, DELETE) took place and passes you the document. You can filter on your fields to know the exact value.
You can refer this blog
This is a classic use case where change streams can be applied. Better than reinventing the wheel :)
Here is how you can get this to work. Instead of looking up the pre-saved/pre-transformed version of the document via Mongoose like this:
// Get a version of the document prior to changes
exports.preSave = async function(doc) {
console.log("preSave firing with doc._id", doc._id); // this ObjectId logs correctly
if (!doc) return;
this.preSaveDoc = await Customer.findOne({ _id: doc._id }).exec();
console.log("this.preSaveDoc: ", this.preSaveDoc);
};
... look up the document this way:
// Get a version of the document prior to changes
exports.preSave = async function(doc) {
let MongoClient = await require("../../config/database")();
let db = MongoClient.connection.db;
db.collection("customers")
.findOne({ _id: doc._id })
.then(doc => {
this.preSaveDoc = doc;
});
};
I'm trying to reproduce the checkout file operation using nodegit for reverting current changes of a file.
git checkout -- filename.ext
My first attempt was to use checkoutRef function from Repository object, like this:
nodegit.Repository.open(gitRepo)
.then(function (repo) {
repo.checkoutRef('filename.ext',{
checkoutStrategy: nodegit.Checkout.STRATEGY.FORCE
}).then(function (data) {
console.log(data);
});
});
Try the following code: ;-)
try {
const commit = await repository.getBranchCommit("origin/master");
const tree = await commit.getTree();
const resCheckout = await Checkout.tree(repository, tree, { checkoutStrategy: Checkout.STRATEGY.FORCE, paths: ["YOUR_FILE_PATH"] });
} catch (err) {
console.log(err)
}
I think Reset.default is what you want.