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.
- package/README.md +2 -2
- package/dist/cli.js +51 -49
- 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
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
71
|
+
const payload = {
|
|
72
|
+
what: frontmatter.document,
|
|
73
|
+
};
|
|
70
74
|
if (existing) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
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).
|
|
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
|
-
|
|
155
|
-
|
|
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');
|