coralite-scripts 0.0.2 → 0.0.3

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.
@@ -5,19 +5,26 @@ import colours from 'kleur'
5
5
  import localAccess from 'local-access'
6
6
  import chokidar from 'chokidar'
7
7
  import loadConfig from '../src/load-config.js'
8
- import html from '../src/build-html.js'
9
8
  import buildSass from '../src/build-sass.js'
10
9
  import { toCode, toMS, toTime } from '../src/build-utils.js'
11
10
  import { extname, join } from 'path'
12
11
  import { readFile, access, constants } from 'fs/promises'
12
+ import Coralite from 'coralite'
13
13
 
14
14
  const config = await loadConfig()
15
15
  const app = express()
16
16
  const port = config.server?.port || 3000
17
+
17
18
  // track active connections.
18
19
  const clients = new Set()
19
20
 
20
- const buildHTML = await html(config)
21
+ // start coralite
22
+ const coralite = new Coralite({
23
+ templates: config.templates,
24
+ pages: config.pages,
25
+ plugins: config.plugins
26
+ })
27
+ await coralite.initialise()
21
28
 
22
29
  const watchPath = [
23
30
  config.public,
@@ -97,37 +104,43 @@ app
97
104
 
98
105
  res.send(data)
99
106
  } catch {
100
- try {
101
- // if that fails, try reading from pages directory.
107
+ if (!path.endsWith('.html')) {
108
+ res.sendStatus(404)
109
+ } else {
102
110
  const filePath = join(config.pages, path)
103
111
 
104
- // check if page source file exists and is readable
105
- await access(filePath, constants.R_OK)
112
+ try {
113
+
114
+ // if that fails, try reading from pages directory.
115
+
116
+ // check if page source file exists and is readable
117
+ await access(filePath, constants.R_OK)
118
+ } catch {
119
+ res.sendStatus(404)
120
+ }
121
+
106
122
  const start = process.hrtime()
107
123
  let duration, dash = colours.gray(' ─ ')
108
124
 
125
+ await coralite.pages.setItem(filePath)
109
126
  // build the HTML for this page using the built-in compiler.
110
- await buildHTML.compile(filePath)
111
- const data = await readFile(filePath, 'utf8')
127
+ const documents = await coralite.compile(path)
112
128
  // inject a script to enable live reload via Server-Sent Events
113
- const injectedHtml = data.replace(/<\/body>/i, `
114
- <script>
115
- const eventSource = new EventSource('/_/rebuild');
116
- eventSource.onmessage = function(event) {
117
- if (event.data === 'connected') return;
118
- // Reload page when file changes
119
- location.reload()
120
- }
121
- </script>
129
+ const injectedHtml = documents[0].html.replace(/<\/body>/i, `\n
130
+ <script>
131
+ const eventSource = new EventSource('/_/rebuild');
132
+ eventSource.onmessage = function(event) {
133
+ if (event.data === 'connected') return;
134
+ // Reload page when file changes
135
+ location.reload()
136
+ }
137
+ </script>
122
138
  </body>`)
123
139
 
124
140
  // prints time and path to the file that has been changed or added.
125
141
  duration = process.hrtime(start)
126
142
  process.stdout.write(toTime() + colours.bgGreen('Compiled HTML') + dash + toMS(duration) + dash + path + '\n')
127
143
  res.send(injectedHtml)
128
- } catch(error) {
129
- // if all attempts fail, respond with a 404.
130
- res.sendStatus(404)
131
144
  }
132
145
  }
133
146
  })
@@ -139,9 +152,13 @@ const watcher = chokidar.watch(watchPath, {
139
152
 
140
153
  watcher
141
154
  .on('change', async (path) => {
142
- if (path.endsWith('.scss') || path.endsWith('.sass')) {
143
- const start = process.hrtime()
144
- let duration, dash = colours.gray(' ─ ')
155
+ const start = process.hrtime()
156
+ let duration, dash = colours.gray(' ─ ')
157
+
158
+ if (path.startsWith(config.templates)) {
159
+ // update template file
160
+ await coralite.templates.setItem(path)
161
+ } else if (path.endsWith('.scss') || path.endsWith('.sass')) {
145
162
  // rebuild CSS and send notification
146
163
  await buildSass({
147
164
  ...config.sass,
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "coralite-scripts",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Configuration and scripts for Create Coralite.",
5
5
  "type": "module",
6
6
  "bin": {
7
- "coralite-scripts": "./bin/coralite-scripts.js"
7
+ "coralite-scripts": "./bin/index.js"
8
8
  },
9
9
  "keywords": [
10
10
  "coralite",
package/src/build-html.js DELETED
@@ -1,60 +0,0 @@
1
- import Coralite from 'coralite'
2
-
3
- /**
4
- * @import { CoralitePluginInstance } from 'coralite/types'
5
- */
6
-
7
- /**
8
- * Builds HTML files from Coralite templates and pages.
9
- *
10
- * @param {Object} options
11
- * @param {string} options.pages - Path to pages directory
12
- * @param {string} options.templates - Path to templates directory
13
- * @param {string} options.output - Output path for HTML files
14
- * @param {string} options.output - Output path for HTML files
15
- * @param {CoralitePluginInstance[]} [options.plugins=[]] - List of Coralite plugins.
16
- */
17
- export default async function html ({
18
- pages,
19
- templates,
20
- output,
21
- plugins
22
- }) {
23
- const coralite = new Coralite({
24
- templates,
25
- pages,
26
- plugins
27
- })
28
- await coralite.initialise()
29
-
30
- return {
31
- /**
32
- * Compiles the specified HTML pages using coralite and saves the output to the configured output path.
33
- * @param {string | string[]} [paths] - Path to a single page or array of page paths relative to the pages directory. If omitted, compiles all pages.
34
- */
35
- async compile (paths) {
36
- let relativePathPages
37
-
38
- if (Array.isArray(paths)) {
39
- relativePathPages = []
40
-
41
- for (let i = 0; i < paths.length; i++) {
42
- const path = paths[i]
43
-
44
- relativePathPages.push(path.replace(pages + '/', ''))
45
- }
46
- } else if (paths) {
47
- relativePathPages = paths.replace(pages + '/', '')
48
- }
49
-
50
- if (relativePathPages && relativePathPages[0] === '/') {
51
- relativePathPages = relativePathPages.slice(1)
52
- }
53
-
54
- const document = await coralite.compile(relativePathPages)
55
-
56
- await coralite.save(document, output)
57
- }
58
- }
59
- }
60
-