-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathhandler.ts
More file actions
136 lines (116 loc) · 3.61 KB
/
Copy pathhandler.ts
File metadata and controls
136 lines (116 loc) · 3.61 KB
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
import {
DeployConfig,
DeployError,
deploy,
getConfig,
validateConfig,
} from '@openfn/deploy';
import type { Logger } from '../util/logger';
import { DeployOptions } from './command';
import * as beta from '../projects/deploy';
import path from 'node:path';
import { fileExists } from '../util/file-exists';
import { yamlToJson } from '@openfn/project';
import fs from 'node:fs/promises';
export type DeployFn = typeof deploy;
const actualDeploy: DeployFn = deploy;
// Flexible `deployFn` interface for testing.
async function deployHandler<F extends (...args: any) => any>(
options: DeployOptions,
logger: Logger,
deployFn: F
): Promise<ReturnType<typeof deployFn>>;
async function deployHandler(
options: DeployOptions,
logger: Logger,
deployFn = actualDeploy
) {
if (options.beta) {
return beta.handler(options as any, logger);
}
try {
const config = mergeOverrides(await getConfig(options.configPath), options);
const v2ConfigPath = path.join(
options.workspace || process.cwd(),
'openfn.yaml'
);
if (!process.env.PREFER_LEGACY_SYNC && (await fileExists(v2ConfigPath))) {
return redirectTov2(v2ConfigPath, options, config, logger);
}
if (options.confirm === false) {
config.requireConfirmation = options.confirm;
}
if (process.env['OPENFN_API_KEY']) {
logger.info('Using OPENFN_API_KEY environment variable');
config.apiKey = process.env['OPENFN_API_KEY'];
}
if (process.env['OPENFN_ENDPOINT']) {
logger.info('Using OPENFN_ENDPOINT environment variable');
config.endpoint = process.env['OPENFN_ENDPOINT'];
}
logger.debug('Deploying with config', config);
logger.info(`Deploying`);
validateConfig(config);
const isOk = await deployFn(config, logger);
process.exitCode = isOk ? 0 : 1;
return isOk;
} catch (error: any) {
if (error instanceof DeployError) {
logger.error(error.message);
process.exitCode = 10;
return false;
}
throw error;
}
}
// Priority
// Config
// Env vars
// Options
function mergeOverrides(
config: DeployConfig,
options: DeployOptions
): DeployConfig {
const workspace = options.workspace || process.cwd();
const resolveRelative = (p: string) =>
path.isAbsolute(p) ? p : path.join(workspace, p);
const specPath = pickFirst(options.projectPath, config.specPath);
const statePath = pickFirst(options.statePath, config.statePath);
return {
...config,
apiKey: pickFirst(process.env['OPENFN_API_KEY'], config.apiKey),
endpoint: pickFirst(process.env['OPENFN_ENDPOINT'], config.endpoint),
statePath: resolveRelative(statePath),
specPath: resolveRelative(specPath),
configPath: resolveRelative(options.configPath),
requireConfirmation: pickFirst(options.confirm, config.requireConfirmation),
};
}
function pickFirst<T>(...args: (T | null | undefined)[]): T {
return args.find((arg) => arg !== undefined && arg !== null) as T;
}
const redirectTov2 = async (
v2ConfigPath: string,
options: DeployOptions,
config: DeployConfig,
logger: Logger
) => {
logger.always(
'Detected openfn.yaml file - switching to v2 deploy (openfn project deploy). Set PREFER_LEGACY_SYNC to disable this.'
);
// default endpoint to one from openfn.yaml
const v2config = yamlToJson(await fs.readFile(v2ConfigPath, 'utf-8'));
if (!config.endpoint && v2config?.project?.endpoint) {
config.endpoint = v2config.project.endpoint;
}
return beta.handler(
{
...options,
force: true,
endpoint: config.endpoint,
apiKey: config.apiKey ?? undefined,
},
logger
);
};
export default deployHandler;