forked from step/c-action
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathranger.js
More file actions
61 lines (53 loc) · 1.57 KB
/
ranger.js
File metadata and controls
61 lines (53 loc) · 1.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
const fs = require('fs');
const SOURCE_PATH = './__source/src';
const getDescription = (assignment) => {
if (assignment.description) {
return assignment.description;
}
return assignment.functionName;
}
const toFunction = (t) => {
const content =
`// ${getDescription(t)}
const ${t.functionName} = function(${t.parameters}) {
return ${t.returnValue};
};
exports.${t.functionName} = ${t.functionName};
`;
return { content, fileName: t.fileName };
}
const shuffle = function (list) {
let index = list.length;
let randomIndex = 0;
while (index !== 0) {
randomIndex = Math.floor(Math.random() * list.length);
index--;
[list[index], list[randomIndex]] = [list[randomIndex], list[index]];
}
return list;
};
const getNextAssignment = (assignments, srcFiles) => {
const existingAssignments = srcFiles.filter(f => f.includes('.js'));
const nextAssignment = assignments.find(assignment => {
return !existingAssignments.includes(assignment.fileName);
});
return nextAssignment;
}
const writeFile = function (f) {
const fileName = `${SOURCE_PATH}/${f.fileName}`;
if (!fs.existsSync(fileName)) {
fs.writeFileSync(fileName, f.content);
console.log(`Created ${f.fileName}`);
}
};
const assign = function (assignments) {
const srcFiles = fs.readdirSync(`${SOURCE_PATH}/`);
const assignment = getNextAssignment(assignments, srcFiles);
if (!assignment) {
return;
}
writeFile(toFunction(assignment));
};
const args = process.argv.slice(2);
const json = args.length ? require(args[0]) : { assignments: [] };
assign(shuffle(json.assignments));