how to put * to execute whole '.gql' files from a folder in scripts of package.json - node.js

i have used scripts body as
"scripts": {
"start_docs":"npm run graphdoc",
"graphdoc": "graphdoc -s schema/*.gql -f -o ./support/reports/graphdoc;exit 0"
}
While I put multiple '.gql' using * from schema folder,
then error like
/Documents/graphql_new/stature-gqdss/node_modules/#2fd/command/lib/command/params.js:104
throw new Error('Unexpected param: ' + param);
^
Error: Unexpected param: schema/basic-profile-schema.gql
at NoParams.parse (/home/jamsheerali/Documents/graphql_new/stature-gqdss/node_modules/#2fd/command/lib/command/params.js:104:15)

Related

Passing parameters from Jenkins CI to npm script

When I run Jenkins build, I would like to pass COMMIT_HASH and BRANCH_NAME to one of my javascript files: publish.js, so that I can remove hard-coded values for tags and consumerVersion.
Here is my code:
Jenkinsfile
stage('Publish Pacts') {
steps {
script {
sh 'npm run publish:pact -Dpact.consumer.version=${COMMIT_HASH} -Dpact.tag=${env.BRANCH_NAME}'
}
}
}
package.json
"scripts": {
"publish:pact": "node ./src/test/pact/publish.js"
}
./src/test/pact/publish.js
let publisher = require('#pact-foundation/pact-node');
let path = require('path');
let opts = {
providerBaseUrl: `http://localhost:${global.port}`,
pactFilesOrDirs: [path.resolve(process.cwd(), 'pacts')],
pactBroker: 'http://localhost:80',
tags: ["prod", "test"], // $BRANCH_NAME
consumerVersion: "2.0.0" // $COMMIT_HASH
};
publisher.publishPacts(opts).then(() => {
console.log("Pacts successfully published");
done()
});
Does anyone know how to do this?
You can pass cli arguments to your node script which end up in your process.argv.
Also npm passes on cli arguments via two dashes --.
To illustrate this consider this example:
Jenkinsfile
stage('Publish Pacts') {
steps {
script {
sh 'npm run publish:pact -- ${COMMIT_HASH} ${env.BRANCH_NAME}'
}
}
}
package.json
"scripts": {
"publish:pact": "node ./src/test/pact/publish.js"
}
publish.js
// process.argv[0] = path to node binary
// process.argv[1] = path to script
console.log('COMMIT_HASH:',process.argv[2]);
console.log('BRANCH_NAME:',process.argv[3]);
I left the cli flags out for simplicity.
Hope this helps

npm bin return me "/usr/local/bin/X: 1: /usr/local/bin/X: Syntax error: "(" unexpected" when calling it

So i'm just trying to create an npm bin who create a file in the current directory.
// ./index.js
const program = require('commander');
const fs = require('fs');
const path = require('path');
program
.command('c <name> <content>')
.action((name, content) => {
fs.writeFile(path.resolve(process.cwd(), name), content, err => err ? console.error(err) : console.log('Success'));
});
program.parse(process.argv);
This is not because of fs, even if i replace the writeFile by a console.log i still have to same error.
Here's my package.json :
{
"name": "test-crayzzit",
"dependencies": {
"commander": "^2.19.0"
},
"bin": {
"testcc": "./index.js"
},
"version": "1.0.3"
}
Everything's work well if i do something like node index.js test.txt hello
But if i install the package with npm : sudo npm i -g test-crayzzit
And do testcc c test.txt hello
It return me an error : /usr/local/bin/testcc: 1: /usr/local/bin/testcc: Syntax error: "(" unexpected
You can try by your self with the package : https://www.npmjs.com/package/test-crayzzit
Looks like you're missing the shebang. The first line of index.js should read as follows:
#!/usr/bin/env node
Moreover, the file should have LFline endings to be read properly on MacOS, Linux and Windows if you care about using the package on different platforms.
EDIT: I have tested your package (same error for me on Linux). Adding the shebang as described above works for me.
See also: Appropriate hashbang for Node.js scripts

Failed to pass command-line arg into Webpack

I'm trying to pass arguments to Webpack via command like (I got the info from here):
// webpack.dev.conf.js
module.exports = function (env) {
if (env.compress === 'true') {
console.log('COMPRESS')
}
}
I did npm run dev --env.compress in the terminal. As you can see I'm have custom scripts:
"scripts": {
"dev": "node --max_old_space_size=10000 build/dev-server.js",
However, I get this error:
throw new Error("Invalid argument: options");
^
Error: Invalid argument: options
How to properly pass command-line arguments to Webpack?

How to execute forever command remotely without using full path?

I am trying to execute “forever” command remotely using powershell but I am getting error
'forever' is not recognized as an internal or external command
Here is what I am doing. I have the node js script "MyNodeScript.js" which executes the forever command. The script is on the WebServer "MyWebServer1".
Node.Js and Forever is installed on MyWebServer1 globally.
var exec = require('child_process').exec;
var _ = require('underscore');
var winston = require('winston');
var async = require('async');
var mycommand = 'forever -p ./logs --sourceDir ../wf_manager --workingDir ../wf_manager start -a --uid "ServiceApp" ServiceApp.js'
function start(callback) {
async.waterfall([
function (cb) {
executeCommand(mycommand, false, cb);
}
], function done(err) {
if (err) {
winston.error('failed to start all instances by forever:' + err);
} else {
winston.info('successfully started all instances by forever');
}
callback();
});
}
function executeCommand(command, skip, callback) {
async.waterfall([
function (cb) {
exec(command, cb);
}
], function done(err) {
if (err) {
if (skip) {
// skip the error
callback(null);
} else {
callback(err);
}
} else {
callback(null);
}
});
}
module.exports = {
executeCommand: executeCommand,
start: start
}
start(function(){});
On the same MyWebServer1 under same folder i have a powershell script "MyPowerShellScript.ps1" which call this node script. The powershell script has only one line
node D:\myfolder\maintenance\MyNodeScript.js
I can run this powershell script locally on MyWebServer1 and it works fine. But when i try to execute this powershell script as below from remote machine
invoke-command -computername MyWebServer1 -filepath \\MyWebServer1\MyFolder\maintenance\MyPowerShellScript.ps1
i am getting error
error: failed to start all instances by forever:Error: Command failed:
C:\Windows\system32\cmd.exe /s /c "forever -p ./logs --sourceDir
../wf_manager --workingDir ../wf_manager start -a --uid "ServiceApp"
ServiceApp.js"
+ CategoryInfo : NotSpecified: (error: failed t...ServiceApp.js":String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : MyWebServer1
'forever' is not recognized as an internal or external command, operable program or
batch file.
Note that i can execute the script remotely without any error if i update "MyNodeScript.js" and use full physical path for the forever command
var mycommand = 'C:\\Users\\admin\\AppData\\Roaming\\npm\\forever -p ./logs --sourceDir ../wf_manager --workingDir ../wf_manager start -a --uid "ServiceApp" ServiceApp.js'
However i would like to use just forever command. The path is already added as Environment variable on MyWebServer1
Try calling the ps1-file like this:
Invoke-Command -Scriptblock {powershell.exe -command {d:\blah\MyPowershellscript.ps1 } }
Via -filepath your specify a local scriptfile that should be executed remotely. As your script is present on the target system, you can omit that.

shell function can't be called in node

I have these:
An shell executable file:
function print() {
echo 1
}
The package.json` file
{
"name": "tests",
"scripts": {
"test": "./shell.sh"
}
}
When I ran npm test on a linux machine, I got this error
> tests# test /home/xxxx/test
> ./shell.sh
./shell.sh: 1: ./shell.sh: Syntax error: "(" unexpected
npm ERR! Test failed. See above for more details.
Why so? Anybody has some insight? I am totally puzzled.
This doesn't really have anything to do with Node or npm, but with the shell script missing the shebang.
Try instead e.g.
#!/bin/sh
# Note the new line above
function print() {
echo 1
}

Resources