Skip to content

Commit

Permalink
Fix bug where directory references would be incorrect after packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
AcroMace committed Jan 4, 2017
1 parent 79f4e0d commit df81697
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions js/botbrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const lunr = require('lunr');
const fs = require('fs');
const path = require('path');

const MAX_REPLIES_CONSIDERED = 100;
const LUNR_INDEX_FILE = 'lunrIndex.json';
Expand Down Expand Up @@ -89,7 +90,7 @@ class BotBrain {
_getLunrInstance() {
try {
// Parse the JSON and send it back
let instance = lunr.Index.load(JSON.parse(fs.readFileSync(LUNR_INDEX_FILE, 'utf8')));
let instance = lunr.Index.load(JSON.parse(fs.readFileSync(path.join(__dirname, '..', LUNR_INDEX_FILE), 'utf8')));
console.log('Found old Lunr instance');
this._isAlreadyTrained = true;
return instance;
Expand Down Expand Up @@ -127,7 +128,7 @@ class BotBrain {
// Returns an empty object if there were no previous saved messages
_getMessages() {
try {
return JSON.parse(fs.readFileSync(MESSAGES_FILE, 'utf8'));
return JSON.parse(fs.readFileSync(path.join(__dirname, '..', MESSAGES_FILE), 'utf8'));
} catch (err) {
return {};
}
Expand All @@ -141,7 +142,7 @@ class BotBrain {
// Serialize the object and save it with the file name
_saveFile(fileName, obj) {
const serializedObject = JSON.stringify(obj);
fs.writeFile(fileName, serializedObject, function(err) {
fs.writeFile(path.join(__dirname, '..', fileName), serializedObject, function(err) {
if (err) {
console.error('Could not save ' + fileName);
} else {
Expand Down
7 changes: 4 additions & 3 deletions js/profileExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

const fs = require('fs');
const path = require('path');
const htmlparser = require('htmlparser2');

const PROFILE_PICTURE_FILE = '/photos/profile.jpg';
Expand Down Expand Up @@ -55,8 +56,8 @@ class ProfileExtractor {

// Copy the profile picture from the archive to the root directory
copyProfilePicture() {
fs.createReadStream(this._dataExportDirectory + PROFILE_PICTURE_FILE)
.pipe(fs.createWriteStream(COPIED_PROFILE_PICTURE_FILE));
fs.createReadStream(path.join(this._dataExportDirectory, PROFILE_PICTURE_FILE))
.pipe(fs.createWriteStream(path.join(__dirname, '..', COPIED_PROFILE_PICTURE_FILE)));
}

// Fetch the previous names of the user
Expand Down Expand Up @@ -101,7 +102,7 @@ class ProfileExtractor {
});

// Start parsing
parser.write(fs.readFileSync(this._dataExportDirectory + PROFILE_FILE, 'utf8'));
parser.write(fs.readFileSync(path.join(this._dataExportDirectory, PROFILE_FILE), 'utf8'));
parser.end();

return names;
Expand Down

0 comments on commit df81697

Please sign in to comment.