create-wirejs-app 2.0.0 → 2.0.2

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.js CHANGED
@@ -18,6 +18,10 @@ const [
18
18
  await copy(`${__dirname}/templates/default`, `./${projectName}`);
19
19
  await copy(`${__dirname}/templates/default/gitignore`, `./${projectName}/.gitignore`);
20
20
  await fs.promises.unlink(`./${projectName}/gitignore`);
21
+ await fs.promises.rename(
22
+ `./${projectName}/src/ssr/simple-wiki/STAR.js`,
23
+ `./${projectName}/src/ssr/simple-wiki/*.js`
24
+ )
21
25
 
22
26
  const packageJson = await fs.readFileSync(`./${projectName}/package.json`);
23
27
  fs.writeFileSync(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-wirejs-app",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Initializes a wirejs package.",
5
5
  "author": "Jon Wire",
6
6
  "license": "MIT",
@@ -1,9 +1,9 @@
1
1
  import { AuthenticationService, FileService, withContext } from 'wirejs-resources';
2
2
  import { defaultGreeting } from '../src/lib/sample-lib.js';
3
3
 
4
- const userTodos = new FileService('userTodoApp');
5
- const wikiPages = new FileService('wikiPages');
6
- const authService = new AuthenticationService('core-users');
4
+ const userTodos = new FileService('app', 'userTodoApp');
5
+ const wikiPages = new FileService('app', 'wikiPages');
6
+ const authService = new AuthenticationService('app', 'core-users');
7
7
 
8
8
  export const auth = authService.buildApi();
9
9
 
@@ -1,7 +1,15 @@
1
1
  import { writeFileSync } from 'fs';
2
2
 
3
+ let API_URL = '/api';
3
4
  const indexModule = await import('./index.js');
4
5
 
6
+ try {
7
+ const backendConfig = await import('./config.js');
8
+ if (backendConfig.apiUrl) API_URL = backendConfig.apiUrl;
9
+ } catch {
10
+ console.log("No backend API config found.");
11
+ }
12
+
5
13
  function dedent(tabs, text) {
6
14
  const tabString = new Array(tabs).fill('\t').join('');
7
15
  return text.trim().replace(new RegExp(`^${tabString}`, 'gm'), '');
@@ -24,7 +32,7 @@ const baseClient = dedent(1, /* js */ `
24
32
  : {};
25
33
  }
26
34
 
27
- const response = await fetch("/api", {
35
+ const response = await fetch("${API_URL}", {
28
36
  method: 'POST',
29
37
  headers: {
30
38
  'Content-Type': 'application/json',
@@ -9,7 +9,7 @@
9
9
  ],
10
10
  "dependencies": {
11
11
  "wirejs-dom": "^1.0.34",
12
- "wirejs-resources": "^0.1.1-alpha",
12
+ "wirejs-resources": "^0.1.2-alpha",
13
13
  "dompurify": "^3.2.3",
14
14
  "marked": "^15.0.6"
15
15
  },
@@ -0,0 +1,101 @@
1
+ import { marked } from 'marked';
2
+ import DOMPurify from 'dompurify';
3
+ import { html, id, text, hydrate, node, list, attribute } from 'wirejs-dom/v2';
4
+ import { accountMenu } from '../../components/account-menu.js';
5
+ import { auth, wiki } from 'my-api';
6
+
7
+ /**
8
+ * @param {{
9
+ * content: string | undefined;
10
+ * user: string | undefined;
11
+ * }}
12
+ * @returns
13
+ */
14
+ async function Wiki({ context }) {
15
+ const filepath = (context || window).location.pathname;
16
+ const content = await wiki.read(context, filepath);
17
+
18
+ const accountMenuNode = accountMenu(auth);
19
+ let markdown = content ?? `This page doesn't exist yet`;
20
+ const signedOutAction = html`<i>(<b>Sign in</b> to edit.)</i>`;
21
+ const signedInAction = html`<button onclick=${enableEditing}>edit</button>`;
22
+ const invisibleDiv = html`<div style='display: none;'></div>`;
23
+ const editor = html`<div>
24
+ <textarea style='width: 20em; height: 10em;' ${id('textarea')}></textarea>
25
+ </div>`;
26
+
27
+ accountMenuNode.data.onchange(async state => {
28
+ if (state.state.user) {
29
+ self.data.actions = signedInAction;
30
+ } else {
31
+ self.data.actions = signedOutAction;
32
+ }
33
+ });
34
+
35
+ function enableEditing() {
36
+ editor.data.textarea.value = markdown;
37
+ self.data.editor = editor;
38
+ self.data.actions = html`<div>
39
+ <button onclick=${submitChanges}>save</button>
40
+ <button onclick=${cancelChanges}>cancel</button>
41
+ </div>`;
42
+ }
43
+
44
+ async function submitChanges() {
45
+ markdown = editor.data.textarea.value;
46
+ await wiki.write(context, filepath, markdown);
47
+ self.data.content = markdown;
48
+ self.data.actions = signedInAction;
49
+ self.data.editor = invisibleDiv;
50
+ }
51
+
52
+ function cancelChanges() {
53
+ self.data.actions = signedInAction;
54
+ self.data.editor = html`<div style='display: none;'></div>`;
55
+ self.data.content = markdown;
56
+ }
57
+
58
+ const self = html`<div id='wiki'>
59
+ <div style='float: right;'>${accountMenuNode}</div>
60
+ ${node('content', markdown, md =>
61
+ html`<div>${DOMPurify.sanitize(marked.parse(md))}</div>`)
62
+ }
63
+ ${node('editor', invisibleDiv)}
64
+ ${node('actions', signedOutAction)}
65
+ </div>`;
66
+
67
+ return self;
68
+ }
69
+
70
+ /**
71
+ *
72
+ * @param {import('wirejs-services').Context} context
73
+ * @returns
74
+ */
75
+ export async function generate(context) {
76
+ const visiblePath = context.location.pathname
77
+ .replaceAll('/', ' > ')
78
+ .replaceAll('<', '&lt;')
79
+ .replaceAll('>', '&gt;')
80
+ .replaceAll('-', ' ')
81
+ .replace(/\s+/g, ' ')
82
+ ;
83
+
84
+ const page = html`
85
+ <!doctype html>
86
+ <html>
87
+ <head>
88
+ <title>Wiki ${visiblePath}</title>
89
+ </head>
90
+ <body>
91
+ <p><a href='/'>Home</a></p>
92
+ <h1>Wiki ${visiblePath}</h1>
93
+ ${await Wiki({ context })}
94
+ </body>
95
+ </html>
96
+ `;
97
+
98
+ return page;
99
+ }
100
+
101
+ hydrate('wiki', Wiki);