Skip to content

Commit 9f843c0

Browse files
committed
fix: parse and push sources to db
1 parent 6b5bc02 commit 9f843c0

File tree

4 files changed

+214
-45
lines changed

4 files changed

+214
-45
lines changed

CoCreate.config.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ module.exports = {
44
"organization_Id": "5ff747727005da1c272740ab",
55
"host": "general.cocreate.app"
66
},
7-
8-
"sources": [{
7+
"sources": [
8+
{
99
"entry": "./docs/index.html",
1010
"collection": "files",
1111
"document_id": "601460b19f64ba1680b8669b",
1212
"key": "src",
13-
"data":{
13+
"data": {
1414
"name": "index.html",
1515
"path": "/docs/docs/index.html",
1616
"domains": [
@@ -22,21 +22,5 @@ module.exports = {
2222
"website_id": "61381ed8829b690010a4f2b2"
2323
}
2424
}
25-
],
26-
27-
"extract": {
28-
"directory": "./src/",
29-
"extensions": [
30-
"js",
31-
"css",
32-
"html"
33-
],
34-
"ignores": [
35-
"node_modules",
36-
"vendor",
37-
"bower_components",
38-
"archive"
39-
]
40-
}
41-
}
42-
25+
]
26+
}

result.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[[{"collection":"docs","data":{"description":"Css comment extract example\n testing....\n end","extension":".css","file_path":"./test_files/box-shadow.css"},"metaData":"./test_files/box-shadow.css"}],[{"collection":"docs","data":{"description":"CoCreateAction's init function","extension":".js","file_path":"./test_files/CoCreate-action.js"},"metaData":"./test_files/CoCreate-action.js"}],[{"collection":"docs","data":{"description":"updateDocument({\n collection: \"test123\",\n document_id: \"document_id\",\n data:{\n \texample: “some example can be html json etc”,\n \tdescription: “update documnets if document does not exist otherwise create”\n },\n })","extension":".js","file_path":"./test_files/CoCreate.js"},"metaData":"./test_files/CoCreate.js"},{"collection":"docs","data":{"description":"readDocument({\n collection: \"test123\",\n document_id: \"document_id\",\n element: “xxxx”,\n metaData: \"xxxx\",\n exclude_fields: [] \n })","extension":".js","file_path":"./test_files/CoCreate.js"},"metaData":"./test_files/CoCreate.js"},{"collection":"docs","data":{"description":"deleteDocument({\n namespace: '',\n room: '',\n broadcast: true/false,\n broadcast_sender: true/false,\n \n collection: \"module\",\n document_id: \"\",\n element: “xxxx”,\n metadata: \"xxxx\"\n })","extension":".js","file_path":"./test_files/CoCreate.js"},"metaData":"./test_files/CoCreate.js"}],[{"collection":"docs","data":{"description":"<script src=\"https://cdn.cocreate.app/latest/CoCreate.min.js\" ></script>\n\t\t\n\t\tParse <testing class=\"\"></testing>\n\t\ttesting...","extension":".html","file_path":"./test_files/test.html"},"metaData":"./test_files/test.html"}]]
1+
[]

src/index.1.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
const CoCreateCrud = require('@cocreate/crud-client')
2+
const CoCreateSocket = require('@cocreate/socket-client')
3+
4+
const CoCreateExtract = require('./extract')
5+
6+
const fs = require('fs');
7+
const path = require('path');
8+
let config;
9+
10+
let jsConfig = path.resolve(process.cwd(), 'CoCreate.config.js');
11+
let jsonConfig = path.resolve(process.cwd(), 'CoCreate.config.json')
12+
if (fs.existsSync(jsConfig))
13+
config = require(jsConfig);
14+
else if (fs.existsSync(jsonConfig)) {
15+
config = require(jsonConfig)
16+
}
17+
else {
18+
process.exit()
19+
console.log('config not found.')
20+
}
21+
22+
const { crud, extract, sources, config : socketConfig } = config;
23+
24+
/** init cocreatecrud and socket **/
25+
let socket = new CoCreateSocket("ws");
26+
CoCreateCrud.setSocket(socket);
27+
socket.create({
28+
namespace: socketConfig.organization_Id,
29+
room: null,
30+
host: socketConfig.host
31+
})
32+
33+
const commonParam = {
34+
apiKey : socketConfig.apiKey,
35+
organization_id : socketConfig.organization_Id,
36+
broadcast: false
37+
}
38+
39+
async function runStore (info, type) {
40+
try {
41+
let status = false;
42+
const event = "docEvent" + Date.now()
43+
if (!info.document_id) {
44+
status = CoCreateCrud.createDocument({
45+
...commonParam,
46+
...info,
47+
event
48+
})
49+
} else {
50+
status = CoCreateCrud.updateDocument({
51+
...commonParam,
52+
...info,
53+
upsert: true,
54+
event
55+
})
56+
}
57+
if (status) {
58+
59+
let response = await CoCreateCrud.listenAsync(event)
60+
console.log('type ------------------------- ', type)
61+
console.log(response)
62+
}
63+
} catch (err) {
64+
console.log(err);
65+
}
66+
}
67+
68+
/**
69+
* Extract comments and store into db
70+
*/
71+
if (extract) {
72+
let result = CoCreateExtract(extract.directory, extract.ignores, extract.extensions);
73+
fs.writeFileSync('result.json', JSON.stringify(result), 'utf8')
74+
75+
result.forEach((docs) => {
76+
docs.forEach(async(doc) => {
77+
if (!doc.collection) return;
78+
await runStore(doc, 'extract')
79+
})
80+
})
81+
}
82+
83+
/**
84+
* update and create document by config crud
85+
*/
86+
87+
// if (crud) {
88+
// crud.forEach(async (info) => {
89+
// await runStore(info, 'crud')
90+
// })
91+
// }
92+
93+
/**
94+
* Store html files by config sources
95+
**/
96+
if (sources) {
97+
sources.forEach(async ({entry, collection, document_id, key, data}) => {
98+
if (!path) return;
99+
100+
let content = fs.readFileSync(path, 'utf8');
101+
102+
if (content && key && collection) {
103+
if (!data) data = {};
104+
let storeData = {
105+
[key]: content,
106+
...data
107+
};
108+
await runStore({collection, document_id, data: storeData}, 'sources');
109+
}
110+
})
111+
}
112+
113+
console.log('end....')
114+
115+
setTimeout(function(){
116+
process.exit()
117+
}, 1000 * 30)

src/index.js

Lines changed: 91 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
const CoCreateCrud = require('@cocreate/crud-client')
22
const CoCreateSocket = require('@cocreate/socket-client')
3-
3+
const mime = require('mime-types')
44
const CoCreateExtract = require('./extract')
55

66
const fs = require('fs');
77
const path = require('path');
88
let config;
99

1010
let jsConfig = path.resolve(process.cwd(), 'CoCreate.config.js');
11-
let jsonConfig = path.resolve(process.cwd(), 'CoCreate.config.json')
11+
// let jsonConfig = path.resolve(process.cwd(), 'CoCreate.config.json')
1212
if (fs.existsSync(jsConfig))
1313
config = require(jsConfig);
14-
else if (fs.existsSync(jsonConfig)) {
15-
config = require(jsonConfig)
16-
}
14+
// else if (fs.existsSync(jsonConfig)) {
15+
// config = require(jsonConfig)
16+
// }
1717
else {
1818
process.exit()
1919
console.log('config not found.')
2020
}
2121

2222
const { crud, extract, sources, config : socketConfig } = config;
23+
console.log(config)
24+
25+
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
2326

2427
/** init cocreatecrud and socket **/
2528
let socket = new CoCreateSocket("ws");
@@ -55,16 +58,92 @@ async function runStore (info, type) {
5558
})
5659
}
5760
if (status) {
58-
5961
let response = await CoCreateCrud.listenAsync(event)
6062
console.log('type ------------------------- ', type)
6163
console.log(response)
64+
return response;
6265
}
6366
} catch (err) {
6467
console.log(err);
68+
return null;
6569
}
6670
}
6771

72+
/**
73+
* update and create document by config crud
74+
*/
75+
76+
if (crud) {
77+
crud.forEach(async (info) => {
78+
await runStore(info, 'crud')
79+
})
80+
}
81+
82+
/**
83+
* Store html files by config sources
84+
**/
85+
if (sources) {
86+
let new_sources_list = [];
87+
88+
async function runSources() {
89+
for (let i = 0; i < sources.length; i++) {
90+
const { entry, collection, document_id, key, data } = sources[i];
91+
92+
let new_source = {...sources[i]};
93+
let response = {};
94+
if (entry) {
95+
96+
try {
97+
let read_type = 'utf8'
98+
let mime_type = mime.lookup(entry) || 'text/html';
99+
if (/^(image|audio|video)\/[-+.\w]+/.test(mime_type)) {
100+
read_type = 'base64'
101+
}
102+
103+
let binary = fs.readFileSync(entry);
104+
105+
let content = new Buffer(binary).toString(read_type);
106+
107+
if (content && key && collection) {
108+
if (!data) data = {};
109+
let storeData = {
110+
[key]: content,
111+
...data,
112+
};
113+
114+
response = await runStore({collection, document_id, data: storeData}, 'sources');
115+
}
116+
} catch (err) {
117+
console.log(err)
118+
}
119+
if (response.document_id) {
120+
new_source.document_id = response.document_id
121+
}
122+
}
123+
new_sources_list.push(new_source)
124+
}
125+
return new_sources_list
126+
127+
}
128+
129+
runSources().then((data) => {
130+
131+
console.log(data)
132+
let new_config = {
133+
config: socketConfig,
134+
sources: new_sources_list,
135+
crud: crud,
136+
}
137+
138+
let write_str = JSON.stringify(new_config, null, 4)
139+
write_str = "module.exports = " + write_str;
140+
141+
fs.writeFileSync(jsConfig, write_str);
142+
// fs.writeFileSync(jsonConfig, write_str);
143+
144+
})
145+
}
146+
68147
/**
69148
* Extract comments and store into db
70149
*/
@@ -93,25 +172,14 @@ if (extract) {
93172
/**
94173
* Store html files by config sources
95174
**/
96-
if (sources) {
97-
sources.forEach(async ({entry, collection, document_id, key, data}) => {
98-
if (!path) return;
99-
100-
let content = fs.readFileSync(path, 'utf8');
101-
102-
if (content && key && collection) {
103-
if (!data) data = {};
104-
let storeData = {
105-
[key]: content,
106-
...data
107-
};
108-
await runStore({collection, document_id, data: storeData}, 'sources');
109-
}
110-
})
111-
}
175+
112176

113177
console.log('end....')
114178

115179
setTimeout(function(){
116180
process.exit()
117-
}, 1000 * 30)
181+
}, 1000 * 60)
182+
183+
184+
185+

0 commit comments

Comments
 (0)