Repository navigation
Expand file tree
/
Copy pathhelpers.js
More file actions
93 lines (73 loc) · 2.57 KB
/
Copy pathhelpers.js
File metadata and controls
93 lines (73 loc) · 2.57 KB
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
/**
* This file provides helper functions
* & is NOT intended to be run as a script.
*/
const shell = require('shelljs');
const exerciseDirs = shell.ls('-d', 'exercises/*');
const assignments = exerciseDirs.map(dir => dir.split('/')[1])
.filter(exercise => !exercise.deprecated);
// Preapre all exercises (see above) & run a given command
function prepareAndRun(command, infoStr, failureStr) {
if(shell.env['PREPARE']) {
const assignment = shell.env['ASSIGNMENT'];
if(assignment) {
prepare(assignment);
}
else {
assignments.forEach(prepare);
}
}
if(infoStr) { shell.echo(infoStr); }
const result = shell.exec(command);
if(shell.env['CLEANUP']) { cleanUp(); }
if(result.code !== 0) {
if(failureStr) { shell.echo(failureStr); }
shell.exit(1);
}
}
// Delete tmp directory
function cleanUp() {
shell.rm('-rf', 'tmp_exercises');
}
// These packages will be skipped while performing checksum
const SKIP_PACKAGES_FOR_CHECKSUM = ['shelljs', '@babel/node'];
// Filter out some unwanted packages and create package.json for exercises
function createExercisePackageJson(assignmentVersion) {
const packageFile = shell.cat('package.json').toString();
const packageJson = JSON.parse(packageFile);
packageJson['version'] = assignmentVersion;
SKIP_PACKAGES_FOR_CHECKSUM.forEach(pkg => delete packageJson['devDependencies'][pkg]);
const shellStr = new shell.ShellString(JSON.stringify(packageJson, null, 2) + '\n');
shellStr.to('exercise-package.json');
}
// PRIVATE
/**
* Copy sample solution and specs for given assignment to tmp_exercises
*/
function prepare(assignment) {
if(!assignment) {
shell.echo('assignment not provided');
shell.exit(1);
}
const exampleFile = ['exercises', assignment, 'example.js'].join('/');
const specFile = ['exercises', assignment, assignment + '.spec.js'].join('/');
shell.mkdir('-p', 'tmp_exercises/lib');
shell.cp(exampleFile, ['tmp_exercises', assignment + '.js'].join('/'));
shell.sed('xtest', 'test', specFile)
.to(['tmp_exercises', assignment + '.spec.js'].join('/'));
const libDir = ['exercises', assignment, 'lib'].join('/');
if(shell.test('-d', libDir)) {
shell.cp(libDir + '/*.js', 'tmp_exercises/lib');
}
shell.mkdir('-p', 'tmp_exercises/data');
const dataDir = ['exercises', assignment, 'data'].join('/');
if(shell.test('-d', dataDir)) {
shell.cp([dataDir, '*'].join('/'), 'tmp_exercises/data');
}
}
module.exports = {
assignments: assignments,
cleanUp: cleanUp,
prepareAndRun: prepareAndRun,
createExercisePackageJson: createExercisePackageJson
};