Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add detect clones and statistic api #549

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/api/example-detectClonesAndStatistic.ts
@@ -0,0 +1,11 @@
import {detectClonesAndStatistic} from "jscpd";

(async () => {
const data = await detectClonesAndStatistic({
path: [
__dirname + '/../fixtures'
],
silent: true
});
console.log(data);
})()
13 changes: 12 additions & 1 deletion packages/jscpd/__tests__/options.test.ts
@@ -1,7 +1,7 @@
import {expect} from 'chai';
import {isAbsolute} from 'path';
import {IClone} from '@jscpd/core';
import {jscpd, detectClones} from '../src';
import {jscpd, detectClones, detectClonesAndStatistic} from '../src';
import {bold, yellow} from 'colors/safe';
import sinon = require('sinon');

Expand Down Expand Up @@ -146,6 +146,17 @@ describe('jscpd options', () => {
_log(log.firstCall);
expect(log.callCount).to.equal(0);
});

it('should not print information about clones and statistic', async () => {
// console.log = _log;
await detectClonesAndStatistic({
silent: true,
pattern: fileWithClones,
});
const log = (console.log as any);
_log(log.firstCall);
expect(log.callCount).to.equal(0);
});
});

describe('Not Supported Format', () => {
Expand Down
13 changes: 10 additions & 3 deletions packages/jscpd/src/index.ts
@@ -1,4 +1,4 @@
import { getDefaultOptions, IClone, IOptions, IStore, Statistic } from '@jscpd/core';
import { getDefaultOptions, IClone, IOptions, IStore, Statistic, IStatistic } from '@jscpd/core';
import { grey, italic } from 'colors/safe';
import { EntryWithContent, getFilesToDetect, InFilesDetector } from '@jscpd/finder';
import { initCli, initOptionsFromCli } from './init';
Expand All @@ -12,7 +12,7 @@ import { registerHooks } from './init/hooks';

const TIMER_LABEL = 'Detection time:';

export const detectClones = (opts: IOptions, store: IStore<IMapFrame> | undefined = undefined): Promise<IClone[]> => {
export const detectClones = (opts: IOptions, store: IStore<IMapFrame> | undefined = undefined, statisticInstance: Statistic | undefined = undefined): Promise<IClone[]> => {
const options: Partial<IOptions> = {...getDefaultOptions(), ...opts};
options.format = options.format || getSupportedFormats();

Expand All @@ -22,7 +22,7 @@ export const detectClones = (opts: IOptions, store: IStore<IMapFrame> | undefine
}
options.hashFunction = options.hashFunction || hashFunction;
const currentStore: IStore<IMapFrame> = store || getStore(options.store);
const statistic = new Statistic();
const statistic = statisticInstance || new Statistic();
const tokenizer = new Tokenizer();
const detector = new InFilesDetector(tokenizer, currentStore, statistic, options);

Expand All @@ -41,6 +41,13 @@ export const detectClones = (opts: IOptions, store: IStore<IMapFrame> | undefine
});
}

export const detectClonesAndStatistic = (opts: IOptions, store: IStore<IMapFrame> | undefined = undefined): Promise<{clones: IClone[], statisticData: IStatistic}> => {
const statistic = new Statistic();
return detectClones(opts, store, statistic).then((clones: IClone[]) => {
return {clones, statisticData: statistic.getStatistic()};
});
}

export async function jscpd(argv: string[], exitCallback?: (code: number) => {}) {
const packageJson = require(__dirname + '/../package.json');

Expand Down