From 011cff193dae173d3a98aabc09bdd03db57585fd Mon Sep 17 00:00:00 2001 From: Mike Lynch <mike@mikelynch.org> Date: Wed, 20 Nov 2019 13:58:31 +1100 Subject: [PATCH] Added simple deposit script --- simple-deposit.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 simple-deposit.js diff --git a/simple-deposit.js b/simple-deposit.js new file mode 100644 index 0000000..e762d82 --- /dev/null +++ b/simple-deposit.js @@ -0,0 +1,71 @@ +const path = require('path'); +const fs = require('fs-extra'); +const ocfl = require('ocfl'); +const OCFLRepository = require('ocfl').Repository; +const argv = require('yargs').argv; + + +async function connectRepo(repoPath) { + const repo = new OCFLRepository(); + try { + const stat = await fs.stat(repoPath); + if( stat.isDirectory() ) { + await repo.load(repoPath); + return repo; + } else { + console.error(`${repoPath} is not a directory`); + process.exit(-1); + } + } catch(e) { + console.error(`Stat ${repoPath} failed`); + console.error(e); + process.exit(-1); + + } +} + + +async function main() { + + const repoPath = argv.repo; + const objectId = argv.oid; + const contentDir = argv.content; + + if( ! repoPath ) { + console.error("Need an --repo to deposit to"); + return; + } + + + if( ! objectId ) { + console.error("Need an --oid to deposit to"); + return; + } + + if( ! contentDir ) { + console.error("Need a --content directory to deposit from"); + return; + } + + try { + const stat = await fs.stat(contentDir); + if( stat.isDirectory() ) { + const repo = await connectRepo(repoPath); + + console.log("Connected to " + repoPath); + + await repo.importNewObjectDir(objectId, contentDir); + + console.log(`Updated ${objectId}`); + } else { + console.error(`${contentDir} is not a directory`); + } + } catch( e ) { + console.error(`An error occured while ingesting ${contentDir}`); + console.error(e); + } +} + + +main(); + -- GitLab