I'm trying to encrypt my .keychain files on my Mac and saving them on in a backup place. When i run grunt enc it just creates 2 very small files in the directory of the source files. So encrypting and moving the files doesn't go well...
package.js is just grunt with 1 devDependency grunt-encrypt.
Gruntfile.js is shown below.
'use strict';
module.exports = function(grunt) {
var npmDependencies = require('./package.json').devDependencies;
grunt.initConfig({
encrypt: {
encryptKeys: {
options: {
key: 'SuperSecretKey',
dest: '/Users/myusername/www/admin/filebackup/encrypted',
},
files: {
'keychains': [
'/Library/Keychains/system.keychain',
'/Users/myusername/Library/Keychains/login.keychain'
]
}
}
},
});
grunt.loadNpmTasks('grunt-encrypt');
grunt.registerTask('enc', [
'encrypt'
]);
};
I've been using Grunt for a week, so could very well be i'm missing something obvious. Anybody used grunt-encrypt before?
Thanks!
Looks as if your SuperSecretKey is not defined yet.
I've solved it like this:
add
module.exports = function (grunt) {
// ad here
var superSecretKey = 'secretpassword';
to your Gruntfile.js
Or put a keyfile (mine is called livereload.key) in the project's root and alter your Gruntfile like this:
//key : superSecretKey,
key: grunt.file.read('livereload.key'),
Related
I am using save2png to provide fallback pngs to svg4everybody. The svgs are created and I can change input and output directory. But svg4everybody requires that the pngs are saved with a filename like: "spritesheetname.svg.id.jpg"
At first, I tried some simple things, but I can't even figure out how to change the filename in the first place.
Here is my code:
module.exports = function(grunt){
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
svg2png: {
all: {
files: [
// rasterize all SVG files in "img" and its subdirectories to "img/png"
{ cwd: '../img/icons/',
src: ['**/*.svg'],
dest: '../symbol/svg/',
rename: function(dest, src) {
var filename = src.substring(src.lastIndexOf('/'), src.length);
return dest + 'prefix' + filename + '.png';
}
}
]
}
}
});
grunt.registerTask('default', []);
grunt.loadNpmTasks('grunt-svg2png');
};
The pngs are created, but the filename is always the same as the original svg.
I stumbled upon this question, Grunt rename not working, but it was no real help. Flatten and expand just changed the directory structure, but had no influence on the filename.
I'm trying to configure grunt-horde so that I can have multiple builds all using a centrally managed task configuration.
The documentation provides the following example of a registerMultiTasks.js file, but I can't get it to work
module.exports = function(grunt) {
var myMultiTask = require('./multi-tasks/secret-sauce.js');
return {
myMultiTask: ['some description', myMultiTask]
};
};
Even if I replace their example with something more simple:
module.exports = function(grunt) {
return {
demo: ['Demo', function() {
console.info('hello');
}]
};
};
When I run grunt demo:test the output is:
Running "demo:test" (demo) task
Verifying property demo.test exists in config...ERROR
>> Unable to process task.
Warning: Required config property "demo.test" missing. Use --force to continue.
Aborted due to warnings.
When I run grunt --help the demo task shows up in the list. Thinking about the warning message I've also tried the following, but again with no luck.
module.exports = function(grunt) {
return {
demo: ['Demo', function(){
return {test: function(){console.info('hello');}};
}]
};
};
...what am I doing wrong?
I figured it out - you need to define the configuration for each target of the multitasks:
initConfig/demo.js
module.exports = function() {
'use strict';
return {
test: {
foo: 'bar'
}
};
};
You can then access this configuration data and the target from within the multitask function:
registerMultiTask.js
module.exports = function(grunt) {
return {
demo: ['Demo', function() {
grunt.log.writeln('target: ' + this.target);
grunt.log.writeln('foo: ' + this.data.foo);
}]
};
};
I'm trying to figure out a way to break out a watch target from the rest of the block. Currently my watch target looks like this:
watch: {
options: {
// Parent-level options
},
coffee: {
// ...
},
stylus: {
// ...
},
test: {
options: {
// Test-specific options
},
files: {
// ...
}
tasks: {
// ...
}
}
}
The problem I'm facing is that my test options include a different livereload port than the top level, so I can simultaneously run grunt server and grunt test with livereloading and not have them interfere with each other.
Beneath that, I have a server alias and a test alias. What I'm looking for is to break the test watch target out into another task so I can simply run watch in my server alias and something like watch-test for testing, such that the server task doesn't run the test target.
Any ideas? Please let me know if I've left out anything important or this isn't clear.
Thanks!
A solution I've used is to define multiple watch targets and rename the watch task like so:
watch: {
scripts: {
files: ['js/**/*.js'],
tasks: ['concat', 'uglify'],
options: {
spawn: false
}
}
},
// Don't uglify in dev task
watchdev: {
scripts: {
files: ['js/**/*.js'],
tasks: ['concat'],
options: {
spawn: false
}
}
}
grunt.loadNpmTasks('grunt-contrib-watch');
// Rename watch to watchdev and load it again
grunt.renameTask('watch', 'watchdev');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['watch']);
grunt.registerTask('dev', ['watchdev']);
Since grunt watch is a multi task, running grunt watch from the CLI means that all targets are watched. You can instead run one target out of those by simply running grunt watch:test or grunt watch:server, whatever is your preference. Hope that helps.
Edit: It might be appropriate to point out this issue on the watch issue tracker:
https://github.com/gruntjs/grunt-contrib-watch/issues/206
The code in the issue is a little old, I would recommend newer code to require lodash and use _ instead of grunt.util._ (that utility is now deprecated). So the code would look like this:
var _ = require('lodash');
module.exports = function(grunt) {
// Run with: grunt switchwatch:target1:target2 to only watch those targets
grunt.registerTask('switchwatch', function() {
var targets = Array.prototype.slice.call(arguments, 0);
Object.keys(grunt.config('watch')).filter(function(target) {
return !(_.indexOf(targets, target) !== -1);
}).forEach(function(target) {
grunt.log.writeln('Ignoring ' + target + '...');
grunt.config(['watch', target], {files: []});
});
grunt.task.run('watch');
});
}
Still, you could modify your server task to run something like switchwatch:coffee:stylus:server:
grunt.registerTask('server', [/* rest of your tasks */, 'switchwatch:coffee:stylus:server']);
I'm running a grunt concat task on one of my projects and it looks something like this:
/**
* Concatenate | Dependencies Scripts
*/
concat: {
dependencies: {
files: {
"./Ditcoop/js/plugins.min.js": ["./Ditcoop/js/vendor/**/*.min.js", "!./Ditcoop/js/vendor/modernizr/*.js", "!./Ditcoop/js/vendor/jquery/*.js"],
"./Global/js/plugins.min.js": ["./Global/js/vendor/**/*.min.js", "!./Global/js/vendor/modernizr/*.js", "!./Global/js/vendor/jquery/*.js"],
"./Webshop/js/plugins.min.js": ["./Webshop/js/vendor/**/*.min.js", "!./Webshop/js/vendor/modernizr/*.js", "!./Webshop/js/vendor/jquery/*.js"]
}
}
}
My question would be if I could somehow make that more dynamic without having to specify each root folder. I was thinking of something like this:
concat: {
dependencies: {
files: {
"./*/js/plugins.min.js": ["./*/js/vendor/**/*.min.js", "!./*/js/vendor/modernizr/*.js", "!./*/js/vendor/jquery/*.js"],
}
}
}
I'm pretty sure I cannot do it this way, but I could use the expand option, I'm just not sure how I could use it so I can do that under the right root folder, so I won't create the same destination file as many times I run the concat.
Always remember Gruntfiles are javascript :)
grunt.initConfig({
concat: {
dependencies: {
files: (function() {
var files = Object.create(null);
grunt.file.expand({filter: 'isDirectory'}, '*').forEach(function(dir) {
files[dir + '/js/plugins.min.js'] = [
dir + '/js/vendor/**/*.min.js',
'!' + dir + '/js/vendor/modernizr/*.js',
'!' + dir + '/js/vendor/jquery/*.js'
];
});
return files;
}()),
},
},
});
But if your dependency handling logic is this complex you may want to consider using a module loader such as browserify or requirejs. The concat task is really just for joining simple files together.
I'm new to Grunt, and from what I understood up till now, Grunt has the "watch" task, which continuously checks files for modifications, and each time modification happens, runs corresponding tasks.
What I'm looking for would be a kind of discrete version of this - a task, that would run other tasks, if and only if some files were changed since the last build.
Seems to be a natural thing to ask for, but I couldn't find this. Is it just me, or is this really an issue?
Configuration file should look like this:
grunt.initConfig({
foo: {
files: "foo/*"
// some task
},
bar: {
files: "bar/*"
// some other task
},
ifModified: {
foo: {
files: "foo/*",
tasks: ['foo']
},
bar: {
files: 'bar/*',
tasks: ['bar', 'foo']
}
}
});
grunt.registerTask('default', ['bar', 'foo']);
Running grunt should always execute tasks 'bar', 'foo', while running grunt ifModified should execute any tasks only if some of the files were actually changed since the previous build.
Made my own task for that. It turned out to be not hard, here is the code:
build/tasks/if-modified.js:
var fs = require('fs');
var crypto = require('crypto');
module.exports = function (grunt) {
grunt.registerMultiTask('if-modified', 'Conditionally running tasks if files are changed.', function () {
var options = this.options({});
grunt.verbose.writeflags(options, 'Options');
var hashes = {};
if (grunt.file.exists(options.hashFile)) {
try {
hashes = grunt.file.readJSON(options.hashFile);
}
catch (err) {
grunt.log.warn(err);
}
}
grunt.verbose.writeflags(hashes, 'Hashes');
var md5 = crypto.createHash('md5');
this.files.forEach(function (f) {
f.src.forEach(function (filepath) {
var stats = fs.statSync(filepath);
md5.update(JSON.stringify({
filepath: filepath,
isFile: stats.isFile(),
size: stats.size,
ctime: stats.ctime,
mtime: stats.mtime
}));
});
});
var hash = md5.digest('hex');
grunt.verbose.writeln('Hash: ' + hash);
if (hash != hashes[this.target]) {
grunt.log.writeln('Something changed, executing tasks: ' + JSON.stringify(options.tasks));
grunt.task.run(options.tasks);
hashes[this.target] = hash;
grunt.file.write(options.hashFile, JSON.stringify(hashes));
}
else
grunt.log.writeln('Nothing changed.');
});
};
Gruntfile.js:
grunt.initConfig({
foo: {
src: ["foo/**/*"],
dest: "foo-dest"
// some task
},
bar: {
src: ["bar/**/*", "foo-dest"]
// some other task
},
'if-modified': {
options: {
hashFile: 'build/hashes.json'
},
foo: {
src: ['foo/**/*', 'Gruntfile.js', 'package.json'],
options: {tasks: ['foo']}
},
bar: {
src: ['bar/**/*', "foo-dest", 'Gruntfile.js', 'package.json'],
options: {tasks: ['bar']}
}
}
});
grunt.loadTasks('build/tasks'); // if-modified.js in this dir
grunt.registerTask('default', ['foo', 'bar']);
run:
grunt if-modified
You could create a task that runs conditionally other tasks, from https://github.com/gruntjs/grunt/wiki/Creating-tasks :
grunt.registerTask('foo', 'My "foo" task.', function() {
// Enqueue "bar" and "baz" tasks, to run after "foo" finishes, in-order.
grunt.task.run('bar', 'baz');
// Or:
grunt.task.run(['bar', 'baz']);
});
What you need might be grunt-newer :
The newer task will configure another task to run with src files that are a) newer than the dest files or b) newer than the last successful run (if there are no dest files). See below for examples and more detail.
https://github.com/tschaub/grunt-newer