-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathWorkspace.ts
More file actions
147 lines (127 loc) · 4.16 KB
/
Workspace.ts
File metadata and controls
147 lines (127 loc) · 4.16 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
137
138
139
140
141
142
143
144
145
146
147
import * as l from '@openfn/lexicon';
import createLogger from '@openfn/logger';
import path from 'node:path';
import fs from 'node:fs';
import { Project } from './Project';
import pathExists from './util/path-exists';
import {
buildConfig,
loadWorkspaceFile,
findWorkspaceFile,
} from './util/config';
import fromProject from './parse/from-project';
import type { Logger } from '@openfn/logger';
import matchProject from './util/match-project';
import { extractAliasFromFilename } from './parse/from-path';
export class Workspace {
// @ts-ignore config not definitely assigned - it sure is
config: l.WorkspaceConfig;
// TODO activeProject should be the actual project
activeProject?: l.ProjectMeta;
root: string;
private projects: Project[] = [];
private projectPaths = new Map<string, string>();
private isValid: boolean = false;
private logger: Logger;
// Set validate to false to suppress warnings if a Workspace doesn't exist
// This is appropriate if, say, fetching a project for the first time
constructor(workspacePath: string, logger?: Logger, validate = true) {
this.root = workspacePath;
this.logger = logger ?? createLogger('Workspace', { level: 'info' });
let context = { workspace: undefined, project: undefined };
try {
const { type, content } = findWorkspaceFile(workspacePath);
context = loadWorkspaceFile(content, type as any);
this.isValid = true;
} catch (e) {
if (validate) {
this.logger.warn(
`Could not find openfn.yaml at ${workspacePath}. Using default configuration.`
);
}
}
this.config = buildConfig(context.workspace);
this.activeProject = context.project;
const projectsPath = path.join(workspacePath, this.config.dirs.projects);
// dealing with projects
if (pathExists(projectsPath, 'directory')) {
const ext = `.${this.config.formats.project}`;
const stateFiles = fs
.readdirSync(projectsPath)
.filter(
(fileName) =>
path.extname(fileName) === ext &&
path.parse(fileName).name !== 'openfn'
);
this.projects = stateFiles
.map((file) => {
const stateFilePath = path.join(projectsPath, file);
try {
const data = fs.readFileSync(stateFilePath, 'utf-8');
const alias = extractAliasFromFilename(file);
const project = fromProject(data, {
...this.config,
alias,
});
this.projectPaths.set(project.id, stateFilePath);
return project;
} catch (e) {
console.warn(`Failed to load project from ${stateFilePath}`);
console.warn(e);
}
})
.filter((s) => s) as Project[];
} else {
if (validate) {
this.logger.warn(
`No projects found: directory at ${projectsPath} does not exist`
);
}
}
}
// TODO
// This will load a project within this workspace
// uses Project.from
// Rather than doing new Workspace + Project.from(),
// you can do it in a single call
loadProject() {}
list() {
return this.projects;
}
get projectsPath() {
return path.join(this.root, this.config.dirs.projects);
}
get workflowsPath() {
return path.join(this.root, this.config.dirs.workflows);
}
/** Get a project by its alias, id or UUID. Can also include a UUID */
get(nameyThing: string) {
return matchProject(nameyThing, this.projects);
}
getProjectPath(id: string) {
return this.projectPaths.get(id);
}
getTrackedProject() {
return (
this.projects.find((p) => p.openfn?.uuid === this.activeProject?.uuid) ??
this.projects.find((p) => p.id === this.activeProject?.id)
);
}
getCheckedOutProject() {
return Project.from('fs', { root: this.root, config: this.config });
}
getCredentialMap() {
return this.config.credentials;
}
// TODO this needs to return default values
// We should always rely on the workspace to load these values
getConfig(): Partial<l.WorkspaceConfig> {
return this.config!;
}
get activeProjectId() {
return this.activeProject?.id;
}
get valid() {
return this.isValid;
}
}