|
| 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 | + |
0 commit comments