Created
December 22, 2016 17:00
-
-
Save designeng/8818324d7e2c1ef86a16d027c8e834c3 to your computer and use it in GitHub Desktop.
Just a tool for removing some dev files from database
This file contains hidden or 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
import chalk from 'chalk'; | |
import prompt from 'prompt'; | |
import when from 'when'; | |
let Promise = when.promise; | |
import wire from 'essential-wire'; | |
import wireDebugPlugin from 'essential-wire/source/debug'; | |
import connectToDatabase from '../plugins/connectToDatabase'; | |
import { sitemapDbUrl } from '../../config/database/urls'; | |
import { CONNECTION_OPTIONS } from '../../config/constants'; | |
function promptPlugin() { | |
function runPromptWithSchema(resolver, compDef, wire) { | |
wire(compDef.options).then((options) => { | |
var schema = options.schema; | |
if(typeof schema === 'undefined') { | |
throw new Error('Schema should be defined!'); | |
} | |
prompt.start(); | |
prompt.get(schema, function (err, result) { | |
resolver.resolve(result); | |
}); | |
}); | |
} | |
return { | |
factories: { | |
runPromptWithSchema: runPromptWithSchema | |
} | |
} | |
} | |
const schema = ({ dbUrl }) => { | |
return { | |
properties: { | |
dbUrl: { | |
message: `Is it the right db url? ${chalk.blue(dbUrl)}`, | |
validator: /y[es]*|n[o]?/, | |
warning: 'Must respond yes or no', | |
default: 'yes' | |
} | |
} | |
}; | |
} | |
const promptSpec = { | |
$plugins: [ | |
promptPlugin, | |
], | |
schema: { | |
create: { | |
module: schema, | |
args: [{ | |
dbUrl: sitemapDbUrl, | |
}] | |
} | |
}, | |
promptQuiz: { | |
runPromptWithSchema: { | |
schema: {$ref: 'schema'} | |
} | |
} | |
} | |
// drop anyway | |
const dropCollection = (db, collName) => { | |
return Promise((resolve, reject) => { | |
db.collection(collName).drop((err, res) => { | |
resolve(); | |
}) | |
}); | |
} | |
const infoString = (url) => { | |
console.log('Removing sitemap files from database ' + chalk.green(url)); | |
} | |
let spec = { | |
$plugins: [ | |
// wireDebugPlugin, | |
connectToDatabase, | |
], | |
infoString: { | |
create: { | |
module: infoString, | |
args: [ | |
sitemapDbUrl, | |
] | |
} | |
}, | |
sitemapDb: { | |
connectToDatabase: { | |
url: sitemapDbUrl, | |
options: CONNECTION_OPTIONS, | |
} | |
}, | |
dropFilesCollection: { | |
create: { | |
module: dropCollection, | |
args: [ | |
{$ref: 'sitemapDb'}, | |
'fs.files', | |
] | |
} | |
}, | |
dropChunksCollection: { | |
create: { | |
module: dropCollection, | |
args: [ | |
{$ref: 'sitemapDb'}, | |
'fs.chunks', | |
] | |
} | |
} | |
} | |
function removeSitemapFiles() { | |
return wire(promptSpec).then((promptContext) => { | |
if(promptContext.promptQuiz.dbUrl.match(/y[es]?/)) { | |
wire(spec).then((context) => { | |
context.destroy(); | |
promptContext.destroy(); | |
process.exit(); | |
}); | |
} else { | |
process.exit(); | |
} | |
}).catch((err) => { | |
console.log('removeSitemapFiles:', err); | |
}); | |
} | |
// stand-alone npm task run | |
removeSitemapFiles(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment