-
-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathdirectory-check.mjs
More file actions
63 lines (52 loc) · 1.82 KB
/
directory-check.mjs
File metadata and controls
63 lines (52 loc) · 1.82 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
#!/usr/bin/env node
/**
* Run this script (from root directory):
*
* $ corepack pnpm node scripts/directory-check.mjs
*
* This will run following checks:
*
* 1. Package has the correct directory based on the path to the exercise.
*
* This script also allows fixing these names:
*
* $ corepack pnpm node scripts/directory-check.mjs --fix
*/
import shelljs from 'shelljs';
import { join } from 'node:path';
const { cat, echo, ShellString, exit } = shelljs;
import { assignments, registerExitHandler } from './helpers.mjs';
registerExitHandler();
// First 2 arguments are node and script name skip them
// Check if rest has --fix
const fix = process.argv.slice(2).includes('--fix');
// Check if package repository directory in each exercises' package.json is of the format "exercises/<exercise-path>"
assignments.forEach((assignment) => {
const filePath = join('exercises', assignment, 'package.json');
const file = JSON.parse(cat(filePath).toString());
const givenRepositoryDirectory = file['repository']['directory'];
const expectedRepositoryDirectory = `exercises/${assignment.replace(
'\\',
'/',
)}`;
if (givenRepositoryDirectory === expectedRepositoryDirectory) {
echo(
`[Success]: Package repository directory for ${assignment} is in correct format.`,
);
return;
}
if (fix) {
file['repository']['directory'] = expectedRepositoryDirectory;
const fileWithFixedRepositoryDirectory = new ShellString(
JSON.stringify(file, undefined, 2) + '\n',
);
fileWithFixedRepositoryDirectory.to(filePath);
echo(`[Success]: Fixed package repository directory in ${filePath}`);
} else {
echo(
`[Failure]: Package repository directory in ${filePath} must be ${expectedRepositoryDirectory}, actual: ${file['repository']['directory']}"`,
);
exit(1);
}
});
exit(0);