-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
sync
executable file
·84 lines (71 loc) · 2.26 KB
/
sync
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
#!/usr/bin/env node
/**
* Run this script (from root directory): npx babel-node scripts/sync
*
* This script is used to copy the following files to all exercises and keep
* them in sync:
*
* - .eslintrc
* - babel.config.js
* - package.json (modified version)
* - .npmrc
*
* There is a CI step which checks that package.json in root & exercises match
* (see checksum script for more info).
*/
const shell = require('shelljs');
const assignment = shell.env['ASSIGNMENT'];
const helpers = require('./helpers');
const path = require('path');
function copyConfigForAssignment(assignment) {
const destination = path.join('exercises', assignment);
const assignmentPackageFilename = path.join(destination, 'package.json');
const packageJson = getCurrentPackageJson(assignmentPackageFilename);
const basePackageJson = JSON.parse(
shell.cat('exercise-package.json').toString()
);
const mergedPackageJson = helpers.mergePackageJsons(
basePackageJson,
packageJson,
assignment
);
shell
.ShellString(JSON.stringify(mergedPackageJson, undefined, 2) + '\n')
.to(assignmentPackageFilename);
['.eslintrc', '.npmrc', 'babel.config.js', 'LICENSE', '.gitignore'].forEach(
(file) => {
shell.cp(file, destination);
}
);
}
function getCurrentPackageJson(assignmentPackageFilename) {
const packageFile = shell.cat(assignmentPackageFilename).toString();
if (!packageFile) {
const packageJson = JSON.parse(
shell.cat('exercise-package.json').toString()
);
const conceptName = path
.dirname(assignmentPackageFilename)
.split(/[/\\]/g)
.slice(1);
packageJson.name = `@exercism/javascript-${conceptName.join('-')}`;
packageJson.description = `Exercism ${conceptName[0]} exercise on ${conceptName[1]}`;
return packageJson;
}
return JSON.parse(packageFile);
}
helpers.registerExitHandler();
helpers.createExercisePackageJson(false);
if (assignment) {
if (!helpers.assertAssignment(assignment)) {
shell.exit(1);
}
shell.echo('Syncing ' + assignment + '...');
copyConfigForAssignment(assignment);
} else {
shell.echo('Syncing all assignments...');
helpers.assignments.forEach((assignment) => {
shell.echo('Syncing ' + assignment + '...');
copyConfigForAssignment(assignment);
});
}