votive 0.1.0 → 0.2.1

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/README.md CHANGED
@@ -11,6 +11,7 @@
11
11
  - [ ] Rename jobs and paths
12
12
  - [ ] Better dependency tracking
13
13
  - [ ] Better query filters
14
+ - [x] File deletion handling
14
15
 
15
16
  ## Project: Jobs
16
17
 
@@ -366,7 +366,7 @@ function prepareStatements(database) {
366
366
  * @returns {SQLiteDependency[]}
367
367
  */
368
368
 
369
- getByTarget: /** @type {{all: SQLiteDependencyGetByTarget}} */ (/** @type {unknown} */ (database.prepare(`
369
+ getAllByTarget: /** @type {{all: SQLiteDependencyGetByTarget}} */ (/** @type {unknown} */ (database.prepare(`
370
370
  SELECT * FROM dependencies WHERE destination = ?
371
371
  `))),
372
372
 
@@ -572,10 +572,11 @@ function createDatabase(databasePath = ".votive.db") {
572
572
  prepared.settings.delete.all(filePath)
573
573
  prepared.dependency.deleteByTarget.all(deletedSource.destination)
574
574
  .forEach(({ dependent }) => {
575
- prepared.dependency.markStale.get(dependent)
575
+ prepared.target.markStale.get(dependent)
576
576
  })
577
577
  prepared.metadata.deleteByTarget.all(deletedSource.destination)
578
578
  prepared.target.delete.get(deletedSource.destination)
579
+ return deletedSource
579
580
  },
580
581
 
581
582
  getAll() {
@@ -666,6 +667,13 @@ function createDatabase(databasePath = ".votive.db") {
666
667
  return prepared.dependency.getAll.all()
667
668
  },
668
669
 
670
+ /**
671
+ * @param {string} target
672
+ */
673
+ getAllByTarget(target) {
674
+ return prepared.dependency.getAllByTarget.all(target)
675
+ },
676
+
669
677
  /**
670
678
  * @param {object} dependencyFile
671
679
  * @param {string} dependencyKey
@@ -688,7 +696,7 @@ function createDatabase(databasePath = ".votive.db") {
688
696
 
689
697
  /**
690
698
  * @param {string} filePath
691
- * @returns {TargetOutput}
699
+ * @returns {TargetOutput | undefined}
692
700
  */
693
701
  get(filePath) {
694
702
  const target = prepared.target.get.get(filePath)
@@ -747,6 +755,8 @@ function createDatabase(databasePath = ".votive.db") {
747
755
  getWithTrackers(filePath, dependent) {
748
756
  const target = queries.target.get(filePath)
749
757
 
758
+ if(!target) return
759
+
750
760
  const trackedTarget = { metadata: {} }
751
761
 
752
762
  queries.dependency.track(
@@ -906,7 +916,13 @@ function createDatabase(databasePath = ".votive.db") {
906
916
  /** @param {string} filePath */
907
917
  markFresh(filePath) {
908
918
  return prepared.target.markFresh.get(filePath)
919
+ },
920
+
921
+ /** @param {string} filePath */
922
+ markStale(filePath) {
923
+ return prepared.target.markStale.get(filePath)
909
924
  }
925
+
910
926
  },
911
927
 
912
928
  url: {
@@ -3,7 +3,7 @@ import fs from "node:fs/promises"
3
3
  import path from "node:path"
4
4
  import pLimit from "p-limit"
5
5
 
6
- /** @import {VotiveConfig, VotivePlugin, VotiveProcessor, FlatProcessors, Abstracts, Abstract, Jobs, ProcessorSyntax} from "./bundle.js" */
6
+ /** @import {VotiveConfig, VotivePlugin, VotiveProcessor, FlatProcessors, Abstracts, Abstract, Jobs, ProcessorSyntax, Router} from "./bundle.js" */
7
7
  /** @import {Dirent} from "node:fs" */
8
8
  /** @import {Database} from "./createDatabase.js" */
9
9
 
@@ -31,28 +31,42 @@ async function readSources(config, database, processors) {
31
31
  const loadingFiles = files.map(a => path.normalize(path.format({ name: a.name, dir: a.parentPath })))
32
32
  if (!files) return { folders, sources: [] }
33
33
  const limit = pLimit(5)
34
- const readingSourceFiles = files.flatMap(readSourceFile(processors, database, config, limit))
35
- // const sources = readingSourceFiles && readingSourceFiles.map(async file => {
36
- // try {
37
- // const loaded = await file
38
- // // console.log(loaded)
39
- // // const i = loadingFiles.find(a => a === loaded.sourceFilePath)
40
- // // loadingFiles.splice(i,1)
41
- // // console.log(loadingFiles)
42
- // return loaded
43
- // } catch (e) {
44
- // console.error(e)
45
- // }
46
- // })
47
- const sources = readingSourceFiles && (await Promise.all(readingSourceFiles)).filter(a => a)
34
+ const readingSourceFiles = files.flatMap(readSourceFile(processors, database, config))
48
35
 
36
+ const reading = [
37
+ (await Promise.all(readingSourceFiles)).filter(a => a),
38
+ pruneDeletions(config, database, filteredDirents)
39
+ ]
40
+
41
+ const [sources, deletedSources] = await Promise.all(reading)
49
42
 
50
43
  return {
51
44
  folders,
52
- sources
45
+ sources: [...sources, ...deletedSources]
53
46
  }
54
47
  }
55
48
 
49
+ /**
50
+ * @param {VotiveConfig} config
51
+ * @param {Database} database
52
+ * @param {Dirent[]} dirents
53
+ */
54
+ async function pruneDeletions(config, database, dirents) {
55
+ const sourceFilePaths = new Set(dirents.map(d => d.isFile() && path.join(d.parentPath, d.name)))
56
+ const sourceRecords = database.source.getAll()
57
+ const sourceRecordPaths = new Set(sourceRecords.map(r => r.path))
58
+
59
+ const deletions = sourceRecordPaths.difference(sourceFilePaths)
60
+
61
+ let deletedSources = []
62
+
63
+ deletions.forEach(deletion => {
64
+ deletedSources.push(database.source.delete(deletion))
65
+ })
66
+
67
+ return deletedSources.filter(a => a)
68
+ }
69
+
56
70
  /**
57
71
  * @param {VotiveConfig} config
58
72
  */
@@ -98,7 +112,7 @@ function fileFilter(config) {
98
112
  * @param {Database} database
99
113
  * @param {VotiveConfig} config
100
114
  */
101
- function readSourceFile(processors, database, config, limit) {
115
+ function readSourceFile(processors, database, config) {
102
116
  /**
103
117
  * @param {import("node:fs").Dirent} dirent
104
118
  * @returns {Promise<ReadSourceFileResult>[]}
@@ -108,14 +122,14 @@ function readSourceFile(processors, database, config, limit) {
108
122
  const sourceFilePath = path.join(parentPath, name)
109
123
  const sourceFileInfo = path.parse(sourceFilePath)
110
124
 
111
- const processing = processors.flatMap(({ plugin, processor }) => process(plugin, processor, config, limit))
125
+ const processing = processors.flatMap(({ plugin, processor }) => process(plugin, processor, config))
112
126
 
113
127
  /**
114
128
  * @param {VotivePlugin} plugin
115
129
  * @param {VotiveProcessor} processor
116
130
  * @param {VotiveConfig} config
117
131
  */
118
- async function process(plugin, processor, config, limit) {
132
+ async function process(plugin, processor, config) {
119
133
  const { readFile: read, extensions: filter, format } = processor
120
134
  if (filter.includes(sourceFileInfo.ext) && read) {
121
135
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "votive",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "A file processor.",
5
5
  "homepage": "https://github.com/samlfair/votive#readme",
6
6
  "bugs": {
package/tests/index.js CHANGED
@@ -93,9 +93,9 @@ test("bundle", async () => {
93
93
  const dir = fs.readdirSync(tempDest.path, { recursive: true })
94
94
  assert(dir.length === 0, "Destination directory is not empty.")
95
95
  } catch (error) {
96
- console.error(error)
97
96
  tempDest.remove()
98
97
  tempSource.remove()
98
+ console.error(error)
99
99
 
100
100
  }
101
101