This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
aquifer-coder.js
174 lines (154 loc) · 5.87 KB
/
aquifer-coder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* @file
* Coding standards sniffers and linters for Aquifer.
*/
/**
* Extension that is exported to Aquifer.
* @param {object} Aquifer instance of Aquifer containing an active project.
* @param {object} AquiferCoderConfig config object passed in from Aquifer project's json file.
* @returns {function} object that consumes Aquifer's extension API.
*/
module.exports = function(Aquifer, AquiferCoderConfig) {
'use strict';
var AquiferCoder = function () {},
colors = require('colors'),
_ = require('lodash'),
path = require('path'),
fs = require('fs-extra'),
syncExec = require('sync-exec');
// Turn any passed-in paths into full paths.
if (AquiferCoderConfig.hasOwnProperty('eslintrc')) {
AquiferCoderConfig.eslintrc = path.join(Aquifer.projectDir, AquiferCoderConfig.eslintrc);
}
if (AquiferCoderConfig.hasOwnProperty('eslintIgnore')) {
AquiferCoderConfig.eslintIgnore = path.join(Aquifer.projectDir, AquiferCoderConfig.eslintIgnore);
}
if (AquiferCoderConfig.hasOwnProperty('phpcsStandard')) {
AquiferCoderConfig.phpcsStandard = path.join(Aquifer.projectDir, AquiferCoderConfig.phpcsStandard);
}
// Create defauts for options.
_.defaults(AquiferCoderConfig, {
eslintrc: path.join(__dirname, 'src', '.eslintrc'),
eslintIgnore: path.join(__dirname, 'src', '.eslintignore'),
phpcsStandard: path.join(__dirname, '/vendor/drupalmodule/coder/coder_sniffer/Drupal')
});
/**
* Creates a commands that are exported to Aquifer command.
* @returns {object} commands to be added to Aquifer.
*/
AquiferCoder.commands = function () {
return {
'jslint': {
description: 'Lints JavaScript code in the project for errors.'
},
'phplint': {
description: 'Lints PHP code in the project for errors.'
},
'lint': {
description: 'Lints PHP and JavaScript code in the project for errors.'
}
};
};
/**
* Runs sniffers and linters depending on the command being run.
* @param {string} command string representing the name of the command that is being run.
* @param {object} options options passed from the command.
* @param {function} callback function that is called when there is an error message to send.
* @returns {undefined} nothing.
*/
AquiferCoder.run = function (command, options, callback) {
if (command === 'jslint' || command === 'lint') {
Aquifer.console.log('Running linters on JavaScript code files...', 'notice');
AquiferCoder.eslint();
}
if (command === 'phplint' || command === 'lint') {
Aquifer.console.log('Running linters on PHP code files...', 'notice');
AquiferCoder.phplint();
Aquifer.console.log('Running coding standards sniffers on PHP code files...', 'notice');
AquiferCoder.phpcs();
}
}
/**
* Run eslint on custom files in project.
* @returns {undefined} nothing.
*/
AquiferCoder.eslint = function () {
var CLIEngine = require('eslint').CLIEngine;
// Create instance of eslint cli engine.
var eslint = new CLIEngine({
envs: ['browser'],
useEslintrc: true,
configFile: AquiferCoderConfig.eslintrc,
ignore: true,
ignorePath: AquiferCoderConfig.eslintIgnore
});
// Execute eslint on js files.
var report = eslint.executeOnFiles([
Aquifer.project.absolutePaths.modules.custom,
Aquifer.project.absolutePaths.modules.features,
Aquifer.project.absolutePaths.themes.custom,
Aquifer.project.directory + '/*.js'
]);
// Loop through report results, and log accordingly.
_.forEach(report.results, function (item) {
// If no errors are found.
if (item.messages.length <= 0) {
console.log(item.filePath.bgGreen + '\n');
return;
}
// If errors are found, log filename, and construct error string.
console.log(item.filePath.bgRed);
var log = '';
_.forEach(item.messages, function(message) {
log = '';
if (message.fatal) {
log += 'Fatal ';
}
log += 'Error on line ' + new String(message.line).bgYellow + ' column ' + new String(message.column).bgYellow + ': ' + new String(message.message).red;
console.log(log);
});
// newline.
console.log();
});
}
/**
* Lints PHP code in codebase.
* @returns {undefined} nothing.
*/
AquiferCoder.phplint = function () {
// Load phplint, and set up extensions.
var phplint = require('phplint').lint,
extensions = '{php,module,inc,install,test,profile,theme}';
// Run phplint on custom modules and themes.
phplint([
Aquifer.project.absolutePaths.modules.custom + '/**/*.' + extensions,
Aquifer.project.absolutePaths.modules.features + '/**/*.' + extensions,
Aquifer.project.absolutePaths.themes.custom + '/**/*.' + extensions
], function (err) {
if (err) {
console.log(err.message);
}
});
}
/**
* Runs phpcs on PHP code in codebase.
* @returns {undefined} nothing.
*/
AquiferCoder.phpcs = function () {
var command = [
path.join(__dirname, '/vendor/bin/phpcs'),
'--standard="' + AquiferCoderConfig.phpcsStandard + '"',
'--extensions="php,module,inc,install,test,profile,theme"',
'--ignore="*.apachesolr_environments.inc,*.apachesolr_search_defaults.inc,*.context.inc,*.features.*.inc,*.features.inc,*.field_group.inc,*.pages_default.inc,*.strongarm.inc,*.views_default.inc"',
Aquifer.project.absolutePaths.modules.custom,
Aquifer.project.absolutePaths.modules.features,
Aquifer.project.absolutePaths.themes.custom
].join(' ');
var report = syncExec(command);
if (report.stdout.length > 0) {
// Log report, and remove silly Code Sniffer 2.0 ad.
console.log(report.stdout.split('UPGRADE TO PHP_CODESNIFFER 2.0 TO FIX ERRORS AUTOMATICALLY')[0]);
}
}
return AquiferCoder;
};