"No such file or directory, scandir './command/'" - discord.js

the error
the code
Please help me fix this, I'm desperately trying to.

You are simply getting this error because in your main.js file, you are expecting the commands to be in the ./commands folder while all the commands are actually in the ./command folder without an s. So all you have to do is either change your command folder's name to commands or you can edit your main.js file and change the command folder's name to ./command like this:
const commandFiles = fs.readdirSync('./command/').filter(file => file.endsWith('.js'))

I will recommend you using glob for command handler npm i glob
With this code you can have file tree like this:
Folder
File in folder
File not in folder
And it will still work well
const { glob } = require('glob')
const { promisify } = require('util');
const globPromise = promisify(glob);
const dir = await globPromise(`${process.cwd()}/commands/**/*.js`);
dir.map((x) => {
const file = require(x)
if(file.name) {
client.commands.set(file.name, file)
}
})

mb u can print "command", not "commandS"

Related

How to use Jenkins file parameter in Pipelines

My Jenkins pipeline runs on the Slave using agent { node { label 'slave_node1' } }.
I use Jenkins file parameter named uploaded_file and upload a file called hello.pdf
My pipeline contains the following code
stage('Precheck')
{
steps {
sh "echo ${WORKSPACE}"
sh "echo ${uploaded_file}
sh "ls -ltr ${WORKSPACE}/*"
Output:
/web/jenkins/workspace/MYCOPY
hello.pdf
ls: cannot access /web/jenkins/workspace/MYCOPY/* No such file or directory
As you can see no files were found on the slave WORKSPACE.
Can you let me understand if I'm checking for the uploaded file in the correct location i.e under WORKSPACE directory?
How can I get the file uploaded to the slave's WORKSPACE?
I'm on jenkins version 2.249.1
Can I get this to work at least on the latest version of Jenkins ?
So do you have a fixed file that that is copied in every build? I.e its the same file?
In that case you can save it as a secret file in jenkins and do the following:
environment {
FILE = credentials('my_file')
}
stages {
stage('Preperation'){
steps {
// Copy your fie to the workspace
sh "cp ${FILE} ${WORKSPACE}"
// Verify the file is copied.
sh "ls -la"
}
}
}

no permanent file created

I'm running a conf file in logstash which gives the output as required but when i open ls to list file it's not getting created.need help in resolving issue.
conf file:
input {
elasticsearch{
hosts =>"localhost:9200"
index=>"index1"
}
}
output{
file{
path => "/opt/elk/logstash-6.5.0/example.json"
}
stdout{}
}
Result of execution:
[2019-10-13T14:07:47,503][INFO ][logstash.outputs.file ]
Opening file {:path=>"/opt/elk/logstash-6.5.0/example.json"}
{
"#timestamp" => 2019-10-13T08:37:46.715Z,
"example" => [
//data within example//
}
[2019-10-13T16:44:19,982][INFO ][logstash.pipeline] Pipeline has terminated {:pipeline_id=>"main", :thread=>"#<Thread:0x4f8848b run>"}
but when I run ls to see example.json, it doesn't show up
ls command:
elastic#es-VirtualBox:~/opt/elk/logstash-6.5.0$ ls
bin CONTRIBUTORS fetch.conf Gemfile.lock lib logs logstash-core-plugin-api NOTICE.TXT throw.conf vendor config data Gemfile grokexample.conf LICENSE.txt logstash-core modules output.json tools x-pack
so was wondering if conf only creates a temporary file?

Jenkins function definition: call a batch file using function arguments

Here is a chunk of code I have in a pipeline:
def doBuild(folder, script, scriptArguments = '') {
bat '''cd folder
call script scriptArgument'''
}
So basically, it is a windows command saying: move to this directory, and call this script.
Of course, during execution, the command will be executed as 'cd folder' and will fail.
How can I make sure the 'folder' is replaced by the value passed in argument ?
Edit:
Following the suggestion of Vitalii, I tried the following code:
/* Environment Properties */
repository_name = 'toto'
extra_repository_branch = 'master'
branch_to_build = "${env.BRANCH_NAME}"
version = "${branch_to_build}_v1.5.4.${env.BUILD_NUMBER}"
/* Methods definitions properties */
def doTag() {
dir(repository_name) {
String tagCommand = """git tag -m "Release tag ${env.BUILD_URL}" ${version}"""
String pushCommand = "git push --tags origin ${branch_to_build}"
bat '''
call $tagCommand
call $pushCommand'''
}
}
Here is the output:
C:\Jenkins\toto>call $tagCommand '$tagCommand' is not recognized
as an internal or external command, operable program or batch file.
C:\Jenkins\toto>call $pushCommand '$pushCommand' is not
recognized as an internal or external command, operable program or
batch file.
Thanks very much for your time
You can use string interpolation
bat """cd $folder
call $script $scriptArgument"""
So I did not think it was the case first but it was really had its answer in What's the difference of strings within single or double quotes in groovy?
Using single quotes, the text is considered as literrate. Using double code, it will interpret correctly the $tagCommand. A working version is:
/* Methods definitions properties */
def doTag() {
dir(repository_name) {
String tagCommand = """git tag -m "Release tag ${env.BUILD_URL}" ${version}"""
String pushCommand = "git push --tags origin ${branch_to_build}"
bat """call $tagCommand
call $pushCommand"""
}
}

How to make a Phonegap plugins installer batch file?

I'm trying to develop a batch file that runs a lot of commands of the type of "phonegap local plugin add" for automate the Phonegap plugin installation process when I want to share my app.
I found the following solution developed in linux:
#!/usr/bin/env node
//this hook installs all your plugins
// add your plugins to this list--either
// the identifier, the filesystem location
// or the URL
var pluginlist = [
"org.apache.cordova.device",
"org.apache.cordova.device-motion",
"org.apache.cordova.device-orientation",
"org.apache.cordova.geolocation",
"https://github.com/chrisekelley/AppPreferences/"
];
// no need to configure below
var fs = require('fs');
var path = require('path');
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
sys.puts(stdout)
}
pluginlist.forEach(function(plug) {
exec("cordova plugin add " + plug, puts);
});
I'm trying to develop this code in a Windows batch file. Can someone tell me how can I do this?
From a script POV this may be close:
#echo off
for %%a in (
"org.apache.cordova.device"
"org.apache.cordova.device-motion"
"org.apache.cordova.device-orientation"
"org.apache.cordova.geolocation"
"https://github.com/chrisekelley/AppPreferences/"
) do cordova plugin add %%a

Node.js: How to check if folder is empty or not with out uploading list of files

I am using Node.js.
I want to check if folder is empty or not? One option is to use fs.readdir but it loads whole bunch of files into an array.
I have more than 10000 files in the folder. Loading files name is useless just to check if folder is empty or not. So looking for alternate solution.
How about using nodes native fs module http://nodejs.org/api/fs.html#fs_fs_readdir_path_callback. It's readdir and readdirSync functions provide you with an array of all the included file names (excluding . and ..). If the length is 0 then your directory is empty.
This is an ugly hack but I'll throw it out there anyway. You could just call fs.rmdir on the directory. If the callback returns an error which contains code: 'ENOTEMPTY', it was not empty. If it succeeds then you can call fs.mkdir and replace it. This solution probably only makes sense if your script was the one which created the directory in the first place, has the proper permissions, etc.
You can execute any *nix shell command from within NodeJS by using exec(). So for this you can use the good old 'ls -A ${folder} | wc -l' command (which lists all files/directories contained within ${folder} hiding the entries for the current directory (.) and parent directory (..) from the output which you want to exclude from the count, and counting their number).
For example in case ./tmp contains no files/directories below will show 'Directory ./tmp is empty.'. Otherwise, it will show the number of files/directories that it contains.
var dir = './tmp';
exec( 'ls -A ' + dir + ' | wc -l', function (error, stdout, stderr) {
if( !error ){
var numberOfFilesAsString = stdout.trim();
if( numberOfFilesAsString === '0' ){
console.log( 'Directory ' + dir + ' is empty.' );
}
else {
console.log( 'Directory ' + dir + ' contains ' + numberOfFilesAsString + ' files/directories.' );
}
}
else {
throw error;
}
});
Duplicate from my answer in how to determine whether the directory is empty directory with nodejs
There is the possibility of using the opendir method call that creates an iterator for the directory.
This will remove the need to read all the files and avoid the potential memory & time overhead
import {promises as fsp} from "fs"
const dirIter = await fsp.opendir(_folderPath);
const {value,done} = await dirIter[Symbol.asyncIterator]().next();
await dirIter.close()
The done value would tell you if the directory is empty
What about globbing? ie, exists myDir/*. It is not supported out of box by node (TOW v0.10.15), but bunch of modules will do that for you, like minimatch
Just like to add that there's a node module extfs which can be used to check if a directory is empty using the function isEmpty() as shown by the code snippet below:
var fs = require('extfs');
fs.isEmpty('/home/myFolder', function (empty) {
console.log(empty);
});
Check out the link for documentation regarding the synchronous version of this function.

Resources