Ghost/ghost/recommendations/src/WellknownService.ts
Daniel Lockyer 6dc1d08590 Re-enabled general eslint rules in TS config
refs https://github.com/TryGhost/DevOps/issues/50

- when creating a TS config in our `eslint-plugin-ghost` dependency, I
  only extended the recommended config, which left out a lot of
  stylistic things we used to enforce in JS
- this fixes that by bumping the dependency to a version which extends
  those shared configs, and fixes all the code that currently goes
  against those rules
2023-09-08 13:47:42 +02:00

49 lines
1.3 KiB
TypeScript

import {Recommendation} from './Recommendation';
import fs from 'fs/promises';
import _path from 'path';
type UrlUtils = {
relativeToAbsolute(url: string): string
}
type Options = {
/**
* Where to publish the wellknown file
*/
dir: string,
urlUtils: UrlUtils
}
export class WellknownService {
dir: string;
urlUtils: UrlUtils;
constructor({dir, urlUtils}: Options) {
this.dir = dir;
this.urlUtils = urlUtils;
}
#formatRecommendation(recommendation: Recommendation) {
return {
url: recommendation.url,
reason: recommendation.reason,
updated_at: (recommendation.updatedAt ?? recommendation.createdAt).toISOString(),
created_at: (recommendation.createdAt).toISOString()
};
}
getPath() {
return _path.join(this.dir, '/.well-known/recommendations.json');
}
getURL(): URL {
return new URL(this.urlUtils.relativeToAbsolute('/.well-known/recommendations.json'));
}
async set(recommendations: Recommendation[]) {
const content = JSON.stringify(recommendations.map(r => this.#formatRecommendation(r)));
const path = this.getPath();
await fs.mkdir(_path.dirname(path), {recursive: true});
await fs.writeFile(path, content);
}
}