This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
cli-utils.js
65 lines (54 loc) · 1.9 KB
/
cli-utils.js
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
'use strict';
const path = require('path');
const semver = require('semver');
const uniq = require('lodash.uniq');
const flatten = require('lodash.flatten');
const isGitClean = require('is-git-clean');
exports.sortByVersion = function sortByVersion(a, b) {
if (a.version === b.version) {
return 0;
}
return semver.lt(a.version, b.version) ? -1 : 1;
};
exports.getVersions = function getVersions(codemods) {
const versionsFromCodemods = codemods.sort(exports.sortByVersion).map(codemod => codemod.version);
const uniqueVersions = uniq(versionsFromCodemods);
const firstVersion = {
name: `older than ${uniqueVersions.sort(exports.sortByVersion)[0]}`,
value: '0.0.0'
};
const lastVersion = {
name: 'latest',
value: '9999.9999.9999'
};
return [firstVersion].concat(versionsFromCodemods).concat(lastVersion);
};
exports.selectScripts = function selectScripts(codemods, currentVersion, nextVersion) {
const semverToRespect = `>${currentVersion} <=${nextVersion}`;
const scripts = codemods
.filter(codemod => semver.satisfies(codemod.version, semverToRespect))
.map(codemod => codemod.scripts);
return flatten(scripts).map(script => path.join(__dirname, script));
};
exports.checkGitStatus = function checkGitStatus(force) {
let clean = false;
let errorMessage = 'Unable to determine if git directory is clean';
try {
clean = isGitClean.sync(process.cwd(), {files: ['!package.json']});
errorMessage = 'Git directory is not clean';
} catch (err) {}
const ENSURE_BACKUP_MESSAGE = 'Ensure you have a backup of your tests or commit the latest changes before continuing.';
if (!clean) {
if (force) {
console.log(`WARNING: ${errorMessage}. Forcibly continuing.`, ENSURE_BACKUP_MESSAGE);
} else {
console.log(
`ERROR: ${errorMessage}. Refusing to continue.`,
ENSURE_BACKUP_MESSAGE,
'You may use the --force flag to override this safety check.'
);
return false;
}
}
return true;
};