From aef1376d811faebdef4969e38f7330e176091f57 Mon Sep 17 00:00:00 2001 From: ikoamu Date: Wed, 24 Jan 2024 21:27:40 +0900 Subject: Add graphdata generation and note creation scripts --- action.yml | 26 ++++++++------ generateJson.js | 106 ------------------------------------------------------ generate_json.js | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ generate_notes.sh | 11 ++++++ package.json | 2 +- 5 files changed, 133 insertions(+), 118 deletions(-) delete mode 100644 generateJson.js create mode 100644 generate_json.js create mode 100755 generate_notes.sh diff --git a/action.yml b/action.yml index 027a0e6..ae2049a 100644 --- a/action.yml +++ b/action.yml @@ -27,18 +27,22 @@ runs: with: node-version: "20.x" - - name: print files - run: ls -la + - name: Install graphdata generator dependencies + working-directory: action + run: npm install shell: bash - # - name: Install dependencies - # run: npm install - # shell: bash + - name: Generate graphdata.json + working-directory: action + run: npm run generate --script_params=../orgs/org-roam.db + shell: bash - # - name: Generate graphdata.json - # run: npm run generate --script_params=./orgs/org-roam.db - # shell: bash + - name: Create Notes + working-directory: action + run: ./create_notes.sh + shell: bash - # - name: print graphdata.json - # run: cat graphdata.json - # shell: bash + - name: print files + run: ls -la + working-directory: action + shell: bash diff --git a/generateJson.js b/generateJson.js deleted file mode 100644 index be51cf0..0000000 --- a/generateJson.js +++ /dev/null @@ -1,106 +0,0 @@ -const sqlite3 = require('sqlite3').verbose(); -const dbFile = process.argv[2]; -const db = new sqlite3.Database(dbFile); -const fs = require('fs'); - -/** - * Parses the string of properties and returns an object containing the key-value pairs. - * If the key is 'FILE', the corresponding value is processed to extract the filename. - * @param {string} s - The string of properties. - * @returns {Object} - The object containing the parsed properties. - */ -function parseProperties(s) { - const properties = {}; - s.match(/\"(.*?)\" \. \"(.*?)\"/g).forEach(match => { - const [key, value] = match.match(/\"(.*?)\"/g).map(v => v.replace(/"/g, '')); - properties[key] = key === 'FILE' ? getFilename(value) : value; - }); - return removeQuotesFromObject(properties); -} - -/** - * Removes the double quotes from the object. - * @param {Object} obj - The object to be processed. - * @returns {Object} - The object without double quotes. - */ -function removeQuotesFromObject(obj) { - for (let key in obj) { - if (typeof obj[key] === 'string') { - obj[key] = obj[key].replace(/^"|"$/g, ''); - } else if (typeof obj[key] === 'object') { - obj[key] = removeQuotesFromObject(obj[key]); - } - } - return obj; -} - -/** - * Extracts the filename from the path. - * @param {string} path - The path to be processed. - * @returns {string} - The filename. - */ -function getFilename(path) { - return path.split('/').pop(); -} - -const queryNodes = ` -SELECT - tags.tag as tags, - nodes.properties, - nodes.olp, - nodes.pos, - nodes.level, - nodes.title, - nodes.file, - nodes.id -FROM - nodes -LEFT JOIN - tags -ON - nodes.id = tags.node_id -GROUP BY - nodes.id -`; - -const queryLinks = ` -SELECT - type, dest as target, source -FROM - links -WHERE - type = '"id"' -`; - -const queryTags = ` -SELECT - * -FROM - tags -`; - -const graphdata = { - type: "graphdata", - data: {}, -}; -db.all(queryNodes, (_, nodes) => - db.all(queryLinks, (_, links) => - db.all(queryTags, (_, tags) => { - graphdata.data.nodes = nodes.map(node => ({ - ...node, - tags: node.tags ?? [null], - file: getFilename(node.file), - properties: parseProperties(node.properties), - })); - graphdata.data.links = links; - graphdata.data.tags = tags.length ? tags : null; - - fs.writeFile('graphdata.json', JSON.stringify(removeQuotesFromObject(graphdata)), (err) => { - if (err) throw err; - console.log('The file has been saved!'); - }); - }), - ), -); - -db.close(); diff --git a/generate_json.js b/generate_json.js new file mode 100644 index 0000000..be51cf0 --- /dev/null +++ b/generate_json.js @@ -0,0 +1,106 @@ +const sqlite3 = require('sqlite3').verbose(); +const dbFile = process.argv[2]; +const db = new sqlite3.Database(dbFile); +const fs = require('fs'); + +/** + * Parses the string of properties and returns an object containing the key-value pairs. + * If the key is 'FILE', the corresponding value is processed to extract the filename. + * @param {string} s - The string of properties. + * @returns {Object} - The object containing the parsed properties. + */ +function parseProperties(s) { + const properties = {}; + s.match(/\"(.*?)\" \. \"(.*?)\"/g).forEach(match => { + const [key, value] = match.match(/\"(.*?)\"/g).map(v => v.replace(/"/g, '')); + properties[key] = key === 'FILE' ? getFilename(value) : value; + }); + return removeQuotesFromObject(properties); +} + +/** + * Removes the double quotes from the object. + * @param {Object} obj - The object to be processed. + * @returns {Object} - The object without double quotes. + */ +function removeQuotesFromObject(obj) { + for (let key in obj) { + if (typeof obj[key] === 'string') { + obj[key] = obj[key].replace(/^"|"$/g, ''); + } else if (typeof obj[key] === 'object') { + obj[key] = removeQuotesFromObject(obj[key]); + } + } + return obj; +} + +/** + * Extracts the filename from the path. + * @param {string} path - The path to be processed. + * @returns {string} - The filename. + */ +function getFilename(path) { + return path.split('/').pop(); +} + +const queryNodes = ` +SELECT + tags.tag as tags, + nodes.properties, + nodes.olp, + nodes.pos, + nodes.level, + nodes.title, + nodes.file, + nodes.id +FROM + nodes +LEFT JOIN + tags +ON + nodes.id = tags.node_id +GROUP BY + nodes.id +`; + +const queryLinks = ` +SELECT + type, dest as target, source +FROM + links +WHERE + type = '"id"' +`; + +const queryTags = ` +SELECT + * +FROM + tags +`; + +const graphdata = { + type: "graphdata", + data: {}, +}; +db.all(queryNodes, (_, nodes) => + db.all(queryLinks, (_, links) => + db.all(queryTags, (_, tags) => { + graphdata.data.nodes = nodes.map(node => ({ + ...node, + tags: node.tags ?? [null], + file: getFilename(node.file), + properties: parseProperties(node.properties), + })); + graphdata.data.links = links; + graphdata.data.tags = tags.length ? tags : null; + + fs.writeFile('graphdata.json', JSON.stringify(removeQuotesFromObject(graphdata)), (err) => { + if (err) throw err; + console.log('The file has been saved!'); + }); + }), + ), +); + +db.close(); diff --git a/generate_notes.sh b/generate_notes.sh new file mode 100755 index 0000000..05c727a --- /dev/null +++ b/generate_notes.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +mkdir -p notes + +cat graphdata.json | +jq -c '.data.nodes[]' | +while read -r nodes; do + id=$(echo "${nodes}" | jq -r '.id') + file=$(echo "${nodes}" | jq -r '.file') + cp -p "${file}" "notes/${id}" +done \ No newline at end of file diff --git a/package.json b/package.json index 31ca4a0..7a52956 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "", "scripts": { - "generate": "node ./generateJson.js $npm_config_script_params" + "generate": "node ./generate_json.js $npm_config_script_params" }, "keywords": [], "author": "", -- cgit