@ptkl/toolkit 0.3.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/dist/bin/toolkit.js +14 -0
- package/dist/builder/compiler.js +1 -0
- package/dist/builder/index.js +60 -0
- package/dist/builder/register.js +25 -0
- package/dist/builder/sdk4/vue2.js +69 -0
- package/dist/builder/sdk5/vue2.js +60 -0
- package/dist/builder/sdk5/vue3.js +56 -0
- package/dist/commands/apiUsers.js +74 -0
- package/dist/commands/apps.js +188 -0
- package/dist/commands/forge.js +104 -0
- package/dist/commands/functions.js +27 -0
- package/dist/commands/index.js +22 -0
- package/dist/commands/outputCommand.js +34 -0
- package/dist/commands/profile.js +119 -0
- package/dist/commands/pull.js +74 -0
- package/dist/commands/role.js +63 -0
- package/dist/commands/users.js +46 -0
- package/dist/config.js +69 -0
- package/dist/lib/cli.js +9 -0
- package/dist/lib/types/profile.js +1 -0
- package/dist/lib/util.js +46 -0
- package/dist/playground.js +171 -0
- package/package.json +39 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import { commands } from "../commands/index.js";
|
|
4
|
+
const program = new Command();
|
|
5
|
+
commands.forEach(c => program.addCommand(c));
|
|
6
|
+
program.parseAsync(process.argv).catch((err) => {
|
|
7
|
+
const { response } = err;
|
|
8
|
+
if (response && response.data) {
|
|
9
|
+
console.error(response.data.message);
|
|
10
|
+
console.log(response);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
console.error(err.message);
|
|
14
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { extname, join } from "path";
|
|
2
|
+
// sdk compilers
|
|
3
|
+
import Register from "./register.js";
|
|
4
|
+
export default class Builder {
|
|
5
|
+
compiler;
|
|
6
|
+
constructor(sdkVersion, engine) {
|
|
7
|
+
this.compiler = Register.getCompiler(sdkVersion, engine);
|
|
8
|
+
}
|
|
9
|
+
async build(files) {
|
|
10
|
+
const dist = {};
|
|
11
|
+
let errors = [];
|
|
12
|
+
for (const file in files) {
|
|
13
|
+
try {
|
|
14
|
+
const fileExt = extname(file).substring(1);
|
|
15
|
+
if (this.compiler.getSupportedExt().includes(fileExt)) {
|
|
16
|
+
dist[file] = await this.compiler.compile(fileExt, files[file]);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
errors.push(`${file}: ${error.message}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (errors.length > 0) {
|
|
24
|
+
throw new BuildError(errors);
|
|
25
|
+
}
|
|
26
|
+
return dist;
|
|
27
|
+
}
|
|
28
|
+
nodesToFiles(nodes, basePath) {
|
|
29
|
+
let files = {};
|
|
30
|
+
nodes.forEach((node) => {
|
|
31
|
+
const currentPath = join(basePath, node.name);
|
|
32
|
+
if (node.isLeaf) {
|
|
33
|
+
files = Object.assign({}, files, {
|
|
34
|
+
[currentPath]: node.content
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const childrenFiles = this.nodesToFiles(node.children ?? [], currentPath);
|
|
39
|
+
files = Object.assign({}, files, childrenFiles);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return files;
|
|
43
|
+
}
|
|
44
|
+
async buildFromNodes(nodes) {
|
|
45
|
+
const files = this.nodesToFiles(nodes, '');
|
|
46
|
+
return this.build(files);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export class BuildError extends Error {
|
|
50
|
+
errors;
|
|
51
|
+
constructor(errors) {
|
|
52
|
+
super(`Error during build: ${errors.join(", ")}`);
|
|
53
|
+
this.name = 'BuildError'; // Set the name to the class name
|
|
54
|
+
this.errors = errors;
|
|
55
|
+
// This is necessary to maintain the stack trace for instance creation
|
|
56
|
+
if (Error.captureStackTrace) {
|
|
57
|
+
Error.captureStackTrace(this, BuildError);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import CompilerSDK4 from "./sdk4/vue2.js";
|
|
2
|
+
import CompilerSDK5 from "./sdk5/vue2.js";
|
|
3
|
+
import CompilerSDK5Vue3 from "./sdk5/vue3.js";
|
|
4
|
+
const latestVersion = 5;
|
|
5
|
+
const defaultEngine = 'vue2';
|
|
6
|
+
const compilers = {
|
|
7
|
+
'sdk4::vue2': new CompilerSDK4,
|
|
8
|
+
'sdk5::vue2': new CompilerSDK5,
|
|
9
|
+
'sdk5::vue3': new CompilerSDK5Vue3,
|
|
10
|
+
};
|
|
11
|
+
export default {
|
|
12
|
+
getCompiler(sdkVersion, engine) {
|
|
13
|
+
if (sdkVersion === undefined) {
|
|
14
|
+
sdkVersion = latestVersion;
|
|
15
|
+
}
|
|
16
|
+
if (!engine) {
|
|
17
|
+
engine = defaultEngine;
|
|
18
|
+
}
|
|
19
|
+
const compiler = compilers[`sdk${sdkVersion}::${engine}`];
|
|
20
|
+
if (!compiler) {
|
|
21
|
+
return new CompilerSDK4;
|
|
22
|
+
}
|
|
23
|
+
return compiler;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as Babel from '@babel/standalone';
|
|
2
|
+
import less from 'less';
|
|
3
|
+
import { parse } from '@vue/compiler-sfc';
|
|
4
|
+
export default class Compiler {
|
|
5
|
+
async compileVue(template) {
|
|
6
|
+
try {
|
|
7
|
+
const { descriptor = {} } = parse(template);
|
|
8
|
+
const styles = descriptor.styles.map((style) => style.content).join(" ");
|
|
9
|
+
const code = Babel.transform(descriptor?.script?.content ?? "", { sourceType: "module", presets: ["env"] }).code;
|
|
10
|
+
const css = await this.compileCSS(null, 'less', styles);
|
|
11
|
+
return {
|
|
12
|
+
template: this.getTmpl(template),
|
|
13
|
+
style: css,
|
|
14
|
+
script: code,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
throw new Error(err.message);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
getStringBetween(str, start, end) {
|
|
22
|
+
const result = str.match(new RegExp("(?:\\" + start + ")(.*)(?=\\" + end + ")", 'gs'));
|
|
23
|
+
if (result === null) {
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
return result[0].replace(start, "");
|
|
27
|
+
}
|
|
28
|
+
getTmpl(c) {
|
|
29
|
+
try {
|
|
30
|
+
return this.getStringBetween(c, "<tmpl>", "</tmpl>");
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
throw new Error("Failed to parse template:" + err.message);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async compileBabel(expression) {
|
|
37
|
+
let code = Babel.transform(expression, { sourceType: "module", presets: ["env"] });
|
|
38
|
+
return code.code;
|
|
39
|
+
}
|
|
40
|
+
async compileCSS(scope, lang, expression) {
|
|
41
|
+
switch (lang) {
|
|
42
|
+
case 'less':
|
|
43
|
+
if (scope) {
|
|
44
|
+
expression = `[data-scope="${scope}"] { ${expression} }`;
|
|
45
|
+
}
|
|
46
|
+
const { css } = await less.render(expression);
|
|
47
|
+
return css;
|
|
48
|
+
default:
|
|
49
|
+
return expression;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async compile(ext, content) {
|
|
53
|
+
switch (ext) {
|
|
54
|
+
case 'vue':
|
|
55
|
+
return await this.compileVue(content);
|
|
56
|
+
case 'js':
|
|
57
|
+
return await this.compileBabel(content);
|
|
58
|
+
case 'css':
|
|
59
|
+
return await this.compileCSS(null, "css", content);
|
|
60
|
+
case 'less':
|
|
61
|
+
return await this.compileCSS(null, "less", content);
|
|
62
|
+
default:
|
|
63
|
+
return await Promise.resolve(content);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
getSupportedExt() {
|
|
67
|
+
return ["vue", "js", "css", "less", "json", "svg"];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as Babel from '@babel/standalone';
|
|
2
|
+
import less from 'less';
|
|
3
|
+
import { compile } from 'vue-template-compiler';
|
|
4
|
+
import { parse } from '@vue/compiler-sfc';
|
|
5
|
+
export default class Compiler {
|
|
6
|
+
async compileVue(scope, template) {
|
|
7
|
+
try {
|
|
8
|
+
const { descriptor = {} } = parse(template);
|
|
9
|
+
const styles = descriptor.styles.map((style) => style.content).join(" ");
|
|
10
|
+
const code = Babel.transform(descriptor?.script?.content ?? "", { sourceType: "module", presets: ["env"] }).code;
|
|
11
|
+
const css = await this.compileCSS(null, 'less', styles);
|
|
12
|
+
const { render, staticRenderFns, errors } = compile(descriptor?.template?.content);
|
|
13
|
+
if (errors && errors.length > 0) {
|
|
14
|
+
throw new Error(errors.join(", "));
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
style: css,
|
|
18
|
+
script: code,
|
|
19
|
+
render: render.toString(),
|
|
20
|
+
staticRenderFns: staticRenderFns
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
throw new Error(err.message);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async compileBabel(expression) {
|
|
28
|
+
let code = Babel.transform(expression, { sourceType: "module", presets: ["env"] });
|
|
29
|
+
return code.code;
|
|
30
|
+
}
|
|
31
|
+
async compileCSS(scope, lang, expression) {
|
|
32
|
+
switch (lang) {
|
|
33
|
+
case 'less':
|
|
34
|
+
if (scope) {
|
|
35
|
+
return await less.render(`.${scope} { ${expression} }`);
|
|
36
|
+
}
|
|
37
|
+
const { css } = await less.render(`${expression}`);
|
|
38
|
+
return css;
|
|
39
|
+
default:
|
|
40
|
+
return expression;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async compile(ext, content) {
|
|
44
|
+
switch (ext) {
|
|
45
|
+
case 'vue':
|
|
46
|
+
return await this.compileVue(null, content);
|
|
47
|
+
case 'js':
|
|
48
|
+
return await this.compileBabel(content);
|
|
49
|
+
case 'css':
|
|
50
|
+
return await this.compileCSS(null, "css", content);
|
|
51
|
+
case 'less':
|
|
52
|
+
return await this.compileCSS(null, "less", content);
|
|
53
|
+
default:
|
|
54
|
+
return await Promise.resolve(content);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
getSupportedExt() {
|
|
58
|
+
return ["vue", "js", "css", "less", "json", "svg"];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as Babel from '@babel/standalone';
|
|
2
|
+
import less from 'less';
|
|
3
|
+
import { compile } from '@vue/compiler-dom';
|
|
4
|
+
import { parse } from '@vue/compiler-sfc';
|
|
5
|
+
export default class Compiler {
|
|
6
|
+
async compileVue(template) {
|
|
7
|
+
try {
|
|
8
|
+
const { descriptor = {} } = parse(template);
|
|
9
|
+
const styles = descriptor.styles.map((style) => style.content).join(" ");
|
|
10
|
+
const code = Babel.transform(descriptor?.script?.content ?? "", { sourceType: "module", presets: ["env"] }).code;
|
|
11
|
+
const css = await this.compileCSS(null, 'less', styles);
|
|
12
|
+
const { code: render } = compile(descriptor?.template?.content);
|
|
13
|
+
return {
|
|
14
|
+
style: css,
|
|
15
|
+
script: code,
|
|
16
|
+
render
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
throw new Error(err.message);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
async compileBabel(expression) {
|
|
24
|
+
let code = Babel.transform(expression, { sourceType: "module", presets: ["env"] });
|
|
25
|
+
return code.code;
|
|
26
|
+
}
|
|
27
|
+
async compileCSS(scope, lang, expression) {
|
|
28
|
+
switch (lang) {
|
|
29
|
+
case 'less':
|
|
30
|
+
if (scope) {
|
|
31
|
+
return await less.render(`.${scope} { ${expression} }`);
|
|
32
|
+
}
|
|
33
|
+
const { css } = await less.render(`${expression}`);
|
|
34
|
+
return css;
|
|
35
|
+
default:
|
|
36
|
+
return expression;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async compile(ext, content) {
|
|
40
|
+
switch (ext) {
|
|
41
|
+
case 'vue':
|
|
42
|
+
return await this.compileVue(content);
|
|
43
|
+
case 'js':
|
|
44
|
+
return await this.compileBabel(content);
|
|
45
|
+
case 'css':
|
|
46
|
+
return await this.compileCSS(null, "css", content);
|
|
47
|
+
case 'less':
|
|
48
|
+
return await this.compileCSS(null, "less", content);
|
|
49
|
+
default:
|
|
50
|
+
return await Promise.resolve(content);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
getSupportedExt() {
|
|
54
|
+
return ["vue", "js", "css", "less", "json", "svg"];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import util from "../lib/util.js";
|
|
3
|
+
import Api from "protokol-sdk";
|
|
4
|
+
import OutputFormatCommand from "./outputCommand.js";
|
|
5
|
+
class ApiUsersCommand {
|
|
6
|
+
register() {
|
|
7
|
+
return (new Command("api-users")
|
|
8
|
+
.description("Manage API users")
|
|
9
|
+
.version("0.1.0")
|
|
10
|
+
.addCommand(new OutputFormatCommand(new Command("new-secret")
|
|
11
|
+
.description("Create a new secret for a user")
|
|
12
|
+
.requiredOption("-u, --uuid <uuid>", "UUID of the user to create a new secret for")).action(this.newSecret))
|
|
13
|
+
.addCommand(new OutputFormatCommand(new Command("revoke-secret")
|
|
14
|
+
.description("Revoke an existing secret")
|
|
15
|
+
.requiredOption("-u, --uuid <uuid>", "UUID of the secret to revoke")).action(this.revokeSecret))
|
|
16
|
+
.addCommand(new OutputFormatCommand(new Command("get-auth-token")
|
|
17
|
+
.description("Retrieve an auth token for an API user")
|
|
18
|
+
.requiredOption("-u, --user <user>", "Username or name of the API user")
|
|
19
|
+
.requiredOption("-p, --password <password>", "Password or secret for the API user")).action(this.getAuthToken))
|
|
20
|
+
// .addCommand( new OutputFormatCommand(
|
|
21
|
+
// new Command("edit")
|
|
22
|
+
// .description("edit API user")
|
|
23
|
+
// .requiredOption("-u, --uuid <uuid>", "UUID of the API user")
|
|
24
|
+
// .requiredOption("-r, --roles <roles...>", "list of role uuids")
|
|
25
|
+
// ).action(this.edit)
|
|
26
|
+
// )
|
|
27
|
+
.addCommand(new OutputFormatCommand(new Command("list").description("list all API user's")).action(this.list))
|
|
28
|
+
.addCommand(new OutputFormatCommand(new Command("get")
|
|
29
|
+
.description("Retrieve an API user by UUID")
|
|
30
|
+
.requiredOption("-u, --uuid <uuid>", "UUID of the API user")).action(this.get)));
|
|
31
|
+
}
|
|
32
|
+
async newSecret(opts) {
|
|
33
|
+
const { uuid } = opts;
|
|
34
|
+
const profile = util.getCurrentProfile();
|
|
35
|
+
const client = new Api({ project: profile.project, token: profile.token, host: profile.host }).apiUser();
|
|
36
|
+
const secret = await client.newSecret(uuid);
|
|
37
|
+
return secret;
|
|
38
|
+
}
|
|
39
|
+
async revokeSecret(opts) {
|
|
40
|
+
const { uuid } = opts;
|
|
41
|
+
const profile = util.getCurrentProfile();
|
|
42
|
+
const client = new Api({ project: profile.project, token: profile.token, host: profile.host }).apiUser();
|
|
43
|
+
const status = await client.revokeSecret(uuid);
|
|
44
|
+
return status;
|
|
45
|
+
}
|
|
46
|
+
async getAuthToken(opts) {
|
|
47
|
+
const { user, password } = opts;
|
|
48
|
+
const profile = util.getCurrentProfile();
|
|
49
|
+
const client = new Api({ project: profile.project, token: profile.token, host: profile.host }).apiUser();
|
|
50
|
+
const auth = await client.apiAuth(user, password);
|
|
51
|
+
return auth.data.Token;
|
|
52
|
+
}
|
|
53
|
+
async edit(opts) {
|
|
54
|
+
const { uuid, roles } = opts;
|
|
55
|
+
const profile = util.getCurrentProfile();
|
|
56
|
+
const client = new Api({ project: profile.project, token: profile.token, host: profile.host }).apiUser();
|
|
57
|
+
const updatedUser = await client.edit(uuid, roles);
|
|
58
|
+
return updatedUser;
|
|
59
|
+
}
|
|
60
|
+
async list() {
|
|
61
|
+
const profile = util.getCurrentProfile();
|
|
62
|
+
const client = new Api({ project: profile.project, token: profile.token, host: profile.host }).apiUser();
|
|
63
|
+
const users = await client.list();
|
|
64
|
+
return users;
|
|
65
|
+
}
|
|
66
|
+
async get(opts) {
|
|
67
|
+
const { uuid } = opts;
|
|
68
|
+
const profile = util.getCurrentProfile();
|
|
69
|
+
const client = new Api({ project: profile.project, token: profile.token, host: profile.host }).apiUser();
|
|
70
|
+
const user = await client.get(uuid);
|
|
71
|
+
return user;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export default new ApiUsersCommand();
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { Command, Option } from "commander";
|
|
2
|
+
import util from "../lib/util.js";
|
|
3
|
+
import Api from "protokol-sdk";
|
|
4
|
+
import { build, createServer } from 'vite';
|
|
5
|
+
import { c } from 'tar';
|
|
6
|
+
import { Writable } from "stream";
|
|
7
|
+
import { writeFileSync } from "fs";
|
|
8
|
+
import Util from "../lib/util.js";
|
|
9
|
+
import Playground from "../playground.js";
|
|
10
|
+
import { join } from 'path';
|
|
11
|
+
class AppsCommand {
|
|
12
|
+
register() {
|
|
13
|
+
return new Command("apps")
|
|
14
|
+
.description("Manage apps")
|
|
15
|
+
.version("0.1.0")
|
|
16
|
+
.addCommand(new Command("download")
|
|
17
|
+
.description("Download app")
|
|
18
|
+
.addOption(new Option("-a, --apptype <apptype>", "App type to manage")
|
|
19
|
+
.choices(["app", "public-app"])
|
|
20
|
+
.makeOptionMandatory())
|
|
21
|
+
.requiredOption("-v, --version <version>", "App version to download")
|
|
22
|
+
.requiredOption("-r, --ref <ref>", "App's identifier")
|
|
23
|
+
.requiredOption("-o, --output <output>", "Download directory relative path")
|
|
24
|
+
.action(this.download))
|
|
25
|
+
.addCommand(new Command("upload")
|
|
26
|
+
.description("Upload app")
|
|
27
|
+
.addOption(new Option("-a, --apptype <apptype>", "App type to manage")
|
|
28
|
+
.choices(["app", "public-app", "portal"])
|
|
29
|
+
.makeOptionMandatory())
|
|
30
|
+
.requiredOption("-d, --directory <directory>", "App directory to upload")
|
|
31
|
+
.option("-b, --build", "Build app")
|
|
32
|
+
.action(this.upload))
|
|
33
|
+
.addCommand(new Command("deploy")
|
|
34
|
+
.description("Deploy app")
|
|
35
|
+
.addOption(new Option("-a, --apptype <apptype>", "App type to manage")
|
|
36
|
+
.choices(["app", "public-app", "portal"])
|
|
37
|
+
.makeOptionMandatory())
|
|
38
|
+
.requiredOption("-r, --ref <ref>", "App's identifier")
|
|
39
|
+
.option("-d, --dev_version <dev_version>", "Development version")
|
|
40
|
+
.option("-p, --public_version <public_version>", "Public version")
|
|
41
|
+
.action(this.deploy))
|
|
42
|
+
.addCommand(new Command("playground")
|
|
43
|
+
.description("Start playground")
|
|
44
|
+
.requiredOption("-p, --path <path>", "Path is directory where app is located.")
|
|
45
|
+
.option(" --port <port>", "WSS port")
|
|
46
|
+
.option("-s --sdk_version <sdk_version>", "SDK Version to be used for build", "5")
|
|
47
|
+
.option("-e --engine <engine>", "Engine to be used for build", "vue2")
|
|
48
|
+
.addOption(new Option("-a, --apptype <apptype>", "App type to manage")
|
|
49
|
+
.choices(["app", "public-app"])
|
|
50
|
+
.makeOptionMandatory())
|
|
51
|
+
.action(this.playground))
|
|
52
|
+
.addCommand(new Command("bundle")
|
|
53
|
+
.description("Bundle app")
|
|
54
|
+
.requiredOption("-p, --path <path>", "Path is directory where app is located.")
|
|
55
|
+
.option("-u, --upload", "Upload bundle")
|
|
56
|
+
.action(this.bundle))
|
|
57
|
+
.addCommand(new Command("dev")
|
|
58
|
+
.description("Run dev app")
|
|
59
|
+
.requiredOption("-p, --path <path>", "Path is directory where app is located.")
|
|
60
|
+
.action(this.runDev))
|
|
61
|
+
.addCommand(new Command("remove-version")
|
|
62
|
+
.description("Remove version from available versions of the app")
|
|
63
|
+
.requiredOption("-v, --version <version>")
|
|
64
|
+
.requiredOption("-r, --ref <ref>")
|
|
65
|
+
.action(this.removeVersion));
|
|
66
|
+
}
|
|
67
|
+
async download(options) {
|
|
68
|
+
const { apptype, version, ref, output } = options;
|
|
69
|
+
const profile = util.getCurrentProfile();
|
|
70
|
+
const client = new Api({ project: profile.project, token: profile.token, host: profile.host }).app(apptype);
|
|
71
|
+
return await client.download(version, ref, output);
|
|
72
|
+
}
|
|
73
|
+
async upload(options) {
|
|
74
|
+
const { apptype, directory, build } = options;
|
|
75
|
+
const profile = util.getCurrentProfile();
|
|
76
|
+
const client = new Api({ project: profile.project, token: profile.token, host: profile.host }).app(apptype);
|
|
77
|
+
return await client.upload(directory, build);
|
|
78
|
+
}
|
|
79
|
+
async deploy(options) {
|
|
80
|
+
const { apptype, dev_version, public_version, ref } = options;
|
|
81
|
+
const profile = util.getCurrentProfile();
|
|
82
|
+
const client = new Api({ project: profile.project, token: profile.token, host: profile.host }).app(apptype);
|
|
83
|
+
return await client.updateSettings({ dev_version, public_version }, ref);
|
|
84
|
+
}
|
|
85
|
+
async playground(options) {
|
|
86
|
+
let { path, port, sdk_version, engine, apptype } = options;
|
|
87
|
+
port = port ? Number(port) : 11400;
|
|
88
|
+
const profile = util.getCurrentProfile();
|
|
89
|
+
const client = new Api({ project: profile.project, token: profile.token, host: profile.host }).app();
|
|
90
|
+
let playgroundWatcher = new Playground(apptype, path, Number(sdk_version), engine, port, profile);
|
|
91
|
+
await playgroundWatcher.init();
|
|
92
|
+
playgroundWatcher.start();
|
|
93
|
+
}
|
|
94
|
+
async bundle(options) {
|
|
95
|
+
const { path, upload } = options;
|
|
96
|
+
const module = await import(`${path}/ptkl.config.js`);
|
|
97
|
+
const { views, name, version, distPath, icon, label, permissions, } = module.default ?? {};
|
|
98
|
+
// build manifest file
|
|
99
|
+
const manifest = {
|
|
100
|
+
name,
|
|
101
|
+
version,
|
|
102
|
+
views: {},
|
|
103
|
+
label,
|
|
104
|
+
icon,
|
|
105
|
+
permissions,
|
|
106
|
+
};
|
|
107
|
+
const profile = Util.getCurrentProfile();
|
|
108
|
+
const client = Util.getClientForProfile();
|
|
109
|
+
// get base url of the platform client
|
|
110
|
+
const baseUrl = client.getPlatformBaseURL();
|
|
111
|
+
const base = `${baseUrl}/v3/system/gateway/app-service/static/bundle/${profile.project}/${name}/${version}`;
|
|
112
|
+
manifest.icon = `${base}/${icon}`;
|
|
113
|
+
const buildViews = Object.keys(views).map((view) => {
|
|
114
|
+
manifest.views[view] = `${view}.bundle.js`;
|
|
115
|
+
return build({
|
|
116
|
+
root: path,
|
|
117
|
+
base,
|
|
118
|
+
build: {
|
|
119
|
+
rollupOptions: {
|
|
120
|
+
input: views[view],
|
|
121
|
+
output: {
|
|
122
|
+
format: 'umd',
|
|
123
|
+
entryFileNames: `${view}.bundle.js`,
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
await Promise.allSettled(buildViews).catch(err => {
|
|
130
|
+
console.error('Error building:', err);
|
|
131
|
+
});
|
|
132
|
+
console.log("Packaging app...");
|
|
133
|
+
// // write manifest file
|
|
134
|
+
const manifestPath = `${distPath}/manifest.json`;
|
|
135
|
+
writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
|
|
136
|
+
if (upload) {
|
|
137
|
+
let buffer = Buffer.alloc(0);
|
|
138
|
+
const bufferStream = new Writable({
|
|
139
|
+
write(chunk, encoding, callback) {
|
|
140
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
141
|
+
callback();
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
c({ gzip: true, cwd: distPath }, ['.']).pipe(bufferStream).on('finish', () => {
|
|
145
|
+
client.app().bundleUpload(buffer).then(() => {
|
|
146
|
+
console.log('Bundle uploaded successfully');
|
|
147
|
+
})
|
|
148
|
+
.catch((error) => {
|
|
149
|
+
console.error('Bundle error:', error.response.data.message);
|
|
150
|
+
});
|
|
151
|
+
})
|
|
152
|
+
.on('error', (error) => {
|
|
153
|
+
console.error('Error creating the archive:', error.message);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
try {
|
|
158
|
+
await c({ gzip: true, cwd: distPath, file: join(distPath, `/../bundle-${manifest.version}.tar.gz`) }, ['.']);
|
|
159
|
+
console.log('Bundle created successfully');
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
console.error('Error creating the archive:', error.message);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async runDev(options) {
|
|
167
|
+
const { path } = options;
|
|
168
|
+
const server = await createServer({
|
|
169
|
+
root: path, // Set your project root
|
|
170
|
+
});
|
|
171
|
+
await server.listen();
|
|
172
|
+
console.log('Dev server running at:', server?.resolvedUrls?.local[0]);
|
|
173
|
+
}
|
|
174
|
+
async removeVersion(options) {
|
|
175
|
+
const { version, ref } = options;
|
|
176
|
+
const profile = util.getClientForProfile();
|
|
177
|
+
const client = profile.app();
|
|
178
|
+
const { data } = await client.getApp(ref);
|
|
179
|
+
if (data.public_version === version) {
|
|
180
|
+
throw new Error("Cannot remove public version");
|
|
181
|
+
}
|
|
182
|
+
const settings = data.settings.filter((s) => s.version !== version);
|
|
183
|
+
return await client.updateSettings({
|
|
184
|
+
'settings': settings
|
|
185
|
+
}, ref);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
export default new AppsCommand();
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { build, createServer } from 'vite';
|
|
3
|
+
import { c } from 'tar';
|
|
4
|
+
import { Writable } from "stream";
|
|
5
|
+
import { writeFileSync } from "fs";
|
|
6
|
+
import Util from "../lib/util.js";
|
|
7
|
+
import { join } from 'path';
|
|
8
|
+
class ForgeCommand {
|
|
9
|
+
register() {
|
|
10
|
+
return new Command("forge")
|
|
11
|
+
.description("Work with protokol forge to seamlessly build and deploy apps")
|
|
12
|
+
.version("0.1.0")
|
|
13
|
+
.addCommand(new Command("bundle")
|
|
14
|
+
.description("Bundle app")
|
|
15
|
+
.requiredOption("-p, --path <path>", "Path is directory where app is located.")
|
|
16
|
+
.option("-u, --upload", "Upload bundle")
|
|
17
|
+
.action(this.bundle))
|
|
18
|
+
.addCommand(new Command("dev")
|
|
19
|
+
.description("Run dev app")
|
|
20
|
+
.requiredOption("-p, --path <path>", "Path is directory where app is located.")
|
|
21
|
+
.action(this.runDev));
|
|
22
|
+
}
|
|
23
|
+
async bundle(options) {
|
|
24
|
+
const { path, upload } = options;
|
|
25
|
+
const module = await import(`${path}/ptkl.config.js`);
|
|
26
|
+
const { views, name, version, distPath, icon, label, permissions, } = module.default ?? {};
|
|
27
|
+
// build manifest file
|
|
28
|
+
const manifest = {
|
|
29
|
+
name,
|
|
30
|
+
version,
|
|
31
|
+
views: {},
|
|
32
|
+
label,
|
|
33
|
+
icon,
|
|
34
|
+
permissions,
|
|
35
|
+
};
|
|
36
|
+
const profile = Util.getCurrentProfile();
|
|
37
|
+
const client = Util.getClientForProfile();
|
|
38
|
+
// get base url of the platform client
|
|
39
|
+
const baseUrl = client.getPlatformBaseURL();
|
|
40
|
+
const base = `${baseUrl}/v3/system/gateway/app-service/forge/static/bundle/${profile.project}/${name}/${version}`;
|
|
41
|
+
manifest.icon = `${base}/${icon}`;
|
|
42
|
+
const buildViews = Object.keys(views).map((view) => {
|
|
43
|
+
manifest.views[view] = `${view}.bundle.js`;
|
|
44
|
+
return build({
|
|
45
|
+
root: path,
|
|
46
|
+
base,
|
|
47
|
+
build: {
|
|
48
|
+
rollupOptions: {
|
|
49
|
+
input: views[view],
|
|
50
|
+
output: {
|
|
51
|
+
format: 'umd',
|
|
52
|
+
entryFileNames: `${view}.bundle.js`,
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
await Promise.allSettled(buildViews).catch(err => {
|
|
59
|
+
console.error('Error building:', err);
|
|
60
|
+
});
|
|
61
|
+
console.log("Packaging app...");
|
|
62
|
+
// // write manifest file
|
|
63
|
+
const manifestPath = `${distPath}/manifest.json`;
|
|
64
|
+
writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
|
|
65
|
+
if (upload) {
|
|
66
|
+
let buffer = Buffer.alloc(0);
|
|
67
|
+
const bufferStream = new Writable({
|
|
68
|
+
write(chunk, encoding, callback) {
|
|
69
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
70
|
+
callback();
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
c({ gzip: true, cwd: distPath }, ['.']).pipe(bufferStream).on('finish', () => {
|
|
74
|
+
client.forge().bundleUpload(buffer).then(() => {
|
|
75
|
+
console.log('Bundle uploaded successfully');
|
|
76
|
+
})
|
|
77
|
+
.catch((error) => {
|
|
78
|
+
console.error('Bundle error:', error);
|
|
79
|
+
});
|
|
80
|
+
})
|
|
81
|
+
.on('error', (error) => {
|
|
82
|
+
console.error('Error creating the archive:', error.message);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
try {
|
|
87
|
+
await c({ gzip: true, cwd: distPath, file: join(distPath, `/../bundle-${manifest.version}.tar.gz`) }, ['.']);
|
|
88
|
+
console.log('Bundle created successfully');
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
console.error('Error creating the archive:', error.message);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async runDev(options) {
|
|
96
|
+
const { path } = options;
|
|
97
|
+
const server = await createServer({
|
|
98
|
+
root: path, // Set your project root
|
|
99
|
+
});
|
|
100
|
+
await server.listen();
|
|
101
|
+
console.log('Dev server running at:', server?.resolvedUrls?.local[0]);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export default new ForgeCommand();
|