Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/deepcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export default {

config,

async initialize(sharedState) {
async initialize() {
const dbConnected = await DB.init();
Logger.log(`DB connected: ${dbConnected}`);

const sharedState = await DB.restoreState(true);
const projectState = await DB.restoreState();

Store.init(sharedState, projectState);

Logger.log('Activating plugin');
Expand Down Expand Up @@ -109,6 +111,8 @@ export default {

serialize() {
const sharedState = Store.getSharedState();

CommonUtils.saveSharedStore();
CommonUtils.saveProjectStore();

const now = new Date();
Expand Down
5 changes: 4 additions & 1 deletion lib/modules/Analyser.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ class Analyser {
return Promise.resolve({
status: ANALYSIS_STATUS.failed,
completed: 0,
analysisResults: {},
analysisResults: {
origin: {},
table: [],
},
analysisURL: '',
});
}
Expand Down
47 changes: 31 additions & 16 deletions lib/modules/CommonUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use babel';

import { groupBy, isArray, keys, values, find } from 'lodash';
import { groupBy, keys, values, find } from 'lodash';

import { DB } from './Database';
import { Store } from './Store';
Expand All @@ -16,20 +16,30 @@ export class CommonUtils {
});
}

static saveSharedStore() {
const sharedState = Store.getSharedState();

setTimeout(async () => {
try {
await DB.storeState(sharedState, true);

} catch (err) {
Logger.log(err);
Logger.dbError(err);
}
}, 0);
}

static saveProjectStore() {
const projectState = Store.getProjectState();

setTimeout(async () => {
try {
await DB.storeState(projectState);

} catch (err) {
Logger.log(err);
let errors = localStorage.getItem('DeepCode:DB:Errors') || [];
if (!isArray(errors)) {
errors = [];
}
errors.unshift(`${new Date()}: ${err.message}`);
localStorage.setItem('DeepCode:DB:Errors', JSON.stringify(errors.slice(0, 10)));
Logger.dbError(err);
}
}, 0);
}
Expand Down Expand Up @@ -74,18 +84,23 @@ export class CommonUtils {
// Others -----------------------------------------------------------------------------------------------------------

static getIndicators() {
const { table, origin } = Store.get(STORE_KEYS.analysisResults);
if (!isArray(table)) {
return {
info: 0,
warning: 0,
critical: 0,
total: 0,
files: 0,
};
const defaultValue = {
info: 0,
warning: 0,
critical: 0,
total: 0,
files: 0,
};

const { origin } = Store.get(STORE_KEYS.analysisResults);
if (!origin) {
return defaultValue;
}

const { files, suggestions } = origin;
if (!files || !suggestions) {
return defaultValue;
}

const filesCount = keys(files).length;
const suggestionsList = values(suggestions);
Expand Down
9 changes: 5 additions & 4 deletions lib/modules/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { FileUtils } from './FileUtils';
import { Logger } from './Logger';
import { PLUGIN } from '../constants/common';
import { STORE_KEYS } from '../constants/store';

const tName = 'projects';

Expand Down Expand Up @@ -53,12 +54,12 @@ class Database {
return Promise.resolve(result);
}

async storeState(state) {
async storeState(state, isShared = false) {
if (!this.db) {
return Promise.reject('DB is not running');
}

const path = FileUtils.getMainProjectPath();
const path = isShared ? STORE_KEYS.state : FileUtils.getMainProjectPath();
if (!path) {
Logger.log('No confirmed main project path. Storing project state failed');
return Promise.resolve();
Expand All @@ -79,12 +80,12 @@ class Database {
});
}

async restoreState() {
async restoreState(isShared = false) {
if (!this.db) {
return Promise.reject('DB is not running');
}

const path = FileUtils.getMainProjectPath();
const path = isShared ? STORE_KEYS.state : FileUtils.getMainProjectPath();
if (!path) {
Logger.log('No confirmed main project path. Restoring project state failed');
return Promise.resolve({});
Expand Down
6 changes: 5 additions & 1 deletion lib/modules/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class FileUtils {
return [];
}

this.projectPaths = projectPaths;

return projectPaths;
}

Expand Down Expand Up @@ -85,7 +87,9 @@ class FileUtils {

getUnconfirmedProjectFolders() {
const confirmedFolders = Store.get(STORE_KEYS.confirmedFolders);
return this.projectPaths.filter(projectFolder => !confirmedFolders.includes(projectFolder));
const projectPaths = this.getProjectPaths();

return projectPaths.filter(projectFolder => !confirmedFolders.includes(projectFolder));
}

isIgnored(filePath) {
Expand Down
10 changes: 10 additions & 0 deletions lib/modules/Logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use babel';

import { isArray } from 'lodash';
import { PLUGIN } from '../constants/common';

const format = (num) => (`0${num}`).slice(-2);
Expand All @@ -25,4 +26,13 @@ export class Logger {
// eslint-disable-next-line no-console
console.log(time, ...args);
}

static dbError(err = {}) {
let errors = localStorage.getItem('DeepCode:DB:Errors') || [];
if (!isArray(errors)) {
errors = [];
}
errors.unshift(`${new Date()}: ${err.message}`);
localStorage.setItem('DeepCode:DB:Errors', JSON.stringify(errors.slice(0, 30)));
}
}
2 changes: 1 addition & 1 deletion lib/modules/PluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class PluginManager {
}

if (firstStart) {
atom.workspace.open(PLUGIN.problemsPanelURI);
// atom.workspace.open(PLUGIN.problemsPanelURI);
Store.set(STORE_KEYS.firstStart, false);
}
}, 0);
Expand Down
20 changes: 9 additions & 11 deletions spec/deepcode-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('Deepcode Plugin tests', () => {
let workspaceElement;
let activationPromise;
let dcPackage;
let timerCallback;

beforeEach(() => {
workspaceElement = atom.views.getView(atom.workspace);
Expand All @@ -23,6 +24,9 @@ describe('Deepcode Plugin tests', () => {

return Promise.resolve();
}

timerCallback = jasmine.createSpy('timerCallback');
jasmine.Clock.useMock();
});

describe('Pre-test configuring', () => {
Expand Down Expand Up @@ -62,24 +66,18 @@ describe('Deepcode Plugin tests', () => {
});

it('fetched filters from server', () => {

dcPackage.setPluginState({
[STORE_KEYS.allowedFiles]: {},
});

const checkFiltersPromise = new Promise(resolve => {
runs(() => {
dcPackage.checkFilters();

setTimeout(() => {
resolve();
}, 100);
const state = dcPackage.getPluginState();
expect(state[STORE_KEYS.allowedFiles]).toEqual(mockState[STORE_KEYS.allowedFiles]);
}, 1000);
});

waitsForPromise(() => checkFiltersPromise);

const state = dcPackage.getPluginState();
console.log('deepcode-spec.js, [75]: ', { state });

expect(state[STORE_KEYS.allowedFiles]).toEqual(mockState[STORE_KEYS.allowedFiles]);
})
})
});