diff --git a/package.json b/package.json index d1a1fd63307a99404bd760ecdd10550813ce64fa..9e0afa158de9e1a90ab1e6f0f751ed98a07fd742 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "jsonld": "^1.6.2", "nunjucks": "^3.2.0", "ocfl": "^1.0.3", - "ro-crate": "^1.2.8", + "ro-crate": "^1.2.10", "uuid": "^3.3.3", "yargs": "^13.3.0" } diff --git a/ro-crate-deposit.js b/ro-crate-deposit.js index 2041b8cb9f6e1689f5a19900a32627090a878087..493f7f76398130c6ac3df2e072f3b34f943fe01b 100644 --- a/ro-crate-deposit.js +++ b/ro-crate-deposit.js @@ -27,6 +27,39 @@ async function connectRepo(repoPath) { } +async function checkin(repo, repoName, rocratePath) { + const rocrateFile = path.join(rocratePath, "ro-crate-metadata.jsonld"); + try { + const jsonld = await fs.readJson(rocrateFile); + const crate = new ROCrate(jsonld); + crate.index(); + const dataset = crate.getRootDataset(); + + console.log("Ingesting ro-crate " + dataset['name']); + + const existingId = crate.getNamedIdentifier(repoName); + + if( existingId ) { + console.log(`Local identifier found ${repoName}/${existingId}`); + await repo.importNewObjectDir(existingId, rocratePath); + console.log(`Updated ${existingId}`); + } else { + const newId = uuid(); + console.log(`Minting new local identifier ${repoName}/${newId}`); + await repo.importNewObjectDir(newId, rocratePath); + console.log(`Imported ${rocratePath} ${dataset['name']} ${newId}`); + crate.addIdentifier({name: repoName, identifier: newId}); + await fs.writeJson(rocrateFile, crate.getJson(), {spaces: 2}); + await repo.importNewObjectDir(newId, rocratePath); + console.log(`Updated ${rocratePath} ${newId} metadata with identifier`); + } + } catch(e) { + console.log(`Error importing ${rocratePath}`); + console.log(e); + } +} + + async function main() { @@ -35,22 +68,8 @@ async function main() { const repo = await connectRepo(repoPath); - for (let fromPath of argv._) { - const rocrateFile = path.join(fromPath, "ro-crate-metadata.jsonld"); - try { - const jsonld = await fs.readJson(rocrateFile); - crate = new ROCrate(jsonld); - crate.index(); - const dataset = crate.getRootDataset(); - const local_id = uuid(); - crate.addIdentifier({name: repoName, identifier: local_id}); - await fs.writeJson(crate.getJson(), rocrateFile); - const ocflObject = await repo.importNewObjectDir(local_id, fromPath); - console.log(`Imported ${fromPath} ${dataset['title']} ${local_id}`); - } catch(e) { - console.log(`Error importing ${fromPath}`); - console.log(e); - } + for (let rocratePath of argv._) { + await checkin(repo, repoName, rocratePath); } }