aeria-populate 0.0.21 → 0.0.23

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 +2 -2
  2. package/dist/cli.js +51 -49
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  - `-c --compileMarkdown`: will compile Markdown to HTML before inserting
6
6
  - `-d --dropCollections`: will drop matching collections before inserting
7
- - `-w --watch`: watch mode (can not be used together with `--dropCollections`)
7
+ - `-w --watch`: watch mode
8
8
 
9
9
  ```sh
10
10
  # when --env-file is applicable
@@ -21,7 +21,7 @@ npx aeria-populate -c "content/**/*.md"
21
21
  ---
22
22
  collection: person
23
23
  unique: slug
24
- content: description
24
+ content: description # optional
25
25
  document:
26
26
  slug: john-doe
27
27
  sex: male
package/dist/cli.js CHANGED
@@ -23,16 +23,18 @@ const { positionals, values: opts } = parseArgs({
23
23
  });
24
24
  const dbPromise = getDatabase();
25
25
  const isValidFrontmatterObject = (value) => {
26
- return !!(value
27
- && typeof value === 'object'
28
- && 'collection' in value
26
+ if (!value || typeof value !== 'object') {
27
+ return false;
28
+ }
29
+ if ('content' in value && typeof value.content !== 'string') {
30
+ return false;
31
+ }
32
+ return !!('collection' in value
29
33
  && 'unique' in value
30
- && 'content' in value
31
34
  && 'document' in value
32
35
  && value.document
33
36
  && typeof value.collection === 'string'
34
37
  && typeof value.unique === 'string'
35
- && typeof value.content === 'string'
36
38
  && typeof value.document === 'object');
37
39
  };
38
40
  const parseMarkdown = async (text) => {
@@ -66,24 +68,16 @@ const work = async (text) => {
66
68
  _id: 1,
67
69
  },
68
70
  });
69
- let insertion;
71
+ const payload = {
72
+ what: frontmatter.document,
73
+ };
70
74
  if (existing) {
71
- insertion = await insert({
72
- what: {
73
- ...frontmatter.document,
74
- [frontmatter.content]: content,
75
- _id: existing._id,
76
- },
77
- }, context);
78
- }
79
- else {
80
- insertion = await insert({
81
- what: {
82
- ...frontmatter.document,
83
- [frontmatter.content]: content,
84
- },
85
- }, context);
75
+ payload.what._id = existing._id;
76
+ }
77
+ if (frontmatter.content) {
78
+ payload.what[frontmatter.content] = content;
86
79
  }
80
+ const insertion = await insert(payload, context);
87
81
  return {
88
82
  insertion,
89
83
  frontmatter,
@@ -91,33 +85,37 @@ const work = async (text) => {
91
85
  };
92
86
  };
93
87
  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++;
88
+ try {
89
+ const content = await fs.promises.readFile(file, {
90
+ encoding: 'utf-8',
91
+ });
92
+ const { insertion: { error }, frontmatter, existing } = await work(content);
93
+ const uniqueName = styleText(['bold'], frontmatter.document[frontmatter.unique]);
94
+ const collectionName = styleText(['bold'], frontmatter.collection);
95
+ if (error) {
96
+ const actionText = existing
97
+ ? `update ${uniqueName} into collection`
98
+ : `add ${uniqueName} to collection`;
99
+ console.log(styleText(['red'], 'x'), "couldn't", actionText, collectionName);
100
+ console.log(inspect(error, {
101
+ depth: null,
102
+ }));
103
+ }
104
+ else {
105
+ const actionText = existing
106
+ ? 'updated into collection'
107
+ : 'added to collection';
108
+ console.log(styleText(['green'], '✓'), uniqueName, 'successfully', actionText, collectionName);
109
+ }
110
+ return {
111
+ success: !error,
112
+ };
113
+ }
114
+ catch (err) {
115
+ console.trace(err);
117
116
  }
118
117
  return {
119
- failed,
120
- successful,
118
+ success: false,
121
119
  };
122
120
  };
123
121
  export const main = async () => {
@@ -143,7 +141,7 @@ export const main = async () => {
143
141
  if (opts.dropCollections) {
144
142
  for (const collection of collections) {
145
143
  if ((await db.listCollections().toArray()).some((subject) => collection === subject.name)) {
146
- await db.collection(collection).drop();
144
+ await db.collection(collection).deleteMany();
147
145
  console.log(styleText(['green'], '✓'), 'dropped collection', styleText(['bold'], collection));
148
146
  dropped++;
149
147
  }
@@ -151,8 +149,12 @@ export const main = async () => {
151
149
  }
152
150
  for (const file of files) {
153
151
  const result = await visitFile(file);
154
- failed += result.failed;
155
- successful += result.successful;
152
+ if (result.success) {
153
+ successful++;
154
+ }
155
+ else {
156
+ failed++;
157
+ }
156
158
  }
157
159
  console.log(dropped, 'dropped collections:', collections.map((collection) => styleText(['bold'], collection)).join(', '));
158
160
  console.log(successful, 'documents imported successfully');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "aeria-populate",
3
3
  "type": "module",
4
- "version": "0.0.21",
4
+ "version": "0.0.23",
5
5
  "description": "",
6
6
  "license": "ISC",
7
7
  "keywords": [],