-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathAppProject.ts
More file actions
162 lines (138 loc) · 4.02 KB
/
AppProject.ts
File metadata and controls
162 lines (138 loc) · 4.02 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { Project, ProjectConfig } from ".";
import {
AppManifestLanguage,
appManifestLanguages,
isAppManifestLanguage,
loadAppManifest,
appManifestLanguageToBindLanguage,
} from "./manifests";
import { AppManifest } from "@polywrap/polywrap-manifest-types-js";
import {
bindSchema,
BindOutput,
BindOptions,
bindLanguageToWrapInfoType,
} from "@polywrap/schema-bind";
import path from "path";
import {
latestWrapManifestVersion,
WrapAbi,
} from "@polywrap/wrap-manifest-types-js";
export interface AppProjectConfig extends ProjectConfig {
appManifestPath: string;
}
export class AppProject extends Project<AppManifest> {
private _appManifest: AppManifest | undefined;
public static cacheLayout = {
root: "app",
};
constructor(protected _config: AppProjectConfig) {
super(_config, {
rootDir: _config.rootDir,
subDir: AppProject.cacheLayout.root,
});
}
/// Project Based Methods
public reset(): void {
this._appManifest = undefined;
this._cache.resetCache();
}
public async validate(): Promise<void> {
const manifest = await this.getManifest();
// Validate language
Project.validateManifestLanguage(
manifest.project.type,
appManifestLanguages,
isAppManifestLanguage
);
}
/// Manifest (polywrap.app.yaml)
public async getName(): Promise<string> {
return (await this.getManifest()).project.name;
}
public async getManifest(): Promise<AppManifest> {
if (!this._appManifest) {
this._appManifest = await loadAppManifest(
this.getManifestPath(),
this.logger
);
}
return Promise.resolve(this._appManifest);
}
public getManifestDir(): string {
return path.dirname(this._config.appManifestPath);
}
public getManifestPath(): string {
return this._config.appManifestPath;
}
public async getManifestLanguage(): Promise<AppManifestLanguage> {
const language = (await this.getManifest()).project.type;
Project.validateManifestLanguage(
language,
appManifestLanguages,
isAppManifestLanguage
);
return language as AppManifestLanguage;
}
/// Schema
public async getSchemaNamedPath(): Promise<string> {
const manifest = await this.getManifest();
const dir = this.getManifestDir();
if (!manifest.source?.schema) {
throw new Error(
`No schema path specified in project manifest with name "${manifest.project.name}". This should never happen.`
);
}
return path.isAbsolute(manifest.source.schema)
? manifest.source.schema
: path.join(dir, manifest.source.schema);
}
public async getImportAbis(): Promise<AppManifest["import_abis"]> {
const manifest = await this.getManifest();
return manifest.import_abis || [];
}
public async getGenerationDirectory(
generationSubPath?: string
): Promise<string> {
return this._getGenerationDirectory(generationSubPath);
}
public async generateSchemaBindings(
abi: WrapAbi,
generationSubPath?: string,
bindgenUri?: string,
bindConfig?: Record<string, unknown>
): Promise<BindOutput> {
const bindLanguage = appManifestLanguageToBindLanguage(
await this.getManifestLanguage()
);
const codegenDir =
generationSubPath || (bindConfig?.codegenDir as string | undefined);
const options: BindOptions = {
bindLanguage,
wrapInfo: {
version: latestWrapManifestVersion,
name: await this.getName(),
type: bindLanguageToWrapInfoType(bindLanguage),
abi,
},
outputDirAbs: await this.getGenerationDirectory(codegenDir),
config: bindConfig,
};
return bindSchema(options, bindgenUri);
}
private _getGenerationDirectory(
useDefinedPath: string | undefined,
defaultDir = "./src/wrap"
): string {
const genPath =
// 1. Use what the user has specified
useDefinedPath ||
// 2. Use the default
defaultDir;
if (path.isAbsolute(genPath)) {
return genPath;
} else {
return path.join(this.getManifestDir(), genPath);
}
}
}