Skip to content

Commit 7291ded

Browse files
committed
fix(cli): use unique temp dir per invocation
Fixes #183
1 parent ed5e112 commit 7291ded

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { tmpdir } from 'os';
22
import { join } from 'path';
3+
import { uuid } from './uuid';
34

45
export interface IApp {
56
readonly bundleId: string;
@@ -39,7 +40,7 @@ export const SUPPORTED_FILE_TYPES = [
3940
...supports.jpegmini,
4041
].filter((value, i, list) => list.indexOf(value) === i);
4142

42-
export const TMPDIR = join(tmpdir(), 'imageoptim-cli');
43+
export const TMPDIR = join(tmpdir(), 'imageoptim-cli', uuid());
4344
export const VERSION = manifest.version;
4445
export const PNGQUANT_NUMBER_OF_COLORS = '256';
4546
export const PNGQUANT_QUALITY = '65-80';

src/uuid.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Cheaply generate a UUID with a low chance of collisions
3+
* https://stackoverflow.com/a/8809472/745158
4+
*/
5+
export function uuid(): string {
6+
let epoch: number = new Date().getTime();
7+
let sessionLength: number = Date.now() * 1000 || 0;
8+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (character) => {
9+
let randomNumber: number = Math.random() * 16;
10+
if (epoch > 0) {
11+
randomNumber = (epoch + randomNumber) % 16 | 0;
12+
epoch = Math.floor(epoch / 16);
13+
} else {
14+
randomNumber = (sessionLength + randomNumber) % 16 | 0;
15+
sessionLength = Math.floor(sessionLength / 16);
16+
}
17+
return (character == 'x' ? randomNumber : (randomNumber & 0x7) | 0x8).toString(16);
18+
});
19+
}

0 commit comments

Comments
 (0)