🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

code-tracker

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

code-tracker - npm Package Compare versions

Comparing version
0.2.5
to
0.2.6
+2
-2
dist/index.d.ts
import type { ICodeMetrics } from './types';
export declare function trackCodeUsage(file: string, linesOfCode: number): void;
export declare function updateCodeUsage(file: string, newLinesOfCode: number): void;
export declare function trackCodeUsage(file: string): Promise<void>;
export declare function updateCodeUsage(file: string): Promise<void>;
export declare function removeCodeUsage(file: string): void;
export declare function getCodeMetrics(): ICodeMetrics;
import { codeTrackerInstance } from './lib/tracker';
// Utility functions to use with the CodeTracker class
export function trackCodeUsage(file, linesOfCode) {
codeTrackerInstance.trackFile(file, linesOfCode);
export async function trackCodeUsage(file) {
await codeTrackerInstance.trackFile(file);
}
export function updateCodeUsage(file, newLinesOfCode) {
codeTrackerInstance.updateFile(file, newLinesOfCode);
export async function updateCodeUsage(file) {
await codeTrackerInstance.updateFile(file);
}

@@ -9,0 +9,0 @@ export function removeCodeUsage(file) {

@@ -5,5 +5,6 @@ import type { ITrackingStrategy, IStorageStrategy, IFileTracker } from '../../types';

private files;
trackFile(file: string, linesOfCode: number): void;
updateFile(file: string, newLinesOfCode: number): void;
removeFile(file: string): void;
private watchers;
trackFile(filePath: string): Promise<void>;
updateFile(filePath: string): Promise<void>;
removeFile(filePath: string): void;
getFiles(): IFileTracker;

@@ -10,0 +11,0 @@ getTotalLinesOfCode(): number;

@@ -0,1 +1,3 @@

import * as fs from "fs";
import chokidar from "chokidar";
export class DefaultTrackingStrategy {

@@ -16,22 +18,65 @@ constructor() {

});
Object.defineProperty(this, "watchers", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
}
trackFile(file, linesOfCode) {
// Implementation of adding a file
this.files[file] = linesOfCode;
this.totalLinesOfCode += linesOfCode;
async trackFile(filePath) {
return new Promise(async (resolve, reject) => {
const currentWorkingDirectory = process.cwd();
const absoluteFilePath = `${currentWorkingDirectory}/${filePath}`;
if (this.files[filePath]) {
throw new Error(`File "${filePath}" is already being tracked.`);
}
// Implementation of adding a file
await this.updateFile(filePath);
const watcher = chokidar.watch(filePath, {
persistent: true,
ignoreInitial: true,
awaitWriteFinish: true,
});
watcher.on('change', async () => {
await this.updateFile(filePath);
});
// const lines = data.split('\n');
// this.files[filePath] = linesOfCode
// this.totalLinesOfCode += linesOfCode
resolve();
});
}
updateFile(file, newLinesOfCode) {
updateFile(filePath) {
// Implementation of updating a file
const oldLinesOfCode = this.files[file];
this.totalLinesOfCode -= oldLinesOfCode;
this.totalLinesOfCode += newLinesOfCode;
this.files[file] = newLinesOfCode;
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err)
throw err;
console.log(data);
// Determine number of lines of code
const lines = data.split('\n');
const linesOfCode = lines.filter(line => {
const trimmedLine = line.trim();
return trimmedLine !== '' && !trimmedLine.startsWith('//');
}).length;
this.files[filePath] = linesOfCode;
console.log(`The file ${filePath} has ${linesOfCode} lines of code`);
resolve();
});
});
}
removeFile(file) {
// Implementation of removing a file
this.totalLinesOfCode -= this.files[file];
delete this.files[file];
removeFile(filePath) {
if (!this.files[filePath]) {
console.warn(`File "${filePath}" is not being tracked.`);
return;
}
// Close the watcher if you have one
this.watchers[filePath]?.close();
// Subtract the lines of code from the total
this.totalLinesOfCode -= this.files[filePath];
// Remove the file from tracking
delete this.files[filePath];
}
getFiles() {
return this.files;
return { ...this.files };
}

@@ -38,0 +83,0 @@ getTotalLinesOfCode() {

@@ -9,4 +9,4 @@ import type { ICodeTrackerCallbackFn, ITrackingStrategy, IStorageStrategy } from '../types';

constructor(callbacks?: ICodeTrackerCallbackFn, trackingStrategy?: ITrackingStrategy, storageStrategy?: IStorageStrategy);
trackFile(file: string, linesOfCode: number): CodeTracker;
updateFile(file: string, newLinesOfCode: number): this;
trackFile(file: string): Promise<CodeTracker>;
updateFile(file: string): Promise<CodeTracker>;
removeFile(file: string): this;

@@ -13,0 +13,0 @@ getTotalLinesOfCode(): number;

@@ -36,3 +36,3 @@ import { DefaultTrackingStrategy } from './strategies/default';

this.files = {};
this.trackingStrategy = trackingStrategy || new DefaultTrackingStrategy();
this.trackingStrategy = new DefaultTrackingStrategy();
this.storageStrategy = storageStrategy;

@@ -45,3 +45,3 @@ this.callbacks = callbacks || {};

}
trackFile(file, linesOfCode) {
async trackFile(file) {
if (!this.files[file]) {

@@ -54,15 +54,15 @@ // console.warn(`File "${file}" is already being tracked.`)

}
if (typeof linesOfCode !== 'number' || linesOfCode < 0) {
throw new Error(`Invalid lines of code: ${linesOfCode}`);
}
// if (typeof linesOfCode !== 'number' || linesOfCode < 0) {
// throw new Error(`Invalid lines of code: ${linesOfCode}`)
// }
// this.files[file] = linesOfCode
// this.totalLinesOfCode += linesOfCode
this.trackingStrategy.trackFile(file, linesOfCode);
await this.trackingStrategy.trackFile(file);
this.storageStrategy?.save(this.getFiles(), this.getTotalLinesOfCode());
if (this.callbacks.onFileAdded) {
this.callbacks.onFileAdded(file, linesOfCode);
}
// if (this.callbacks.onFileAdded) {
// this.callbacks.onFileAdded(file, linesOfCode)
// }
return this;
}
updateFile(file, newLinesOfCode) {
async updateFile(file) {
if (!this.files[file]) {

@@ -75,3 +75,3 @@ // console.warn(`File "${file}" is not being tracked.`)

}
this.trackingStrategy.updateFile(file, newLinesOfCode);
await this.trackingStrategy.updateFile(file);
// const previousLinesOfCode = this.files[file]

@@ -92,15 +92,16 @@ // this.totalLinesOfCode -= previousLinesOfCode

}
const linesOfCode = this.files[file];
this.totalLinesOfCode -= this.files[file];
delete this.files[file];
if (this.callbacks.onFileRemoved) {
this.callbacks.onFileRemoved(file, linesOfCode);
}
this.trackingStrategy.removeFile(file);
// const linesOfCode = this.files[file]
// this.totalLinesOfCode -= this.files[file]
// delete this.files[file]
// if (this.callbacks.onFileRemoved) {
// this.callbacks.onFileRemoved(file, linesOfCode)
// }
return this;
}
getTotalLinesOfCode() {
return this.totalLinesOfCode;
return this.trackingStrategy.getTotalLinesOfCode();
}
getFiles() {
return this.files;
return this.trackingStrategy.getFiles();
}

@@ -112,3 +113,3 @@ getFileDetails(file) {

for (const file in files) {
this.trackFile(file, files[file]);
this.trackFile(file);
}

@@ -115,0 +116,0 @@ return this;

@@ -11,3 +11,3 @@ export interface IFileTracker {

removeFile(file: string): void;
getTotalLinesOfCode(): number;
getTotalLinesOfCode(filePath: string): number;
getFiles(): IFileTracker;

@@ -14,0 +14,0 @@ }

{
"name": "code-tracker",
"version": "0.2.5",
"version": "0.2.6",
"type": "module",

@@ -5,0 +5,0 @@ "files": [