|
| 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 | + // let content = fs.readFileSync(jsonConfig, 'utf8').toString(); |
| 12 | + // console.log(jsonConfig, content); |
| 13 | + config = require(jsonConfig) |
| 14 | + // config = JSON.parse(content); |
| 15 | +} |
| 16 | +else { |
| 17 | + console.log('config not found.') |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +const { directory, ignores, extensions, socket, sources } = config; |
| 22 | +const { CoCreateSocketInit, CoCreateUpdateDocument, CoCreateCreateDocument } = require("./socket_process.js") |
| 23 | +/** |
| 24 | + * Socket init |
| 25 | + */ |
| 26 | +CoCreateSocketInit(socket) |
| 27 | + |
| 28 | +/** |
| 29 | + * Extract comments |
| 30 | + */ |
| 31 | +let result = CoCreateExtract(directory, ignores, extensions); |
| 32 | +fs.writeFileSync('result.json', JSON.stringify(result), 'utf8') |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * Store data into db |
| 37 | + */ |
| 38 | +// result.forEach((docs) => { |
| 39 | +// docs.forEach((doc) => { |
| 40 | +// if (!doc.document_id) { |
| 41 | +// CoCreateCreateDocument(doc, socket.config); |
| 42 | +// } else { |
| 43 | +// CoCreateUpdateDocument(doc, socket.config); |
| 44 | +// } |
| 45 | +// }) |
| 46 | +// }) |
| 47 | + |
| 48 | +/** |
| 49 | + * update document by config sources |
| 50 | + */ |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +sources.forEach(({ path, collection, document_id, name, category, ...rest }) => { |
| 55 | + if (!path) return; |
| 56 | + // let content = fs.readFileSync(path, 'utf8'); |
| 57 | + // console.log(content) |
| 58 | + process.exit() |
| 59 | + if (content) { |
| 60 | + CoCreateUpdateDocument({ |
| 61 | + collection, |
| 62 | + document_id, |
| 63 | + data: { |
| 64 | + [name]: content, |
| 65 | + category, |
| 66 | + ...rest |
| 67 | + }, |
| 68 | + upsert: true |
| 69 | + }, socket.config); |
| 70 | + } |
| 71 | +}) |
| 72 | + |
0 commit comments