@asterai/cli 0.3.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -50
- package/asterai-init-plugin/package.json +14 -11
- package/asterai-init-plugin/plugin.ts +9 -33
- package/asterai-init-plugin/plugin.wit +12 -0
- package/asterai-init-plugin/tsconfig.json +9 -2
- package/dist/commands/deploy.d.ts +4 -6
- package/dist/commands/deploy.js +41 -64
- package/dist/commands/pkg.d.ts +27 -0
- package/dist/commands/pkg.js +98 -0
- package/dist/commands/query.d.ts +13 -0
- package/dist/commands/query.js +93 -0
- package/dist/const.d.ts +2 -0
- package/dist/const.js +2 -0
- package/oclif.manifest.json +111 -105
- package/package.json +4 -2
- package/asterai-init-plugin/plugin.asterai.proto +0 -22
- package/dist/commands/build.d.ts +0 -23
- package/dist/commands/build.js +0 -139
- package/dist/commands/codegen.d.ts +0 -22
- package/dist/commands/codegen.js +0 -149
- package/dist/compile.d.ts +0 -7
- package/dist/compile.js +0 -25
package/dist/commands/codegen.js
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { Command, Flags } from "@oclif/core";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import fs from "fs";
|
|
4
|
-
import { execSync } from "node:child_process";
|
|
5
|
-
import axios from "axios";
|
|
6
|
-
import { getConfigValue } from "../config.js";
|
|
7
|
-
import os from "os";
|
|
8
|
-
// Relative path from the plugin root directory.
|
|
9
|
-
const AS_PROTO_GEN_PATH = "./node_modules/.bin/as-proto-gen";
|
|
10
|
-
const PRODUCTION_ENDPOINT_BASE_URL = "https://api.asterai.io";
|
|
11
|
-
const STAGING_ENDPOINT_BASE_URL = "https://staging.api.asterai.io";
|
|
12
|
-
export default class Codegen extends Command {
|
|
13
|
-
static args = {};
|
|
14
|
-
static description = "Generate code from the plugin manifest";
|
|
15
|
-
static examples = [`<%= config.bin %> <%= command.id %>`];
|
|
16
|
-
static flags = {
|
|
17
|
-
manifest: Flags.string({
|
|
18
|
-
char: "m",
|
|
19
|
-
description: "manifest path",
|
|
20
|
-
default: "plugin.asterai.proto",
|
|
21
|
-
}),
|
|
22
|
-
outputDir: Flags.string({
|
|
23
|
-
char: "o",
|
|
24
|
-
description: "output directory",
|
|
25
|
-
default: "generated",
|
|
26
|
-
}),
|
|
27
|
-
appId: Flags.string({
|
|
28
|
-
char: "a",
|
|
29
|
-
description: "app id",
|
|
30
|
-
required: false,
|
|
31
|
-
}),
|
|
32
|
-
language: Flags.string({
|
|
33
|
-
char: "l",
|
|
34
|
-
description: "language of generated typings",
|
|
35
|
-
required: false,
|
|
36
|
-
default: "js",
|
|
37
|
-
}),
|
|
38
|
-
staging: Flags.boolean({
|
|
39
|
-
char: "s",
|
|
40
|
-
description: "use staging endpoint",
|
|
41
|
-
required: false,
|
|
42
|
-
default: false,
|
|
43
|
-
}),
|
|
44
|
-
};
|
|
45
|
-
async run() {
|
|
46
|
-
const { flags } = await this.parse(Codegen);
|
|
47
|
-
codegen(flags);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
export const codegen = (flags) => {
|
|
51
|
-
const manifestPath = path.resolve(flags.manifest);
|
|
52
|
-
const baseDir = path.dirname(manifestPath);
|
|
53
|
-
const outDir = path.join(baseDir, flags.outputDir);
|
|
54
|
-
if (!fs.existsSync(outDir)) {
|
|
55
|
-
fs.mkdirSync(outDir, { recursive: true });
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
deleteOldGeneratedFiles(outDir);
|
|
59
|
-
}
|
|
60
|
-
if (flags.appId && flags.language) {
|
|
61
|
-
return generateClientTypings(outDir, flags.language, flags.appId, flags.staging ?? false);
|
|
62
|
-
}
|
|
63
|
-
return generateAssemblyScriptPluginTypings(flags, baseDir);
|
|
64
|
-
};
|
|
65
|
-
const generateAssemblyScriptPluginTypings = async (flags, baseDir) => {
|
|
66
|
-
const absoluteAsProtoGenPath = path.join(baseDir, AS_PROTO_GEN_PATH);
|
|
67
|
-
try {
|
|
68
|
-
execSync("protoc --version");
|
|
69
|
-
}
|
|
70
|
-
catch (e) {
|
|
71
|
-
console.error("No protoc binary found. " +
|
|
72
|
-
"Is protocol buffers installed on the system? " +
|
|
73
|
-
"Download protocol buffers here: https://protobuf.dev/downloads");
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
try {
|
|
77
|
-
execSync("protoc " +
|
|
78
|
-
`--plugin='protoc-gen-as=${absoluteAsProtoGenPath}' ` +
|
|
79
|
-
`--experimental_allow_proto3_optional ` +
|
|
80
|
-
`--as_out='./${flags.outputDir}' ./${flags.manifest}`);
|
|
81
|
-
}
|
|
82
|
-
catch (e) {
|
|
83
|
-
console.error("Failed to generate protobuf types:", e);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
const deleteOldGeneratedFiles = (outDir) => {
|
|
87
|
-
const oldFiles = fs.readdirSync(outDir);
|
|
88
|
-
for (const oldFile of oldFiles) {
|
|
89
|
-
const file = path.parse(oldFile);
|
|
90
|
-
if (file.ext !== ".ts") {
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
const deletePath = path.join(outDir, oldFile);
|
|
94
|
-
fs.unlinkSync(deletePath);
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
const generateClientTypings = async (outDir, language, appId, shouldUseStaging) => {
|
|
98
|
-
const manifestsResponse = await downloadEnabledPluginsManifests(appId, shouldUseStaging);
|
|
99
|
-
const asteraiProto = await fetchAsteraiProto();
|
|
100
|
-
const aggregatedManifest = aggregateManifests(manifestsResponse.manifests, asteraiProto);
|
|
101
|
-
const appPrefix = `app.${appId}`;
|
|
102
|
-
fs.writeFileSync(path.join(outDir, `${appPrefix}.proto`), aggregatedManifest.content);
|
|
103
|
-
if (language === "ts") {
|
|
104
|
-
console.log("generating TypeScript typings for plugin manifest...");
|
|
105
|
-
const jsOutput = `${appPrefix}.js`;
|
|
106
|
-
const dTsOutput = `${appPrefix}.d.ts`;
|
|
107
|
-
execSync(`
|
|
108
|
-
npx -p protobufjs-cli pbjs -t static --no-service ${aggregatedManifest.filePath} -o ${path.join(outDir, jsOutput)}
|
|
109
|
-
`);
|
|
110
|
-
execSync(`
|
|
111
|
-
npx -p protobufjs-cli pbts -o ${path.join(outDir, dTsOutput)} ${path.join(outDir, jsOutput)}
|
|
112
|
-
`);
|
|
113
|
-
fs.unlinkSync(aggregatedManifest.filePath);
|
|
114
|
-
console.log("Typings generated successfully.");
|
|
115
|
-
}
|
|
116
|
-
console.log("done.");
|
|
117
|
-
};
|
|
118
|
-
const fetchAsteraiProto = () => {
|
|
119
|
-
// TODO: fetch this from the local file system instead.
|
|
120
|
-
return axios
|
|
121
|
-
.get("https://unpkg.com/@asterai/sdk@latest/protobuf/asterai.proto")
|
|
122
|
-
.then(r => r.data);
|
|
123
|
-
};
|
|
124
|
-
const aggregateManifests = (manifests, asteraiProto) => {
|
|
125
|
-
let aggregatedManifest = `${asteraiProto}\n`;
|
|
126
|
-
const osTmpDir = os.tmpdir();
|
|
127
|
-
const aggregatedManifestPath = path.join(osTmpDir, "plugins.asterai.proto");
|
|
128
|
-
for (const manifest of manifests) {
|
|
129
|
-
aggregatedManifest += `${manifest.proto}\n`;
|
|
130
|
-
}
|
|
131
|
-
fs.writeFileSync(aggregatedManifestPath, aggregatedManifest);
|
|
132
|
-
return {
|
|
133
|
-
content: aggregatedManifest,
|
|
134
|
-
filePath: aggregatedManifestPath,
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
const downloadEnabledPluginsManifests = async (appId, shouldUseStaging) => {
|
|
138
|
-
const baseUrl = shouldUseStaging
|
|
139
|
-
? STAGING_ENDPOINT_BASE_URL
|
|
140
|
-
: PRODUCTION_ENDPOINT_BASE_URL;
|
|
141
|
-
const response = await axios({
|
|
142
|
-
url: `${baseUrl}/app/${appId}/plugin/manifests`,
|
|
143
|
-
method: "GET",
|
|
144
|
-
headers: {
|
|
145
|
-
Authorization: getConfigValue("key"),
|
|
146
|
-
},
|
|
147
|
-
});
|
|
148
|
-
return response.data;
|
|
149
|
-
};
|
package/dist/compile.d.ts
DELETED
package/dist/compile.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import * as asc from "assemblyscript/asc";
|
|
2
|
-
const COMPILER_OPTIONS = {
|
|
3
|
-
stdout: process.stdout,
|
|
4
|
-
stderr: process.stderr,
|
|
5
|
-
};
|
|
6
|
-
export const compile = async (options) => {
|
|
7
|
-
const args = [
|
|
8
|
-
"--exportRuntime",
|
|
9
|
-
"--runtime",
|
|
10
|
-
"stub",
|
|
11
|
-
...options.inputFiles,
|
|
12
|
-
"--baseDir",
|
|
13
|
-
options.baseDir,
|
|
14
|
-
"--lib",
|
|
15
|
-
options.libs,
|
|
16
|
-
"--outFile",
|
|
17
|
-
options.outputFile,
|
|
18
|
-
"--optimize",
|
|
19
|
-
"--debug",
|
|
20
|
-
];
|
|
21
|
-
const result = await asc.main(args, COMPILER_OPTIONS);
|
|
22
|
-
if (result.error) {
|
|
23
|
-
throw result.error;
|
|
24
|
-
}
|
|
25
|
-
};
|