Skip to content

Commit

Permalink
feat(import): add dryRun option and enable single file import
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Jan 25, 2017
1 parent 978c043 commit 6a28969
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
18 changes: 12 additions & 6 deletions bin/rest-on-couch-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ const importFiles = {};
const createdDirs = {};

program
.usage('<file>')
.usage('<file> <database> <kind>')
.option('-l, --limit <number>', 'Limit of files to import', Number)
.option('-w, --watch', 'Watch files')
.option('--continuous', 'Continuous mode. When import is finished, wait for some time and then import again')
.option('--wait <time>', 'Wait time in seconds between imports for continuous mode (default: 60)', Number, 60)
.option('--sort <order>', 'Sorting order of the files when to_processed is walked (default: asc)', String, 'asc')
.option('-c --config <path>', 'Path to custom config file')
.option('--dry-run', 'Do all the steps without updating the database')
.parse(process.argv);

if (program.sort !== 'asc' && program.sort !== 'desc') {
Expand Down Expand Up @@ -312,11 +313,16 @@ function shouldIgnore(name) {
}

if (program.args[0]) {
debug(`file argument: ${program.args[0]}`);
// TODO add 2 arguments: db and import names
throw new Error('not ready');
//const file = path.resolve(program.args[0]);
//imp.import(config, file);
if (program.args.length !== 3) {
program.help();
}
debug(`Import with arguments: ${program.args.join(' ')}`);
const file = path.resolve(program.args[0]);
const database = program.args[1];
const kind = program.args[2];
return imp.import(database, kind, file, {dryRun: program.dryRun})
.then(() => console.error('Imported successfully'))
.catch((e) => console.error(`Import error:\n${e.stack}`));
} else if (program.watch) {
// watch files to import
let homeDir = getHomeDir();
Expand Down
7 changes: 6 additions & 1 deletion src/import/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ const Couch = require('../index');
const debug = require('../util/debug')('import');
const getConfig = require('../config/config').getConfig;

exports.import = async function (database, importName, file) {
exports.import = async function (database, importName, file, options) {
debug(`import ${file} (${database}, ${importName})`);

options = options || {};
const dryRun = !!options.dryRun;

const filename = path.parse(file).base;
let contents = fs.readFileSync(file);

Expand Down Expand Up @@ -53,6 +56,8 @@ exports.import = async function (database, importName, file) {
}
}

if (dryRun) return;

try {
const docInfo = await checkDocumentExists(info, filename, contents, couch);
await updateDocument(info, docInfo, isParse, isJson, filename, contents, couch);
Expand Down

0 comments on commit 6a28969

Please sign in to comment.