UNPKG

2.89 kBJavaScriptView Raw
1/*
2 * grunt
3 * https://github.com/cowboy/grunt
4 *
5 * Copyright (c) 2012 "Cowboy" Ben Alman
6 * Licensed under the MIT license.
7 * http://benalman.com/about/license/
8 */
9
10// Basic template description.
11exports.description = 'Create a basic grunt.js gruntfile.';
12
13// Template-specific notes to be displayed before question prompts.
14exports.notes = 'This template tries to guess file and directory paths, but ' +
15 'you will most likely need to edit the generated grunt.js file before ' +
16 'running grunt. _If you run grunt after generating grunt.js, and grunt ' +
17 'exits with errors, edit the grunt.js file!_';
18
19// Any existing file or directory matching this wildcard will cause a warning.
20exports.warnOn = 'grunt.js';
21
22// The actual init template.
23exports.template = function(grunt, init, done) {
24 // Grunt utilities.
25 var task = grunt.task;
26 var file = grunt.file;
27 var utils = grunt.utils;
28 var log = grunt.log;
29 var verbose = grunt.verbose;
30 var fail = grunt.fail;
31 var option = grunt.option;
32 var config = grunt.config;
33 var template = grunt.template;
34
35 grunt.helper('prompt', {}, [
36 // Prompt for these values.
37 {
38 name: 'dom',
39 message: 'Is the DOM involved in ANY way?',
40 default: 'Y/n',
41 warning: 'Yes: QUnit unit tests + JSHint "browser" globals. No: Nodeunit unit tests.'
42 },
43 {
44 name: 'min_concat',
45 message: 'Will files be concatenated or minified?',
46 default: 'Y/n',
47 warning: 'Yes: min + concat tasks. No: nothing to see here.'
48 },
49 {
50 name: 'package_json',
51 message: 'Will you have a package.json file?',
52 default: 'Y/n',
53 warning: 'This changes how filenames are determined and banners are generated.'
54 }
55 ], function(err, props) {
56 props.dom = /y/i.test(props.dom);
57 props.min_concat = /y/i.test(props.min_concat);
58 props.package_json = /y/i.test(props.package_json);
59 props.test_task = props.dom ? 'qunit' : 'test';
60 props.file_name = props.package_json ? '<%= pkg.name %>' : 'FILE_NAME';
61
62 // Find the first `preferred` item existing in `arr`.
63 function prefer(arr, preferred) {
64 for (var i = 0; i < preferred.length; i++) {
65 if (arr.indexOf(preferred[i]) !== -1) {
66 return preferred[i];
67 }
68 }
69 return preferred[0];
70 }
71
72 // Guess at some directories, if they exist.
73 var dirs = file.expandDirs('*').map(function(d) { return d.slice(0, -1); });
74 props.lib_dir = prefer(dirs, ['lib', 'src']);
75 props.test_dir = prefer(dirs, ['test', 'tests', 'unit', 'spec']);
76
77 // Maybe this should be extended to support more libraries. Patches welcome!
78 props.jquery = file.expandFiles('**/jquery*.js').length > 0;
79
80 // Files to copy (and process).
81 var files = init.filesToCopy(props);
82
83 // Actually copy (and process) files.
84 init.copyAndProcess(files, props);
85
86 // All done!
87 done();
88 });
89
90};