Skip to content

Commit

Permalink
for #3 first iteration of upsert cleanup using async await
Browse files Browse the repository at this point in the history
  • Loading branch information
victorkane committed Sep 6, 2018
1 parent d528fe5 commit 34cfa19
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ author: 'Victor Kane'
articleBody:
value: |
## Vestibulum ante ipsum primis in faucibus orci luctus

Et ultrices posuere cubilia Curae; Nullam venenatis nisl eu ultricies finibus. Suspendisse egestas id lorem sit amet auctor. Quisque tristique purus non metus dignissim, eu ultrices nisl condimentum. Proin facilisis id mi vel euismod. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam ipsum tellus, luctus lobortis gravida vitae, dictum in erat. Maecenas feugiat sed mauris eu facilisis. Curabitur porttitor lacinia feugiat.
articleSummary:
value: null
Expand Down
20 changes: 20 additions & 0 deletions cms/content/case-studies/one.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
metaData:
itemSlug: case-study-one
itemName: Case Study One
itemType: caseStudy
language: en-US
published: true
publishedDate: 2018-03-04T00:00:00
createdDate: 2018-03-04T00:00:00
modifiedDate: 2018-03-04T00:00:00
articles:
- weight: 1
article: what-is-content-migration-rescue
- weight: 2
article: the-content-migration-rescue-process
tags:
- tagSlug: ddcmr
tagName: Durable Drupal Content Migration Rescue
- tagSlug: content-migration-rescue
tagName: Content Migration Rescue

177 changes: 88 additions & 89 deletions cms/upsert/upsert.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// run with npm from project root: 'npm run upsert -- authors nora-alicia-perusin'
// else 'cd upsert; node upsert authors nora-alicia-perusin'

var fs = require('fs')
var rp = require('request-promise');
const fs = require('fs')
const YAML = require('yamljs');
const rp = require('request-promise');
require('dotenv').config()
// for console log
var util = require('util')
var YAML = require('yamljs');

var apiHost = 'http://' + process.env.API_HOST + ':' + process.env.API_PORT
// for more complete console log if necessary, such as the following:
// console.log(util.inspect(contentItem, { showHidden: false, depth: null}))
// const util = require('util')

const apiHost = 'http://' + process.env.API_HOST + ':' + process.env.API_PORT

if (process.argv.length > 3) {
contentType = process.argv[2]
Expand All @@ -19,99 +21,96 @@ if (process.argv.length > 3) {
process.exit(0)
}

async function processArticles(contentItem) {
const optionsGet = {
method: 'GET',
uri: apiHost + '/api/authors?itemName=' + contentItem.author + '&select=_id',
json: true
}
const authors = await rp(optionsGet)
if (authors.length > 0) {
contentItem.author = authors[0]._id
} else {
console.log("no author specified\n")
}
const optionsPut = {
method: 'PUT',
uri: apiHost + '/api/' + contentType,
body: contentItem,
json: true
}
const res = await rp(optionsPut)
console.log("res", res.message)
return res
}

async function processBooks(contentItem) {
const optionsGetAuthor = {
method: 'GET',
uri: apiHost + '/api/authors?itemName=' + contentItem.author + '&select=_id',
json: true
}
const authors = await rp(optionsGetAuthor)
if (authors.length > 0) {
contentItem.author = authors[0]._id
} else {
console.log("no author specified\n")
}
const optionsGetPublisher = {
method: 'GET',
uri: apiHost + '/api/publishers?itemName=' + contentItem.publisher + '&select=_id',
json: true
}
const publishers = await rp(optionsGetPublisher)
if (publishers.length > 0) {
contentItem.publisher = publishers[0]._id
} else {
console.log("no publisher specified\n")
}
const optionsPut = {
method: 'PUT',
uri: apiHost + '/api/' + contentType,
body: contentItem,
json: true
}
const res = await rp(optionsPut)
console.log("res", res.message)
return res
}

// doesn't have (i.e. populate) dependencies to grab first
async function processGenericContent(contentItem) {
const optionsPut = {
method: 'PUT',
uri: apiHost + '/api/' + contentType,
body: contentItem,
json: true
}
const res = await rp(optionsPut)
console.log("res", res.message)
return res
}

const file = './content/' + contentType + '/' + param + '.md'

fs.readFile(file, 'utf8', function (err,content) {
if (err) {
return console.log(err)
}
contentItem = YAML.parse(content);
// console.log(util.inspect(contentItem, { showHidden: false, depth: null}))
// return
// console.log('theUri', theUri)
let result = 0
if (contentType === 'articles') {
rp({
method: 'GET',
uri: apiHost + '/api/authors?itemName=' + contentItem.author + '&select=_id',
json: true
})
.then(function (authorbody) {
// console.log('author', authorbody)
if (typeof authorbody[0] === "undefined" ) {
console.log("Unknown author", contentItem.author + "\n")
} else {
// console.log('authorId', authorId)
contentItem.author = authorbody[0]._id
console.log('contentItem', contentItem)
rp({
method: 'PUT',
uri: apiHost + '/api/' + contentType,
body: contentItem,
json: true
})
.then(function (body) {
// console.log('body ', body)
})
.catch(function (err) {
console.log('error', err)
})
}
})
result = processArticles(contentItem)
} else if (contentType === 'books') {
// TODO support editor and/or author, at least one value of either
rp({
method: 'GET',
uri: apiHost + '/api/authors?itemName=' + contentItem.author + '&select=_id',
json: true
})
.then(function (authorbody) {
// console.log('author', authorbody)
if (typeof authorbody[0] === "undefined" ) {
console.log("Unknown author", contentItem.author + "\n")
} else {
contentItem.author = authorbody[0]._id
// get publisher
rp({
method: 'GET',
uri: apiHost + '/api/publishers?itemName=' + contentItem.publisher + '&select=_id',
json: true
})
.then(function (thePublishers) {
if (typeof thePublishers[0] === "undefined" ) {
console.log("Unknown publisher", contentItem.publisher + "\n")
} else {
// console.log('authorId', authorId)
contentItem.publisher = thePublishers[0]._id
// console.log('contentItem', contentItem)
rp({
method: 'PUT',
uri: apiHost + '/api/' + contentType,
body: contentItem,
json: true
})
.then(function (body) {
console.log('body ', body)
})
.catch(function (err) {
console.log('error', err)
})
}
})
}
})
result = processBooks(contentItem)
} else {
rp({
method: 'PUT',
uri: apiHost + '/api/' + contentType,
body: contentItem,
json: true
})
.then(function (body) {
console.log('body ', body)
})
.catch(function (err) {
console.log('error', err)
})
result = processGenericContent(contentItem)
}
try {
return Promise.resolve(result)
}
catch (error) {
return Promise.reject(error)
}
})

0 comments on commit 34cfa19

Please sign in to comment.