I'm trying to commit and push changes using node without using any external npm packages
const { spawn,exec } = require('child_process');
const fs = require('fs');
const { stderr } = require('process');
(async ()=>{
if(fs.existsSync('./repo_dir'))
fs.rmSync('./repo_dir',{recursive:true});
exec(`git config --global --replace-all user.name "NAMEHERE" && git config --global --replace-all user.email "EMAIL#gmail.com" && git config --list`,(err,stdout,stderr)=>{
if(err)
console.log(`error: ${err}`);
if(stderr)
console.log(`stderr: ${stderr}`);
console.log(`stdout: ${stdout}`);
let git_spawn=spawn(`git`,[`clone`,`https://username:token#github.com/username/repo_dir.git`]);
git_spawn.stdout.on("data", data => {
console.log(`cloning stdout: ${data}`);
});
git_spawn.stderr.on("data", data => {
console.log(`cloning stderr: ${data}`);
});
git_spawn.on('error', (error) => {
console.log(`cloning error: ${error.message}`);
});
git_spawn.on("close", code => {
console.log(`child process exited with code ${code}`);
exec(`cd ./git_repo && touch blank.js && git add -A && git commit -m "updating" && git push && cd ..`,(err,stdout,stderr)=>{ //error somewhere here
console.log(`pushing error: ${err}`);
console.log(`pushing stdout: ${stdout}`);
console.log(`pushing stderr: ${stderr}`);
});
});
})
})();
but for some reason it doesn't work and I can't even pin point the error
output:
stdout: cloning stderr: Cloning into 'repo_dir'...
child process exited with code 0 pushing error: Error: Command failed:
cd ./repo_dir && touch blank.js && git add -A && git commit -m
"updating" && git push && cd ..
pushing stdout: On branch main Your branch is up to date with
'origin/main'.
nothing to commit, working tree clean
pushing stderr:
I'm guessing the problem is somewhere in committing and pushing
Related
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const root = __dirname;
function createSpawn(command, next) {
const child = spawn(command, {
stdio: 'inherit',
shell: true,
cwd: root,
});
child.on('exit', function () {
if (typeof next === 'function') {
next();
}
});
}
function install() {
fs.rmSync(path.resolve(root, '.husky'), {
recursive: true,
force: true,
});
createSpawn('npx husky install', () => {
createSpawn('npx husky add .husky/pre-commit "npm run pre-commit"', () => {
createSpawn('npx husky add .husky/commit-msg "npx --no -- commitlint --edit #EE#"', () => {
const file = path.resolve(root, '.husky/commit-msg');
let content = fs.readFileSync(file).toString();
content = content.replace('#EE#', '"$1"');
fs.writeFileSync(file, content);
});
});
});
}
install();
I test on windows, and it work fine
I used the yarn command to install dependencies, but an error occurred. It was still good before. I suspect that the permissions of the husky file are incorrect, but an error is reported in the yarn script
but when i use the commnad node install.js it work fine!!!
here is the error
yarn install v1.22.19
[1/4] 🔍 Resolving packages...
success Already up-to-date.
$ node install.js
node:internal/fs/utils:344
throw err;
^
Error: ENOENT: no such file or directory, open '/Users/ao/Desktop/Yeastar/PPV3/ppfe/.husky/commit-msg'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at /Users/ao/Desktop/Yeastar/PPV3/ppfe/install.js:32:34
at ChildProcess.<anonymous> (/Users/ao/Desktop/Yeastar/PPV3/ppfe/install.js:17:13)
at ChildProcess.emit (node:events:526:28)
at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12) {
errno: -2,
syscall: 'open',
code: 'ENOENT',
path: '/Users/ao/Desktop/Yeastar/PPV3/ppfe/.husky/commit-msg'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
I am currently using axios through npm import axios from 'axios'
Is there o possibility to console.log the axios version after the whole application has been built?
You could execute a bash command from nodejs to get an output of npm ls for axios.
const { exec } = require('child_process');
exec('npm ls | grep axios', (err, stdout, stderr) => {
if (err) {
return;
}
console.log(`Axios package: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
I'm currently using the node command to run a few premade scripts. Right now this is what I type into Git:
node file1.js
node file2.js
node file3.js
I have to wait for each one to finish before typing the next "node file.js"
Is there a way to do that for all of the files in the folder as opposed to typing them out one after the other? Thanks!
You can use fs.readdir to first get all files in current dir
const fs = require('fs')
const files = fs.readdirSync('.')
Then filter out the .js files:
const jsFiles = files.filter(f => f.endsWith('.js'))
The execute them one by one using child_process:
const { spawnSync } = require('child_process')
for (const file of jsFiles) {
spawnSync('node', [file], { shell: true, stdio: 'inherit' })
}
I'm using spawnSync so that it'll execute the files synchronously (one-by-one).
You can use exec() function to run commands through Javascript. Insert this line of code at the top of file1.js and call exec() function at the end of the execution and so on.
Example
index.js
const { exec } = require("child_process");
exec("dir", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
The output of node index.js:
stdout: Volume in drive C is Windows 10
Volume Serial Number is 3457-05DE
Directory of C:\dev\node
10.08.2020 20:52 <DIR> .
10.08.2020 20:52 <DIR> ..
10.08.2020 20:52 <DIR> .vscode
10.08.2020 21:39 310 index.js
10.08.2020 21:15 239 package.json
2 File(s) 549 bytes
I'm trying to open application using nodejs (I'd like this process to be workable in cross-platform , MAC/Windows ..etc
here's the code
const { exec } = require('child_process')
const fs = require('fs')
// giving the required permissions , not really sure if 777 is the right choice
fs.chmodSync(appPath,777);
exec("open -a " + path , (err, stdout, stderr) => {
console.log(err);
console.log(stdout);
console.log(stderr);
});
console.log(err); // logs this error
{ Error: Command failed: open -a /location.app
LSOpenURLsWithRole() failed for the application /location.app with error -10810
when executing the same command in terminal (out of node) , it fires the same issue
I'm facing some weird behavior when runing npm pack using the child_process.exec method.
const { exec } = require('child_process');
function execCommand( command ) {
exec(
command,
( err, stdout, stderr ) => {
if ( err != null ) {
throw err;
}
console.log( 'stdout -> ', stdout );
console.log( 'stderr -> ', stderr );
}
);
}
function packProject( projectName ) {
console.log('Packing Project');
const distDir = `dist/${projectName}`;
execCommand('cd "'+distDir+'" && npm pack', {
encoding: 'utf8'
});
}
packProject('test-app')
It does execute both commands as expected, but then it doesn't terminate the process after running npm pack.
Packing Project
stdout ->
stderr ->
stdout ->
stderr ->
stdout ->
stderr ->
I'm not getting any errors nor warnings during the process. Only after pressing Ctrl+C. But I suppose it's only because the process was manually terminated.
^Cnpm ERR! code ELIFECYCLE
npm ERR! errno 3221225786