Skip to content

Commit a6e8d72

Browse files
committed
fix: docs
1 parent 7f2aced commit a6e8d72

File tree

2 files changed

+190
-38
lines changed

2 files changed

+190
-38
lines changed

src/index.2.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
const CoCreateCrud = require('@cocreate/crud-client')
2+
const CoCreateSocket = require('@cocreate/socket-client')
3+
const mime = require('mime-types')
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+
const { crud, sources, config : socketConfig } = config;
24+
console.log(config)
25+
26+
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
27+
28+
/** init cocreatecrud and socket **/
29+
let socket = new CoCreateSocket("ws");
30+
CoCreateCrud.setSocket(socket);
31+
socket.create({
32+
namespace: socketConfig.organization_Id,
33+
room: null,
34+
host: socketConfig.host
35+
})
36+
37+
const commonParam = {
38+
apiKey : socketConfig.apiKey,
39+
organization_id : socketConfig.organization_Id,
40+
broadcast: false
41+
}
42+
43+
async function runStore (info, type) {
44+
try {
45+
let status = false;
46+
const event = "docEvent" + Date.now()
47+
if (!info.document_id) {
48+
status = CoCreateCrud.createDocument({
49+
...commonParam,
50+
...info,
51+
event
52+
})
53+
} else {
54+
status = CoCreateCrud.updateDocument({
55+
...commonParam,
56+
...info,
57+
upsert: true,
58+
event
59+
})
60+
}
61+
if (status) {
62+
let response = await CoCreateCrud.listenAsync(event)
63+
console.log('type ------------------------- ', type)
64+
// console.log(response)
65+
return response;
66+
}
67+
} catch (err) {
68+
console.log(err);
69+
return null;
70+
}
71+
}
72+
73+
/**
74+
* update and create document by config crud
75+
*/
76+
77+
if (crud) {
78+
crud.forEach(async (info) => {
79+
await runStore(info, 'crud')
80+
})
81+
}
82+
83+
/**
84+
* Store html files by config sources
85+
**/
86+
if (sources) {
87+
let new_sources_list = [];
88+
89+
async function runSources() {
90+
for (let i = 0; i < sources.length; i++) {
91+
const { entry, collection, document_id, key, data } = sources[i];
92+
93+
let new_source = {...sources[i]};
94+
let response = {};
95+
if (entry) {
96+
97+
try {
98+
let read_type = 'utf8'
99+
let mime_type = mime.lookup(entry) || 'text/html';
100+
if (/^(image|audio|video)\/[-+.\w]+/.test(mime_type)) {
101+
read_type = 'base64'
102+
}
103+
104+
let binary = fs.readFileSync(entry);
105+
106+
let content = new Buffer(binary).toString(read_type);
107+
108+
if (content && key && collection) {
109+
if (!data) data = {};
110+
let storeData = {
111+
[key]: content,
112+
...data,
113+
};
114+
115+
response = await runStore({collection, document_id, data: storeData}, 'sources');
116+
}
117+
} catch (err) {
118+
console.log(err)
119+
}
120+
if (response.document_id) {
121+
new_source.document_id = response.document_id
122+
}
123+
}
124+
new_sources_list.push(new_source)
125+
}
126+
return new_sources_list
127+
128+
}
129+
130+
runSources().then((data) => {
131+
132+
// console.log(data)
133+
let new_config = {
134+
config: socketConfig,
135+
sources: new_sources_list,
136+
crud: crud,
137+
}
138+
139+
let write_str = JSON.stringify(new_config, null, 4)
140+
write_str = "module.exports = " + write_str;
141+
142+
fs.writeFileSync(jsConfig, write_str);
143+
// fs.writeFileSync(jsonConfig, write_str);
144+
145+
})
146+
}
147+
148+
/**
149+
* Extract comments and store into db
150+
*/
151+
// if (extract) {
152+
// let result = CoCreateExtract(extract.directory, extract.ignores, extract.extensions);
153+
// fs.writeFileSync('result.json', JSON.stringify(result), 'utf8')
154+
155+
// result.forEach((docs) => {
156+
// docs.forEach(async(doc) => {
157+
// if (!doc.collection) return;
158+
// await runStore(doc, 'extract')
159+
// })
160+
// })
161+
// }
162+
163+
/**
164+
* update and create document by config crud
165+
*/
166+
167+
// if (crud) {
168+
// crud.forEach(async (info) => {
169+
// await runStore(info, 'crud')
170+
// })
171+
// }
172+
173+
/**
174+
* Store html files by config sources
175+
**/
176+
177+
178+
console.log('end....')
179+
180+
setTimeout(function(){
181+
process.exit()
182+
}, 1000 * 60)
183+
184+
185+
186+

src/index.js

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const CoCreateCrud = require('@cocreate/crud-client')
22
const CoCreateSocket = require('@cocreate/socket-client')
33
const mime = require('mime-types')
4-
const CoCreateExtract = require('./extract')
54

65
const fs = require('fs');
76
const path = require('path');
@@ -19,7 +18,8 @@ else {
1918
console.log('config not found.')
2019
}
2120

22-
const { crud, extract, sources, config : socketConfig } = config;
21+
const { crud, sources, config : socketConfig } = config;
22+
2323
console.log(config)
2424

2525
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
@@ -60,7 +60,7 @@ async function runStore (info, type) {
6060
if (status) {
6161
let response = await CoCreateCrud.listenAsync(event)
6262
console.log('type ------------------------- ', type)
63-
// console.log(response)
63+
console.log(response)
6464
return response;
6565
}
6666
} catch (err) {
@@ -128,7 +128,7 @@ if (sources) {
128128

129129
runSources().then((data) => {
130130

131-
// console.log(data)
131+
console.log(data)
132132
let new_config = {
133133
config: socketConfig,
134134
sources: new_sources_list,
@@ -144,42 +144,8 @@ if (sources) {
144144
})
145145
}
146146

147-
/**
148-
* Extract comments and store into db
149-
*/
150-
if (extract) {
151-
let result = CoCreateExtract(extract.directory, extract.ignores, extract.extensions);
152-
fs.writeFileSync('result.json', JSON.stringify(result), 'utf8')
153-
154-
result.forEach((docs) => {
155-
docs.forEach(async(doc) => {
156-
if (!doc.collection) return;
157-
await runStore(doc, 'extract')
158-
})
159-
})
160-
}
161-
162-
/**
163-
* update and create document by config crud
164-
*/
165-
166-
// if (crud) {
167-
// crud.forEach(async (info) => {
168-
// await runStore(info, 'crud')
169-
// })
170-
// }
171-
172-
/**
173-
* Store html files by config sources
174-
**/
175-
176-
177147
console.log('end....')
178148

179149
setTimeout(function(){
180150
process.exit()
181151
}, 1000 * 60)
182-
183-
184-
185-

0 commit comments

Comments
 (0)