How to read a pom.xml and get 'artifactId' in Grunt - node.js

How we can read a pom.xml and get the 'artifactId' 'groupId' etc in the GruntFile.js.
'xml-parser'- I saw in the NPM but how can I use that in GruntFile.js , Anyone has any example . I am very new to Grunt .
Thanks for your advise.

Search npm registry for XML parser
var fs = require('fs');
var parse = require('xml-parser');
var xml = fs.readFileSync('example.xml', 'utf8');
var obj = parse(xml);
obj is your parsed xml

The best way is to use 'pom-parser' (https://github.com/intuit/node-pom-parser)
Install the package:
npm install --save node-pom-parser
then, in the grunt file
var ext = require('pom-parser');
var pom = ext.parsePom({ filePath: "pom.xml"});
var artifactId = pom.artifactId;
console.log(artifactId)
module.exports = function(grunt) {
//GRUNT ....
}
But it won't work in Windows. https://github.com/marcellodesales/node-pom-parser/issues/1
UPDATE: pom-parser has been refactored and is now compatible with Windows (see resolution on linked issue).

Related

Error: Cannot find module 'togeojson'

I am attempting to use this module in node.js and am running into an "Error: Cannot find module 'togeojson'" error when I attempt to use the documented example code:
// using togeojson in nodejs
var tj = require('togeojson'),
fs = require('fs'),
// node doesn't have xml parsing or a dom. use xmldom
DOMParser = require('xmldom').DOMParser;
var kml = new DOMParser().parseFromString(fs.readFileSync('foo.kml', 'utf8'));
var converted = tj.kml(kml);
var convertedWithStyles = tj.kml(kml, { styles: true });
I ran npm init in the same directory that my app.js file (where the above code resides) is stored and I used the --save flag when installing the #mapbox/togeojson package to my application.
I am running node version 8.11.2 and npm v 6.1.0.
How do I go about debugging an issue like this in node/npm?
It is #mapbox/togeojson package, not togeojson, so it should be required like:
var tj = require('#mapbox/togeojson');

node_modules path programmatically

I am using protractor and I want to get programmatically the npm node_modules path from the global system.
For example my selenium jar is installed here:
C:/Users/myuser/AppData/Roaming/npm/node_modules/protractor/node_modules/webdriver-manager/selenium/selenium-server-standalone-3.7.1.jar
and I wanted to get
C:/Users/myuser/AppData/Roaming/npm/node_modules/
or
C:/Users/myuser/AppData/Roaming/npm/node_modules/protractor/node_modules/
I wrote this small script, which will look for my jar in the paths
var path = require('path');
var fs = require('fs');
var paths = path.getModulePaths()
for (i=0;i<paths.length;i++) {
file = path.join(paths[i],'webdriver-manager','selenium','selenium-server-standalone-3.7.1.jar')
if (fs.existsSync(file)) {
var seleniumServerJar = file
continue
}
}
here I supposed that this function is available
var paths = path.getModulePaths()
but it's not. I used to write an equivalent in Python, which is:
import sys
print sys.path
I guess you are expecting to start a webdriver manager programatically. Try below code:
var pkgPath = require.resolve('protractor');
var protractorDir = path.resolve(path.join(path.dirname(pkgPath), '..', 'bin'));
var webdriverManagerPath = path.join(protractorDir, '/' + 'webdriver-manager'));

gulp-minify not working with gulp-rename

code:
var gulp = require('gulp');
var minify = require('gulp-minify');
var rename = require('gulp-rename');
gulp.task('compress', function() {
gulp.src('script-source.js')
.pipe(minify())
.pipe(rename('script1.js'))
.pipe(gulp.dest('.'));
});
in same folder it is creating a new file script1.js but its not compressed, its same copy..
Using gulp-rename could be the choice https://github.com/hparra/gulp-rename
For more detail https://github.com/gulpjs/gulp/issues/34
you forgot to add return statement in your gulp task.

How do I get the ‘program name’ (invocation location) in Node.js?

I want the equivalent of $PROGRAM_NAME in Ruby, or ARGV[0] in C-likes, for Node.js. When I have a Node script that looks something like this,
#!/usr/bin/env node
console.log(process.argv[0])
… I get “node” instead of the name of the file that code is saved in. How do I get around this?
You can either use this (if called from your main.js file):
var path = require('path');
var programName = path.basename(__filename);
Or this, anywhere:
var path = require('path');
var programName = path.basename(process.argv[1]);
Use __filename? It returns the currently executing script.
http://nodejs.org/docs/latest/api/globals.html#globals_filename
For working with command line option strings check out the optimist module by Substack. Optimist will make your life much easier.
var inspect = require('eyespect').inspector();
var optimist = require('optimist')
var path = require('path');
var argv = optimist.argv
inspect(argv, 'argv')
var programName = path.basename(__filename);
inspect(programName, 'programName')
To install the needed dependencies:
npm install -S eyespect optimist

Meteor project path from a smartpackage

I was looking for a way to look up the meteor project path from a smart package (e.g.: obtain the path of the directory where the .meteor folder is ...).
I was not able to do it using node's __dirname and __filename because somehow in meteor they are not avaiable.
Any tips ?
As of Meteor 0.6.0 this would be:
var path = Npm.require('path');
var basepath = path.resolve('.');
From a smartpackage (0.6.5+):
var path = Npm.require('path');
var base = path.resolve('.');
base in this case gets you the position of your package ..
/User/username/projects/project/.meteor/local/programm/server/...
.. might even be deeper
but we want
/User/username/projects/project/
.. so split at .meteor
base = base.split('.meteor')[0];
Or as two-liner
var path = Npm.require('path');
var base = path.resolve('.').split('.meteor')[0];;
This works for me in Meteor 0.5.0:
var require = __meteor_bootstrap__.require;
var path = require('path');
var basepath = (path.resolve('.'));
You can actually get access to node:
var __dirname = __meteor_bootstrap__.__dirname;
You could try (only on server side)
process.env.PWD which returns something like that for me (OSX):
'/Users/myusername/Desktop/myproject'
With that command you get the root of meteor project.

Resources