Skip to content

Commit

Permalink
Closes #188: Quiet mode for the file importer
Browse files Browse the repository at this point in the history
  • Loading branch information
jendib committed Mar 5, 2018
1 parent 1479b81 commit 2771e56
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
*.iml
node_modules
import_test
docs-importer-linux
docs-importer-macos
docs-importer-win.exe
5 changes: 5 additions & 0 deletions docs-importer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ docs-importer-win.exe (for Windows)

A wizard will ask you for the import configuration and write it in `~/.config/preferences/com.sismics.docs.importer.pref`

For the next start, pass the `-d` argument to skip the wizard:
```console
./docs-importer-linux -d
```

Daemon mode
-----------
The daemon mode scan the input directory every 30 seconds for new files. Once a file is found and imported, it is **deleted**.
Expand Down
58 changes: 44 additions & 14 deletions docs-importer/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ora = require('ora');
const inquirer = require('inquirer');
const preferences = require('preferences');
const fs = require('fs');
const argv = require('minimist')(process.argv);
const request = require('request').defaults({
jar: true
});
Expand Down Expand Up @@ -55,7 +56,6 @@ const askBaseUrl = () => {
});
});
};
askBaseUrl();

// Ask for credentials
const askCredentials = () => {
Expand Down Expand Up @@ -162,24 +162,42 @@ const askDaemon = () => {
// Save daemon
prefs.importer.daemon = answers.daemon;

// Save all preferences in case the program is sig-killed
prefs.save();

start();
});
};

// Start the import
// Start the importer
const start = () => {
if (prefs.importer.daemon) {
console.log('\nPolling the input folder for new files...');
request.post({
url: prefs.importer.baseUrl + '/api/user/login',
form: {
username: prefs.importer.username,
password: prefs.importer.password,
remember: true
}
}, function (error, response) {
if (error || !response || response.statusCode !== 200) {
console.error('\nUsername or password incorrect');
return;
}

let resolve = () => {
importFiles(true, () => {
setTimeout(resolve, 30000);
});
};
resolve();
} else {
importFiles(false, () => {});
}
// Start the actual import
if (prefs.importer.daemon) {
console.log('\nPolling the input folder for new files...');

let resolve = () => {
importFiles(true, () => {
setTimeout(resolve, 30000);
});
};
resolve();
} else {
importFiles(false, () => {});
}
});
};

// Import the files
Expand Down Expand Up @@ -227,4 +245,16 @@ const importFile = (file, remove, resolve) => {
}
resolve();
});
};
};

// Entrypoint: daemon mode or wizard
if (argv.hasOwnProperty('d')) {
console.log('Starting in quiet mode with the following configuration:\n' +
'Base URL: ' + prefs.importer.baseUrl + '\n' +
'Username: ' + prefs.importer.username + '\n' +
'Password: ***********\n' +
'Daemon mode: ' + prefs.importer.daemon);
start();
} else {
askBaseUrl();
}
15 changes: 11 additions & 4 deletions docs-importer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs-importer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docs-importer",
"version": "1.0.0",
"version": "1.5.1",
"description": "Import files to Sismics Docs",
"bin": "main.js",
"scripts": {
Expand All @@ -18,6 +18,7 @@
"homepage": "https://github.com/sismics/docs#readme",
"dependencies": {
"inquirer": "^5.1.0",
"minimist": "^1.2.0",
"ora": "^2.0.0",
"preferences": "^1.0.2",
"recursive-readdir": "^2.2.2",
Expand Down

0 comments on commit 2771e56

Please sign in to comment.