UNPKG

3.2 kBJavaScriptView Raw
1/*
2 * grunt-appc-js
3 * https://github.com/ingo/grunt-appc-js
4 *
5 * Copyright (c) 2015 Ingo Muschenetz
6 * Licensed under the MIT license.
7 */
8
9'use strict';
10
11var extendGruntPlugin = require('extend-grunt-plugin');
12var packpath = require('packpath');
13var path = require('path');
14var _ = require('lodash');
15
16module.exports = function (grunt) {
17
18 grunt.registerMultiTask('appcJs', 'Linting and style checks for Appcelerator JavaScript', function (lintOnly) {
19 var that = this;
20 var source = this.data;
21 if (this.data.src) {
22 source = this.data.src;
23 }
24
25 initializeJscsPlugin();
26 initializeJshintPlugin();
27 if(lintOnly) {
28 grunt.task.run('jshint:src', 'jscs:src');
29 } else {
30 initializeContinuePlugin();
31 initializeRetirePlugin();
32 grunt.task.run('jshint:src', 'jscs:src', 'continue:on', 'retire', 'continue:off');
33
34 }
35
36
37
38 /**
39 * Initializes the jscs plugin
40 *
41 * @return {void}
42 */
43 function initializeJscsPlugin() {
44 // there is likely a better way to specify the path to the files
45 var optionsJscs = {
46 src: source,
47 options: _.omit(that.options({
48 config: path.join(__dirname, '..', '.jscsrc'),
49 reporter: require('jscs-stylish').path,
50 }), 'globals')
51 };
52
53 var jscs = require('grunt-jscs/tasks/jscs');
54 extendGruntPlugin(grunt, jscs, {
55 'jscs.src' : optionsJscs
56 });
57 }
58
59 /**
60 * Initializes the jshint plugin
61 *
62 * @return {void}
63 */
64 function initializeJshintPlugin() {
65 var jsHintConfig = {
66 browser: true,
67 curly: true,
68 eqeqeq: true,
69 eqnull: true,
70 expr: true,
71 immed: true,
72 indent: 4,
73 latedef: false,
74 newcap: true,
75 noarg: true,
76 nonew: true,
77 undef: true,
78 unused: false,
79 trailing: true,
80 loopfunc: true,
81 proto: true,
82 node: true,
83 '-W104': true,
84 '-W068': true,
85 globals: {
86 after : false,
87 afterEach : false,
88 before : false,
89 beforeEach : false,
90 describe : false,
91 it : false,
92 Titanium : false,
93 Ti : false
94 }
95 };
96
97 var optionsJsHint = {
98 src: source,
99 options: _.omit(_.merge(that.options(),
100 {reporter: require('jshint-stylish')},
101 jsHintConfig), 'fix')
102 };
103
104 var jshint = require('grunt-contrib-jshint/tasks/jshint');
105 extendGruntPlugin(grunt, jshint, {
106 'jshint.src' : optionsJsHint
107 });
108 }
109
110 /**
111 * Initializes the retire.js plugin
112 *
113 * The way the retire plugin works is not compatible with extendGruntPlugin
114 * so we simply add the config manually.
115 *
116 * @return {void}
117 */
118 function initializeRetirePlugin() {
119 var optionsRetire = {
120 js: source,
121 node: ['.'],
122 options: {
123 packageOnly: false
124 }
125 };
126
127 var retire = require('grunt-retire/tasks/retire');
128 retire(grunt);
129 grunt.config.set('retire', optionsRetire);
130 }
131
132 /**
133 * Initializes the continue plugin
134 *
135 * This plugin allows us to temporarily set the --force flag so retire.js
136 * wont't fail our build. Should be removed after a certain transition
137 * period.
138 *
139 * @return {void}
140 */
141 function initializeContinuePlugin() {
142 var gruntContinue = require('grunt-continue/tasks/continue');
143 gruntContinue(grunt);
144 }
145 });
146
147};