I'm trying to change a task from my gulpfile to create a rev manifest to be used in another moment. After this code work, I will add gulp-sourcemap to create some sourcemaps. But, this simple thing need to work first.
The objective is concatenate some scripts that are in scripts folder and inside a scripts subfolder (like 'c.js'), apply rev in this new script, vendor.js, and then fullfill the rev manifest to be used on another task.
I'm not a expert in Node.js or Gulp. It's de very first time I try to do this. So, it would be a stupid thing I forgot.
Here is my code (modified to look like an example) and thanks in advance
var vendorJs = ['scripts/a.js','scripts/b.js','scripts/anotherFolder/c.js'];
gulp.task("concat-vendor-js", function () {
return gulp.src(vendorJs)
.pipe(concat('vendor.js'))
.pipe(uglify())
.pipe(rev())
.pipe(gulp.dest('dist/Scripts'))
.pipe(rev.manifest())
.pipe(gulp.dest('dist'));
}
I'm getting this on my rev-manifest.json:
{
"/vendor.js": "/vendor-66687d0f7d.js"
}
But it should be like this:
{
"Scripts/vendor.js": "Scripts/vendor-66687d0f7d.js"
}
Related
I'm developing a node.js package that generates some boilerplate code for me.
I was able to create this package and if I run every module individual it works fine and generates the code as expected.
My idea now is to import this package in another project and have it generate the code.
I have no idea to how to achieve this or even if it's possible.
The perfect solution would be that every time a file changed in a set of folders it would run the package and generate the files but if this isn't possible it would be ok as well to expose or command to manually generate this files.
I have created a script to run the generator script but it only works on the package itself and not when I import it in another project.
Thanks
I think you want the fs.watch() function. It invokes a callback when a file (or directory) changes.
import { watch } from 'fs';
function watchHandler (eventType, filename) {
console.log(`event type is: ${eventType}`) /* 'change' or 'rename' */
if (filename) {
console.log(`filename provided: ${filename}`)
} else {
console.log('filename not provided');
}
}
const options = { persistent: true }
const watcher = fs.watch('the/path/you/want', options, watchHandler)
...
watcher.close()
You can use this from within your nodejs app to invoke watchHandler() for each file involved in your code generation problem.
Question is too broad / unclear. Anyone interested in this answer would be better served by visiting: Creating Callbacks for required modules in node.js
Basically I have included a CLI package in my node application. I need the CLI to spin up a new project (this entails creating a folder for the project). After the project folder is created, I need to create some files in the folder (using fs writeFile). The problem is right now, my writeFile function executes BEFORE the folder is created by the CLI package (This is detected by my console.log. This brings me to main main question.
Can I add an async callback function to the CLI.new without modifying the package I included?
FoundationCLI.new(null, {
framework: 'sites', // 'apps' or 'emails' also
template: 'basic', // 'advanced' also
name: projectName,
directory: $scope.settings.path.join("")
});
try{
if (!fs.existsSync(path)){
console.log("DIRECTORY NOT THERE!!!!!");
}
fs.writeFileSync(correctedPath, JSON.stringify(project) , 'utf-8');
} catch(err) {
throw err;
}
It uses foundation-cli. The new command executes the following async series. I'd love to add a callback to the package - still not quite sure how.
async.series(tasks, finish);
Anyone interested in this can probably get mileage out of:
Creating Callbacks for required modules in node.js
The code for the new command seem to be available on https://github.com/zurb/foundation-cli/blob/master/lib/commands/new.js
this code was not written to allow programmatic usage of the new command (it uses console.log everywhere) and does not call any callback when the work is finished.
so no there is no way to use this package to do what you are looking for. Either patch the package or find another way to do what you want to achieve.
I am not very much familiar with nodejs but, I need some guidance in my task. Any help would be appreciated.
I have nodejs file which runs from command line.
filename arguments and that do some operation whatever arguments I have passed.
Now, I have html page and different options to select different operation. Based on selection, I can pass my parameters to any file. that can be any local node js file which calls my another nodejs file internally. Is that possible ? I am not sure about what would be my approach !
I always have to run different command from terminal to execute different task. so, my goal is to reduce that overhead. I can select options from UI and do operations through nodejs file.
I was bored so I decided to try to answer this even though I'm not totally sure it's what you're asking. If you mean you just need to run a node script from a node web app and you normally run that script from the terminal, just require your script and run it programmatically.
Let's pretend this script you run looks like this:
// myscript.js
var task = process.argv[2];
if (!task) {
console.log('Please provide a task.');
return;
}
switch (task.toLowerCase()) {
case 'task1':
console.log('Performed Task 1');
break;
case 'task2':
console.log('Performed Task 2');
break;
default:
console.log('Unrecognized task.');
break;
}
With that you'd normally do something like:
$ node myscript task1
Instead you could modify the script to look like this:
// Define our task logic as functions attached to exports.
// This allows our script to be required by other node apps.
exports.task1 = function () {
console.log('Performed Task 1');
};
exports.task2 = function () {
console.log('Performed Task 2');
};
// If process.argv has more than 2 items then we know
// this is running from the terminal and the third item
// is the task we want to run :)
if (process.argv.length > 2) {
var task = process.argv[2];
if (!task) {
console.error('Please provide a task.');
return;
}
// Check the 3rd command line argument. If it matches a
// task name, invoke the related task function.
if (exports.hasOwnProperty(task)) {
exports[task]();
} else {
console.error('Unrecognized task.');
}
}
Now you can run it from the terminal the same way:
$ node myscript task1
Or you can require it from an application, including a web application:
// app.js
var taskScript = require('./myscript.js');
taskScript.task1();
taskScript.task2();
Click the animated gif for a larger smoother version. Just remember that if a user invokes your task script from your web app via a button or something, the script will be running on the web server and not the user's local machine. That should be obvious but I thought I'd remind you anyway :)
EDIT
I already did the video so I'm not going to redo it, but I just discovered module.parent. The parent property is only populated if your script was loaded from another script via require. This is a better way to test if your script is being run directly from the terminal or not. The way I did it might have problems if you pass an argument in when you start your app.js file, such as --debug. It would try to run a task called "--debug" and then print out "Unrecognized task." to the console when you start your app.
I suggest changing this:
if (process.argv.length > 2) {
To this:
if (!module.parent) {
Reference: Can I know, in node.js, if my script is being run directly or being loaded by another script?
I'm trying to do the following:
gulp.src('js/*.js')
.pipe(concat('_temp.js'))
.pipe(gulp.dest('js/'));
gulp.src('build/js/app.js')
.pipe(replace('// includes', fs.readFileSync('js/_temp.js')))
.pipe(uglify())
.pipe(rename('app.min.js'))
.pipe(gulp.dest('build/js'));
So, I'm concatenating all .js files in js/. The concatenated file is named _temp.js;
After that, I'm reading a app.js and try to replace the // includes string with the concatenated _temp.js file. This doesn't work as node says the file doesn't exist. It does exist though, so I second time I run this task, it works.
This has most probably to do with asynchronisity, but I'm unable to see how I would do this differently?
You can split your task into 2 and make one of the tasks run after another by using Gulp's dependency resolution system.
gulp.task('task1', function () { ... });
gulp.task('task2', ['task1'], function () { ... });
I'm building an MMO using Node.js, and I'd like some scripters from my team to create scripts (duh) for bosses and other scripted objects. My first thought was to have a folder on the server where people can upload javascript files and let node.js automatically read and parse every script in the directory, but I don't want them to be able to execute process.exit() for example, or other hazardous stuff.
Is there a solution that lets me control what functions the scripters are able to call?
You can control what functions are they unable to call with vm module.
For example,
vm.runInNewContext(userCode, {
require: null,
process: null,
someFunc: function (x) { return x+1 },
someData: { abc: 'def' }
});
Will javascript work as scripting language. I guess it should.
so, I think vm.runInNewContext is might be all you need.
Have a look at http://nodejs.org/docs/latest/api/vm.html