-
Notifications
You must be signed in to change notification settings - Fork 5
/
configuration.ts
101 lines (78 loc) · 3.96 KB
/
configuration.ts
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
import { resolve } from './dependencies/path.ts';
import { ensureDir, exists } from './dependencies/fs.ts';
import { getTopLevel } from './lib/git/getTopLevel.ts';
import { readTextFile } from './lib/readTextFile.ts';
import { getConstants } from './constants.ts';
import { getPermissionOrExit } from './lib/getPermissionOrExit.ts';
import { mergeDeepRight, set } from './dependencies/ramda.ts';
import { passCreator } from './lib/passwordManager/passwordManagers/pass.ts';
import { PasswordManager, SupportedPasswordManager } from './lib/passwordManager/passwordManager.ts';
export interface Tool {
accountName: string,
passwordManagerType: SupportedPasswordManager,
passwordManager?: PasswordManager,
}
export interface Executable {
command: string,
args: string[],
}
export interface GlobalGutConfiguration {
tools: { [ key: string ]: Tool },
preferredGitServer: string, // A key to a tool with server facet
forgePath: string,
tempFolderPath: string, // Used to create temporary files, i.e. for error/retry
browser?: Executable
}
export type MessageFormat = 'standard' | 'emoji' | 'angular' | 'prefix'
export const DEFAULT_MESSAGE_FORMAT: MessageFormat = 'emoji';
export interface RepositoryGutConfiguration {
reviewTool: string, // A key to a tool with review facet in global configuration
messageFormat: MessageFormat,
shouldUseIssueNumbers: boolean,
}
export interface FullGutConfiguration {
global: GlobalGutConfiguration,
repository?: RepositoryGutConfiguration,
}
async function readRepositoryConfiguration (repositoryConfigurationPath: string) {
if (!await exists(repositoryConfigurationPath)) {
return {};
}
const repositoryConfigurationAsJson = await readTextFile(repositoryConfigurationPath, {});
const repositoryConfiguration: RepositoryGutConfiguration = JSON.parse(repositoryConfigurationAsJson);
if (!repositoryConfiguration.messageFormat) {
set(repositoryConfiguration, [ 'messageFormat' ], DEFAULT_MESSAGE_FORMAT);
}
return repositoryConfiguration;
}
export async function getConfiguration (): Promise<FullGutConfiguration> {
const { GUT_CONFIGURATION_FOLDER, CONFIGURATION_FILE_NAME } = await getConstants();
await getPermissionOrExit({ name: 'read', path: GUT_CONFIGURATION_FOLDER });
await ensureDir(GUT_CONFIGURATION_FOLDER);
const globalConfigurationPath = resolve(GUT_CONFIGURATION_FOLDER, CONFIGURATION_FILE_NAME);
const globalConfigurationAsJson = await exists(globalConfigurationPath)
? await readTextFile(globalConfigurationPath, { permissionPath: GUT_CONFIGURATION_FOLDER })
: '{}'; // TODO: make sure forge path is added at first run or you're screwed
const globalConfiguration: GlobalGutConfiguration = JSON.parse(globalConfigurationAsJson);
const tempFolderPath = resolve(GUT_CONFIGURATION_FOLDER, 'temp');
globalConfiguration.tempFolderPath = tempFolderPath;
await ensureDir(tempFolderPath);
Object.entries(globalConfiguration.tools)
.forEach(([ toolName, tool ]) =>
tool.passwordManager = passCreator(toolName, tool.accountName), // TODO: make it configurable
);
await getPermissionOrExit({ name: 'read', path: globalConfiguration.forgePath });
const currentRepositoryTopLevel = await getTopLevel();
if (!currentRepositoryTopLevel || !currentRepositoryTopLevel.startsWith(globalConfiguration.forgePath)) {
return { global: globalConfiguration };
}
const publicRepositoryConfigurationPath = resolve(currentRepositoryTopLevel, CONFIGURATION_FILE_NAME);
const publicRepositoryConfiguration = await readRepositoryConfiguration(publicRepositoryConfigurationPath);
const privateRepositoryConfigurationPath = resolve(currentRepositoryTopLevel, '.git', CONFIGURATION_FILE_NAME);
const privateRepositoryConfiguration = await readRepositoryConfiguration(privateRepositoryConfigurationPath);
const repositoryConfiguration = mergeDeepRight(publicRepositoryConfiguration, privateRepositoryConfiguration);
return {
global: globalConfiguration,
repository: repositoryConfiguration,
};
}