|
| 1 | +var System = require('systemjs'); |
| 2 | +var fs = require('fs'); |
| 3 | +var dedent = require('dedent'); |
| 4 | +var inquirer = require('inquirer'); |
| 5 | +var gulp = require('gulp'); |
| 6 | +var git = require('gulp-git'); |
| 7 | +var process = require('process'); |
| 8 | +var minimist = require('minimist'); |
| 9 | + |
| 10 | +exports.init = function(rawGitArgs, environment, config) { |
| 11 | + if(typeof environment === 'undefined') { |
| 12 | + environment = {}; |
| 13 | + } |
| 14 | + |
| 15 | + if(typeof config !== 'undefined') { |
| 16 | + withConfig(rawGitArgs, environment, config); |
| 17 | + } else { |
| 18 | + withoutConfig(rawGitArgs, environment); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function withConfig(rawGitArgs, environment, config) { |
| 23 | + |
| 24 | + System.baseURL = config.path; |
| 25 | + |
| 26 | + // TODO Store the stripped out m's for use later |
| 27 | + strippedGitArgs = stripGitArgsMessages(rawGitArgs); |
| 28 | + |
| 29 | + // Load the config |
| 30 | + fs.open(config.path, 'r', function() { |
| 31 | + |
| 32 | + // Load the module based on the config |
| 33 | + System.import('index').then(function(m) { |
| 34 | + |
| 35 | + // Call the prompter method on the module, get the template |
| 36 | + m.prompter(inquirer, function(template) { |
| 37 | + |
| 38 | + // TODO, apply the stored m's to the template |
| 39 | + |
| 40 | + // Get a gulp stream based off the config |
| 41 | + gulp.src(config.path) |
| 42 | + |
| 43 | + // Format then commit |
| 44 | + .pipe(git.commit(dedent(template), {args: strippedGitArgs, disableAppendPaths: true})) |
| 45 | + |
| 46 | + // Handle commit success |
| 47 | + .on('end', function() { |
| 48 | + console.log('✓ Commit succeeded.'); |
| 49 | + }) |
| 50 | + |
| 51 | + // Handle commit failure |
| 52 | + .on('error', function (error) { |
| 53 | + console.error('✗ Commit failed. Did you forget to \'git add\' your files?'); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +// We don't have a config, so either we use raw args to try to commit |
| 61 | +// or if debug is enabled then we do a strict check for a config file. |
| 62 | +function withoutConfig(rawGitArgs, environment) { |
| 63 | + if(environment.debug === true) { |
| 64 | + console.error('COMMITIZEN DEBUG: No git-cz friendly config was detected. I looked for .czrc, .cz.json, or czConfig in package.json.'); |
| 65 | + } else { |
| 66 | + // Get a gulp stream based off the config |
| 67 | + gulp.src(process.cwd()) |
| 68 | + |
| 69 | + // Format then commit |
| 70 | + .pipe(git.commit(undefined, {args: rawGitArgs, disableMessageRequirement: true})) |
| 71 | + |
| 72 | + // Handle commit success |
| 73 | + .on('end', function() { |
| 74 | + console.log('✓ Commit succeeded.'); |
| 75 | + }) |
| 76 | + |
| 77 | + // Handle commit failure |
| 78 | + .on('error', function (error) { |
| 79 | + console.error('✗ Commit failed. Did you forget to \'git add\' your files or add a commit message?'); |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Aww shit it is ugly |
| 85 | +function stripGitArgsMessages(rawGitArgs) { |
| 86 | + |
| 87 | + var args = minimist(rawGitArgs, { |
| 88 | + alias: { |
| 89 | + m: 'message' |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + // Loop through all keys |
| 94 | + var output = ' '; |
| 95 | + |
| 96 | + for (arg in args) { |
| 97 | + |
| 98 | + if (!args.hasOwnProperty(arg)) { |
| 99 | + //The current property is not a direct property |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + var key = arg; |
| 104 | + var value = args[arg]; |
| 105 | + |
| 106 | + /** |
| 107 | + * Ugly, but this is recompiles an argument string without |
| 108 | + * any messages passed in. |
| 109 | + */ |
| 110 | + if (key === '_' && value.length > 0) { |
| 111 | + // Anything in the _ array of strings is a one off file |
| 112 | + output += value.join(' ') + ' '; |
| 113 | + } else if (key === 'message') { |
| 114 | + /** |
| 115 | + * We strip out message because we're already handling this |
| 116 | + * in minimist's aliases. |
| 117 | + */ |
| 118 | + continue; |
| 119 | + } else if (typeof value === String) { |
| 120 | + output += '-' + key + ' ' + value + ' '; |
| 121 | + } else if (typeof value === Array) { |
| 122 | + output += '-' + key + ' ' + value.join(' -' + key) + ' '; |
| 123 | + } else if (value === true || value === false) { |
| 124 | + output += '-' + key + ' '; |
| 125 | + } else { |
| 126 | + /** |
| 127 | + * Based on the current minimist object structure, we should |
| 128 | + * never get here, but we'll protect against breaking changes. |
| 129 | + */ |
| 130 | + continue; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if(output.trim().length < 1) { |
| 135 | + return ''; |
| 136 | + } else { |
| 137 | + return output; |
| 138 | + } |
| 139 | +} |
0 commit comments