@rollrook-ai/plugin-cli 0.0.0-stage → 0.1.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/bin/rollrook-plugin.js +2 -0
- package/dist/cli.d.ts +25 -0
- package/dist/cli.js +194 -0
- package/dist/cli.js.map +1 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.js +12 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +3 -0
- package/dist/main.js.map +1 -0
- package/dist/manifest.d.ts +200 -0
- package/dist/manifest.js +723 -0
- package/dist/manifest.js.map +1 -0
- package/dist/packer.d.ts +44 -0
- package/dist/packer.js +78 -0
- package/dist/packer.js.map +1 -0
- package/dist/project.d.ts +59 -0
- package/dist/project.js +149 -0
- package/dist/project.js.map +1 -0
- package/dist/scaffold.d.ts +49 -0
- package/dist/scaffold.js +127 -0
- package/dist/scaffold.js.map +1 -0
- package/dist/vite.d.ts +40 -0
- package/dist/vite.js +124 -0
- package/dist/vite.js.map +1 -0
- package/package.json +49 -4
- package/src/cli.ts +206 -0
- package/src/errors.ts +11 -0
- package/src/index.ts +24 -0
- package/src/main.ts +3 -0
- package/src/manifest.ts +882 -0
- package/src/packer.ts +104 -0
- package/src/project.ts +168 -0
- package/src/scaffold.ts +156 -0
- package/src/vite.ts +136 -0
- package/template/README.md +12 -0
- package/template/_gitignore +4 -0
- package/template/icon.svg +14 -0
- package/template/index.html +33 -0
- package/template/package.json +22 -0
- package/template/rollrook-plugin.json +18 -0
- package/template/src/main.ts +78 -0
- package/template/src/style.css +35 -0
- package/template/src/vite-env.d.ts +1 -0
- package/template/tsconfig.json +18 -0
- package/template/vite.config.ts +10 -0
- package/README.md +0 -3
package/src/packer.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { zipSync } from "fflate";
|
|
4
|
+
import type { Zippable } from "fflate";
|
|
5
|
+
import { CliError } from "./errors.js";
|
|
6
|
+
import { ManifestValidator } from "./manifest.js";
|
|
7
|
+
import type { PluginManifest } from "./manifest.js";
|
|
8
|
+
|
|
9
|
+
/** What to pack and where. */
|
|
10
|
+
export interface PackOptions {
|
|
11
|
+
/** The folder whose contents become the package; it holds the manifest at its root. */
|
|
12
|
+
folder: string;
|
|
13
|
+
/** The `.rrplugin` file to write; `<id>-<version>.rrplugin` beside the folder by default. */
|
|
14
|
+
out_file?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The written package. */
|
|
18
|
+
export interface PackResult {
|
|
19
|
+
out_file: string;
|
|
20
|
+
manifest: PluginManifest;
|
|
21
|
+
/** The files in the package, relative to its root. */
|
|
22
|
+
list_files: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Zips a plugin folder into a `.rrplugin` package with the manifest at its root. */
|
|
26
|
+
export class PluginPacker {
|
|
27
|
+
/** The package's file extension. */
|
|
28
|
+
static readonly EXTENSION = ".rrplugin";
|
|
29
|
+
|
|
30
|
+
/** Names never put into a package. */
|
|
31
|
+
static readonly SET_SKIPPED_NAMES = new Set([".DS_Store", ".git", "node_modules", "Thumbs.db"]);
|
|
32
|
+
|
|
33
|
+
/** The deflate level the package is compressed with. */
|
|
34
|
+
static readonly COMPRESSION_LEVEL = 9;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Check the folder's manifest and zip the folder.
|
|
38
|
+
*
|
|
39
|
+
* @param options - the folder and the output file
|
|
40
|
+
* @returns the package written
|
|
41
|
+
* @throws CliError when the manifest has errors
|
|
42
|
+
*/
|
|
43
|
+
static pack(options: PackOptions): PackResult {
|
|
44
|
+
const folder = resolve(options.folder);
|
|
45
|
+
const report = ManifestValidator.read(folder);
|
|
46
|
+
if (report.manifest === undefined) {
|
|
47
|
+
const list_lines = report.list_problems
|
|
48
|
+
.filter((problem) => problem.severity === "error")
|
|
49
|
+
.map((problem) => ` ${problem.field}: ${problem.message}`);
|
|
50
|
+
throw new CliError(`the manifest in ${folder} has errors:\n${list_lines.join("\n")}`);
|
|
51
|
+
}
|
|
52
|
+
const manifest = report.manifest;
|
|
53
|
+
const out_file = resolve(
|
|
54
|
+
options.out_file ?? join(dirname(folder), PluginPacker.file_name(manifest)),
|
|
55
|
+
);
|
|
56
|
+
const list_files = PluginPacker.list_files(folder).filter(
|
|
57
|
+
(path: string) => resolve(folder, path) !== out_file,
|
|
58
|
+
);
|
|
59
|
+
const zippable: Zippable = {};
|
|
60
|
+
for (const path of list_files) {
|
|
61
|
+
zippable[path] = readFileSync(join(folder, path));
|
|
62
|
+
}
|
|
63
|
+
mkdirSync(dirname(out_file), { recursive: true });
|
|
64
|
+
writeFileSync(out_file, zipSync(zippable, { level: PluginPacker.COMPRESSION_LEVEL }));
|
|
65
|
+
return {
|
|
66
|
+
out_file,
|
|
67
|
+
manifest,
|
|
68
|
+
list_files,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param manifest - the plugin's manifest
|
|
74
|
+
* @returns `<id>-<version>.rrplugin`
|
|
75
|
+
*/
|
|
76
|
+
static file_name(manifest: PluginManifest): string {
|
|
77
|
+
return `${manifest.id}-${manifest.version}${PluginPacker.EXTENSION}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* List the files a package of the folder holds.
|
|
82
|
+
*
|
|
83
|
+
* @param folder - the plugin's folder
|
|
84
|
+
* @returns the paths, relative to the folder, with `/` separators, sorted
|
|
85
|
+
*/
|
|
86
|
+
static list_files(folder: string): string[] {
|
|
87
|
+
const list_paths: string[] = [];
|
|
88
|
+
const list_pending = [folder];
|
|
89
|
+
for (let current = list_pending.pop(); current !== undefined; current = list_pending.pop()) {
|
|
90
|
+
for (const name of readdirSync(current)) {
|
|
91
|
+
const path = join(current, name);
|
|
92
|
+
if (PluginPacker.SET_SKIPPED_NAMES.has(name) || name.endsWith(PluginPacker.EXTENSION)) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (statSync(path).isDirectory()) {
|
|
96
|
+
list_pending.push(path);
|
|
97
|
+
} else {
|
|
98
|
+
list_paths.push(relative(folder, path).split(sep).join("/"));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return list_paths.sort();
|
|
103
|
+
}
|
|
104
|
+
}
|
package/src/project.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { build } from "vite";
|
|
4
|
+
import type { InlineConfig } from "vite";
|
|
5
|
+
import { CliError } from "./errors.js";
|
|
6
|
+
import { ManifestValidator } from "./manifest.js";
|
|
7
|
+
import type { PluginManifest } from "./manifest.js";
|
|
8
|
+
import { RollRookVite } from "./vite.js";
|
|
9
|
+
|
|
10
|
+
/** Options for watching a project. */
|
|
11
|
+
export interface WatchOptions {
|
|
12
|
+
/** Called after every build, the first one included. */
|
|
13
|
+
on_built: (is_first: boolean) => void;
|
|
14
|
+
/** Called when a build fails; the watch goes on. */
|
|
15
|
+
on_error: (error: Error) => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A plugin's source folder: `rollrook-plugin.json` at its root, built with
|
|
20
|
+
* Vite into `dist/` when it has a Vite setup, and packaged as it is otherwise.
|
|
21
|
+
*/
|
|
22
|
+
export class PluginProject {
|
|
23
|
+
/** The folder Vite builds into. */
|
|
24
|
+
static readonly OUT_DIR = "dist";
|
|
25
|
+
|
|
26
|
+
/** The Vite config files a project may have. */
|
|
27
|
+
static readonly LIST_VITE_CONFIGS = [
|
|
28
|
+
"vite.config.ts",
|
|
29
|
+
"vite.config.mts",
|
|
30
|
+
"vite.config.js",
|
|
31
|
+
"vite.config.mjs",
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
/** The project's root folder. */
|
|
35
|
+
readonly root: string;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param root - the project's root folder
|
|
39
|
+
* @throws CliError when it holds no manifest
|
|
40
|
+
*/
|
|
41
|
+
constructor(root: string) {
|
|
42
|
+
this.root = resolve(root);
|
|
43
|
+
if (!existsSync(join(this.root, ManifestValidator.MANIFEST_FILE))) {
|
|
44
|
+
throw new CliError(`no ${ManifestValidator.MANIFEST_FILE} in ${this.root}; run this in a plugin's folder`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @returns the folder that becomes the plugin: `dist/` for a Vite project, the root otherwise
|
|
50
|
+
*/
|
|
51
|
+
package_folder(): string {
|
|
52
|
+
return this.uses_vite() ? join(this.root, PluginProject.OUT_DIR) : this.root;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @returns whether the project builds with Vite
|
|
57
|
+
*/
|
|
58
|
+
uses_vite(): boolean {
|
|
59
|
+
if (PluginProject.LIST_VITE_CONFIGS.some((name: string) => existsSync(join(this.root, name)))) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
const package_path = join(this.root, "package.json");
|
|
63
|
+
if (!existsSync(package_path)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
const text = readFileSync(package_path, "utf8");
|
|
67
|
+
return /"vite"\s*:/.test(text);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Read the source manifest.
|
|
72
|
+
*
|
|
73
|
+
* @returns the manifest
|
|
74
|
+
* @throws CliError when it has errors
|
|
75
|
+
*/
|
|
76
|
+
manifest(): PluginManifest {
|
|
77
|
+
const report = ManifestValidator.read(this.root);
|
|
78
|
+
if (report.manifest === undefined) {
|
|
79
|
+
const list_lines = report.list_problems
|
|
80
|
+
.filter((problem) => problem.severity === "error")
|
|
81
|
+
.map((problem) => ` ${problem.field}: ${problem.message}`);
|
|
82
|
+
throw new CliError(`${ManifestValidator.MANIFEST_FILE} has errors:\n${list_lines.join("\n")}`);
|
|
83
|
+
}
|
|
84
|
+
return report.manifest;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Build the plugin once into `dist/`, with the manifest and icon beside the page.
|
|
89
|
+
*
|
|
90
|
+
* @throws CliError when the project has no Vite setup
|
|
91
|
+
*/
|
|
92
|
+
async build(): Promise<void> {
|
|
93
|
+
if (!this.uses_vite()) {
|
|
94
|
+
throw new CliError(`${this.root} has no Vite setup to build; it is packed as it is`);
|
|
95
|
+
}
|
|
96
|
+
await build(this.vite_config(false));
|
|
97
|
+
this.copy_manifest_files();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Build the plugin into `dist/` and again whenever its sources change.
|
|
102
|
+
*
|
|
103
|
+
* @param options - what to do after each build and on failures
|
|
104
|
+
* @returns a function that stops watching
|
|
105
|
+
*/
|
|
106
|
+
async watch(options: WatchOptions): Promise<() => Promise<void>> {
|
|
107
|
+
if (!this.uses_vite()) {
|
|
108
|
+
throw new CliError(`${this.root} has no Vite setup to watch; link the folder itself instead`);
|
|
109
|
+
}
|
|
110
|
+
const result = await build(this.vite_config(true));
|
|
111
|
+
if (!("on" in result)) {
|
|
112
|
+
throw new CliError("Vite did not start watching");
|
|
113
|
+
}
|
|
114
|
+
let build_count = 0;
|
|
115
|
+
result.on("event", async (event) => {
|
|
116
|
+
if (event.code === "BUNDLE_END") {
|
|
117
|
+
await event.result.close();
|
|
118
|
+
} else if (event.code === "ERROR") {
|
|
119
|
+
await event.result.close();
|
|
120
|
+
options.on_error(event.error);
|
|
121
|
+
} else if (event.code === "END") {
|
|
122
|
+
this.copy_manifest_files();
|
|
123
|
+
build_count += 1;
|
|
124
|
+
options.on_built(build_count === 1);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
return () => result.close();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @returns the URL that asks RollRook to link the built folder
|
|
132
|
+
*/
|
|
133
|
+
link_url(): string {
|
|
134
|
+
return `rollrook://plugins/link?path=${encodeURIComponent(this.package_folder())}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private vite_config(is_watching: boolean): InlineConfig {
|
|
138
|
+
const manifest = this.manifest();
|
|
139
|
+
return {
|
|
140
|
+
root: this.root,
|
|
141
|
+
mode: "production",
|
|
142
|
+
build: {
|
|
143
|
+
outDir: PluginProject.OUT_DIR,
|
|
144
|
+
emptyOutDir: true,
|
|
145
|
+
watch: is_watching ? {} : null,
|
|
146
|
+
rolldownOptions: {
|
|
147
|
+
input: RollRookVite.inputs(manifest, this.root),
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private copy_manifest_files(): void {
|
|
154
|
+
const out_dir = this.package_folder();
|
|
155
|
+
const list_names = [
|
|
156
|
+
ManifestValidator.MANIFEST_FILE,
|
|
157
|
+
...RollRookVite.files(ManifestValidator.read(this.root).manifest),
|
|
158
|
+
];
|
|
159
|
+
for (const name of list_names) {
|
|
160
|
+
const target = join(out_dir, name);
|
|
161
|
+
const source = join(this.root, name);
|
|
162
|
+
if (!existsSync(target) && existsSync(source)) {
|
|
163
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
164
|
+
copyFileSync(source, target);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
package/src/scaffold.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { cpSync, existsSync, readFileSync, readdirSync, renameSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { basename, join, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { CliError } from "./errors.js";
|
|
5
|
+
import { ManifestValidator } from "./manifest.js";
|
|
6
|
+
|
|
7
|
+
/** What to create. */
|
|
8
|
+
export interface CreateOptions {
|
|
9
|
+
/** The new plugin's folder; it must not exist or be empty. */
|
|
10
|
+
dir: string;
|
|
11
|
+
/** The plugin's display name; derived from the folder's name by default. */
|
|
12
|
+
name?: string;
|
|
13
|
+
/** The plugin's id; `local.<folder name>` by default. */
|
|
14
|
+
id?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The plugin that was created. */
|
|
18
|
+
export interface CreateResult {
|
|
19
|
+
dir: string;
|
|
20
|
+
id: string;
|
|
21
|
+
name: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Creates a plugin project from the basic template. */
|
|
25
|
+
export class Scaffolder {
|
|
26
|
+
/** The publisher part of the default id. */
|
|
27
|
+
static readonly DEFAULT_PUBLISHER = "local";
|
|
28
|
+
|
|
29
|
+
/** Files stored under another name in the template, because npm drops them from packages. */
|
|
30
|
+
static readonly map_renamed_files: Record<string, string> = {
|
|
31
|
+
"_gitignore": ".gitignore",
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** The SDK's packages a new plugin depends on, with the section each is in; they move in lockstep. */
|
|
35
|
+
static readonly map_pinned_packages: Record<string, string> = {
|
|
36
|
+
"@rollrook-ai/sdk": "dependencies",
|
|
37
|
+
"@rollrook-ai/plugin-cli": "devDependencies",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Copy the template into a new folder and name the plugin.
|
|
42
|
+
*
|
|
43
|
+
* @param options - the folder, name and id
|
|
44
|
+
* @returns the plugin created
|
|
45
|
+
* @throws CliError when the folder is not empty or the id is malformed
|
|
46
|
+
*/
|
|
47
|
+
static create(options: CreateOptions): CreateResult {
|
|
48
|
+
const dir = resolve(options.dir);
|
|
49
|
+
if (existsSync(dir) && readdirSync(dir).length > 0) {
|
|
50
|
+
throw new CliError(`${dir} is not empty`);
|
|
51
|
+
}
|
|
52
|
+
const slug = Scaffolder.slug(basename(dir));
|
|
53
|
+
const name = options.name ?? Scaffolder.title(slug);
|
|
54
|
+
const id = options.id ?? `${Scaffolder.DEFAULT_PUBLISHER}.${slug}`;
|
|
55
|
+
if (!ManifestValidator.ID_PATTERN.test(id)) {
|
|
56
|
+
throw new CliError(`${id} is not a plugin id: use two or more dot-separated parts of lowercase letters, digits and dashes, e.g. octo.pomodoro`);
|
|
57
|
+
}
|
|
58
|
+
cpSync(Scaffolder.template_dir(), dir, { recursive: true });
|
|
59
|
+
for (const [stored, real] of Object.entries(Scaffolder.map_renamed_files)) {
|
|
60
|
+
if (existsSync(join(dir, stored))) {
|
|
61
|
+
renameSync(join(dir, stored), join(dir, real));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
Scaffolder.edit_json(join(dir, ManifestValidator.MANIFEST_FILE), (record) => {
|
|
65
|
+
record["id"] = id;
|
|
66
|
+
record["name"] = name;
|
|
67
|
+
record["publisher"] = id.split(".")[0] ?? Scaffolder.DEFAULT_PUBLISHER;
|
|
68
|
+
record["version"] = "0.1.0";
|
|
69
|
+
const views = record["views"];
|
|
70
|
+
const first_view: unknown = Array.isArray(views) ? views[0] : undefined;
|
|
71
|
+
if (typeof first_view === "object" && first_view !== null && "title" in first_view) {
|
|
72
|
+
Reflect.set(first_view, "title", name);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
const version = Scaffolder.own_version();
|
|
76
|
+
Scaffolder.edit_json(join(dir, "package.json"), (record) => {
|
|
77
|
+
record["name"] = id.replaceAll(".", "-");
|
|
78
|
+
record["version"] = "0.1.0";
|
|
79
|
+
Scaffolder.pin(record, `^${version}`);
|
|
80
|
+
});
|
|
81
|
+
const index_path = join(dir, "index.html");
|
|
82
|
+
if (existsSync(index_path)) {
|
|
83
|
+
const html = readFileSync(index_path, "utf8");
|
|
84
|
+
writeFileSync(index_path, html.replace(/<title>[^<]*<\/title>/, `<title>${Scaffolder.escape_html(name)}</title>`));
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
dir,
|
|
88
|
+
id,
|
|
89
|
+
name,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @returns the template's folder: shipped inside the package, or the repository's when run from source
|
|
95
|
+
*/
|
|
96
|
+
static template_dir(): string {
|
|
97
|
+
const list_candidates = [
|
|
98
|
+
fileURLToPath(new URL("../template/", import.meta.url)),
|
|
99
|
+
fileURLToPath(new URL("../../../templates/basic/", import.meta.url)),
|
|
100
|
+
];
|
|
101
|
+
const found = list_candidates.find((candidate: string) => existsSync(join(candidate, ManifestValidator.MANIFEST_FILE)));
|
|
102
|
+
if (found === undefined) {
|
|
103
|
+
throw new CliError("the plugin template is missing from this installation");
|
|
104
|
+
}
|
|
105
|
+
return found;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @returns this package's version, which the SDK shares
|
|
110
|
+
*/
|
|
111
|
+
static own_version(): string {
|
|
112
|
+
const raw: unknown = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
113
|
+
if (typeof raw === "object" && raw !== null && "version" in raw && typeof raw.version === "string") {
|
|
114
|
+
return raw.version;
|
|
115
|
+
}
|
|
116
|
+
throw new CliError("this installation's package.json has no version");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @param text - a folder name
|
|
121
|
+
* @returns the name as lowercase letters, digits and dashes
|
|
122
|
+
*/
|
|
123
|
+
static slug(text: string): string {
|
|
124
|
+
const slug = text.toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "").replace(/-{2,}/g, "-");
|
|
125
|
+
return slug === "" ? "plugin" : slug;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private static title(slug: string): string {
|
|
129
|
+
return slug.split("-").filter((word: string) => word !== "").map(
|
|
130
|
+
(word: string) => word.charAt(0).toUpperCase() + word.slice(1),
|
|
131
|
+
).join(" ");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private static escape_html(text: string): string {
|
|
135
|
+
return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private static pin(record: Record<string, unknown>, range: string): void {
|
|
139
|
+
for (const [name, section] of Object.entries(Scaffolder.map_pinned_packages)) {
|
|
140
|
+
const map_dependencies = record[section];
|
|
141
|
+
if (typeof map_dependencies === "object" && map_dependencies !== null && name in map_dependencies) {
|
|
142
|
+
Reflect.set(map_dependencies, name, range);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private static edit_json(path: string, edit: (record: Record<string, unknown>) => void): void {
|
|
148
|
+
const raw: unknown = JSON.parse(readFileSync(path, "utf8"));
|
|
149
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
150
|
+
throw new CliError(`${path} in the template is not a JSON object`);
|
|
151
|
+
}
|
|
152
|
+
const record: Record<string, unknown> = { ...raw };
|
|
153
|
+
edit(record);
|
|
154
|
+
writeFileSync(path, `${JSON.stringify(record, null, 2)}\n`);
|
|
155
|
+
}
|
|
156
|
+
}
|
package/src/vite.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import type { Plugin, ResolvedConfig, UserConfig } from "vite";
|
|
4
|
+
import { ManifestValidator } from "./manifest.js";
|
|
5
|
+
import type { PluginManifest } from "./manifest.js";
|
|
6
|
+
|
|
7
|
+
/** Options for the RollRook Vite plugin. */
|
|
8
|
+
export interface RollRookViteOptions {
|
|
9
|
+
/** The folder holding `rollrook-plugin.json` and the icons; Vite's root by default. */
|
|
10
|
+
manifest_folder?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Builds a RollRook plugin with Vite: takes every view's entry page as an
|
|
15
|
+
* input, emits `rollrook-plugin.json`, the icons and the assistant's skill
|
|
16
|
+
* file it names into the output, rebuilds when they change, and stops the build when the manifest
|
|
17
|
+
* has errors.
|
|
18
|
+
*/
|
|
19
|
+
export class RollRookVite {
|
|
20
|
+
/** The plugin's name, as Vite reports it. */
|
|
21
|
+
static readonly PLUGIN_NAME = "rollrook-plugin";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param options - where the manifest lives
|
|
25
|
+
* @returns the Vite plugin
|
|
26
|
+
*/
|
|
27
|
+
static plugin(options: RollRookViteOptions = {}): Plugin {
|
|
28
|
+
let root = process.cwd();
|
|
29
|
+
let public_dir = "";
|
|
30
|
+
return {
|
|
31
|
+
name: RollRookVite.PLUGIN_NAME,
|
|
32
|
+
apply: "build",
|
|
33
|
+
config(config: UserConfig): UserConfig {
|
|
34
|
+
const folder = resolve(config.root ?? process.cwd(), options.manifest_folder ?? ".");
|
|
35
|
+
const manifest = ManifestValidator.read(folder).manifest;
|
|
36
|
+
if (manifest === undefined) {
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
build: {
|
|
41
|
+
rolldownOptions: {
|
|
42
|
+
input: RollRookVite.inputs(manifest, folder),
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
configResolved(config: ResolvedConfig): void {
|
|
48
|
+
root = resolve(config.root, options.manifest_folder ?? ".");
|
|
49
|
+
public_dir = config.publicDir;
|
|
50
|
+
},
|
|
51
|
+
buildStart(): void {
|
|
52
|
+
this.addWatchFile(join(root, ManifestValidator.MANIFEST_FILE));
|
|
53
|
+
const report = ManifestValidator.read(root);
|
|
54
|
+
for (const problem of report.list_problems) {
|
|
55
|
+
const line = `${ManifestValidator.MANIFEST_FILE}: ${problem.field}: ${problem.message}`;
|
|
56
|
+
if (problem.severity === "error") {
|
|
57
|
+
this.error(line);
|
|
58
|
+
}
|
|
59
|
+
this.warn(line);
|
|
60
|
+
}
|
|
61
|
+
for (const file of RollRookVite.files(report.manifest)) {
|
|
62
|
+
this.addWatchFile(join(root, file));
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
generateBundle(): void {
|
|
66
|
+
const manifest = ManifestValidator.read(root).manifest;
|
|
67
|
+
if (manifest === undefined) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.emitFile({
|
|
71
|
+
type: "asset",
|
|
72
|
+
fileName: ManifestValidator.MANIFEST_FILE,
|
|
73
|
+
source: readFileSync(join(root, ManifestValidator.MANIFEST_FILE), "utf8"),
|
|
74
|
+
});
|
|
75
|
+
for (const file of RollRookVite.files(manifest)) {
|
|
76
|
+
if (!(public_dir !== "" && existsSync(join(public_dir, file)))) {
|
|
77
|
+
this.emitFile({
|
|
78
|
+
type: "asset",
|
|
79
|
+
fileName: file,
|
|
80
|
+
source: readFileSync(join(root, file)),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The pages Vite builds: each view's entry, named by the entry's path.
|
|
90
|
+
*
|
|
91
|
+
* @param manifest - the plugin's manifest
|
|
92
|
+
* @param folder - the folder the entries are relative to
|
|
93
|
+
* @returns the inputs, by name
|
|
94
|
+
*/
|
|
95
|
+
static inputs(manifest: PluginManifest, folder: string): Record<string, string> {
|
|
96
|
+
const map_inputs: Record<string, string> = {};
|
|
97
|
+
for (const view of manifest.list_views) {
|
|
98
|
+
const name = view.entry.replace(/\.html?$/i, "").replaceAll("/", "-");
|
|
99
|
+
map_inputs[name] = resolve(folder, view.entry);
|
|
100
|
+
}
|
|
101
|
+
return map_inputs;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @param manifest - the plugin's manifest, or undefined
|
|
106
|
+
* @returns the files besides the pages the package needs: the icons and the assistant's skill, each once
|
|
107
|
+
*/
|
|
108
|
+
static files(manifest: PluginManifest | undefined): string[] {
|
|
109
|
+
const list_files = RollRookVite.icons(manifest);
|
|
110
|
+
const skill = manifest?.contributes.skill;
|
|
111
|
+
if (skill !== undefined && !list_files.includes(skill)) {
|
|
112
|
+
list_files.push(skill);
|
|
113
|
+
}
|
|
114
|
+
return list_files;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @param manifest - the plugin's manifest, or undefined
|
|
119
|
+
* @returns the plugin's icon and its views' icons, each once
|
|
120
|
+
*/
|
|
121
|
+
static icons(manifest: PluginManifest | undefined): string[] {
|
|
122
|
+
if (manifest === undefined) {
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
const set_icons = new Set<string>();
|
|
126
|
+
if (manifest.icon !== undefined) {
|
|
127
|
+
set_icons.add(manifest.icon);
|
|
128
|
+
}
|
|
129
|
+
for (const view of manifest.list_views) {
|
|
130
|
+
if (view.icon !== undefined) {
|
|
131
|
+
set_icons.add(view.icon);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return [...set_icons];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# RollRook plugin
|
|
2
|
+
|
|
3
|
+
A RollRook plugin built with Vite, TypeScript and `@rollrook-ai/sdk`. It shows as a tab in RollRook's dock.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
npm install
|
|
8
|
+
npm run dev # develop in the browser against the mock host
|
|
9
|
+
npm run link # build on every change and link the plugin into RollRook
|
|
10
|
+
npm run pack # write <id>-<version>.rrplugin
|
|
11
|
+
|
|
12
|
+
The manifest is `rollrook-plugin.json`; see the [SDK](https://github.com/rollrook-ai/rollrook-sdk) for the API and the permissions.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="tile" x1="0" y1="0" x2="0" y2="1">
|
|
4
|
+
<stop offset="0" stop-color="#3a3a3c"/>
|
|
5
|
+
<stop offset="1" stop-color="#1c1c1e"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect x="2" y="2" width="60" height="60" rx="14" fill="url(#tile)"/>
|
|
9
|
+
<rect x="2.5" y="2.5" width="59" height="59" rx="13.5" fill="none" stroke="#ffffff" stroke-opacity="0.12"/>
|
|
10
|
+
<rect x="17" y="17" width="12" height="12" rx="3.5" fill="#f5f5f7"/>
|
|
11
|
+
<rect x="35" y="17" width="12" height="12" rx="3.5" fill="#f5f5f7" fill-opacity="0.55"/>
|
|
12
|
+
<rect x="17" y="35" width="12" height="12" rx="3.5" fill="#f5f5f7" fill-opacity="0.55"/>
|
|
13
|
+
<rect x="35" y="35" width="12" height="12" rx="3.5" fill="#f5f5f7" fill-opacity="0.3"/>
|
|
14
|
+
</svg>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<title>Basic</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<main class="rr-stack">
|
|
10
|
+
<header class="rr-row header">
|
|
11
|
+
<img class="header-icon" src="./icon.svg" alt="" width="28" height="28" />
|
|
12
|
+
<h1 id="title" class="rr-grow">Basic</h1>
|
|
13
|
+
</header>
|
|
14
|
+
<section class="rr-section rr-appear">
|
|
15
|
+
<h2>Counter</h2>
|
|
16
|
+
<div class="rr-row">
|
|
17
|
+
<span id="count" class="count">0</span>
|
|
18
|
+
<span class="rr-grow"></span>
|
|
19
|
+
<button id="reset" type="button" class="rr-plain">Reset</button>
|
|
20
|
+
<button id="increment" type="button" class="rr-primary">Add one</button>
|
|
21
|
+
</div>
|
|
22
|
+
</section>
|
|
23
|
+
<section class="rr-section rr-appear">
|
|
24
|
+
<h2>Say hello</h2>
|
|
25
|
+
<form id="toast-form" class="rr-row">
|
|
26
|
+
<input id="message" class="rr-grow" placeholder="A message for RollRook" autocomplete="off" />
|
|
27
|
+
<button type="submit">Show</button>
|
|
28
|
+
</form>
|
|
29
|
+
</section>
|
|
30
|
+
</main>
|
|
31
|
+
<script type="module" src="/src/main.ts"></script>
|
|
32
|
+
</body>
|
|
33
|
+
</html>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "local-basic",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "rollrook-plugin build",
|
|
9
|
+
"link": "rollrook-plugin dev",
|
|
10
|
+
"pack": "rollrook-plugin pack",
|
|
11
|
+
"validate": "rollrook-plugin validate",
|
|
12
|
+
"typecheck": "tsc --noEmit"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@rollrook-ai/sdk": "^0.1.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@rollrook-ai/plugin-cli": "^0.1.0",
|
|
19
|
+
"typescript": "^7.0.2",
|
|
20
|
+
"vite": "^8.4.1"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "local.basic",
|
|
3
|
+
"name": "Basic",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "A starting point for a RollRook plugin.",
|
|
6
|
+
"publisher": "local",
|
|
7
|
+
"views": [
|
|
8
|
+
{
|
|
9
|
+
"id": "panel",
|
|
10
|
+
"slot": "dock",
|
|
11
|
+
"entry": "index.html",
|
|
12
|
+
"title": "Basic"
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
"icon": "icon.svg",
|
|
16
|
+
"permissions": [],
|
|
17
|
+
"minRollRook": "0.7.0"
|
|
18
|
+
}
|