Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a grunt task #1

Open
benallard opened this issue May 21, 2014 · 0 comments
Open

Add a grunt task #1

benallard opened this issue May 21, 2014 · 0 comments

Comments

@benallard
Copy link

I created a grunt task from this api to integrate it with my Gruntfile:

grunt.registerMultiTask('neocities', 'Upload files to the Neocities web-service', function(){
    'use strict';
    var done = this.async();

    var NeoCities = require('neocities');

    var options = this.options();

    if (options.username === undefined || options.password === undefined){
        grunt.log.writeln(options);
        grunt.log.error('options.username and options.password are mandatory !');
        done(false);
        return;
    }

    var api = new NeoCities(options.username, options.password);

    var uploads = [];

    this.files.forEach(function(filePair) {
        if (filePair.src.length != 1 && filePair.dest !== undefined){

            grunt.log.error('Same destination for multiple sources ?!');
            done(false);
            return;
        }
        filePair.src.forEach(function(src) {
            var pair = {path: src};
            if (filePair.dest !== undefined){
                pair.name = filePair.dest;
            } else {
                pair.name = src.split(/[\\/]/).pop();
            }
            uploads.push(pair);
        });
    });
    api.upload(uploads, function(resp) {
        if (resp.result == 'error'){
            grunt.log.error(resp.message);
            done(false);
            return;
        }
        grunt.log.writeln(resp.message);
        done(true);
    })
});

I use the task as follow:

grunt.initConfig({
    neo: grunt.file.readYAML('neocities_credentials.yaml'),
    neocities: {
        options: {
            username: '<%= neo.username %>',
            password: '<%= neo.password %>'
        },
        dist: {
            src: 'dist/*',
        }
    },
});

Some more flexibility is allowed (src ->dest mapping), see the code.

I thought maybe you're interested in publishing it.

It is the first time I wrote a grunt task, so there are probably lots of improvements to do.

Cheers !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant