|
| 1 | +require! <[liftoff gulp fs]> |
| 2 | + |
| 3 | +handleArguments = (args) -> |
| 4 | + argv = args.argv |
| 5 | + cliPackage = require './package' |
| 6 | + versionFlag = argv.v or argv.version |
| 7 | + tasksFlag = argv.T or argv.tasks |
| 8 | + tasks = argv._ |
| 9 | + toRun = if tasks.length then tasks else ['default'] |
| 10 | + if versionFlag |
| 11 | + gutil.log 'CLI version', cliPackage.version |
| 12 | + gutil.log 'Local version', args.modulePackage.version if args.localPackage |
| 13 | + process.exit 0 |
| 14 | + if not args.modulePath |
| 15 | + gutil.log (gutil.colors.red 'No local gulp install found in'), gutil.colors.magenta args.cwd |
| 16 | + gutil.log gutil.colors.red 'Try running: npm install gulp' |
| 17 | + process.exit 1 |
| 18 | + if not args.configPath |
| 19 | + gutil.log gutil.colors.red 'No gulpfile found' |
| 20 | + process.exit 1 |
| 21 | + if semver.gt cliPackage.version, args.modulePackage.version |
| 22 | + gutil.log gutil.colors.red 'Warning: gulp version mismatch:' |
| 23 | + gutil.log gutil.colors.red 'Running gulp is', cliPackage.version |
| 24 | + gutil.log gutil.colors.red 'Local gulp (installed in gulpfile dir) is', args.modulePackage.version |
| 25 | + gulpFile = require args.configPath |
| 26 | + gutil.log 'Using gulpfile', gutil.colors.magenta args.configPath |
| 27 | + gulpInst = require args.modulePath |
| 28 | + logEvents gulpInst |
| 29 | + if process.cwd! isnt args.cwd |
| 30 | + process.chdir args.cwd |
| 31 | + gutil.log 'Working directory changed to', gutil.colors.magenta args.cwd |
| 32 | + process.nextTick (-> |
| 33 | + return logTasks gulpFile, gulpInst if tasksFlag |
| 34 | + gulpInst.start.apply gulpInst, toRun) |
| 35 | + |
| 36 | +logTasks = (gulpFile, localGulp) -> |
| 37 | + tree = taskTree localGulp.tasks |
| 38 | + tree.label = 'Tasks for ' + gutil.colors.magenta gulpFile |
| 39 | + |
| 40 | +formatError = (e) -> |
| 41 | + return e.message if not e.err |
| 42 | + if e.err.message then return e.err.message |
| 43 | + JSON.stringify e.err |
| 44 | + |
| 45 | +logEvents = (gulpInst) -> |
| 46 | + gulpInst.on 'task_start', (e) -> |
| 47 | + process.send {running: e.task} |
| 48 | + gutil.log 'Running', '\'' + gutil.colors.cyan e.task + '\'...' |
| 49 | + gulpInst.on 'task_stop', (e) -> |
| 50 | + time = prettyTime e.hrDuration |
| 51 | + gutil.log 'Finished', '\'' + gutil.colors.cyan e.task + '\'', 'in', gutil.colors.magenta time |
| 52 | + gulpInst.on 'task_err', (e) -> |
| 53 | + msg = formatError e |
| 54 | + time = prettyTime e.hrDuration |
| 55 | + gutil.log 'Errored', '\'' + gutil.colors.cyan e.task + '\'', 'in', (gutil.colors.magenta time), gutil.colors.red msg |
| 56 | + gulpInst.on 'task_not_found', (err) -> |
| 57 | + gutil.log gutil.colors.red 'Task \'' + err.task + '\' was not defined in your gulpfile but you tried to run it.' |
| 58 | + gutil.log 'Please check the documentation for proper gulpfile formatting.' |
| 59 | + process.exit 1 |
| 60 | + |
| 61 | +gutil = require 'gulp-util' |
| 62 | +prettyTime = require 'pretty-hrtime' |
| 63 | +semver = require 'semver' |
| 64 | +cli = new liftoff name: 'gulp' |
| 65 | + |
| 66 | +cli.on 'require', (name) -> gutil.log 'Requiring external module', gutil.colors.magenta name |
| 67 | +cli.on 'requireFail', (name) -> gutil.log (gutil.colors.red 'Failed to load external module'), gutil.colors.magenta name |
| 68 | + |
| 69 | +try require \LiveScript |
| 70 | +process.on \message ({dir, target}) -> |
| 71 | + cli.launch (-> handleArguments @), {cwd: dir, _: [target]} |
0 commit comments