Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed May 1, 2021
1 parent efbfbfa commit 453a37a
Show file tree
Hide file tree
Showing 11 changed files with 385 additions and 49 deletions.
1 change: 1 addition & 0 deletions node_modules/@cocreate/crud-client

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/@cocreate/socket-client

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion result.json
Original file line number Diff line number Diff line change
@@ -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"}]]
[[{"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.
File renamed without changes.
106 changes: 106 additions & 0 deletions src.backup/extract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const extract = require('extract-comments');
const fs = require('fs');
const path = require("path")
const glob = require('glob');
const parseHtmlComments = require('parse-html-comments')

class ExtractComment {
constructor() {

}

run(filePath, collection, name) {
let content = fs.readFileSync(filePath, 'utf8');
let extension = path.extname(filePath)

let comments = [];
let docItems = [];

if (extension == '.html') {
comments = this.extractHtml(content)
} else {
comments = extract(content)
}

comments.forEach(({value}) => {
let ret_value = this.extractValue(value)
if (ret_value) {
docItems.push({
collection,
data: {
[name]:ret_value,
extension,
file_path: filePath
},
metaData: filePath
})
}
})
return docItems;
}

extractValue(valueStr) {
var regResult = /@value_start(?<value>.*?)@value_end/gs.exec(valueStr);
if (regResult) {
return regResult.groups.value.trim()
} else {
return null
}
}

extractHtml(content) {
let htmlComments = parseHtmlComments(content)
let result_comment = [];

htmlComments.matches.forEach(({groups}) => {
let comment_value = groups.commentOnly;
comment_value = comment_value.replace(/<!--|-->/gs, '');

result_comment.push({
value: comment_value
})
})
return result_comment;
}
}

const extractInstance = new ExtractComment()

function CoCreateExtract (directory, ignoreFolders, extensions ) {
let extensionsStr = "*";
let ignoreFolderStr = "";

if (extensions && extensions.length > 0) {
extensionsStr = extensions.join(',');
}

if (ignoreFolders && ignoreFolders.length > 0) {
ignoreFolderStr = ignoreFolders.join('|');
}

let result = [];

if (!directory) {
directory = ".";
}

const files = glob.sync(directory + `/**/*.{${extensionsStr}}`, {});

files.forEach((file) => {
var regex = new RegExp(ignoreFolderStr, 'g');
if (!regex.test(file)) {
const docData = extractInstance.run(file, 'docs', 'description');
if (docData.length > 0) {
const fileName = path.basename(file);
result.push(docData)
}
}
})

return result;
}



module.exports = CoCreateExtract

105 changes: 105 additions & 0 deletions src.backup/index.20210430.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const CoCreateExtract = require('./extract')
const fs = require('fs');
const path = require('path');
let config;

let jsConfig = path.resolve(process.cwd(), 'CoCreate.config.js');
let jsonConfig = path.resolve(process.cwd(), 'CoCreate.config.json')
if (fs.existsSync(jsConfig))
config = require(jsConfig);
else if (fs.existsSync(jsonConfig)) {
config = require(jsonConfig)
}
else {
process.exit()
console.log('config not found.')
}

const { crud, extract, sources } = config;
const { CoCreateSocketInit, CoCreateUpdateDocument, CoCreateCreateDocument } = require("./socket_process.js")
/**
* Socket init
*/
CoCreateSocketInit(config.config)

/**
* Extract comments and store into db
*/
if (extract) {
let result = CoCreateExtract(extract.directory, extract.ignores, extract.extensions);
fs.writeFileSync('result.json', JSON.stringify(result), 'utf8')

result.forEach((docs) => {
docs.forEach((doc) => {
if (!doc.collection) return;
if (!doc.document_id) {
CoCreateCreateDocument(doc, config.config);
} else {
CoCreateUpdateDocument(doc, config.config);
}
})
})
}


/**
* update and create document by config crud
*/

if (crud) {
crud.forEach(({collection, document_id, data}) => {
if (!document_id) {
CoCreateCreateDocument({
collection,
data
}, config.config);
} else {
CoCreateUpdateDocument({
collection,
document_id,
data,
upsert: true
}, config.config);

}
})
}

/**
* Store html files by config sources
**/
if (sources) {
sources.forEach(({path, collection, document_id, key, data}) => {
if (!path) return;

let content = fs.readFileSync(path, 'utf8');

if (content && key && collection) {
if (!data) data = {};

let storeData = {
[key]: content,
...data
};
if (!document_id) {
CoCreateCreateDocument({
collection,
data: storeData,
}, config.config)
} else {
CoCreateUpdateDocument({
collection,
document_id,
data: storeData,
upsert: true
}, config.config)
}
}
})
}

setTimeout(function(){
process.exit()
}, 1000 * 30)


File renamed without changes.
114 changes: 114 additions & 0 deletions src.backup/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const CoCreateCrud = require('@cocreate/crud-client')
const CoCreateSocket = require('@cocreate/socket-client')

const CoCreateExtract = require('./extract')

const fs = require('fs');
const path = require('path');
let config;

let jsConfig = path.resolve(process.cwd(), 'CoCreate.config.js');
let jsonConfig = path.resolve(process.cwd(), 'CoCreate.config.json')
if (fs.existsSync(jsConfig))
config = require(jsConfig);
else if (fs.existsSync(jsonConfig)) {
config = require(jsonConfig)
}
else {
process.exit()
console.log('config not found.')
}

const { crud, extract, sources, config : socketConfig } = config;

/** init cocreatecrud and socket **/
let socket = new CoCreateSocket("ws");
CoCreateCrud.setSocket(socket);
socket.create({
namespace: socketConfig.organization_Id,
room: null,
host: socketConfig.host
})

const commonParam = {
apiKey : socketConfig.apiKey,
organization_id : socketConfig.organization_Id,
broadcast: false
}

async function runStore (info, type) {
try {
let status = false;
const event = "docEvent" + Date.now()
if (!info.document_id) {
status = CoCreateCrud.createDocument({
...commonParam,
...info,
event
})
} else {
status = CoCreateCrud.updateDocument({
...commonParam,
...info,
upsert: true,
event
})
}
if (status) {

let response = await CoCreateCrud.listenAsync(event)
console.log('type ------------------------- ', type)
console.log(response)
}
} catch (err) {
console.log(err);
}
}

/**
* Extract comments and store into db
*/
if (extract) {
let result = CoCreateExtract(extract.directory, extract.ignores, extract.extensions);
fs.writeFileSync('result.json', JSON.stringify(result), 'utf8')

result.forEach((docs) => {
docs.forEach(async(doc) => {
if (!doc.collection) return;
await runStore(doc, 'extract')
})
})
}

/**
* update and create document by config crud
*/

if (crud) {
crud.forEach(async (info) => {
await runStore(info, 'crud')
})
}

/**
* Store html files by config sources
**/
if (sources) {
sources.forEach(async ({path, collection, document_id, key, data}) => {
if (!path) return;

let content = fs.readFileSync(path, 'utf8');

if (content && key && collection) {
if (!data) data = {};
let storeData = {
[key]: content,
...data
};
await runStore({collection, document_id, data: storeData}, 'sources');
}
})
}

console.log('end....')

File renamed without changes.
Loading

0 comments on commit 453a37a

Please sign in to comment.