forked from exercism/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr-check
executable file
·150 lines (121 loc) · 3.83 KB
/
pr-check
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
#!/usr/bin/env node
/**
* Run this script (from root directory):
*
* npx babel-node scripts/pr-check path/1 path/2 path/3
*
* This will run following checks:
* 1. Find the exercises at all the paths provided
* 2. Check stub existance for those exercises
* 3. Check integrity of configuration for those exercises
* 4. Run eslint for those exercises to check code-style
*/
const {
findExerciseDirectory,
cleanUp,
registerExitHandler,
envIsThruthy,
hasStub,
prepare,
assignments,
} = require('./helpers');
const shell = require('shelljs');
const path = require('path');
const files = process.argv.slice(2);
if (files.length === 0) {
shell.echo(
'[Failure] No files passed in. Pass in paths to exercise directories or its file.'
);
shell.exit(-1);
}
shell.echo('[Files] The files passed in are as follows:');
files.forEach((file) => {
shell.echo(`[Files] ${file}`);
});
const _exercises = files
.map(findExerciseDirectory)
.filter(Boolean)
.filter((exercise, index, array) => array.indexOf(exercise) === index);
const hasRootFile = files.some((file) => file === 'package.json');
if (hasRootFile) {
shell.echo(
'[Root PR] When package.json is changed, all exercises need to be checked'
);
} else if (_exercises.length > 8) {
shell.echo(
'[Big PR] When more than 8 exercises are being checked, all of them are ' +
'checked as this is likely a PR affecting everything.'
);
}
const exercises =
hasRootFile || _exercises.length > 8 ? assignments : _exercises;
if (exercises.length === 0) {
shell.echo('[Skip] None of the files are inside an exercise directory.');
shell.exit(0);
}
registerExitHandler();
if (!envIsThruthy('SKIP_STUB', false)) {
shell.echo('\n==========\nEnsure stubs are present\n');
// Inline the stub check instead of running scripts/stub-check, to save a
// few seconds.
const noStubs = exercises.filter((assignment) => !hasStub(assignment));
if (noStubs.length > 0) {
shell.echo(`[FAILURE] ${noStubs.length} missing a stub`);
noStubs.forEach((stub) => shell.echo(`${stub} is missing a stub file`));
shell.exit(-1);
} else {
shell.echo('[SUCCES] All stubs are present');
}
}
if (!envIsThruthy('SKIP_INTEGRITY', false)) {
shell.echo('\n==========\nCheck configuration and packages integrity\n');
exercises.forEach((exercise) => {
shell.env['ASSIGNMENT'] = exercise;
const checkResult = shell.exec(
`npx babel-node ${path.join('scripts', 'checksum')}`
).code;
if (checkResult !== 0) {
shell.echo(
`scripts/checksum returned a non-zero exit code: ${checkResult}`
);
shell.exit(checkResult);
}
const nameCheckResult = shell.exec(
`npx babel-node ${path.join('scripts', 'name-check')}`
).code;
if (nameCheckResult !== 0) {
shell.echo(
`scripts/name-check returned a non-zero exit code: ${nameCheckResult}`
);
shell.exit(nameCheckResult);
}
});
const nameUniqResult = shell.exec(
`npx babel-node ${path.join('scripts', 'name-uniq')}`
).code;
if (nameUniqResult !== 0) {
shell.echo(
`scripts/name-uniq returned a non-zero exit code: ${nameUniqResult}`
);
shell.exit(nameUniqResult);
}
}
// Cleanup tmp directory if any exists
cleanUp();
/**
* Moves all example and test files to single directory - tmp_exercises
* This allows running the test command together for all files
* which is way faster than running once for each exercise
*/
shell.echo('\n==========\nLint all the files\n');
shell.env['PREPARE'] = false;
shell.env['CLEANUP'] = false;
delete shell.env['ASSIGNMENT'];
exercises.forEach(prepare);
shell.env['CLEANUP'] = true;
const checkResult = shell.exec(`npx babel-node ${path.join('scripts', 'lint')}`)
.code;
if (checkResult !== 0) {
shell.echo(`scripts/lint returned a non-zero exit code: ${checkResult}`);
shell.exit(checkResult);
}