Skip to content

Add a grunt task #1

@benallard

Description

@benallard

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 !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions