forked from commitizen/cz-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
122 lines (105 loc) · 3.51 KB
/
init.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import path from 'path';
import * as configLoader from './configLoader';
import { executeShellCommand } from '../common/util';
import * as adapter from './adapter';
let {
addPathToAdapterConfig,
generateNpmInstallAdapterCommand,
getNpmInstallStringMappings,
generateYarnAddAdapterCommand,
getYarnAddStringMappings,
} = adapter;
export default init;
const CLI_PATH = path.normalize(path.join(__dirname, '../../'));
/**
* CZ INIT
*
* Init is generally responsible for initializing an adapter in
* a user's project. The goal is to be able to run
* `commitizen init` and be prompted for certain fields which
* will help you install the proper adapter for your project.
*
* Init does not actually create the adapter (it defers to adapter
* for this). Instead, it is specifically designed to help gather
* and validate the information needed to install the adapter
* properly without interfering with a previous adapter config.
*/
/**
* The defaults for init
*/
const defaultInitOptions = {
save: false,
saveDev: true,
saveExact: false,
force: false,
// for --yarn use
// @see https://github.com/commitizen/cz-cli/issues/527#issuecomment-392653897
yarn: false,
dev: true,
exact: false, // should add trailing comma, thus next developer doesn't got blamed for this line
};
/**
* Runs npm install for the adapter then modifies the config.commitizen as needed
*/
function init (sh, repoPath, adapterNpmName, {
save = false,
saveDev = true,
saveExact = false,
force = false,
yarn = false,
dev = false,
exact = false,
} = defaultInitOptions) {
// Don't let things move forward if required args are missing
checkRequiredArguments(sh, repoPath, adapterNpmName);
// Move to the correct directory so we can run commands
sh.cd(repoPath);
// Load the current adapter config
let adapterConfig = loadAdapterConfig();
// Get the npm string mappings based on the arguments provided
let stringMappings = yarn ? getYarnAddStringMappings(dev, exact, force) : getNpmInstallStringMappings(save, saveDev, saveExact, force);
// Generate a string that represents the npm install command
let installAdapterCommand = yarn ? generateYarnAddAdapterCommand(stringMappings, adapterNpmName) : generateNpmInstallAdapterCommand(stringMappings, adapterNpmName);
// Check for previously installed adapters
if (adapterConfig && adapterConfig.path && adapterConfig.path.length > 0 && !force) {
throw new Error(`A previous adapter is already configured. Use --force to override
adapterConfig.path: ${adapterConfig.path}
repoPath: ${repoPath}
CLI_PATH: ${CLI_PATH}
installAdapterCommand: ${installAdapterCommand}
adapterNpmName: ${adapterNpmName}
`);
}
try {
executeShellCommand(sh, repoPath, installAdapterCommand);
addPathToAdapterConfig(sh, CLI_PATH, repoPath, adapterNpmName);
} catch (e) {
console.error(e);
}
}
/**
* Checks to make sure that the required arguments are passed
* Throws an exception if any are not.
*/
function checkRequiredArguments (sh, path, adapterNpmName) {
if (!sh) {
throw new Error("You must pass an instance of shelljs when running init.");
}
if (!path) {
throw new Error("Path is required when running init.");
}
if (!adapterNpmName) {
throw new Error("The adapter's npm name is required when running init.");
}
}
/**
* CONFIG
* Loads and returns the adapter config at key config.commitizen, if it exists
*/
function loadAdapterConfig () {
let config = configLoader.load();
if (config) {
return config;
} else {
}
}