Last active
October 12, 2017 08:22
-
-
Save jimlambie/c2736c84144157af170722d2d1b8e563 to your computer and use it in GitHub Desktop.
Pyncheon to Publish Migration Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/node | |
const fs = require('fs') | |
const path = require('path') | |
const args = process.argv | |
if (!args && args[2]) { | |
process.exit(1) | |
} | |
const directory = path.resolve(args[2]) | |
var files = [] | |
var getFiles = function (path, files) { | |
fs.readdirSync(path).forEach(file => { | |
var subpath = path + '/' + file | |
if (fs.lstatSync(subpath).isDirectory()) { | |
getFiles(subpath, files) | |
} else { | |
files.push(path + '/' + file) | |
} | |
}) | |
} | |
getFiles(directory, files) | |
files.forEach(file => { | |
if (path.extname(file) === '.json') { | |
let schema = require(file) | |
removeRedundantProperties(schema) | |
addPublishBlockToFields(schema) | |
addMultilineProperty(schema) | |
migrateOptionFields(schema) | |
migratePlacementField(schema) | |
migrateReferenceFields(schema) | |
addDisplayName(schema, file) | |
// backup? | |
if (args[3]) { | |
let backupFile = file.replace('.json', '.bak') | |
fs.rename(file, backupFile, err => { | |
if (err) throw err | |
fs.writeFile(file, JSON.stringify(schema, null, 2)) | |
}) | |
} else { | |
fs.writeFile(file, JSON.stringify(schema, null, 2)) | |
} | |
} | |
}) | |
function removeRedundantProperties (schema) { | |
Object.keys(schema.fields).forEach(key => { | |
let field = schema.fields[key] | |
if (field.hasOwnProperty('limit')) { | |
if (field.limit === '') delete field.limit | |
} | |
if (field.hasOwnProperty('comments') && field.comments === '') delete field.comments | |
if (field.hasOwnProperty('message') && field.message === '') delete field.message | |
}) | |
} | |
function addPublishBlockToFields (schema) { | |
Object.keys(schema.fields).forEach(key => { | |
let field = schema.fields[key] | |
field.publish = field.publish || {} | |
if (field.display) { | |
if (field.display.tab) field.publish.section = field.publish.section || field.display.tab | |
if (field.display.group) field.publish.subSection = field.publish.subSection || field.display.group | |
field.publish.display = field.publish.display || {} | |
if (field.display.hasOwnProperty('index')) field.publish.display.list = field.publish.display.list || field.display.index | |
if (field.display.hasOwnProperty('edit')) field.publish.display.edit = field.publish.display.edit || field.display.edit | |
} | |
}) | |
} | |
function addMultilineProperty (schema) { | |
Object.keys(schema.fields).forEach(key => { | |
let field = schema.fields[key] | |
if (field.display) { | |
if (field.display.subtype && field.display.subtype === 'Textarea') { | |
field.publish.multiline = true | |
} | |
} | |
}) | |
} | |
function migrateOptionFields (schema) { | |
Object.keys(schema.fields).forEach(key => { | |
let field = schema.fields[key] | |
if (field.display && field.display.options && !field.publish.options) { | |
field.publish.options = field.publish.options || [] | |
let options = field.display.options | |
options.sort((a, b) => { | |
if (a.option_title < b.option_title) return -1 | |
if (a.option_title > b.option_title) return 1 | |
return 0 | |
}) | |
options.forEach(option => { | |
let newOption = {} | |
newOption['value'] = option.option_value | |
newOption['label'] = option.option_title | |
field.publish.options.push(newOption) | |
}) | |
} | |
}) | |
} | |
function migratePlacementField (schema) { | |
Object.keys(schema.fields).forEach(key => { | |
let field = schema.fields[key] | |
if (field.placement) { | |
field.publish.placement = field.publish.placement || (field.placement === 'Main content' ? 'main' : field.placement) | |
delete field.placement | |
} | |
}) | |
} | |
function migrateReferenceFields (schema) { | |
Object.keys(schema.fields).forEach(key => { | |
let field = schema.fields[key] | |
if (field.display && field.display.subtype && field.display.subtype === 'Reference') { | |
let settings = field.display.query.split('/') | |
field.type = 'Reference' | |
field.settings = { | |
database: settings[2], | |
collection: settings[3] | |
} | |
} | |
}) | |
} | |
function addDisplayName (schema, file) { | |
if (!schema.settings.hasOwnProperty('displayName')) { | |
let displayName = path.basename(file).split('.')[1].replace(/([A-Z]+)/g, ' $1') | |
displayName = displayName.charAt(0).toUpperCase() + displayName.slice(1) | |
schema.settings.displayName = displayName | |
} | |
} | |
/* | |
"subVerdict_9_image" : [ | |
{ | |
"title" : "", | |
"description" : "", | |
"author" : "admin", | |
"original_filename" : "3:6.jpg", | |
"subdir" : "reviews_9", | |
"hash" : "616f5b886421ca78284abffb3a79fcd2619e8015", | |
"filename" : "616f5b886421ca78284abffb3a79fcd2619e8015.jpg", | |
"mimeType" : "image/jpg", | |
"apiVersion" : "1.0", | |
"createdAt" : 1463736831216, | |
"createdBy" : "wc-test", | |
"_id" : "573ed9ffd15d869b752ba088" | |
} | |
], | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment