@black-studio/plugin 0.0.0-stage → 1.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.
Files changed (5) hide show
  1. package/README.md +954 -2
  2. package/build.mjs +118 -0
  3. package/cli.mjs +28 -0
  4. package/index.d.ts +1021 -0
  5. package/package.json +18 -4
package/build.mjs ADDED
@@ -0,0 +1,118 @@
1
+ // Builds a Bläck plugin: its source (src/main.ts, .tsx, .js or .jsx) into one
2
+ // script, main.js, next to its manifest.json. What the app provides (the
3
+ // Plugin class, React, TipTap) is left out and taken from the app at run
4
+ // time, so the plugin uses the app's copies. Everything else it imports
5
+ // is bundled in.
6
+ import { createHash } from 'node:crypto'
7
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
8
+ import os from 'node:os'
9
+ import path from 'node:path'
10
+
11
+ /**
12
+ * The name the app knows the SDK by at run time. Plugins import '@black-studio/plugin', the package's name on npm, and
13
+ * the build turns that into this, so a plugin also works in app versions from before the package was renamed.
14
+ */
15
+ export const RUNTIME_SDK = '@black/plugin'
16
+
17
+ /** What the app gives a plugin that runs in the window. A sandboxed plugin gets only the SDK. */
18
+ export const PROVIDED = [
19
+ '@black-studio/plugin',
20
+ RUNTIME_SDK,
21
+ 'react',
22
+ 'react/jsx-runtime',
23
+ '@tiptap/core',
24
+ '@tiptap/pm/state',
25
+ '@tiptap/pm/view',
26
+ '@tiptap/pm/model',
27
+ ]
28
+
29
+ /** Where the app looks for plugins on this computer (set BLACK_PLUGINS_DIR to change it). */
30
+ export function pluginsDir() {
31
+ if (process.env.BLACK_PLUGINS_DIR) return process.env.BLACK_PLUGINS_DIR
32
+ if (process.platform === 'linux')
33
+ return path.join(process.env.XDG_DATA_HOME || path.join(os.homedir(), '.local', 'share'), 'black-studio', 'plugins')
34
+ if (process.platform === 'darwin') return path.join(os.homedir(), 'Library', 'Application Support', 'black-studio', 'plugins')
35
+ return path.join(process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming'), 'black-studio', 'plugins')
36
+ }
37
+
38
+ export function readManifest(dir) {
39
+ const file = path.join(dir, 'manifest.json')
40
+ if (!existsSync(file)) throw new Error(`No manifest.json in ${dir}`)
41
+ return JSON.parse(readFileSync(file, 'utf8'))
42
+ }
43
+
44
+ function entryOf(dir) {
45
+ for (const name of ['main.tsx', 'main.ts', 'main.jsx', 'main.js']) {
46
+ const file = path.join(dir, 'src', name)
47
+ if (existsSync(file)) return file
48
+ }
49
+ throw new Error(`No src/main.ts (or .tsx, .js, .jsx) in ${dir}`)
50
+ }
51
+
52
+ /**
53
+ * Builds the plugin in `dir` into `outDir` (manifest.json and main.js).
54
+ * Returns the manifest. With `watch`, it rebuilds on every change and
55
+ * calls `onBuild` each time; the returned close() stops watching.
56
+ */
57
+ export async function buildPlugin(dir, outDir, { watch = false, onBuild, minify = false } = {}) {
58
+ const { rolldown, watch: rolldownWatch } = await import('rolldown')
59
+ const manifest = readManifest(dir)
60
+ const options = {
61
+ input: entryOf(dir),
62
+ external: PROVIDED,
63
+ transform: { jsx: { runtime: 'automatic' } },
64
+ logLevel: 'warn',
65
+ }
66
+ const output = {
67
+ file: path.join(outDir, manifest.main ?? 'main.js'),
68
+ format: 'iife',
69
+ // The script leaves its class in this global for the app to find.
70
+ name: '__blackPlugin',
71
+ exports: 'auto',
72
+ globals: (id) => `__black.require(${JSON.stringify(id === '@black-studio/plugin' ? RUNTIME_SDK : id)})`,
73
+ minify,
74
+ }
75
+ const copyManifest = () => {
76
+ mkdirSync(outDir, { recursive: true })
77
+ writeFileSync(path.join(outDir, 'manifest.json'), JSON.stringify(readManifest(dir), null, 2) + '\n')
78
+ // The plugin's styles.css, next to its manifest, is copied with it.
79
+ if (existsSync(path.join(dir, 'styles.css'))) writeFileSync(path.join(outDir, 'styles.css'), readFileSync(path.join(dir, 'styles.css')))
80
+ }
81
+ if (!watch) {
82
+ const bundle = await rolldown(options)
83
+ await bundle.write(output)
84
+ await bundle.close()
85
+ copyManifest()
86
+ return manifest
87
+ }
88
+ const watcher = rolldownWatch({
89
+ ...options,
90
+ output,
91
+ watch: { include: [path.join(dir, 'src', '**'), path.join(dir, 'manifest.json')] },
92
+ })
93
+ watcher.on('event', (event) => {
94
+ if (event.code === 'BUNDLE_END') {
95
+ copyManifest()
96
+ event.result?.close()
97
+ onBuild?.(null)
98
+ }
99
+ if (event.code === 'ERROR') onBuild?.(event.error)
100
+ })
101
+ return { manifest, close: () => watcher.close() }
102
+ }
103
+
104
+ /** Zips the built plugin for the plugin list and returns the zip's SHA-256. */
105
+ export async function zipPlugin(outDir, zipFile) {
106
+ const { default: JSZip } = await import('jszip')
107
+ const zip = new JSZip()
108
+ const manifest = readManifest(outDir)
109
+ // Make the same zip every time for the same plugin: the files get a fixed date instead of the build date, so
110
+ // the SHA-256 reflects what's in the zip and can be checked against the source.
111
+ const stamp = { date: new Date(Date.UTC(1980, 0, 1)) }
112
+ zip.file('manifest.json', readFileSync(path.join(outDir, 'manifest.json')), stamp)
113
+ zip.file(manifest.main ?? 'main.js', readFileSync(path.join(outDir, manifest.main ?? 'main.js')), stamp)
114
+ for (const extra of ['styles.css']) if (existsSync(path.join(outDir, extra))) zip.file(extra, readFileSync(path.join(outDir, extra)), stamp)
115
+ const data = await zip.generateAsync({ type: 'nodebuffer', compression: 'DEFLATE', compressionOptions: { level: 9 } })
116
+ writeFileSync(zipFile, data)
117
+ return createHash('sha256').update(data).digest('hex')
118
+ }
package/cli.mjs ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env node
2
+ // black-plugin build → dist/<id>/ (manifest.json, main.js) and dist/<id>-<version>.zip, with its SHA-256
3
+ // black-plugin dev → builds into the app's plugins folder on every save; the app reloads it after each build
4
+ import path from 'node:path'
5
+ import { buildPlugin, pluginsDir, readManifest, zipPlugin } from './build.mjs'
6
+
7
+ const [command = 'build'] = process.argv.slice(2)
8
+ const dir = process.cwd()
9
+ const manifest = readManifest(dir)
10
+
11
+ if (command === 'build') {
12
+ const out = path.join(dir, 'dist', manifest.id)
13
+ await buildPlugin(dir, out, { minify: true })
14
+ const zip = path.join(dir, 'dist', `${manifest.id}-${manifest.version}.zip`)
15
+ const sha256 = await zipPlugin(out, zip)
16
+ console.log(`Built ${path.relative(dir, out)}/ and ${path.relative(dir, zip)}`)
17
+ console.log(`sha256: ${sha256}`)
18
+ } else if (command === 'dev') {
19
+ const out = path.join(pluginsDir(), manifest.id)
20
+ await buildPlugin(dir, out, {
21
+ watch: true,
22
+ onBuild: (err) => console.log(err ? `✗ ${err.message}` : `✓ ${new Date().toLocaleTimeString()} built into ${out}`),
23
+ })
24
+ console.log(`Watching ${manifest.name}. Turn it on in Bläck Studio: Settings → Plugins.`)
25
+ } else {
26
+ console.error('Use: black-plugin build | black-plugin dev')
27
+ process.exit(1)
28
+ }