/**
 * Generate a package json from a apemanfile.
 * @function ci.worker.generatePackageJson
 * @param {object} config - Task configuration
 * @param {string} config.src - Apemanfile file name.
 * @param {string} config.dest - Destination file name.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 */
var path = require('path'),
    debug = require('./_debug'),
    apeman = require('apeman'),
    lib = require('../../lib'),
    EOL = require('os').EOL,
    file = lib.file,
    writeReadonlyFile = file.writeReadonlyFile;
exports = module.exports = function (config, callback) {
    var project = apeman.loadProject(path.resolve(config.src || '.')),
        meta = project.meta,
        dest = path.resolve(config.dest);
    var data = {};
    exports._packageJsonKeys.forEach(function (key) {
        data[key] = meta[key];
    });
    var tasks = project.tasks;
    data.dependencies = tasks.installNodePackages.config.packages || {};
    data.dependencies.apeman = apeman.version || '*';
    data.devDependencies = tasks.installNodeDevPackages.config.packages || {};
    data.main = 'Apemanfile.js';
    writeReadonlyFile(dest, JSON.stringify(data, null, 4) + EOL, function (err) {
        if (!err) debug.didCreateFile(dest);
        callback(err);
    });
};
exports._packageJsonKeys = [
    'name',
    'version',
    'description',
    'author',
    'dependencies',
    'devDependencies',
    'repository',
    'scripts'
];
