votive 0.3.0 → 0.3.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.
@@ -1,5 +1,6 @@
1
- import { mkdir, writeFile } from "node:fs/promises"
1
+ import { mkdir, writeFile, rm } from "node:fs/promises"
2
2
  import path from "node:path"
3
+ import { checkFile } from "./utils/index.js"
3
4
 
4
5
  /** @import {Abstract, Abstracts, VotiveConfig} from "./bundle.js" */
5
6
  /** @import {Database} from "./createDatabase.js" */
@@ -11,27 +12,54 @@ import path from "node:path"
11
12
  */
12
13
  async function writeDestinations(config, database) {
13
14
  const writeProcessors = config && config.plugins && config.plugins.flatMap(plugin => (
14
- plugin.processors && plugin.processors.map(({ write, syntax }) => write && { write, syntax }).filter(a => a)
15
+ plugin.processors && plugin.processors.map(processor => processor.writeFile && processor).filter(a => a)
15
16
  )).filter(a => a)
16
17
 
17
18
  if (!writeProcessors || !writeProcessors.length) throw "No write processor provided"
18
19
 
19
- const destinations = database.getStaleDestinations()
20
- if (!destinations) return
20
+ const targets = database.target.getStale()
21
21
 
22
- const writing = destinations.flatMap(destination => {
22
+ if (!targets) return
23
+
24
+ const writing = targets.filter(({ path }) => path !== "0").flatMap(destination => {
25
+
26
+ /* FIXME the following line doesn't make sense */
27
+ if (!destination.path) database.target.markFresh(destination.path)
28
+ if (!Object.keys(destination.abstract).length) return
23
29
  const destinationPath = path.join(config.destinationFolder, String(destination.path))
24
30
  const { dir } = path.parse(destinationPath)
25
- return writeProcessors.map(processor => {
26
- if (processor.syntax === destination.syntax) {
27
- const { data, encoding = 'utf-8' } = processor.write(destination, database, config)
31
+ return writeProcessors.map(async processor => {
32
+ if (processor.extensions.includes(destination.syntax)) {
33
+ const writeInfo = await processor.writeFile(destination, database, config)
34
+ if (!writeInfo) {
35
+ try {
36
+ database.target.delete(destination.path)
37
+ return await rm(destination.path)
38
+ } catch (e) {
39
+ console.error(e)
40
+ }
41
+ }
42
+
43
+ const { data, encoding = 'utf-8' } = writeInfo
28
44
 
29
45
  async function write() {
30
- await mkdir(dir, { recursive: true })
31
- await writeFile(destinationPath, data, encoding)
32
- database.freshenDependency(destination.path)
46
+ const destinationExists = checkFile(dir)
47
+
48
+ if (!destinationExists) {
49
+ await mkdir(dir, { recursive: true })
50
+ }
51
+
52
+ if (data) {
53
+ if (processor.format === "text") {
54
+ await writeFile(destinationPath, data, "utf-8")
55
+ database.target.markFresh(destination.path)
56
+ } else {
57
+ await writeFile(destinationPath, data)
58
+ database.target.markFresh(destination.path)
59
+ }
60
+ }
33
61
  }
34
-
62
+
35
63
  return write()
36
64
  }
37
65
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "votive",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "A file processor.",
5
5
  "homepage": "https://github.com/samlfair/votive#readme",
6
6
  "bugs": {