2014-08-28 03:24:23 +02:00
|
|
|
'use strict';
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
|
2014-09-09 02:50:35 +02:00
|
|
|
var COMMONJS_BANNER = '// This file is generated. You can require() it in a CommonJS environment.\n';
|
|
|
|
|
2014-08-28 03:39:41 +02:00
|
|
|
module.exports = function generateCommonJSModule(grunt, srcFiles, destFilepath) {
|
|
|
|
var destDir = path.dirname(destFilepath);
|
2014-08-28 03:24:23 +02:00
|
|
|
|
2014-08-28 03:39:41 +02:00
|
|
|
function srcPathToDestRequire(srcFilepath) {
|
|
|
|
var requirePath = path.relative(destDir, srcFilepath);
|
2014-08-28 03:54:51 +02:00
|
|
|
return 'require(\'' + requirePath + '\')';
|
2014-08-28 03:39:41 +02:00
|
|
|
}
|
2014-08-28 03:24:23 +02:00
|
|
|
|
2014-09-09 02:50:35 +02:00
|
|
|
var moduleOutputJs = COMMONJS_BANNER + srcFiles.map(srcPathToDestRequire).join('\n');
|
2014-08-28 03:24:23 +02:00
|
|
|
try {
|
|
|
|
fs.writeFileSync(destFilepath, moduleOutputJs);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
grunt.fail.warn(err);
|
|
|
|
}
|
|
|
|
grunt.log.writeln('File ' + destFilepath.cyan + ' created.');
|
|
|
|
};
|