@shiqianjiang/ceru-plugin-cli 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.
Files changed (314) hide show
  1. package/ASSET_LICENSE +238 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE +1 -0
  4. package/README.md +29 -0
  5. package/assets/catalog.json +1 -0
  6. package/assets/electron-app/main.cjs +1 -0
  7. package/assets/electron-app/package.json +8 -0
  8. package/assets/electron.cjs +42 -0
  9. package/assets/index.html +93 -0
  10. package/assets/originals/account.svg +1 -0
  11. package/assets/originals/add.svg +1 -0
  12. package/assets/originals/app.logo.svg +10 -0
  13. package/assets/originals/chart.svg +1 -0
  14. package/assets/originals/close.svg +1 -0
  15. package/assets/originals/cloud.svg +1 -0
  16. package/assets/originals/download.svg +1 -0
  17. package/assets/originals/heart.svg +1 -0
  18. package/assets/originals/import.svg +1 -0
  19. package/assets/originals/info.svg +1 -0
  20. package/assets/originals/library.svg +1 -0
  21. package/assets/originals/music-note.svg +1 -0
  22. package/assets/originals/pause.svg +1 -0
  23. package/assets/originals/placeholder.avatar.webp +0 -0
  24. package/assets/originals/placeholder.cover.png +0 -0
  25. package/assets/originals/platform.all.svg +1 -0
  26. package/assets/originals/platform.kg.svg +1 -0
  27. package/assets/originals/platform.kw.svg +1 -0
  28. package/assets/originals/platform.local.svg +1 -0
  29. package/assets/originals/platform.mg.svg +1 -0
  30. package/assets/originals/platform.tx.svg +1 -0
  31. package/assets/originals/platform.wy.svg +1 -0
  32. package/assets/originals/play.svg +1 -0
  33. package/assets/originals/refresh.svg +1 -0
  34. package/assets/originals/search.svg +1 -0
  35. package/assets/originals/server.svg +1 -0
  36. package/assets/originals/settings.svg +1 -0
  37. package/assets/originals/share.svg +1 -0
  38. package/assets/playground.js +446 -0
  39. package/assets/sandbox.js +36 -0
  40. package/assets/shared-react.js +77 -0
  41. package/assets/shared-vue.js +44 -0
  42. package/assets/style.css +264 -0
  43. package/assets/vendor-licenses/lodash-es.txt +47 -0
  44. package/assets/vendor-licenses/react-dom.txt +21 -0
  45. package/assets/vendor-licenses/react.txt +21 -0
  46. package/assets/vendor-licenses/scheduler.txt +21 -0
  47. package/assets/vendor-licenses/vue.txt +21 -0
  48. package/assets/vendor-licenses/vue_reactivity.txt +21 -0
  49. package/assets/vendor-licenses/vue_runtime-core.txt +21 -0
  50. package/assets/vendor-licenses/vue_runtime-dom.txt +21 -0
  51. package/assets/vendor-licenses/vue_shared.txt +21 -0
  52. package/dist/bin.d.ts +2 -0
  53. package/dist/bin.js +6 -0
  54. package/dist/dev.d.ts +7 -0
  55. package/dist/dev.js +507 -0
  56. package/dist/frameworks.d.ts +9 -0
  57. package/dist/frameworks.js +310 -0
  58. package/dist/index.d.ts +2 -0
  59. package/dist/index.js +206 -0
  60. package/dist/project.d.ts +34 -0
  61. package/dist/project.js +342 -0
  62. package/package.json +67 -0
  63. package/schemas/config.schema.json +610 -0
  64. package/templates/connected-library/js/.prettierrc.json +5 -0
  65. package/templates/connected-library/js/.vscode/ceru.code-snippets +27 -0
  66. package/templates/connected-library/js/.vscode/extensions.json +3 -0
  67. package/templates/connected-library/js/.vscode/launch.json +30 -0
  68. package/templates/connected-library/js/.vscode/settings.json +20 -0
  69. package/templates/connected-library/js/.vscode/tasks.json +36 -0
  70. package/templates/connected-library/js/README.md +38 -0
  71. package/templates/connected-library/js/_gitignore +6 -0
  72. package/templates/connected-library/js/ceru.plugin.json +76 -0
  73. package/templates/connected-library/js/package.json +21 -0
  74. package/templates/connected-library/js/src/index.js +38 -0
  75. package/templates/connected-library/js/tsconfig.json +16 -0
  76. package/templates/connected-library/js/ui/settings.json +24 -0
  77. package/templates/connected-library/ts/.prettierrc.json +5 -0
  78. package/templates/connected-library/ts/.vscode/ceru.code-snippets +27 -0
  79. package/templates/connected-library/ts/.vscode/extensions.json +3 -0
  80. package/templates/connected-library/ts/.vscode/launch.json +30 -0
  81. package/templates/connected-library/ts/.vscode/settings.json +20 -0
  82. package/templates/connected-library/ts/.vscode/tasks.json +36 -0
  83. package/templates/connected-library/ts/README.md +38 -0
  84. package/templates/connected-library/ts/_gitignore +6 -0
  85. package/templates/connected-library/ts/ceru.plugin.json +76 -0
  86. package/templates/connected-library/ts/package.json +21 -0
  87. package/templates/connected-library/ts/src/index.ts +38 -0
  88. package/templates/connected-library/ts/tsconfig.json +16 -0
  89. package/templates/connected-library/ts/ui/settings.json +24 -0
  90. package/templates/guest-adapter/js/.prettierrc.json +5 -0
  91. package/templates/guest-adapter/js/.vscode/ceru.code-snippets +27 -0
  92. package/templates/guest-adapter/js/.vscode/extensions.json +3 -0
  93. package/templates/guest-adapter/js/.vscode/launch.json +30 -0
  94. package/templates/guest-adapter/js/.vscode/settings.json +20 -0
  95. package/templates/guest-adapter/js/.vscode/tasks.json +36 -0
  96. package/templates/guest-adapter/js/README.md +38 -0
  97. package/templates/guest-adapter/js/_gitignore +6 -0
  98. package/templates/guest-adapter/js/ceru.plugin.json +95 -0
  99. package/templates/guest-adapter/js/package.json +21 -0
  100. package/templates/guest-adapter/js/src/guest.js +6 -0
  101. package/templates/guest-adapter/js/src/index.js +8 -0
  102. package/templates/guest-adapter/js/tsconfig.json +16 -0
  103. package/templates/guest-adapter/js/ui/settings.json +14 -0
  104. package/templates/guest-adapter/ts/.prettierrc.json +5 -0
  105. package/templates/guest-adapter/ts/.vscode/ceru.code-snippets +27 -0
  106. package/templates/guest-adapter/ts/.vscode/extensions.json +3 -0
  107. package/templates/guest-adapter/ts/.vscode/launch.json +30 -0
  108. package/templates/guest-adapter/ts/.vscode/settings.json +20 -0
  109. package/templates/guest-adapter/ts/.vscode/tasks.json +36 -0
  110. package/templates/guest-adapter/ts/README.md +38 -0
  111. package/templates/guest-adapter/ts/_gitignore +6 -0
  112. package/templates/guest-adapter/ts/ceru.plugin.json +95 -0
  113. package/templates/guest-adapter/ts/package.json +21 -0
  114. package/templates/guest-adapter/ts/src/guest.ts +6 -0
  115. package/templates/guest-adapter/ts/src/index.ts +8 -0
  116. package/templates/guest-adapter/ts/tsconfig.json +16 -0
  117. package/templates/guest-adapter/ts/ui/settings.json +14 -0
  118. package/templates/importer/js/.prettierrc.json +5 -0
  119. package/templates/importer/js/.vscode/ceru.code-snippets +27 -0
  120. package/templates/importer/js/.vscode/extensions.json +3 -0
  121. package/templates/importer/js/.vscode/launch.json +30 -0
  122. package/templates/importer/js/.vscode/settings.json +20 -0
  123. package/templates/importer/js/.vscode/tasks.json +36 -0
  124. package/templates/importer/js/README.md +38 -0
  125. package/templates/importer/js/_gitignore +6 -0
  126. package/templates/importer/js/ceru.plugin.json +63 -0
  127. package/templates/importer/js/package.json +21 -0
  128. package/templates/importer/js/src/index.js +37 -0
  129. package/templates/importer/js/tsconfig.json +16 -0
  130. package/templates/importer/js/ui/settings.json +24 -0
  131. package/templates/importer/ts/.prettierrc.json +5 -0
  132. package/templates/importer/ts/.vscode/ceru.code-snippets +27 -0
  133. package/templates/importer/ts/.vscode/extensions.json +3 -0
  134. package/templates/importer/ts/.vscode/launch.json +30 -0
  135. package/templates/importer/ts/.vscode/settings.json +20 -0
  136. package/templates/importer/ts/.vscode/tasks.json +36 -0
  137. package/templates/importer/ts/README.md +38 -0
  138. package/templates/importer/ts/_gitignore +6 -0
  139. package/templates/importer/ts/ceru.plugin.json +63 -0
  140. package/templates/importer/ts/package.json +21 -0
  141. package/templates/importer/ts/src/index.ts +37 -0
  142. package/templates/importer/ts/tsconfig.json +16 -0
  143. package/templates/importer/ts/ui/settings.json +24 -0
  144. package/templates/react/js/.prettierrc.json +5 -0
  145. package/templates/react/js/.vscode/ceru.code-snippets +27 -0
  146. package/templates/react/js/.vscode/extensions.json +3 -0
  147. package/templates/react/js/.vscode/launch.json +30 -0
  148. package/templates/react/js/.vscode/settings.json +20 -0
  149. package/templates/react/js/.vscode/tasks.json +36 -0
  150. package/templates/react/js/README.md +36 -0
  151. package/templates/react/js/_gitignore +6 -0
  152. package/templates/react/js/ceru.plugin.json +60 -0
  153. package/templates/react/js/package.json +25 -0
  154. package/templates/react/js/src/App.jsx +22 -0
  155. package/templates/react/js/src/index.js +7 -0
  156. package/templates/react/js/src/view.jsx +8 -0
  157. package/templates/react/js/tsconfig.json +16 -0
  158. package/templates/react/ts/.prettierrc.json +5 -0
  159. package/templates/react/ts/.vscode/ceru.code-snippets +27 -0
  160. package/templates/react/ts/.vscode/extensions.json +3 -0
  161. package/templates/react/ts/.vscode/launch.json +30 -0
  162. package/templates/react/ts/.vscode/settings.json +20 -0
  163. package/templates/react/ts/.vscode/tasks.json +36 -0
  164. package/templates/react/ts/README.md +36 -0
  165. package/templates/react/ts/_gitignore +6 -0
  166. package/templates/react/ts/ceru.plugin.json +60 -0
  167. package/templates/react/ts/package.json +25 -0
  168. package/templates/react/ts/src/App.tsx +23 -0
  169. package/templates/react/ts/src/index.ts +7 -0
  170. package/templates/react/ts/src/view.tsx +8 -0
  171. package/templates/react/ts/tsconfig.json +16 -0
  172. package/templates/snapshot.json +257 -0
  173. package/templates/source/js/.prettierrc.json +5 -0
  174. package/templates/source/js/.vscode/ceru.code-snippets +27 -0
  175. package/templates/source/js/.vscode/extensions.json +3 -0
  176. package/templates/source/js/.vscode/launch.json +30 -0
  177. package/templates/source/js/.vscode/settings.json +20 -0
  178. package/templates/source/js/.vscode/tasks.json +36 -0
  179. package/templates/source/js/README.md +38 -0
  180. package/templates/source/js/_gitignore +6 -0
  181. package/templates/source/js/ceru.plugin.json +51 -0
  182. package/templates/source/js/package.json +21 -0
  183. package/templates/source/js/src/index.js +50 -0
  184. package/templates/source/js/tsconfig.json +16 -0
  185. package/templates/source/ts/.prettierrc.json +5 -0
  186. package/templates/source/ts/.vscode/ceru.code-snippets +27 -0
  187. package/templates/source/ts/.vscode/extensions.json +3 -0
  188. package/templates/source/ts/.vscode/launch.json +30 -0
  189. package/templates/source/ts/.vscode/settings.json +20 -0
  190. package/templates/source/ts/.vscode/tasks.json +36 -0
  191. package/templates/source/ts/README.md +38 -0
  192. package/templates/source/ts/_gitignore +6 -0
  193. package/templates/source/ts/ceru.plugin.json +51 -0
  194. package/templates/source/ts/package.json +21 -0
  195. package/templates/source/ts/src/index.ts +50 -0
  196. package/templates/source/ts/tsconfig.json +16 -0
  197. package/templates/vue/js/.prettierrc.json +5 -0
  198. package/templates/vue/js/.vscode/ceru.code-snippets +27 -0
  199. package/templates/vue/js/.vscode/extensions.json +3 -0
  200. package/templates/vue/js/.vscode/launch.json +30 -0
  201. package/templates/vue/js/.vscode/settings.json +20 -0
  202. package/templates/vue/js/.vscode/tasks.json +36 -0
  203. package/templates/vue/js/README.md +36 -0
  204. package/templates/vue/js/_gitignore +6 -0
  205. package/templates/vue/js/ceru.plugin.json +59 -0
  206. package/templates/vue/js/package.json +22 -0
  207. package/templates/vue/js/src/App.vue +31 -0
  208. package/templates/vue/js/src/env.d.ts +5 -0
  209. package/templates/vue/js/src/index.js +7 -0
  210. package/templates/vue/js/src/view.js +8 -0
  211. package/templates/vue/js/tsconfig.json +17 -0
  212. package/templates/vue/ts/.prettierrc.json +5 -0
  213. package/templates/vue/ts/.vscode/ceru.code-snippets +27 -0
  214. package/templates/vue/ts/.vscode/extensions.json +3 -0
  215. package/templates/vue/ts/.vscode/launch.json +30 -0
  216. package/templates/vue/ts/.vscode/settings.json +20 -0
  217. package/templates/vue/ts/.vscode/tasks.json +36 -0
  218. package/templates/vue/ts/README.md +36 -0
  219. package/templates/vue/ts/_gitignore +6 -0
  220. package/templates/vue/ts/ceru.plugin.json +59 -0
  221. package/templates/vue/ts/package.json +22 -0
  222. package/templates/vue/ts/src/App.vue +32 -0
  223. package/templates/vue/ts/src/env.d.ts +5 -0
  224. package/templates/vue/ts/src/index.ts +7 -0
  225. package/templates/vue/ts/src/view.ts +8 -0
  226. package/templates/vue/ts/tsconfig.json +17 -0
  227. package/templates/vue-tsx/js/.prettierrc.json +5 -0
  228. package/templates/vue-tsx/js/.vscode/ceru.code-snippets +27 -0
  229. package/templates/vue-tsx/js/.vscode/extensions.json +3 -0
  230. package/templates/vue-tsx/js/.vscode/launch.json +30 -0
  231. package/templates/vue-tsx/js/.vscode/settings.json +20 -0
  232. package/templates/vue-tsx/js/.vscode/tasks.json +36 -0
  233. package/templates/vue-tsx/js/README.md +36 -0
  234. package/templates/vue-tsx/js/_gitignore +6 -0
  235. package/templates/vue-tsx/js/ceru.plugin.json +59 -0
  236. package/templates/vue-tsx/js/package.json +22 -0
  237. package/templates/vue-tsx/js/src/App.jsx +14 -0
  238. package/templates/vue-tsx/js/src/env.d.ts +5 -0
  239. package/templates/vue-tsx/js/src/index.js +7 -0
  240. package/templates/vue-tsx/js/src/view.js +8 -0
  241. package/templates/vue-tsx/js/tsconfig.json +17 -0
  242. package/templates/vue-tsx/ts/.prettierrc.json +5 -0
  243. package/templates/vue-tsx/ts/.vscode/ceru.code-snippets +27 -0
  244. package/templates/vue-tsx/ts/.vscode/extensions.json +3 -0
  245. package/templates/vue-tsx/ts/.vscode/launch.json +30 -0
  246. package/templates/vue-tsx/ts/.vscode/settings.json +20 -0
  247. package/templates/vue-tsx/ts/.vscode/tasks.json +36 -0
  248. package/templates/vue-tsx/ts/README.md +36 -0
  249. package/templates/vue-tsx/ts/_gitignore +6 -0
  250. package/templates/vue-tsx/ts/ceru.plugin.json +59 -0
  251. package/templates/vue-tsx/ts/package.json +22 -0
  252. package/templates/vue-tsx/ts/src/App.tsx +15 -0
  253. package/templates/vue-tsx/ts/src/env.d.ts +5 -0
  254. package/templates/vue-tsx/ts/src/index.ts +7 -0
  255. package/templates/vue-tsx/ts/src/view.ts +8 -0
  256. package/templates/vue-tsx/ts/tsconfig.json +17 -0
  257. package/templates/web-dist/js/.prettierrc.json +5 -0
  258. package/templates/web-dist/js/.vscode/ceru.code-snippets +27 -0
  259. package/templates/web-dist/js/.vscode/extensions.json +3 -0
  260. package/templates/web-dist/js/.vscode/launch.json +30 -0
  261. package/templates/web-dist/js/.vscode/settings.json +20 -0
  262. package/templates/web-dist/js/.vscode/tasks.json +36 -0
  263. package/templates/web-dist/js/README.md +38 -0
  264. package/templates/web-dist/js/_gitignore +6 -0
  265. package/templates/web-dist/js/ceru.plugin.json +57 -0
  266. package/templates/web-dist/js/package.json +21 -0
  267. package/templates/web-dist/js/src/index.js +7 -0
  268. package/templates/web-dist/js/tsconfig.json +16 -0
  269. package/templates/web-dist/js/web-dist/index.html +14 -0
  270. package/templates/web-dist/js/web-dist/main.js +4 -0
  271. package/templates/web-dist/js/web-dist/sample.png +0 -0
  272. package/templates/web-dist/js/web-dist/style.css +11 -0
  273. package/templates/web-dist/ts/.prettierrc.json +5 -0
  274. package/templates/web-dist/ts/.vscode/ceru.code-snippets +27 -0
  275. package/templates/web-dist/ts/.vscode/extensions.json +3 -0
  276. package/templates/web-dist/ts/.vscode/launch.json +30 -0
  277. package/templates/web-dist/ts/.vscode/settings.json +20 -0
  278. package/templates/web-dist/ts/.vscode/tasks.json +36 -0
  279. package/templates/web-dist/ts/README.md +38 -0
  280. package/templates/web-dist/ts/_gitignore +6 -0
  281. package/templates/web-dist/ts/ceru.plugin.json +57 -0
  282. package/templates/web-dist/ts/package.json +21 -0
  283. package/templates/web-dist/ts/src/index.ts +7 -0
  284. package/templates/web-dist/ts/tsconfig.json +16 -0
  285. package/templates/web-dist/ts/web-dist/index.html +14 -0
  286. package/templates/web-dist/ts/web-dist/main.js +4 -0
  287. package/templates/web-dist/ts/web-dist/sample.png +0 -0
  288. package/templates/web-dist/ts/web-dist/style.css +11 -0
  289. package/templates/web-surface/js/.prettierrc.json +5 -0
  290. package/templates/web-surface/js/.vscode/ceru.code-snippets +27 -0
  291. package/templates/web-surface/js/.vscode/extensions.json +3 -0
  292. package/templates/web-surface/js/.vscode/launch.json +30 -0
  293. package/templates/web-surface/js/.vscode/settings.json +20 -0
  294. package/templates/web-surface/js/.vscode/tasks.json +36 -0
  295. package/templates/web-surface/js/README.md +36 -0
  296. package/templates/web-surface/js/_gitignore +6 -0
  297. package/templates/web-surface/js/ceru.plugin.json +55 -0
  298. package/templates/web-surface/js/package.json +21 -0
  299. package/templates/web-surface/js/src/index.js +7 -0
  300. package/templates/web-surface/js/src/view.js +8 -0
  301. package/templates/web-surface/js/tsconfig.json +16 -0
  302. package/templates/web-surface/ts/.prettierrc.json +5 -0
  303. package/templates/web-surface/ts/.vscode/ceru.code-snippets +27 -0
  304. package/templates/web-surface/ts/.vscode/extensions.json +3 -0
  305. package/templates/web-surface/ts/.vscode/launch.json +30 -0
  306. package/templates/web-surface/ts/.vscode/settings.json +20 -0
  307. package/templates/web-surface/ts/.vscode/tasks.json +36 -0
  308. package/templates/web-surface/ts/README.md +36 -0
  309. package/templates/web-surface/ts/_gitignore +6 -0
  310. package/templates/web-surface/ts/ceru.plugin.json +55 -0
  311. package/templates/web-surface/ts/package.json +21 -0
  312. package/templates/web-surface/ts/src/index.ts +7 -0
  313. package/templates/web-surface/ts/src/view.ts +8 -0
  314. package/templates/web-surface/ts/tsconfig.json +16 -0
@@ -0,0 +1,310 @@
1
+ import { readFile, realpath } from 'node:fs/promises';
2
+ import { dirname, resolve, relative, isAbsolute, sep, extname } from 'node:path';
3
+ import { createRequire } from 'node:module';
4
+ import { createHash } from 'node:crypto';
5
+ import { build } from 'esbuild';
6
+ import { parse as parseSfc, compileScript, compileStyleAsync } from '@vue/compiler-sfc';
7
+ import { transformAsync } from '@babel/core';
8
+ import vueJsx from '@vue/babel-plugin-jsx';
9
+ import { parse as parseHtml, serialize } from 'parse5';
10
+ import { parse as parseJs } from 'acorn';
11
+ export const SHARED_IMPORTS = {
12
+ vue: 'vue',
13
+ 'vue/jsx-runtime': 'vue',
14
+ react: 'react',
15
+ 'react/jsx-runtime': 'react',
16
+ 'react/jsx-dev-runtime': 'react',
17
+ 'react-dom': 'react-dom',
18
+ 'react-dom/client': 'react-dom',
19
+ };
20
+ const mime = {
21
+ '.png': 'image/png',
22
+ '.jpg': 'image/jpeg',
23
+ '.jpeg': 'image/jpeg',
24
+ '.svg': 'image/svg+xml',
25
+ '.webp': 'image/webp',
26
+ '.gif': 'image/gif',
27
+ '.ico': 'image/x-icon',
28
+ '.woff': 'font/woff',
29
+ '.woff2': 'font/woff2',
30
+ '.ttf': 'font/ttf',
31
+ };
32
+ const loaders = Object.fromEntries(Object.keys(mime).map((key) => [key, 'dataurl']));
33
+ function styleCode(css, id) {
34
+ return ('\nif (typeof document !== "undefined" && !document.querySelector(' +
35
+ JSON.stringify('style[data-ceru-style="' + id + '"]') +
36
+ ')) { const s=document.createElement("style");s.dataset.ceruStyle=' +
37
+ JSON.stringify(id) +
38
+ ';s.textContent=' +
39
+ JSON.stringify(css) +
40
+ ';document.head.append(s); }\n');
41
+ }
42
+ async function cssBundle(css, directory) {
43
+ const result = await build({
44
+ stdin: { contents: css, loader: 'css', resolveDir: directory },
45
+ bundle: true,
46
+ write: false,
47
+ outfile: 'style.css',
48
+ loader: loaders,
49
+ logLevel: 'silent',
50
+ plugins: [
51
+ {
52
+ name: 'local-css-only',
53
+ setup(ctx) {
54
+ ctx.onResolve({ filter: /^https?:\/\// }, () => ({
55
+ errors: [
56
+ { text: 'Remote CSS resources must be downloaded and included at build time' },
57
+ ],
58
+ }));
59
+ },
60
+ },
61
+ ],
62
+ });
63
+ if (result.outputFiles.length !== 1)
64
+ throw new Error('CSS emitted an external resource');
65
+ return result.outputFiles[0].text;
66
+ }
67
+ export function sharedPlugin(libraries = {}) {
68
+ if (!!libraries.react !== !!libraries['react-dom'])
69
+ throw new Error('Share react and react-dom together to avoid duplicate React runtimes');
70
+ return {
71
+ name: 'ceru-host-libraries',
72
+ setup(ctx) {
73
+ ctx.onResolve({ filter: /^(vue(?:\/.*)?|react(?:\/.*)?|react-dom(?:\/.*)?)$/ }, (args) => {
74
+ const root = SHARED_IMPORTS[args.path];
75
+ const family = args.path.startsWith('vue')
76
+ ? 'vue'
77
+ : args.path.startsWith('react-dom')
78
+ ? 'react-dom'
79
+ : 'react';
80
+ if (!libraries[family])
81
+ return;
82
+ if (!root)
83
+ return { errors: [{ text: 'Unsupported shared runtime import: ' + args.path }] };
84
+ return { path: args.path, namespace: 'ceru-host-library' };
85
+ });
86
+ ctx.onLoad({ filter: /.*/, namespace: 'ceru-host-library' }, (args) => ({
87
+ contents: 'module.exports = globalThis.__ceruSharedRequire(' + JSON.stringify(args.path) + ');',
88
+ loader: 'js',
89
+ }));
90
+ },
91
+ };
92
+ }
93
+ export function vuePlugin(root, framework) {
94
+ return {
95
+ name: 'ceru-vue',
96
+ setup(ctx) {
97
+ ctx.onLoad({ filter: /\.vue$/ }, async (args) => {
98
+ const original = await readFile(args.path, 'utf8');
99
+ let parsed = parseSfc(original, { filename: args.path, sourceMap: true });
100
+ if (parsed.errors.length)
101
+ throw new Error(parsed.errors.map(String).join('\n'));
102
+ if (!parsed.descriptor.script && !parsed.descriptor.scriptSetup)
103
+ parsed = parseSfc('<script>export default {}</script>\n' + original, {
104
+ filename: args.path,
105
+ });
106
+ const descriptor = parsed.descriptor;
107
+ if (descriptor.script?.src ||
108
+ descriptor.template?.src ||
109
+ descriptor.styles.some((s) => s.src))
110
+ throw new Error('Use local imports instead of SFC src attributes');
111
+ const id = createHash('sha256').update(original).digest('hex').slice(0, 12);
112
+ const compiled = compileScript(descriptor, {
113
+ id,
114
+ inlineTemplate: true,
115
+ genDefaultAs: '__ceru_component',
116
+ });
117
+ let contents = compiled.content;
118
+ for (const style of descriptor.styles) {
119
+ const result = await compileStyleAsync({
120
+ filename: args.path,
121
+ source: style.content,
122
+ id: 'data-v-' + id,
123
+ scoped: style.scoped,
124
+ preprocessLang: style.lang,
125
+ preprocessCustomRequire: createRequire(resolve(root, 'package.json')),
126
+ });
127
+ if (result.errors.length)
128
+ throw new Error(result.errors.map(String).join('\n'));
129
+ contents += styleCode(await cssBundle(result.code, dirname(args.path)), id + String(descriptor.styles.indexOf(style)));
130
+ }
131
+ if (descriptor.styles.some((s) => s.scoped))
132
+ contents += '\n__ceru_component.__scopeId = ' + JSON.stringify('data-v-' + id) + ';';
133
+ contents += '\nexport default __ceru_component;\n';
134
+ if (compiled.map)
135
+ contents +=
136
+ '//# sourceMappingURL=data:application/json;base64,' +
137
+ Buffer.from(JSON.stringify(compiled.map)).toString('base64');
138
+ return {
139
+ contents,
140
+ loader: descriptor.scriptSetup?.lang === 'tsx' || descriptor.script?.lang === 'tsx'
141
+ ? 'tsx'
142
+ : 'ts',
143
+ resolveDir: dirname(args.path),
144
+ };
145
+ });
146
+ if (framework === 'vue')
147
+ ctx.onLoad({ filter: /\.[jt]sx$/ }, async (args) => {
148
+ const source = await readFile(args.path, 'utf8');
149
+ const result = await transformAsync(source, {
150
+ filename: args.path,
151
+ babelrc: false,
152
+ configFile: false,
153
+ sourceMaps: 'inline',
154
+ parserOpts: {
155
+ plugins: ['jsx', ...(args.path.endsWith('.tsx') ? ['typescript'] : [])],
156
+ },
157
+ plugins: [vueJsx.default ?? vueJsx],
158
+ });
159
+ return { contents: result?.code ?? source, loader: 'ts', resolveDir: dirname(args.path) };
160
+ });
161
+ },
162
+ };
163
+ }
164
+ async function localFile(directory, base, input) {
165
+ if (/^(?:[a-z]+:|\/\/|#)/i.test(input))
166
+ throw new Error('External/ambiguous web-dist resource: ' + input);
167
+ const clean = decodeURIComponent(input.split(/[?#]/)[0]);
168
+ const path = await realpath(clean.startsWith('/') ? resolve(directory, '.' + clean) : resolve(base, clean));
169
+ const rel = relative(directory, path);
170
+ if (rel === '..' || rel.startsWith('..' + sep) || isAbsolute(rel))
171
+ throw new Error('web-dist resource escapes its directory');
172
+ return path;
173
+ }
174
+ async function dataUrl(path) {
175
+ const bytes = await readFile(path);
176
+ if (bytes.length > 8 * 1024 * 1024)
177
+ throw new Error('web-dist asset is too large');
178
+ const type = mime[extname(path).toLowerCase()];
179
+ if (!type)
180
+ throw new Error('Unsupported static asset: ' + path);
181
+ return 'data:' + type + ';base64,' + bytes.toString('base64');
182
+ }
183
+ /** Convert a local, already-built SPA into a Surface entry. No uploaded HTML is executed by the CLI. */
184
+ export async function webDistEntry(directoryInput) {
185
+ const directory = await realpath(directoryInput);
186
+ const document = parseHtml(await readFile(resolve(directory, 'index.html'), 'utf8'));
187
+ const scripts = [];
188
+ const styles = [];
189
+ let body;
190
+ async function walk(node) {
191
+ if (node.tagName === 'body')
192
+ body = node;
193
+ const next = [];
194
+ for (const child of node.childNodes ?? []) {
195
+ const attrs = Object.fromEntries((child.attrs ?? []).map((a) => [a.name, a.value]));
196
+ if (['base', 'iframe', 'object', 'embed'].includes(child.tagName))
197
+ throw new Error('Unsupported web-dist element: ' + child.tagName);
198
+ if (Object.keys(attrs).some((key) => /^on/i.test(key)))
199
+ throw new Error('Use JS event listeners instead of inline HTML event handlers');
200
+ if (child.tagName === 'script') {
201
+ if (attrs.type && !['module', 'text/javascript'].includes(attrs.type))
202
+ continue;
203
+ scripts.push(attrs.src
204
+ ? { file: await localFile(directory, directory, attrs.src) }
205
+ : { code: (child.childNodes ?? []).map((n) => n.value ?? '').join('') });
206
+ continue;
207
+ }
208
+ if (child.tagName === 'link') {
209
+ if (attrs.rel === 'stylesheet') {
210
+ const path = await localFile(directory, directory, attrs.href);
211
+ styles.push(await cssBundle(await readFile(path, 'utf8'), dirname(path)));
212
+ }
213
+ continue;
214
+ }
215
+ if (child.tagName === 'style') {
216
+ styles.push(await cssBundle((child.childNodes ?? []).map((n) => n.value ?? '').join(''), directory));
217
+ continue;
218
+ }
219
+ for (const attr of child.attrs ?? []) {
220
+ if (['src', 'poster'].includes(attr.name) && !attr.value.startsWith('data:'))
221
+ attr.value = await dataUrl(await localFile(directory, directory, attr.value));
222
+ if (attr.name === 'srcset')
223
+ throw new Error('Inline srcset assets in the web build before packing');
224
+ }
225
+ await walk(child);
226
+ next.push(child);
227
+ }
228
+ node.childNodes = next;
229
+ }
230
+ await walk(document);
231
+ if (!body)
232
+ throw new Error('web-dist/index.html must have a body');
233
+ const html = serialize(body);
234
+ const entry = 'ceru-web-dist-entry';
235
+ const plugin = {
236
+ name: 'ceru-web-dist',
237
+ setup(ctx) {
238
+ ctx.onResolve({ filter: /^ceru-web-dist-/ }, (args) => ({
239
+ path: args.path,
240
+ namespace: 'ceru-web-dist',
241
+ }));
242
+ ctx.onLoad({ filter: /.*/, namespace: 'ceru-web-dist' }, (args) => {
243
+ if (args.path === entry)
244
+ return {
245
+ contents: 'export default async function(ctx) { globalThis.ceru = ctx; ctx.root.innerHTML = ' +
246
+ JSON.stringify(html) +
247
+ ';\n' +
248
+ styleCode(styles.join('\n'), 'web-dist') +
249
+ '\n' +
250
+ scripts
251
+ .map((_script, i) => 'await import("ceru-web-dist-script-' + i + '");')
252
+ .join('\n') +
253
+ '\nreturn () => { ctx.root.replaceChildren(); }; }',
254
+ loader: 'js',
255
+ resolveDir: directory,
256
+ };
257
+ const index = Number(args.path.slice('ceru-web-dist-script-'.length));
258
+ const script = scripts[index];
259
+ if (!script)
260
+ throw new Error('Unknown web-dist script');
261
+ return script.file
262
+ ? {
263
+ contents: 'import ' + JSON.stringify(script.file) + ';',
264
+ loader: 'js',
265
+ resolveDir: directory,
266
+ }
267
+ : { contents: script.code ?? '', loader: 'js', resolveDir: directory };
268
+ });
269
+ ctx.onLoad({ filter: /\.[cm]?js$/ }, async (args) => {
270
+ const rel = relative(directory, args.path);
271
+ if (rel.startsWith('..') || isAbsolute(rel))
272
+ return;
273
+ let source = await readFile(args.path, 'utf8');
274
+ const tree = parseJs(source, { ecmaVersion: 'latest', sourceType: 'module' });
275
+ const replacements = [];
276
+ const stack = [tree];
277
+ while (stack.length) {
278
+ const node = stack.pop();
279
+ if (node.type === 'Literal' &&
280
+ typeof node.value === 'string' &&
281
+ mime[extname(node.value.split(/[?#]/)[0]).toLowerCase()] &&
282
+ !/^(?:[a-z]+:|\/\/)/i.test(node.value)) {
283
+ try {
284
+ replacements.push({
285
+ start: node.start,
286
+ end: node.end,
287
+ text: JSON.stringify(await dataUrl(await localFile(directory, dirname(args.path), node.value))),
288
+ });
289
+ }
290
+ catch { }
291
+ }
292
+ for (const value of Object.values(node)) {
293
+ if (Array.isArray(value))
294
+ for (const item of value) {
295
+ if (item && typeof item === 'object' && 'type' in item)
296
+ stack.push(item);
297
+ }
298
+ else if (value && typeof value === 'object' && 'type' in value)
299
+ stack.push(value);
300
+ }
301
+ }
302
+ for (const replacement of replacements.sort((a, b) => b.start - a.start))
303
+ source =
304
+ source.slice(0, replacement.start) + replacement.text + source.slice(replacement.end);
305
+ return { contents: source, loader: 'js', resolveDir: dirname(args.path) };
306
+ });
307
+ },
308
+ };
309
+ return { plugin, entry };
310
+ }
@@ -0,0 +1,2 @@
1
+ export { buildProject, loadProject, scaffoldProject, TEMPLATES } from './project.js';
2
+ export declare function runCli(argv?: string[]): Promise<void>;
package/dist/index.js ADDED
@@ -0,0 +1,206 @@
1
+ import { parseArgs } from 'node:util';
2
+ import { readFile, stat } from 'node:fs/promises';
3
+ import { resolve, extname } from 'node:path';
4
+ import { createInterface } from 'node:readline/promises';
5
+ import { createTemplate, generateSigningKeys, parseJsonStrict, PreparedIssuer, readArtifact, signArtifact, } from '@shiqianjiang/ceru-plugin-issuer';
6
+ import { buildProject, scaffoldProject, TEMPLATES, writeAtomic } from './project.js';
7
+ import { runDev } from './dev.js';
8
+ export { buildProject, loadProject, scaffoldProject, TEMPLATES } from './project.js';
9
+ const VERSION = '0.1.0';
10
+ const HELP = 'Ceru Plugin CLI ' +
11
+ VERSION +
12
+ '\n\n' +
13
+ ' init <directory> [--template ' +
14
+ TEMPLATES.join('|') +
15
+ '] [--lang ts|js]\n' +
16
+ ' list-templates\n' +
17
+ ' build [--project directory] [--out dist/plugin.js]\n' +
18
+ ' dev [--project directory] [--port 4179] [--debug-port 9223] [--no-open]\n' +
19
+ ' validate <plugin.js> [--trusted-key publisher.public.pem] [--json]\n' +
20
+ ' keygen --out .keys/publisher\n' +
21
+ ' sign <plugin.js> --key private.pem [--out signed.js] [--force]\n' +
22
+ ' template <plugin.js> --key publisher.private.pem --issuer-key issuer.public.pem --out template.js [--policy policy.json]\n' +
23
+ ' issue <template.js> --key issuer.private.pem --config delivery.json --out customer.js [--id delivery-id]\n\n' +
24
+ 'TypeScript is type-checked before build. Final output is one .js file.\n' +
25
+ 'v2 Host integration is required. Validation does not execute or sandbox plugin code.\n';
26
+ async function jsOutput(path, bytes, force) {
27
+ if (!['.js', '.jsx'].includes(extname(path)))
28
+ throw new Error('Output must be .js or .jsx containing compiled JavaScript');
29
+ await writeAtomic(resolve(path), bytes, force);
30
+ console.log('Wrote ' + resolve(path) + ' (' + bytes.length + ' bytes)');
31
+ }
32
+ export async function runCli(argv = process.argv.slice(2)) {
33
+ const { values: flags, positionals } = parseArgs({
34
+ args: argv,
35
+ allowPositionals: true,
36
+ options: {
37
+ help: { type: 'boolean', short: 'h' },
38
+ version: { type: 'boolean', short: 'v' },
39
+ template: { type: 'string' },
40
+ lang: { type: 'string' },
41
+ project: { type: 'string' },
42
+ out: { type: 'string' },
43
+ port: { type: 'string' },
44
+ 'debug-port': { type: 'string' },
45
+ 'no-open': { type: 'boolean' },
46
+ hidden: { type: 'boolean' },
47
+ electron: { type: 'string' },
48
+ key: { type: 'string' },
49
+ 'issuer-key': { type: 'string', multiple: true },
50
+ 'trusted-key': { type: 'string', multiple: true },
51
+ policy: { type: 'string' },
52
+ config: { type: 'string' },
53
+ id: { type: 'string' },
54
+ force: { type: 'boolean', default: false },
55
+ json: { type: 'boolean', default: false },
56
+ },
57
+ });
58
+ if (flags.version) {
59
+ console.log(VERSION);
60
+ return;
61
+ }
62
+ const command = positionals[0];
63
+ if (flags.help || !command) {
64
+ console.log(HELP);
65
+ return;
66
+ }
67
+ if (command === 'list-templates') {
68
+ console.log(TEMPLATES.join('\n'));
69
+ return;
70
+ }
71
+ if (positionals.length > 2)
72
+ throw new Error('Unexpected arguments: ' + positionals.slice(2).join(' '));
73
+ const required = (name) => {
74
+ const v = flags[name];
75
+ if (!v)
76
+ throw new Error('--' + name + ' is required');
77
+ return v;
78
+ };
79
+ if (command === 'init') {
80
+ let destination = positionals[1];
81
+ if (!destination && process.stdin.isTTY) {
82
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
83
+ try {
84
+ destination = (await rl.question('项目目录 [my-ceru-plugin]: ')).trim() || 'my-ceru-plugin';
85
+ }
86
+ finally {
87
+ rl.close();
88
+ }
89
+ }
90
+ if (!destination)
91
+ throw new Error('Specify a project directory');
92
+ let template = flags.template;
93
+ let language = flags.lang;
94
+ if (process.stdin.isTTY && (!template || !language)) {
95
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
96
+ try {
97
+ if (!template) {
98
+ console.log(TEMPLATES.map((name, i) => String(i + 1) + '. ' + name).join('\n'));
99
+ const answer = (await rl.question('选择模板 [1]: ')).trim() || '1';
100
+ template = /^\d+$/.test(answer) ? TEMPLATES[Number(answer) - 1] : answer;
101
+ if (!template)
102
+ throw new Error('Invalid template selection');
103
+ }
104
+ if (!language)
105
+ language = (await rl.question('语言 ts / js [ts]: ')).trim() || 'ts';
106
+ }
107
+ finally {
108
+ rl.close();
109
+ }
110
+ }
111
+ const path = await scaffoldProject(destination, { template, language });
112
+ console.log('Created ' +
113
+ path +
114
+ '\n\nNext:\n cd ' +
115
+ JSON.stringify(path) +
116
+ '\n npm install\n npm run build\n\nShare only dist/plugin.js. A v2-compatible Host is required.');
117
+ return;
118
+ }
119
+ if (command === 'build') {
120
+ const output = await buildProject(flags.project ?? '.', { out: flags.out });
121
+ console.log('Built ' + output.path + ' (' + output.bytes + ' bytes)');
122
+ return;
123
+ }
124
+ if (command === 'dev') {
125
+ await runDev(flags.project ?? '.', {
126
+ port: Number(flags.port ?? 4179),
127
+ debugPort: Number(flags['debug-port'] ?? 9223),
128
+ noOpen: flags['no-open'],
129
+ hidden: flags.hidden,
130
+ electron: flags.electron,
131
+ });
132
+ return;
133
+ }
134
+ if (command === 'keygen') {
135
+ const prefix = resolve(required('out'));
136
+ for (const suffix of ['.private.pem', '.public.pem']) {
137
+ try {
138
+ await stat(prefix + suffix);
139
+ throw new Error('Key already exists: ' + prefix + suffix);
140
+ }
141
+ catch (error) {
142
+ if (error.code !== 'ENOENT')
143
+ throw error;
144
+ }
145
+ }
146
+ const keys = generateSigningKeys();
147
+ await writeAtomic(prefix + '.private.pem', keys.privateKey, false, 0o600);
148
+ await writeAtomic(prefix + '.public.pem', keys.publicKey);
149
+ console.log('Created ' +
150
+ prefix +
151
+ '.private.pem and .public.pem. Keep the private key outside source control.');
152
+ return;
153
+ }
154
+ const input = positionals[1];
155
+ if (!input)
156
+ throw new Error('An input .js file is required\n\n' + HELP);
157
+ const bytes = await readFile(input);
158
+ if (command === 'validate') {
159
+ const keys = flags['trusted-key']
160
+ ? await Promise.all(flags['trusted-key'].map((path) => readFile(path, 'utf8')))
161
+ : undefined;
162
+ const result = readArtifact(bytes, { trustedPublicKeys: keys });
163
+ const summary = {
164
+ id: result.header.manifest.id,
165
+ version: result.header.manifest.version,
166
+ bytes: bytes.length,
167
+ modules: Object.keys(result.modules),
168
+ resources: Object.keys(result.resources),
169
+ signature: result.signatureStatus,
170
+ codeDigest: result.codeDigest,
171
+ templateDigest: result.templateDigest,
172
+ personalized: !!result.header.delivery,
173
+ };
174
+ console.log(flags.json
175
+ ? JSON.stringify(summary, null, 2)
176
+ : 'Valid Ceru v2 artifact\n' + JSON.stringify(summary, null, 2));
177
+ return;
178
+ }
179
+ if (command === 'sign') {
180
+ const output = flags.out ?? input;
181
+ await jsOutput(output, signArtifact(bytes, await readFile(required('key'), 'utf8')), resolve(output) === resolve(input) || flags.force);
182
+ return;
183
+ }
184
+ if (command === 'template') {
185
+ if (!flags['issuer-key']?.length)
186
+ throw new Error('--issuer-key is required');
187
+ const output = createTemplate(bytes, {
188
+ privateKey: await readFile(required('key'), 'utf8'),
189
+ issuerPublicKeys: await Promise.all(flags['issuer-key'].map((path) => readFile(path, 'utf8'))),
190
+ personalizationSchema: flags.policy
191
+ ? parseJsonStrict(await readFile(flags.policy, 'utf8'))
192
+ : undefined,
193
+ });
194
+ await jsOutput(required('out'), output, flags.force);
195
+ return;
196
+ }
197
+ if (command === 'issue') {
198
+ const issuer = new PreparedIssuer(bytes, {
199
+ issuerPrivateKey: await readFile(required('key'), 'utf8'),
200
+ });
201
+ const data = parseJsonStrict(await readFile(required('config'), 'utf8'));
202
+ await jsOutput(required('out'), issuer.issue(data, { deliveryId: flags.id }), flags.force);
203
+ return;
204
+ }
205
+ throw new Error('Unknown command: ' + command + '\n\n' + HELP);
206
+ }
@@ -0,0 +1,34 @@
1
+ import { type ArtifactHeader } from '@shiqianjiang/ceru-plugin-issuer';
2
+ export interface BuildConfig {
3
+ manifest: ArtifactHeader['manifest'];
4
+ entries: Record<string, string>;
5
+ resources?: Record<string, {
6
+ path: string;
7
+ type: 'json' | 'text' | 'base64';
8
+ mime?: string;
9
+ }>;
10
+ output?: string;
11
+ framework?: 'vanilla' | 'vue' | 'react';
12
+ sharedLibraries?: Partial<Record<'vue' | 'react' | 'react-dom', string>>;
13
+ webDist?: Record<string, string>;
14
+ }
15
+ export declare function writeAtomic(path: string, data: string | Uint8Array, overwrite?: boolean, mode?: number): Promise<void>;
16
+ export declare function loadProject(project: string): Promise<{
17
+ root: string;
18
+ config: BuildConfig;
19
+ }>;
20
+ export declare function buildProject(project?: string, options?: {
21
+ out?: string;
22
+ overwrite?: boolean;
23
+ development?: boolean;
24
+ }): Promise<{
25
+ path: string;
26
+ bytes: number;
27
+ devModules: Record<string, string>;
28
+ }>;
29
+ export declare const TEMPLATES: readonly ["source", "connected-library", "importer", "guest-adapter", "web-surface", "vue", "vue-tsx", "react", "web-dist"];
30
+ export type TemplateName = (typeof TEMPLATES)[number];
31
+ export declare function scaffoldProject(destination: string, options?: {
32
+ template?: string;
33
+ language?: string;
34
+ }): Promise<string>;