grunt-exec doesn't recognize a custom function defined in bash - javascript

Grunt version:
CLI - v1.2.0
local - v1.0.1
I'm using grunt-exec to start a local DynamoDB server. I'm doing this by creating a custom function in .bashrc then calling it inside grunt-exec. I also tried explicitly creating an alias, which didn't fix it.
~/.bashrc
runDynamo () {
java -Djava.library.path=~/DynamoDBServer/DynamoDBLocal_lib -jar ~/DynamoDBServer/DynamoDBLocal.jar -sharedDb
}
Gruntfile.js
// ...
exec: {
dynamo: {
// Run DynamoDB locally at port 8000
// This alias has been set during the inital installation
command: "runDynamo"
}
}
// ...
var mode = grunt.option("mode") || "test";
grunt.registerTask("run", ["exec:" + mode]);
When I run grunt run --mode=dynamo, I get the following error in stdout:
Running "exec:dynamo" (exec) task
>> /bin/sh: 1: runDynamo: not found
>> Exited with code: 127.
>> Error executing child process: Error: Process exited with code 127.
The command works fine when used directly in bash (i.e. $ runDynamo), so I'm not sure why grunt-exec isn't working here.

Related

Command not working inside execSync in nodejs

I have one shell command which is working fine from terminal but when I try to run from nodejs it is giving me the error
Orignal Command
awk -v RS='"[^"]*"' '{n+=gsub(/\n/, "&")} END{print n}' <(sed '$s/$//' file.txt)
Node Js Code
execSync("awk -v RS='\"[^\"]*\"' '{n+=gsub(/\\n/, \"&\")} END{print n}' <(sed '$s/$//' "+ filePath+')')
The exesync is giving the same output but it is showing me the error Syntax error: "(" unexpected
<() is a bash-specific process substitution syntax. execSync defaults to using /bin/sh, usually a narrowly POSIX-compliant shell, which means it doesn't support the syntax. Explicitly use bash instead:
execSync("command goes here", {
shell: "/bin/bash"
});

Error running forever.js on Windows - "'C:\Program' is not recognized as an internal or external command, operable program or batch file"

I am trying to run forever.js via the Windows command prompt and I get the following output:
>npm i -g forever
/my-project>forever start index.js
Log output:
'C:\Program' is not recognized as an internal or external command, operable program or batch file
I think it's something to do with the PATH that forever is using for the node binary, but I don't know how to fix it...
EDIT: Forever is using the following command (which is surrounded by quotes " "):
"C:\Program Files\nodejs\node.exe"
If you type dir /x in the root of c:, you can see the short name of the directory.
So, try the following instead:
C:\PROGRA~1\nodejs\node.exe
installing forever version 1.0.0 solved the problem for me
i have the same problem with forever 2.0.0.
I use a workaround in "forever.js": (I start my app by "node forever.js" )
const configChild = {
//
// Basic configuration options
//
silent: true, // Silences the output from stdout and stderr in the parent process
'killTree': true, // Kills the entire child process tree on `exit`
....
}
// =======================================================
// **Workaround for Windows**
// =======================================================
if (process.platform === 'win32' && process.execPath == "C:\\Program Files\\nodejs\\node.exe") {
configChild.command = '"C:\\PROGRA~1\\nodejs\\node.exe"';
}
const child = new (forever.Monitor)('app.js', configChild);
....
child.start();

How to run a shell command from Grunt task function

I'm trying to move some icons in my app directory based on a function i have inside my Gruntfile.js. Would it be possible to do something like this? I've tried the following (going into dev or staging folder and copying all files to the previous directory), but coudn't get it to work. Thanks in advance.
grunt.registerTask('setAppIcon', 'Task that sets the app icon', function(environment) {
if (environment.toLowerCase() == "development") {
grunt.task.run(['exec:command:cd app/lib/extras/res/icon/ios/dev && cp -a . ../']);
} else if (environment.toLowerCase() == "staging") {
grunt.task.run(['exec:command:cd app/lib/extras/res/icon/ios/staging && cp -a . ../']);
}
});
Yes, it's possible to achieve your requirement, however, when you invoke the grunt.task.run command inside your function (i.e. custom task) you need to provide a reference to a task to run.
If you define a separate Target, (Let's call call them copy_dev and copy_staging - as shown in the example below), for each cd ... && cp ... command in the grunt-exec Task it should work successfully.
Gruntfile.js
The following Gruntfile.js gist shows how this can be achieved:
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-exec');
grunt.initConfig({
exec: {
copy_dev: {
cmd: 'cd app/lib/extras/res/icon/ios/dev && cp -a . ../'
},
copy_staging: {
cmd: 'cd app/lib/extras/res/icon/ios/staging && cp -a . ../'
}
}
});
grunt.registerTask('setAppIcon', 'Task that sets the app icon', function() {
var environment = process.env.NODE_ENV;
// Exit early if NODE_ENV variable has not been set.
if (!environment) {
grunt.log.writeln(
'\"setAppIcon\"" task failed - NODE_ENV has not been set.'['yellow']
)
return
}
if (environment.toLowerCase() == "development") {
grunt.task.run('exec:copy_dev');
grunt.log.writeln('>> Copying icons from \"dev\"...')
} else if (environment.toLowerCase() == "staging") {
grunt.task.run('exec:copy_staging');
grunt.log.writeln('>> Copying icons from \"staging\"...')
}
});
grunt.registerTask('default', [ 'setAppIcon' ]);
};
Additional notes
Inside the custom task/function named setAppIcon we obtain the current node environment using nodes builtin process.env
When running $ grunt via your CLI (using the gist shown above), and assuming your process.env.NODE_ENV variable has not been set, or it has possibly been unset by running $ unset NODE_ENV, you will see the following message:
"setAppIcon"" task failed - NODE_ENV has not been set.
However, if the process.env.NODE_ENV variable has been set to either development or staging the files will be copied as expected.
For example running either of the following via your CLI will work successfully:
$ export NODE_ENV=development && grunt
or
$ export NODE_ENV=staging && grunt
You will also see either of the following messages logged to the console:
>> Copying icons from "dev"...
or
>> Copying icons from "staging"...
After process.env.NODE_ENV has been set to either development or staging then just running $ grunt via your CLI, will copy files according to which environment is set.

Node npm package throw use strict: command not found after publish and install globaly

I am trying to publish npm package, when i am install the package globally and try to run the cli command i get this errors:
/.nvm/versions/node/v0.12.2/bin/myPack: line 1: use strict: command not found
/.nvm/versions/node/v0.12.2/bin/myPack: line 3: syntax error near unexpected token `('
/.nvm/versions/node/v0.12.2/bin/myPack: line 3: `var _commandLineArgs = require('command-line-args');'
The top of the file that the error refer to:
'use strict';
var _commandLineArgs = require('command-line-args');
var _commandLineArgs2 = _interopRequireDefault(_commandLineArgs);
The package.json bin section:
"bin": {
"myPack": "dist/myPack.js"
}
When i am running this in my local development this works well, what is the problem?
Your script should start with a shebang line, otherwise it will be executed as a shell script (hence the errors).
Add this as first line to dist/myPack.js:
#!/usr/bin/env node

Uncaught exception with qunit and jquery

I'm facing an issue while trying to get javascript unit tests to work at the command line using qunit.
Here's some sample code to reproduce the error:
file util.js:
function abc() {
return 'abc';
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
abc: abc
};
}
file util-tests.js
var qunit = require("qunit");
test("Test abc function", function () {
equal(util.abc(), 'abc');
});
With these files, I can run tests using the following command (gives a table-like output in the shell with the test results):
qunit -c util:util.js -t util-tests.js
Now it breaks if I add the following to util.js
$(document).ready(function () {
/* some code here */
});
Here's the error output:
qunit -c util:util.js -t util-tests.js
Testing /home/mfrere/jstst/util.js ... [Error: Uncaught exception in child process.]
same problem with:
var a = $;
or:
var a = document;
So this makes me think that I need to import jQuery somehow, so I thought about adding jquery.js as a dependency to the command, like this:
qunit -c util:util.js -t util-tests.js -d jquery.js
The above command gives me the same 'Uncaught exception' error, even if util.js doesn't contain any reference to '$'.
I'll probably need to do something else to get qunit to recognize 'document' as well, but I don't know what or how.
Now here's my question: what should I do to get this to work? It is important to keep in mind I want to test my files at the command line, not in a browser.
Just in case I did something wrong in the setup process, this is how I installed node/qunit (under ubuntu):
git clone git://github.com/creationix/nvm.git ~/.nvm
in .bashrc, I added the following line:
source ~/.nvm/nvm.sh
picked a specific version of node
nvm install v0.9.2
nvm alias default 0.9
and installed qunit
npm install -g qunit
finally I had to add this in .bashrc as well:
export NODE_PATH=~/.nvm/v0.9.2/lib/node_modules
You haven't imported jQuery:
$ = require('jquery'),
jQuery = require('jquery');
If you're using browserify, change that to 'jquery-browserify'.

Categories