1 | #!/usr/bin/env node
|
2 | 'strict';
|
3 | const fs = require('fs');
|
4 | const {exec} = require('child_process');
|
5 |
|
6 | function precheck(location) {
|
7 | console.log('Checking: ', location);
|
8 | let directory = '';
|
9 | const fileExists = fs.existsSync(location);
|
10 | if (! fileExists) {
|
11 | console.log('File/Folder does not exist, using current directory');
|
12 | directory = '.';
|
13 | } else {
|
14 | directory = location;
|
15 | }
|
16 | return directory;
|
17 | }
|
18 |
|
19 |
|
20 | function getRC(file) {
|
21 | return fs.existsSync(file) ? file : `node_modules/code-assess/${file}`;
|
22 | }
|
23 |
|
24 | function loadConfig(file, config) {
|
25 | let rc;
|
26 | if (config) {
|
27 | if (fs.existsSync(config)) {
|
28 | rc = config;
|
29 | } else {
|
30 | console.log('Config file not found, using defualt');
|
31 | rc = getRC(file);
|
32 | }
|
33 | } else {
|
34 | rc = getRC(file);
|
35 | }
|
36 | return rc;
|
37 | }
|
38 |
|
39 | function eslint(location, config) {
|
40 | console.log('Running ESLint');
|
41 | const rc = loadConfig('.eslintrc.json', config);
|
42 | return new Promise((resolve, reject) => {
|
43 | exec(`./node_modules/.bin/eslint -c ${rc} ${location}`, (err, stdout, stderr) => {
|
44 | if (err) {
|
45 | console.log('Errors from eslint:');
|
46 | console.log(stdout);
|
47 | }
|
48 | resolve(err);
|
49 | });
|
50 | });
|
51 | }
|
52 |
|
53 | function htmlhint(location, config) {
|
54 | console.log('Running HTMLHint');
|
55 | const rc = loadConfig('.htmlhintrc.json', config);
|
56 | return new Promise((resolve, reject) => {
|
57 | exec(`./node_modules/.bin/htmlhint --config ${rc} ${location}`, (err, stdout, stderr) => {
|
58 | if (err) {
|
59 | console.log('Errors from htmlhint');
|
60 | console.log(stdout);
|
61 | }
|
62 | resolve(err);
|
63 | });
|
64 | });
|
65 | }
|
66 |
|
67 | function scsslint(location, config) {
|
68 | console.log('Running SCSSLint');
|
69 | const rc = loadConfig('.sass-lint.yml', config);
|
70 | return new Promise((resolve, reject) => {
|
71 | exec(`./node_modules/.bin/sass-lint '${location}/**/*.scss' -v -q --config ${rc}`, (err, stdout, stderr) => {
|
72 | if (err) {
|
73 | console.log('Errors from scsslint');
|
74 | console.log(stdout);
|
75 | }
|
76 | resolve(err);
|
77 | });
|
78 | });
|
79 | }
|
80 |
|
81 | function sonarwhal(location, config) {
|
82 | console.log('Running Sonarwhal');
|
83 |
|
84 | return new Promise((resolve, reject) => {
|
85 | exec(`./node_modules/.bin/sonarwhal ${location}`, (err, stdout, stderr) => {
|
86 | if (err) {
|
87 | console.log('Errors from Sonarwhal');
|
88 | console.log(err);
|
89 | console.log(stderr);
|
90 | console.log(stdout);
|
91 | }
|
92 | resolve(err);
|
93 | });
|
94 | });
|
95 | }
|
96 |
|
97 | async function main(location) {
|
98 | precheck(location);
|
99 | let options;
|
100 | try {
|
101 | options = JSON.parse(fs.readFileSync(getRC('.code-assessrc.json')));
|
102 | } catch (err) {
|
103 | console.log('Error parsing code-assessrc.json file.');
|
104 | throw err;
|
105 | }
|
106 | if (options.eslint !== undefined && options.eslint.run) {
|
107 | await eslint(location);
|
108 | }
|
109 | if (options.htmlhint !== undefined && options.htmlhint.run) {
|
110 | await htmlhint(location);
|
111 | }
|
112 | if (options.scsslint !== undefined && options.scsslint.run) {
|
113 | await scsslint(location);
|
114 | }
|
115 | if (options.sonarwhal !== undefined && options.sonarwhal.run) {
|
116 | await sonarwhal(options.sonarwhal.uri);
|
117 | }
|
118 | }
|
119 |
|
120 |
|
121 | export default main;
|
122 | export {eslint, htmlhint, scsslint, sonarwhal};
|
123 |
|
124 |
|
125 | if (require.main === module) {
|
126 | (async () => {
|
127 | console.log('Running tests.');
|
128 | const arg = process.argv[2];
|
129 | await main(arg);
|
130 | })();
|
131 | }
|