-
-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathname-check.mjs
More file actions
68 lines (56 loc) · 1.86 KB
/
name-check.mjs
File metadata and controls
68 lines (56 loc) · 1.86 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
#!/usr/bin/env node
/**
* Run this script (from root directory):
*
* $ corepack pnpm node scripts/name-check.mjs
*
* This will run following checks:
*
* 1. Package name is of the format "@exercism/javascript-<exercise>"
*
* This script also allows fixing these names:
*
* $ corepack pnpm node scripts/name-check.mjs --fix
*/
import shell from 'shelljs';
import path from 'node:path';
import { packageFiles, registerExitHandler } from './helpers.mjs';
registerExitHandler();
let exitCode = 0;
// First 2 arguments are node and script name skip them
// Check if rest has --fix
const fix = process.argv.slice(2).includes('--fix');
if (fix) {
shell.echo('==============================================');
shell.echo('Fixing package names where necessary');
shell.echo('----------------------------------------------');
}
const envAssignment = shell.env['ASSIGNMENT'];
const finalPackageFiles = envAssignment
? [path.join('exercises', envAssignment, 'package.json')]
: packageFiles;
// Check if package name in each exercises' package.json is of the format "@exercism/javascript-<exercise>"
finalPackageFiles.forEach((filePath) => {
const file = JSON.parse(shell.cat(filePath).toString());
const givenName = file['name'];
const exerciseName = filePath.split(/[/\\]/g)[2];
const expectedName = `@exercism/javascript-${exerciseName}`;
if (givenName === expectedName) {
shell.echo(`[Success]: Package name ${givenName} is in correct format`);
return;
}
if (fix) {
file['name'] = expectedName;
const fileWithFixedName = new shell.ShellString(
JSON.stringify(file, undefined, 2) + '\n',
);
fileWithFixedName.to(filePath);
shell.echo(`[Success]: Fixed package name in ${filePath}`);
} else {
exitCode = 1;
shell.echo(
`[Failure]: Package name in ${filePath} must be ${expectedName}"`,
);
}
});
shell.exit(exitCode);