-
Notifications
You must be signed in to change notification settings - Fork 20
/
taqueria-config.ts
154 lines (146 loc) · 3.93 KB
/
taqueria-config.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as Config from '@taqueria/protocol/Config';
import type { i18n } from '@taqueria/protocol/i18n';
import * as InstalledPlugin from '@taqueria/protocol/InstalledPlugin';
import * as LoadedConfig from '@taqueria/protocol/LoadedConfig';
import * as SanitizedAbsPath from '@taqueria/protocol/SanitizedAbsPath';
import * as SHA256 from '@taqueria/protocol/SHA256';
import * as TaqError from '@taqueria/protocol/TaqError';
import * as Task from '@taqueria/protocol/Task';
import { attemptP, both, chain, chainRej, FutureInstance as Future, go, map, mapRej, reject, resolve } from 'fluture';
import { pipe } from 'fun';
import {
dirOf,
doesPathExist,
doesPathNotExist,
eager,
ensureDirExists,
joinPaths,
readJsonFile,
writeJsonFile,
} from './taqueria-utils/taqueria-utils.ts';
export type AddTaskCallback = (
task: Task.t,
plugin: InstalledPlugin.t,
handler: (taskArgs: Record<string, unknown>) => Promise<number>,
) => unknown;
export const defaultConfig: Config.t = Config.create({
language: 'en',
contractsDir: 'contracts',
artifactsDir: 'artifacts',
environment: {
default: 'development',
development: {
networks: [],
sandboxes: ['local'],
},
testing: {
networks: ['ghostnet'],
sandboxes: [],
},
production: {
networks: ['mainnet'],
sandboxes: [],
},
},
sandbox: {
local: {
label: 'Local Tezos Sandbox',
rpcUrl: 'http://localhost:20000',
},
},
accounts: {
bob: '30_000_000',
alice: '30_000_000',
john: '30_000_000',
jane: '30_000_000',
joe: '30_000_000',
},
network: {
ghostnet: {
label: 'ghostnet',
rpcUrl: 'https://ghostnet.ecadinfra.com',
},
mainnet: {
label: 'mainnet',
rpcUrl: 'https://mainnet.api.tez.ie',
},
},
});
export const getDefaultMaxConcurrency = () => 10;
export const getProjectDir = (projectDir: SanitizedAbsPath.t): Future<TaqError.t, string> => {
const taqDir = joinPaths(projectDir, '.taq');
return pipe(
doesPathExist(taqDir),
chainRej(previous => {
if (projectDir === '/') {
return reject(TaqError.create({
kind: 'E_TAQ_PROJECT_NOT_FOUND',
msg: "This doesn't appear to be a taqueria project.",
context: projectDir,
previous,
}));
}
return pipe(
joinPaths(projectDir, '../'),
SanitizedAbsPath.make,
chain(getProjectDir),
);
}),
);
};
export const getConfigPath = (projectDir: SanitizedAbsPath.t, create = false) =>
pipe(
create ? ensureDirExists(joinPaths(projectDir, '.taq')) : getProjectDir(projectDir),
map(projectDir => joinPaths(projectDir, 'config.json')),
);
export const getRawConfig = (projectDir: SanitizedAbsPath.t, create = false) =>
pipe(
getConfigPath(projectDir, create),
chain((configPath: string) =>
pipe(
readJsonFile<Config.t>(configPath),
chainRej(err => {
if (!create) return reject(err);
else {
return pipe(
writeJsonFile<Config.t>(configPath)(defaultConfig),
chain((configPath: string) => readJsonFile<Config.t>(configPath)),
);
}
}),
)
),
mapRej<TaqError.t, TaqError.t>(previous =>
previous.kind === 'E_TAQ_PROJECT_NOT_FOUND'
? previous
: ({
kind: 'E_INVALID_CONFIG',
msg: 'Your config.json file is invalid',
previous,
})
),
chain(Config.of),
);
export const toLoadedConfig = (
configPath: string,
config: Config.t,
): Future<TaqError.t, LoadedConfig.t> =>
pipe(
attemptP<TaqError.t, SHA256.t>(() => SHA256.toSHA256(JSON.stringify(config))),
chain(hash =>
attemptP<TaqError.t, LoadedConfig.t>(async () =>
await eager(LoadedConfig.of({
...config,
configFile: await eager(SanitizedAbsPath.make(configPath)),
projectDir: await eager(SanitizedAbsPath.make(joinPaths(dirOf(configPath), '../'))),
hash,
}))
)
),
);
export const getConfig = (projectDir: SanitizedAbsPath.t, _i18n: i18n, create = false) =>
pipe(
getRawConfig(projectDir, create),
both(getConfigPath(projectDir, create)),
chain(([configPath, config]) => toLoadedConfig(configPath, config)),
);