aeria-populate 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.
Files changed (3) hide show
  1. package/README.md +28 -1
  2. package/dist/cli.js +41 -9
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -2,5 +2,32 @@
2
2
 
3
3
  ## Usage
4
4
 
5
- ...
5
+ - `-c`: will compile Markdown to HTML before inserting
6
+ - `-d`: will drop matching collections before inserting
7
+
8
+ ```sh
9
+ # when --env-file is applicable
10
+ node --env-file .env node_modules/aeria-populate/bin/index.js "content/**/*.md"
11
+
12
+ # otherwise
13
+ npx aeria-populate "content/**/*.md"
14
+ npx aeria-populate -c "content/**/*.md"
15
+ ```
16
+
17
+ ## Frontmatter format
18
+
19
+ ```md
20
+ ---
21
+ collection: person
22
+ unique: slug
23
+ content: description
24
+ document:
25
+ slug: john-doe
26
+ sex: male
27
+ ---
28
+
29
+ # John Doe
30
+
31
+ This will be inserted in the `description` property...
32
+ ```
6
33
 
package/dist/cli.js CHANGED
@@ -10,6 +10,10 @@ const { positionals, values: opts } = parseArgs({
10
10
  type: 'boolean',
11
11
  short: 'c',
12
12
  },
13
+ dropCollections: {
14
+ type: 'boolean',
15
+ short: 'd',
16
+ },
13
17
  },
14
18
  });
15
19
  const dbPromise = getDatabase();
@@ -26,11 +30,7 @@ const isValidFrontmatterObject = (value) => {
26
30
  && typeof value.content === 'string'
27
31
  && typeof value.document === 'object');
28
32
  };
29
- const work = async (text) => {
30
- const { db } = await dbPromise;
31
- if (!db) {
32
- throw new Error();
33
- }
33
+ const parseMarkdown = async (text) => {
34
34
  const [, frontmatterString, ...splitContent] = text.split('---');
35
35
  let content = splitContent.join('---').trim();
36
36
  if (opts.compileMarkdown) {
@@ -40,6 +40,17 @@ const work = async (text) => {
40
40
  if (!isValidFrontmatterObject(frontmatter)) {
41
41
  throw new Error('invalid frontmatter');
42
42
  }
43
+ return {
44
+ frontmatter,
45
+ content,
46
+ };
47
+ };
48
+ const work = async (text) => {
49
+ const { db } = await dbPromise;
50
+ if (!db) {
51
+ throw new Error();
52
+ }
53
+ const { frontmatter, content } = await parseMarkdown(text);
43
54
  const context = await createContext({
44
55
  collectionName: frontmatter.collection,
45
56
  });
@@ -80,9 +91,30 @@ export const main = async () => {
80
91
  console.error('this command takes a glob pattern as positional parameter');
81
92
  process.exit(1);
82
93
  }
83
- const files = fs.promises.glob(pattern);
84
- let failed = 0, sucessful = 0;
85
- for await (const file of files) {
94
+ const { client, db } = await dbPromise;
95
+ if (!db) {
96
+ throw new Error();
97
+ }
98
+ let failed = 0, sucessful = 0, dropped = 0;
99
+ 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',
104
+ });
105
+ const { frontmatter } = await parseMarkdown(content);
106
+ collections.push(frontmatter.collection);
107
+ }
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++;
114
+ }
115
+ }
116
+ }
117
+ for (const file of files) {
86
118
  const content = await fs.promises.readFile(file, {
87
119
  encoding: 'utf-8',
88
120
  });
@@ -107,9 +139,9 @@ export const main = async () => {
107
139
  sucessful++;
108
140
  }
109
141
  }
142
+ console.log(dropped, 'dropped collections:', collections.map((collection) => styleText(['bold'], collection)).join(', '));
110
143
  console.log(sucessful, 'documents imported sucessfully');
111
144
  console.log(failed, 'failed to import');
112
- const { client } = await dbPromise;
113
145
  await client.close();
114
146
  if (failed) {
115
147
  process.exit(1);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "aeria-populate",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "description": "",
6
6
  "license": "ISC",
7
7
  "keywords": [],