UNPKG

1.17 kBJavaScriptView Raw
1var path = require('path');
2var fs = require('fs');
3
4var async = require('async');
5var sprintf = require('sprintf').sprintf;
6var templates = require('magic-templates');
7templates.setTemplatesDir(path.join(__dirname, '../assets/'));
8templates.setDebug(false);
9
10/**
11 * Generate and write a Makefile with Whiskey related targets.
12 * @param {Array} testFiles Test files.
13 * @param {String} targetPath path where a generated Makefile is saved.
14 * @param {Function} callback Callback called with (err).
15 */
16function generateMakefile(testFiles, targetPath, callback) {
17 var template = new templates.Template('Makefile.magic');
18 var fullPath = path.join(targetPath, 'Makefile');
19 var context = {
20 test_files: testFiles.join(' \\\n ')
21 };
22
23 if (path.existsSync(fullPath)) {
24 callback(new Error(sprintf('File "%s" already exists', fullPath)));
25 return;
26 }
27
28 async.waterfall([
29 template.load.bind(template),
30
31 function render(template, callback) {
32 template.render(context, callback);
33 },
34
35 function save(output, callback) {
36 fs.writeFile(fullPath, output.join(''), callback);
37 }
38 ], callback);
39}
40
41exports.generateMakefile = generateMakefile;