Unable to get the .js file with Minium Developer. I have the route like in the example proyect but it doesn't work. What am i doing wrong?
Helper.js
var helperFuntions = {
findExistingDataOnTable : function(cssSelector,query){
var encontrado = $(cssSelector).matchingText(query);
if (encontrado) {
console.log("Superado");
}else{
console.log("No superado");
}
}
};
ProyectoPrueba.js (not working, wrong import)
var helperFuntions = require("modules/Helper/Helper.js");
When(/^Compruebo la existencia de "(.*?)"$/, function (query) {
var cssTable = "\".ym-cbox\"";
helperFuntions.findExistingDataOnTable(cssTable, query);
});
ProyectoPrueba.js (working, no import)
When(/^Compruebo la existencia de "(.*?)"$/, function (query) {
var found= $(".ym-cbox").matchingText(query);
if (found) {
console.log("Superado");
}else{
console.log("No superado");
}
});
Proyect hierarchy
You must exclude the "modules" folder from the require path:
require("Helper/Helper");
Also, the extension ".js" is not required.
Your imports need to be absolute paths unless you're importing modules from a node_modules folder.
Let's say the modules folder is a sibling of the current folder you're importing it from.
In that case require("modules/Helper/Helper.js"); should be require("../modules/Helper/Helper.js");
Related
I have a nodejs package and i have a linkedList code. I want to include my LinkedList code in my NodeJs js folder but I can't do it. What is the my fault ?
I want to use it like this
This is my code in my NodeJs folder.(app.js)
var KullaniciJS=require('KullaniciLibrary.js');
This is my required folder codes.(KullaniciLibrary.js)
function Kullanici(KullaniciAdi)
{
this.KullaniciAdi=KullaniciAdi;
this.Sonraki=null;
}
function KullaniciListe()
{
this.Bas=null;
this.Uzunluk=0;
}
KullaniciListe.prototype.Ekle=function(EklenecekKullaniciAdi)
{
var Bas=this.Bas;
if (!Bas) {
this.Bas = EklenecekKullaniciAdi;
this.Uzunluk++;
return EklenecekKullaniciAdi;
}
while (Bas.Sonraki!==null) {
Bas = Bas.Sonraki;
}
Bas.Sonraki = EklenecekKullaniciAdi;
this.Uzunluk++;
return EklenecekKullaniciAdi;
};
First you have to declare inside of your KullaniciLibrary.js file, what you want to export. you do this by adding following line at the end of the file:
module.exports = YOUREXPORT;
YOUREXPORT will probably just be the methodname (or some object).
After that, you wanna import it like this:
var KullaniciJS = require('./KullaniciLibrary');
Note that you have to prefix the name with ./ and you don't need to define the file-ending.
If you don't prefix the module with ./, node will search for the module in the node_modules folder, instead of the current directory
In nodejs to require a function from another file you have to export then function.
To export the function use:
module.export='your function name';
and to require the function use:
const variable=require(./filename)
for more understanding use this link:
https://nodejs.org/api/modules.html
I'm building an MVC application, which is making use of Areas. So I've got a folder structure as follows:
Areas/
AreaOne/
Views/
About/
ScriptOne.js
ScriptTwo.js
Index.cshtml
Home/
ScriptThree.js
ScriptFour.js
Index.cshtml
AreaTwo
Views/...
I'd like to bundle the JavaScript files within the individual page's folder within the View folder.
E.g. ScriptOne.js, ScriptTwo.js should be bundled into About.js and output into a seperate folder like:
js/
Areas/
AreaOne/
About.js
Home.js
I'm able to grab the files, however it will grab all .js files under Areas.
gulp.src('./Areas/**/*.js')
Areas\AreaOne\Views\About\ScriptOne.js
Areas\AreaOne\Views\About\ScriptTwo.js
Areas\AreaOne\Views\Home\ScriptThree.js
Areas\AreaOne\Views\Home\ScriptFour.js
If I concat this I'll end up with a single .js file. However I'd like About.js and Home.js
If I can achieve the above in theory I can use the following (but i'm unsure how to get the appropriate area name:
pipe(gulp.dest('./js/Areas/...AREA NAME GOES HERE.../'))
I'm trying to avoid manual concatenation of files.. like so..
gulp.src(['./Areas/AreaOne/Views/About/ScriptOne.js', './Areas/AreaOne/Views/About/ScriptTwo.js'])
Not sure it's the most succinct way but it works and it's fairly simple.
I use Glob to get the file names within the Areas.
I then add the view paths to a JavaScript object (this works like a set, it prevents duplicates), I also add the name of the view folder as this will be the output file name.
I use gulp.src with the distinct view folder names to collect all of the Javascript files within the individual view folder and then concat them, and finally if it's a release build the files get ugilfied.
var gulp = require('gulp'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
glob = require('glob'),
path = require('path');
gulp.task('process-areas-js', function () {
glob('./Areas/**/*.js*', null, function (er, files) {
var distinctAreaPaths = {};
for (var i = 0; i < files.length; i++) {
var filePath = path.dirname(files[i]).replace(/\./g, "");
var viewFolderName = path.basename(filePath);
distinctAreaPaths[filePath] = viewFolderName;
}
for (distinctPath in distinctAreaPaths) {
var concatName = distinctAreaPaths[distinctPath] + ".js";
var destination = './js' + path.dirname(distinctPath);
var sourceGlob = "." + distinctPath + "/*.js";
var pipeline = gulp.src(sourceGlob)
.pipe(concat(concatName))
uglifyOnRelease(pipeline)
.pipe(gulp.dest(destination));
}
});
});
gulp.task('default', ['process-areas-js']);
function uglifyOnRelease(pipeline) {
return process.env.NODE_ENV === 'Release' || process.env.NODE_ENV == null ? pipeline.pipe(uglify()) : pipeline;
}
I'm trying to write a gulp task to build javascript file and concatenate a single file for each folder, including the root folder.
I have found this solution: https://github.com/gulpjs/gulp/blob/master/docs/recipes/running-task-steps-per-folder.md
If you have a set of folders, and wish to perform a set of tasks on
each, for instance...
/scripts
/scripts/jquery/*.js
/scripts/angularjs/*.js
...and want to end up with...
/scripts
/scripts/jquery.min.js
/scripts/angularjs.min.js
However, this only builds *.js file for all the subfolders inside scripts folder. I'm trying to build the *.js file inside the root folder scripts, i.e. my expected output will be:
/scripts.min.js
/scripts/jquery.min.js
/scripts/angularjs.min.js
I'm new to node so I'm confused now how to achieve that. Really appreciate your help on this. Thanks alot.
You can create a separate task (baseScripts) for creating the minified scripts for the base directory. Then create another task (allScripts) that runs both the baseScripts and subScripts tasks.
var scriptsPath = 'src/scripts';
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
gulp.task('allScripts', ['baseScripts', 'subScripts']);
gulp.task('subScripts', function() {
var folders = getFolders(scriptsPath);
var tasks = folders.map(function(folder) {
return gulp.src(path.join(scriptsPath, folder, '/*.js'))
.pipe(uglify())
.pipe(rename(folder + '.min.js'))
.pipe(gulp.dest(scriptsPath));
});
return merge(tasks);
});
gulp.task('baseScripts', function(){
return gulp.src(scriptsPath + '/*.js')
.pipe(uglify())
.pipe(concat('scripts.min.js'))
.pipe(gulp.dest('src'));
});
After a day, I have come up with some sort of modifying as below.
var paths = {
js: {
folder: 'dev/assets/js'
}
};
gulp.task('js', function() {
var folders = getFolders(paths.js.folder);
var tasks = folders.map(function(folder) {
return gulp.src(path.join(paths.js.folder, folder.path, '/*.js'))
.pipe(uglify())
.pipe(concat(folder.name + '.min.js'))
.pipe(gulp.dest(paths.js.dest));
});
merge(tasks);
browserSync.reload();
});
var getFolders = function(dir) {
var folders = [{path:'',name:'app'}];
var folder = fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
for (var i=0; i<folder.length;i++) {
folders.push({path: folder[i], name: folder[i]});
}
return folders;
};
I have separated the directories and the names for combined script into 2 properties of an object. So I don't need to have different tasks for the root folder and subfolders.
Please feel free to give your comments on my approach :)
Using phpStorm, I would like to merge multiple JavaScript files into one.
I installed the closure compiler and configured the file watcher to minify each JavaScript file.
Now, I would like to combine all of my JavaScript files into one.
Here's the architecture of my project (a test project to merge js files) :
index.php
js(folder) >
first.js (+first.min.js),
second.js (+second.min.js),
third.js (+third.min.js)
cache (folder)
main.js
I would like to merge (first.min.js, second.min.js, third.min.js) into folder cache > main.js.
Ideally, merging all of the files would happen automatically; I don't want to specify each js file manually.
Can someone explain the arguments I must use to configure my filewatcher?
I used npm plugins concat, minifier and walk.
Here is the script I made :
var walk = require('walk'),
concat = require('concat'),
minifier = require('minifier'),
files = [];
var JS_SOURCES_DIR = 'app/components',
JS_LAST_FILE = 'app/app.module.js',
JS_DIR = 'app/',
JS_FULL_FILE = JS_DIR + 'app.js',
JS_MINIFIED_FILE = 'app.min.js',
JS_MINIFIED_FILE_PATH = JS_DIR + JS_MINIFIED_FILE;
var walker = walk.walk(JS_SOURCES_DIR, {followLinks: false});
walker.on('file', (root, stat, next) => {
var fullpath = root.replace(/\\/g, '/');
var regex = new RegExp(/.+\.js$/);
if (stat.name.match(regex)) {
files.push(fullpath + '/' + stat.name);
}
next();
});
walker.on('end', function () {
files.push(JS_LAST_FILE);
files.forEach(function (item) {
console.log(item);
})
concat(files, JS_FULL_FILE).then((result) => {
minifier.minify(JS_FULL_FILE, {output: JS_MINIFIED_FILE_PATH});
console.log('\n[OK] ' + JS_MINIFIED_FILE + ' sucessfully updated');
}, function (error) {
console.log('[ERROR] JS concat failure: ' + error.message);
});
});
minifier.on('error', function (error) {
console.log('\n[ERROR] JS minify error: ' + error);
});
First with walker, files are added to var "files". I used JS_LAST_FILE for angularjs concerns, as I build the module and add all the dependencies in that file. Then files are concatenated to JS_FULL_FILE. Finally JS_FULL_FILE is minified to JS_MINIFIED_FILE.
I do not use a watcher to trigger the concat script when a file is updated.
Instead when I work locally, I don't concatenate files but I simply add them in the head part of the page using a homemade function that uses php scandir().
I'm writing a new kind of structure and scalability for an express application.
Issue:
I don't like to define each route on app.js file.
Solution:
Make something automated in order to load the routes automatically.
So far, I have this code on index.js (routes folder) file:
var fs = require('fs');
module.exports = function(app) {
recursive_require(__dirname, app);
function recursive_require(directory, app) {
fs.readdirSync(directory).forEach(function(file) {
if (fs.lstatSync(directory + '/' + file + '/').isDirectory()) {
var has_no_js_files = false;
directory = directory + '/' + file + '/';
console.log('Scanning recursively on ' + directory);
// We run across the directory to check if there are any js files.
fs.readdirSync(directory).forEach(function(file) {
console.log('Reading file/directory ' + file);
if (file.match(/\.js$/g)) {
has_no_js_files = true;
console.log('Found js files on directory ' + directory);
}
});
// If the folder has no js files, we take in mind that there are other folders inside
// so we scan the folder recursively.
if (!has_no_js_files) {
console.log('No JS files found on ' + directory + ' going to scan recursively');
recursive_require(directory.substr(0, directory.lastIndexOf('/')));
} else {
// Otherwise, we require the directory taking in mind that we have just js files.
console.log('Found JS files on ' + directory + ', require them');
require(directory)(app);
}
}
});
}
}
Now, This seems to work but I have a bit of an issue..
My idea is to have everything on folders, so say, this structure:
routes
admin
posts
index.js <- handles add, remove, edit for posts
users
index.js <- handles add, remove, edit for users
blog
posts
index.js <- handles show for frontend
index.js <- Loads all of the files recursively.
Now, I have a bit of an issue with this code...
I'm having this error:
PS C:\Users\bony-_000\Documents\GitHub\node-blog> node app
Scanning recursively on C:\Users\bony-_000\Documents\GitHub\node-blog\routes/admin/
Reading file/directory posts
No JS files found on C:\Users\bony-_000\Documents\GitHub\node-blog\routes/admin/ going to scan recursively
Scanning recursively on C:\Users\bony-_000\Documents\GitHub\node-blog\routes/admin/posts/
Reading file/directory index.js
Found js files on directory C:\Users\bony-_000\Documents\GitHub\node-blog\routes/admin/posts/
Found JS files on C:\Users\bony-_000\Documents\GitHub\node-blog\routes/admin/posts/, require them
C:\Users\bony-_000\Documents\GitHub\node-blog\routes\admin\posts\index.js:2
app.get('/admin/posts/add', function(req, res) {
^
TypeError: Cannot call method 'get' of undefined
Though I'm sending the app var...
Any help will be much appreciated, also, feel free to use the code.
I've solved all the issues and now I have my structured MVC Online!
This is the code for anyone to use:
var fs = require('fs'),
required_files = [];
module.exports = function(app) {
recursive_require(__dirname, __dirname, app);
function recursive_require(directory, base_dir, app) {
fs.readdirSync(directory).forEach(function (input) {
var next_directory = directory + '/' + input + '/';
// If we are on the base dir, we ignore the index.js file
if (!(required_files.indexOf(base_dir + '/index') > -1)) {
required_files.push(base_dir + '/index');
}
// Check if it's a directory
if (fs.lstatSync(next_directory).isDirectory()) {
// We require it recursively
console.log('Reading directory ' + next_directory);
recursive_require(next_directory.substr(0, next_directory.lastIndexOf('/')), base_dir, app);
} else {
// We require all (except the index.js file if the var is set to true) js files on folder
require_files(directory, app);
return;
}
});
}
function require_files(directory, app) {
fs.readdir(directory, function(err, files) {
files.forEach(function(file) {
if (file.match(/\.js$/g)) {
var file_path = directory + '/' + file;
file_path = file_path.substr(0, file_path.indexOf('.js'));
if (required_files.indexOf(file_path) == -1) {
required_files.push(file_path);
require(file_path)(app);
}
}
});
});
return;
}
}
Any suggestions are welcome.