Skip to content

Commit 6b3399b

Browse files
committed
fix(cache): move cache folder and ensure it exists programatically
1 parent 53b2c5e commit 6b3399b

File tree

7 files changed

+81
-24
lines changed

7 files changed

+81
-24
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ $RECYCLE.BIN/
8787

8888
app/config.ts
8989
lib/
90-
cache/*.json
90+
src/cache/*.json
9191
coverage
9292
.nyc_output

.gitlab-ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ build:
3333
stage: build
3434
before_script:
3535
- echo "$BOT_CONFIG" > app/config.ts
36-
- touch cache/cookie.json
3736
script:
3837
- npm run build
3938
artifacts:

package-lock.json

Lines changed: 3 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
{
22
"name": "spectrum-bot",
3-
"version": "2.0.0",
4-
"description": "A crude API for StarCitizen's SPECTRUM@^0.3.5",
3+
"version": "0.0.0",
4+
"description": "API for StarCitizen's SPECTRUM & RSI website",
55
"main": "index.js",
66
"scripts": {
77
"test": "jasmine-ts --config=jasmine.json",
88
"coverage": "nyc -r text -e .ts -x \"spec/**.spec.ts\" npm run test",
99
"coverage:html": "nyc -r html -e .ts -x \"spec/**.spec.ts\" npm run test",
1010
"start": "ts-node app/bot.ts",
1111
"build": "tsc -d -p src/ --outdir lib/",
12-
"prepublishOnly": "node bin/prepublish.js",
13-
"prepare": "mkdir -p cache/ && touch cache/cookie.json"
12+
"prepublishOnly": "node bin/prepublish.js"
1413
},
1514
"author": {
1615
"name": "David FAIN",
@@ -23,6 +22,7 @@
2322
"@types/node": "^7.0.12",
2423
"draft-js": "^0.10.0",
2524
"draft-js-emoji-plugin": "^2.0.0-rc9",
25+
"fs-extra": "^7.0.1",
2626
"popsicle": "^10.0.1",
2727
"react": "^16.7.0",
2828
"react-dom": "^16.7.0",
@@ -44,6 +44,7 @@
4444
"@semantic-release/release-notes-generator": "^7.1.4",
4545
"@types/core-js": "^2.5.0",
4646
"@types/draft-js": "^0.10.16",
47+
"@types/fs-extra": "^5.0.4",
4748
"@types/jasmine": "^3.3.5",
4849
"@types/node": "^10.12.18",
4950
"jasmine": "^3.3.1",

spec/0.rsi.spec.ts

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,70 @@
1-
import { TestShared } from './_.shared';
2-
import { RSI } from '../src/';
3-
import { } from 'jasmine';
4-
5-
1+
import { Container } from "typedi";
2+
import { TestShared } from "./_.shared";
3+
import { RSI } from "../src/";
4+
import {} from "jasmine";
5+
import * as fs from "fs-extra";
66

77
interface ThisContext {
88
rsi: RSI;
99
}
1010

11-
describe("RSI Service", function (this: ThisContext) {
11+
describe("RSI Service", function(this: ThisContext) {
1212
TestShared.commonSetUp();
1313

1414
it("should instantiate", () => {
1515
this.rsi = RSI.getInstance();
1616
expect(this.rsi instanceof RSI).toBe(true);
1717
});
1818

19+
describe("Cookie Jar", () => {
20+
beforeEach(() => {
21+
Container.set(RSI, new RSI());
22+
});
23+
it(`Should create cookie jar if file doesn't exist`, async () => {
24+
try {
25+
await fs.remove("../src/cache/cookie.json");
26+
} catch (e) {}
27+
28+
Container.set(RSI, new RSI());
29+
this.rsi = RSI.getInstance();
30+
expect(this.rsi instanceof RSI).toBe(true);
31+
});
32+
33+
it(`Should create cookie jar if directory doesn't exist`, async () => {
34+
try {
35+
await fs.remove("../src/cache");
36+
} catch (e) {}
37+
38+
Container.set(RSI, new RSI());
39+
this.rsi = RSI.getInstance();
40+
expect(this.rsi instanceof RSI).toBe(true);
41+
});
42+
43+
it(`Should leave cookie jar as is if exists`, async () => {
44+
try {
45+
await fs.remove("../src/cache");
46+
await fs.ensureFile("../src/cache/cookie.json");
47+
await fs.writeFile("../src/cache/cookie.json", '{"test":123}');
48+
} catch (e) {}
49+
50+
Container.set(RSI, new RSI());
51+
this.rsi = RSI.getInstance();
52+
expect(this.rsi instanceof RSI).toBe(true);
53+
54+
const cookie = await fs.readFile("../src/cache/cookie.json", "utf-8");
55+
expect(cookie).toBe('{"test":123}');
56+
});
57+
58+
afterAll(async () => {
59+
try {
60+
await fs.remove("../src/cache");
61+
} catch (e) {}
62+
63+
Container.set(RSI, new RSI());
64+
this.rsi = RSI.getInstance();
65+
});
66+
});
67+
1968
describe("Should handle login operation", () => {
2069
it("Should get a base token from RSI", async () => {
2170
let token = await this.rsi.getBaseToken();
@@ -48,5 +97,4 @@ describe("RSI Service", function (this: ThisContext) {
4897
expect(login).toBeTruthy();
4998
});
5099
});
51-
52-
});
100+
});

src/RSI/services/rsi.service.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import { RSIApiResponse } from "./../interfaces/RSIApiResponse.interface";
66
import { ApiResponse } from "./../interfaces/APIResponse.interface";
77
import { Container, Service } from "typedi";
8+
import { CookieJar } from "tough-cookie";
89
import * as cookieStore from "tough-cookie-file-store";
910
import * as popsicle from "popsicle";
1011
import * as rl from "readline";
11-
12+
import * as fs from "fs-extra";
13+
import * as path from "path";
1214
/**
1315
* Main API class to handle every call to the RSI-API as well as
1416
* user-identification.
@@ -25,10 +27,21 @@ export class RSIService {
2527
/** a collection of tokens */
2628
private tokens = {};
2729
/** cookieJar for api calls */
28-
private cookieJar = popsicle.jar(new cookieStore(__dirname + "/../../../cache/cookie.json"));
30+
private cookieJar: CookieJar;
2931
private input = rl.createInterface(process.stdin, process.stdout, null);
3032

31-
constructor() {}
33+
constructor() {
34+
this.ensureCookieJar();
35+
}
36+
37+
protected async ensureCookieJar() {
38+
const path = __dirname + "/../../cache/cookie.json";
39+
await fs.ensureFile(path);
40+
41+
this.cookieJar = popsicle.jar(new cookieStore(path));
42+
}
43+
44+
3245

3346
/**
3447
* @deprecated use Container.get(RSIService) instead
File renamed without changes.

0 commit comments

Comments
 (0)