summaryrefslogtreecommitdiff
path: root/gulpfile.js
blob: fbc58a101769c848a63c14970239504a5b498539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict';
// This is used to generate individual packages for each async method

var gulp = require('gulp');
var path = require('path');
var fs = require('fs-extra');
var pkg = require('./package.json');
var jsonFuture = require('json-future');
var template = require('lodash.template');

var moduleDeps = JSON.parse(template(fs.readFileSync('./support/dependencies.json').toString())({
    version: pkg.version}
    ));

var MODULES_PATH = './lib/';

function getFolders(dir) {
    return fs.readdirSync(dir)
        .filter(function(file) {
            return fs.statSync(path.join(dir, file)).isDirectory();
        });
}

function generatePackage(name) {
    function generateKeywords(name) {
        var keywords = [
            'async',
            'async-modularized'
        ];

        keywords.push(name);
        return keywords;
    }

    function generateDefaultFields(name) {
        var ORIGINAL_FIELDS = [
            'author',
            'version',
            'repository',
            'license'
        ];

        var structure = {
            name: 'async.' + name,
            description: 'async ' + name + 'method as module.',
            main: './index.js',
            repository: "async-js/async." + name
        };

        ORIGINAL_FIELDS.forEach(function(field) {
            structure[field] = pkg[field];
        });

        if (Object.keys(moduleDeps[name]).length > 0)
            structure.dependencies = moduleDeps[name];

        return structure;
    }

    var modulePackage = generateDefaultFields(name);
    modulePackage.keywords = generateKeywords(name);
    return modulePackage;
}

function generateReadme(name, dist) {
    var filepath = path.resolve('support/module_template.md');
    var tpl = fs.readFileSync(filepath).toString();
    tpl = template(tpl)({name: name});
    fs.writeFileSync(dist, tpl);
}

function copyMetaFiles(dist) {
    var files = ['.editorconfig', '.jscsrc', '.jshintrc', '.gitignore'];

    files.forEach(function(file) {
        var metafile = path.resolve(file);
        var distFile = path.resolve(dist, file);
        fs.copySync(metafile, distFile);
    });
}

gulp.task('package', function() {
    return getFolders(MODULES_PATH).map(function(module) {
        var dist = path.resolve(MODULES_PATH, module);
        jsonFuture.save(path.resolve(dist, 'package.json'), generatePackage(module));
        generateReadme(module, path.resolve(dist, 'README.md'));
        copyMetaFiles(dist);
    });
});