aeria-populate 0.0.3 → 0.0.5

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
@@ -2,8 +2,9 @@
2
2
 
3
3
  ## Usage
4
4
 
5
- - `-c`: will compile Markdown to HTML before inserting
6
- - `-d`: will drop matching collections before inserting
5
+ - `-c --compileMarkdown`: will compile Markdown to HTML before inserting
6
+ - `-d --dropCollections`: will drop matching collections before inserting
7
+ - `-w --watch`: watch mode (can not be used together with `--drop-collections`)
7
8
 
8
9
  ```sh
9
10
  # when --env-file is applicable
package/dist/cli.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const main: () => Promise<never>;
1
+ export declare const main: () => Promise<void>;
package/dist/cli.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { getDatabase, insert, createContext } from 'aeria';
2
2
  import { parseArgs, styleText, inspect } from 'node:util';
3
+ import * as fs from 'node:fs';
3
4
  import * as yaml from 'yaml';
4
5
  import * as markdown from 'marked';
5
- import * as fs from 'fs';
6
+ import * as chokidar from 'chokidar';
6
7
  const { positionals, values: opts } = parseArgs({
7
8
  allowPositionals: true,
8
9
  options: {
@@ -14,6 +15,10 @@ const { positionals, values: opts } = parseArgs({
14
15
  type: 'boolean',
15
16
  short: 'd',
16
17
  },
18
+ watch: {
19
+ type: 'boolean',
20
+ short: 'w',
21
+ },
17
22
  },
18
23
  });
19
24
  const dbPromise = getDatabase();
@@ -85,6 +90,36 @@ const work = async (text) => {
85
90
  existing,
86
91
  };
87
92
  };
93
+ const visitFile = async (file) => {
94
+ let failed = 0, successful = 0;
95
+ const content = await fs.promises.readFile(file, {
96
+ encoding: 'utf-8',
97
+ });
98
+ const { insertion: { error }, frontmatter, existing } = await work(content);
99
+ const uniqueName = styleText(['bold'], frontmatter.document[frontmatter.unique]);
100
+ const collectionName = styleText(['bold'], frontmatter.collection);
101
+ if (error) {
102
+ const actionText = existing
103
+ ? `update ${uniqueName} into collection`
104
+ : `add ${uniqueName} to collection`;
105
+ console.log(styleText(['red'], 'x'), "couldn't", actionText, collectionName);
106
+ console.log(inspect(error, {
107
+ depth: null,
108
+ }));
109
+ failed++;
110
+ }
111
+ else {
112
+ const actionText = existing
113
+ ? 'updated into collection'
114
+ : 'added to collection';
115
+ console.log(styleText(['green'], '✓'), uniqueName, 'successfully', actionText, collectionName);
116
+ successful++;
117
+ }
118
+ return {
119
+ failed,
120
+ successful,
121
+ };
122
+ };
88
123
  export const main = async () => {
89
124
  const [pattern] = positionals;
90
125
  if (!pattern) {
@@ -95,58 +130,50 @@ export const main = async () => {
95
130
  if (!db) {
96
131
  throw new Error();
97
132
  }
98
- let failed = 0, sucessful = 0, dropped = 0;
99
133
  const files = await Array.fromAsync(fs.promises.glob(pattern));
100
- const collections = [];
101
- for (const file of files) {
102
- const content = await fs.promises.readFile(file, {
103
- encoding: 'utf-8',
134
+ if (opts.watch) {
135
+ if (opts.dropCollections) {
136
+ console.error("--dropCollections can't be used together with --watch");
137
+ process.exit(1);
138
+ }
139
+ const watcher = chokidar.watch(files);
140
+ console.log('watching for changes in ', styleText(['bold'], pattern));
141
+ watcher.on('change', async (filePath) => {
142
+ await client.connect();
143
+ await visitFile(filePath);
104
144
  });
105
- const { frontmatter } = await parseMarkdown(content);
106
- collections.push(frontmatter.collection);
107
145
  }
108
- if (opts.dropCollections) {
109
- for (const collection of collections) {
110
- if ((await db.listCollections().toArray()).some((subject) => collection === subject.name)) {
111
- await db.collection(collection).drop();
112
- console.log(styleText(['green'], '✓'), 'dropped collection', styleText(['bold'], collection));
113
- dropped++;
146
+ else {
147
+ let failed = 0, successful = 0, dropped = 0;
148
+ const collections = [];
149
+ for (const file of files) {
150
+ const content = await fs.promises.readFile(file, {
151
+ encoding: 'utf-8',
152
+ });
153
+ const { frontmatter } = await parseMarkdown(content);
154
+ collections.push(frontmatter.collection);
155
+ }
156
+ if (opts.dropCollections) {
157
+ for (const collection of collections) {
158
+ if ((await db.listCollections().toArray()).some((subject) => collection === subject.name)) {
159
+ await db.collection(collection).drop();
160
+ console.log(styleText(['green'], '✓'), 'dropped collection', styleText(['bold'], collection));
161
+ dropped++;
162
+ }
114
163
  }
115
164
  }
116
- }
117
- for (const file of files) {
118
- const content = await fs.promises.readFile(file, {
119
- encoding: 'utf-8',
120
- });
121
- const { insertion: { error }, frontmatter, existing } = await work(content);
122
- const uniqueName = styleText(['bold'], frontmatter.document[frontmatter.unique]);
123
- const collectionName = styleText(['bold'], frontmatter.collection);
124
- if (error) {
125
- const actionText = existing
126
- ? `update ${uniqueName} into collection`
127
- : `add ${uniqueName} to collection`;
128
- console.log(styleText(['red'], 'x'), "couldn't", actionText, collectionName);
129
- console.log(inspect(error, {
130
- depth: null,
131
- }));
132
- failed++;
165
+ for (const file of files) {
166
+ const result = await visitFile(file);
167
+ failed += result.failed;
168
+ successful += result.successful;
133
169
  }
134
- else {
135
- const actionText = existing
136
- ? 'updated into collection'
137
- : 'added to collection';
138
- console.log(styleText(['green'], '✓'), uniqueName, 'successfully', actionText, collectionName);
139
- sucessful++;
170
+ console.log(dropped, 'dropped collections:', collections.map((collection) => styleText(['bold'], collection)).join(', '));
171
+ console.log(successful, 'documents imported successfully');
172
+ console.log(failed, 'failed to import');
173
+ if (failed) {
174
+ await client.close();
175
+ process.exit(1);
140
176
  }
141
177
  }
142
- console.log(dropped, 'dropped collections:', collections.map((collection) => styleText(['bold'], collection)).join(', '));
143
- console.log(sucessful, 'documents imported sucessfully');
144
- console.log(failed, 'failed to import');
145
178
  await client.close();
146
- if (failed) {
147
- process.exit(1);
148
- }
149
- else {
150
- process.exit(0);
151
- }
152
179
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "aeria-populate",
3
3
  "type": "module",
4
- "version": "0.0.3",
4
+ "version": "0.0.5",
5
5
  "description": "",
6
6
  "license": "ISC",
7
7
  "keywords": [],
@@ -20,9 +20,10 @@
20
20
  "aeria": "file:../aeria"
21
21
  },
22
22
  "peerDependencies": {
23
- "aeria": "^0.0.331"
23
+ "aeria": "^0.0.333"
24
24
  },
25
25
  "dependencies": {
26
+ "chokidar": "^4.0.3",
26
27
  "marked": "^16.0.0",
27
28
  "yaml": "^2.8.0"
28
29
  },