Skip to content

Commit

Permalink
Suffix problem names with MD5 to prevent collisions and save in '.cph…
Browse files Browse the repository at this point in the history
…' for clarity.
  • Loading branch information
agrawal-d committed Jul 8, 2020
1 parent 2066630 commit 7bc440d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import fs from 'fs';
import { Problem } from '../types';
import { getSaveLocationPref } from '../preferences';
import crypto from 'crypto';

/**
* Get the location (file path) to save the generated problem file in. If save
Expand All @@ -13,11 +14,21 @@ import { getSaveLocationPref } from '../preferences';
export const getProbSaveLocation = (srcPath: string): string => {
const savePreference = getSaveLocationPref();
const srcFileName = path.basename(srcPath);
const binFileName = `${srcFileName}.prob`;
const srcFolder = path.dirname(srcPath);
const hash = crypto
.createHash('md5')
.update(srcPath)
.digest('hex')
.substr(0);
const baseProbName = `.${srcFileName}_${hash}.prob`;
const cphFolder = path.join(srcFolder, '.cph');
if (savePreference && savePreference !== '') {
return path.join(savePreference, binFileName);
return path.join(savePreference, baseProbName);
}
return `${srcPath}.prob`;
if (!fs.existsSync(cphFolder)) {
fs.mkdirSync(cphFolder);
}
return path.join(cphFolder, baseProbName);
};

/** Get the problem for a source, `null` if does not exist on the filesystem. */
Expand Down

0 comments on commit 7bc440d

Please sign in to comment.