<commit_before>import * as fs from "fs-extra";
import * as webpack from "webpack";
import * as _ from "lodash";
import * as md5 from "md5";
import { getDependency, PackageDependency } from "./utils/packageDependency";

export type DllEntry = string | string[] | webpack.Entry;

export interface DllConfigFile {
	entry: DllEntry;
	outputJSNames: string[];
}

export interface ManifestCache {
	configFiles: { [index: string]: DllConfigFile };
	currentConfigIndex: string;
	entryVersion: PackageDependency;
}

export interface CacheOptions {
	configIndex: string;
	entry: DllEntry;
	cacheDir: { js: string; json: string };
	manifestFile: string;
}

export class CacheController {
	private manifestCache: ManifestCache;
	private currentConfigContent: DllConfigFile;
	private configIndex: string;
	private shouldUpdate: boolean;
	private cacheJSDir: string;
	private cacheJSONDir: string;
	private manifestFile: string;

	constructor(options: CacheOptions) {
		const { cacheDir, configIndex, manifestFile } = options;

		this.configIndex = configIndex;

		this.manifestFile = manifestFile;
		this.cacheJSDir = cacheDir.js;
		this.cacheJSONDir = cacheDir.json;

		this.readCacheFile();
		this.checkCache(options.entry);
	}

	private readCacheFile() {
		try {
			const content = fs.readFileSync(this.manifestFile);
			this.manifestCache = JSON.parse(content.toString());
		} catch (e) {
			this.manifestCache = {
				configFiles: {},
				currentConfigIndex: "",
				entryVersion: {}
			};
		}

		this.currentConfigContent = this.manifestCache.configFiles[
			this.configIndex
		] || { entry: "", outputJSNames: [] };
	}

	private checkCache(entry: DllEntry) {
		let updateEntry = !_.isEqual(this.currentConfigContent.entry, entry);
		if (updateEntry) {
			this.updateEntryCache(entry);
		}

		const entryVersion = getDependency(entry);
		let updateDependency = !_.isEqual(this.manifestCache.entryVersion, entryVersion);

		if (updateDependency) {
			this.manifestCache.entryVersion = entryVersion;
		}

		this.shouldUpdate = updateDependency || updateEntry;
		this.manifestCache.currentConfigIndex = this.configIndex;
	}

	public writeCache() {
		fs.writeFileSync(this.manifestFile, JSON.stringify(this.manifestCache));
	}

	public updateEntryCache(val: DllEntry) {
		this.manifestCache.configFiles[
			this.configIndex
		] = this.currentConfigContent = Object.assign(
			{},
			this.currentConfigContent,
			{ entry: val }
		);
	}

	public updateJSNamesCache(val: string[]) {
		this.manifestCache.configFiles[
			this.configIndex
		] = this.currentConfigContent = Object.assign(
			{},
			this.currentConfigContent,
			{ outputJSNames: val }
		);
	}
<commit_msg>refactor code.(remove check entry)<commit_after>import * as fs from "fs-extra";
import * as webpack from "webpack";
import * as _ from "lodash";
import * as md5 from "md5";
import { getDependency, PackageDependency } from "./utils/packageDependency";

export type DllEntry = string | string[] | webpack.Entry;

export interface DllConfigFile {
	outputJSNames: string[];
}

export interface ManifestCache {
	configFiles: { [index: string]: DllConfigFile };
	entryVersion: PackageDependency;
}

export interface CacheOptions {
	configIndex: string;
	entry: DllEntry;
	cacheDir: { js: string; json: string };
	manifestFile: string;
}

export class CacheController {
	private manifestCache: ManifestCache;
	private currentConfigContent: DllConfigFile;
	private configIndex: string;
	private shouldUpdate: boolean;
	private cacheJSDir: string;
	private cacheJSONDir: string;
	private manifestFile: string;

	constructor(options: CacheOptions) {
		const { cacheDir, configIndex, manifestFile } = options;

		this.configIndex = configIndex;

		this.manifestFile = manifestFile;
		this.cacheJSDir = cacheDir.js;
		this.cacheJSONDir = cacheDir.json;

		this.readCacheFile();
		this.checkCache(options.entry);
	}

	private readCacheFile() {
		try {
			const content = fs.readFileSync(this.manifestFile);
			this.manifestCache = JSON.parse(content.toString());
		} catch (e) {
			this.manifestCache = {
				configFiles: {},
				entryVersion: {}
			};
		}

		this.currentConfigContent = this.manifestCache.configFiles[
			this.configIndex
		] || { outputJSNames: [] };
	}

	private checkCache(entry: DllEntry) {
		const entryVersion = getDependency(entry);
		this.shouldUpdate =
			this.currentConfigContent.outputJSNames.length === 0 ||
			!_.isEqual(this.manifestCache.entryVersion, entryVersion);
		this.manifestCache.entryVersion = entryVersion;
	}

	public writeCache() {
		fs.writeFileSync(this.manifestFile, JSON.stringify(this.manifestCache));
	}

	public updateJSNamesCache(val: string[]) {
		this.manifestCache.configFiles[
			this.configIndex
		] = this.currentConfigContent = Object.assign(
			{},
			this.currentConfigContent,
			{ outputJSNames: val }
		);
	}

{'fraction_non_alphanumeric': 0.1133250311332503, 'fraction_numerical': 0.0010377750103777502, 'mean_word_length': 4.716488730723606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1422618', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1453, 'n_words': 318}