Skip to content

Commit

Permalink
feat(config): add cache flag
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Nov 28, 2020
1 parent 36587e8 commit 5d8d21d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ module.exports = {
/** Optional boolean flag used to set CI mode. process.env.CI is used when not set. */
CI: false,

/** Optional boolean flag used to enable caching of cloned repositories. For CI's it's ideal to disable caching. Defauls to true. */
cache: true,

/**
* Optional callback invoked once scan is complete.
*
Expand Down
2 changes: 2 additions & 0 deletions eslint-remote-tester.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ module.exports = {
extends: ['eslint:recommended'],
},

cache: true,

/**
* Optional callback invoked once scan is complete.
*
Expand Down
3 changes: 3 additions & 0 deletions lib/config/config-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export const CONFIGURATION_FILE_TEMPLATE =
/** Optional boolean flag used to set CI mode. process.env.CI is used when not set. */
CI: false,
/** Optional boolean flag used to enable caching of cloned repositories. For CI's it's ideal to disable caching. Defauls to true. */
cache: true,
/**
* Optional callback invoked once scan is complete.
*
Expand Down
1 change: 1 addition & 0 deletions lib/config/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export interface Config {
concurrentTasks: number;
eslintrc: Linter.Config;
CI: boolean;
cache: boolean;
onComplete?: (results: ResultTemplateOptions[]) => Promise<void> | void;
}
7 changes: 7 additions & 0 deletions lib/config/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function constructAndValidateConfiguration(
concurrentTasks,
eslintrc,
CI,
cache,
onComplete,
} = configToValidate;

Expand Down Expand Up @@ -62,6 +63,12 @@ export default function constructAndValidateConfiguration(
config.CI = CI == null ? process.env.CI === 'true' : CI;
}

if (cache != null && typeof cache !== 'boolean') {
errors.push(`cache (${cache}) should be a boolean.`);
} else if (cache == null) {
config.cache = true;
}

if (resultParser && !RESULT_PARSERS.includes(resultParser)) {
errors.push(
`resultParser (${resultParser}) is not valid value. Known values are ${RESULT_PARSERS.join(
Expand Down
6 changes: 5 additions & 1 deletion lib/engine/worker-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ESLint, Linter } from 'eslint';

import { LintMessage, WorkerData } from './types';
import config from '@config';
import { getFiles, SourceFile } from '@file-client';
import { getFiles, removeCachedRepository, SourceFile } from '@file-client';

export type WorkerMessage =
| { type: 'START' }
Expand Down Expand Up @@ -216,5 +216,9 @@ export default async function workerTask(): Promise<void> {
postMessage({ type: 'FILE_LINT_END', payload: index + 1 });
}

if (!config.cache) {
await removeCachedRepository(repository);
}

postMessage({ type: 'LINT_END', payload: results });
}
2 changes: 1 addition & 1 deletion lib/file-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export {
RESULTS_LOCATION,
RESULT_TEMPLATE,
} from './results-writer';
export { CACHE_LOCATION } from './repository-client';
export { CACHE_LOCATION, removeCachedRepository } from './repository-client';
export { default as ResultsStore } from './results-store';
export { RESULTS_TEMPLATE_CI_BASE } from './result-templates';
13 changes: 13 additions & 0 deletions lib/file-client/repository-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,16 @@ export async function cloneRepository({
}
}
}

/**
* Remove repository from the cache
*/
export async function removeCachedRepository(
repository: string
): Promise<void> {
const repoLocation = `${CACHE_LOCATION}/${repository}`;

if (fs.existsSync(repoLocation)) {
fs.rmdirSync(repoLocation, { recursive: true });
}
}
1 change: 1 addition & 0 deletions test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const DEFAULT_CONFIGURATION: Config = {
concurrentTasks: undefined as any,
eslintrc: {},
CI: undefined as any,
cache: undefined as any,
};

describe('Config validator', () => {
Expand Down

0 comments on commit 5d8d21d

Please sign in to comment.