Skip to content

Commit 453a37a

Browse files
committed
update
1 parent efbfbfa commit 453a37a

File tree

11 files changed

+385
-49
lines changed

11 files changed

+385
-49
lines changed

node_modules/@cocreate/crud-client

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@cocreate/socket-client

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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>\r\n\t\t\r\n\t\tParse <testing class=\"\"></testing>\r\n\t\ttesting...","extension":".html","file_path":"./test_files/test.html"},"metaData":"./test_files/test.html"}]]
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"}]]
File renamed without changes.

src.backup/extract.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const extract = require('extract-comments');
2+
const fs = require('fs');
3+
const path = require("path")
4+
const glob = require('glob');
5+
const parseHtmlComments = require('parse-html-comments')
6+
7+
class ExtractComment {
8+
constructor() {
9+
10+
}
11+
12+
run(filePath, collection, name) {
13+
let content = fs.readFileSync(filePath, 'utf8');
14+
let extension = path.extname(filePath)
15+
16+
let comments = [];
17+
let docItems = [];
18+
19+
if (extension == '.html') {
20+
comments = this.extractHtml(content)
21+
} else {
22+
comments = extract(content)
23+
}
24+
25+
comments.forEach(({value}) => {
26+
let ret_value = this.extractValue(value)
27+
if (ret_value) {
28+
docItems.push({
29+
collection,
30+
data: {
31+
[name]:ret_value,
32+
extension,
33+
file_path: filePath
34+
},
35+
metaData: filePath
36+
})
37+
}
38+
})
39+
return docItems;
40+
}
41+
42+
extractValue(valueStr) {
43+
var regResult = /@value_start(?<value>.*?)@value_end/gs.exec(valueStr);
44+
if (regResult) {
45+
return regResult.groups.value.trim()
46+
} else {
47+
return null
48+
}
49+
}
50+
51+
extractHtml(content) {
52+
let htmlComments = parseHtmlComments(content)
53+
let result_comment = [];
54+
55+
htmlComments.matches.forEach(({groups}) => {
56+
let comment_value = groups.commentOnly;
57+
comment_value = comment_value.replace(/<!--|-->/gs, '');
58+
59+
result_comment.push({
60+
value: comment_value
61+
})
62+
})
63+
return result_comment;
64+
}
65+
}
66+
67+
const extractInstance = new ExtractComment()
68+
69+
function CoCreateExtract (directory, ignoreFolders, extensions ) {
70+
let extensionsStr = "*";
71+
let ignoreFolderStr = "";
72+
73+
if (extensions && extensions.length > 0) {
74+
extensionsStr = extensions.join(',');
75+
}
76+
77+
if (ignoreFolders && ignoreFolders.length > 0) {
78+
ignoreFolderStr = ignoreFolders.join('|');
79+
}
80+
81+
let result = [];
82+
83+
if (!directory) {
84+
directory = ".";
85+
}
86+
87+
const files = glob.sync(directory + `/**/*.{${extensionsStr}}`, {});
88+
89+
files.forEach((file) => {
90+
var regex = new RegExp(ignoreFolderStr, 'g');
91+
if (!regex.test(file)) {
92+
const docData = extractInstance.run(file, 'docs', 'description');
93+
if (docData.length > 0) {
94+
const fileName = path.basename(file);
95+
result.push(docData)
96+
}
97+
}
98+
})
99+
100+
return result;
101+
}
102+
103+
104+
105+
module.exports = CoCreateExtract
106+

src.backup/index.20210430.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
const CoCreateExtract = require('./extract')
2+
const fs = require('fs');
3+
const path = require('path');
4+
let config;
5+
6+
let jsConfig = path.resolve(process.cwd(), 'CoCreate.config.js');
7+
let jsonConfig = path.resolve(process.cwd(), 'CoCreate.config.json')
8+
if (fs.existsSync(jsConfig))
9+
config = require(jsConfig);
10+
else if (fs.existsSync(jsonConfig)) {
11+
config = require(jsonConfig)
12+
}
13+
else {
14+
process.exit()
15+
console.log('config not found.')
16+
}
17+
18+
const { crud, extract, sources } = config;
19+
const { CoCreateSocketInit, CoCreateUpdateDocument, CoCreateCreateDocument } = require("./socket_process.js")
20+
/**
21+
* Socket init
22+
*/
23+
CoCreateSocketInit(config.config)
24+
25+
/**
26+
* Extract comments and store into db
27+
*/
28+
if (extract) {
29+
let result = CoCreateExtract(extract.directory, extract.ignores, extract.extensions);
30+
fs.writeFileSync('result.json', JSON.stringify(result), 'utf8')
31+
32+
result.forEach((docs) => {
33+
docs.forEach((doc) => {
34+
if (!doc.collection) return;
35+
if (!doc.document_id) {
36+
CoCreateCreateDocument(doc, config.config);
37+
} else {
38+
CoCreateUpdateDocument(doc, config.config);
39+
}
40+
})
41+
})
42+
}
43+
44+
45+
/**
46+
* update and create document by config crud
47+
*/
48+
49+
if (crud) {
50+
crud.forEach(({collection, document_id, data}) => {
51+
if (!document_id) {
52+
CoCreateCreateDocument({
53+
collection,
54+
data
55+
}, config.config);
56+
} else {
57+
CoCreateUpdateDocument({
58+
collection,
59+
document_id,
60+
data,
61+
upsert: true
62+
}, config.config);
63+
64+
}
65+
})
66+
}
67+
68+
/**
69+
* Store html files by config sources
70+
**/
71+
if (sources) {
72+
sources.forEach(({path, collection, document_id, key, data}) => {
73+
if (!path) return;
74+
75+
let content = fs.readFileSync(path, 'utf8');
76+
77+
if (content && key && collection) {
78+
if (!data) data = {};
79+
80+
let storeData = {
81+
[key]: content,
82+
...data
83+
};
84+
if (!document_id) {
85+
CoCreateCreateDocument({
86+
collection,
87+
data: storeData,
88+
}, config.config)
89+
} else {
90+
CoCreateUpdateDocument({
91+
collection,
92+
document_id,
93+
data: storeData,
94+
upsert: true
95+
}, config.config)
96+
}
97+
}
98+
})
99+
}
100+
101+
setTimeout(function(){
102+
process.exit()
103+
}, 1000 * 30)
104+
105+

src.backup/index.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 ({path, 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+

0 commit comments

Comments
 (0)