Skip to content

Commit

Permalink
refactor: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Jun 9, 2020
1 parent ccb044b commit bfa739c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
16 changes: 10 additions & 6 deletions src/files/ContributorsJsonFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import writeJsonFile from "write-json-file";
import loadJsonFile from "load-json-file";
import { ContreebutorsError } from "../ContreebutorsError";

export type ContributorsListContributor = {
username: string;
Expand All @@ -25,7 +26,7 @@ export default class ContributorsJsonFile {
this.mocks = null;
}

async loadContributorsList() {
async load() {
if (this.content) {
return this.content;
}
Expand All @@ -42,11 +43,16 @@ export default class ContributorsJsonFile {
return this.content;
}

saveContributorsList() {
async save() {
return writeJsonFile(this.config.path, this.content);
}

getContributorsList() {
if (!this.content) {
throw new ContreebutorsError({
message: "Can access contributors list - file not loaded."
});
}
return this.content;
}

Expand All @@ -58,9 +64,7 @@ export default class ContributorsJsonFile {
return Boolean(this.content.find(item => item.username === user.username));
}

async add(user: ContributorsListContributor) {
await this.loadContributorsList();

add(user: ContributorsListContributor) {
if (this.isAdded(user)) {
return;
}
Expand All @@ -74,6 +78,6 @@ export default class ContributorsJsonFile {
addedOn: new Date()
});

await this.saveContributorsList();
return this;
}
}
10 changes: 4 additions & 6 deletions src/files/RenderToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class RenderToFile {
this.content = null;
}

private loadRenderTo() {
load() {
if (this.content !== null) {
return this.content;
}
Expand All @@ -35,17 +35,15 @@ export default class RenderToFile {
return this.content;
}

private saveRenderTo() {
save() {
return fs.writeFileSync(this.config.path, this.content);
}

getContent() {
return this.content;
}

async generate({ contributorsListFile, renderer }: GenerateArgs) {
await this.loadRenderTo();

generate({ contributorsListFile, renderer }: GenerateArgs) {
const content = render(contributorsListFile.getContributorsList(), renderer);
const regex = new RegExp(/<!-- CONTREEBUTORS:START.*<!-- CONTREEBUTORS:END -->/s);
if (this.content.match(regex)) {
Expand All @@ -54,6 +52,6 @@ export default class RenderToFile {
this.content += `\n${content}`;
}

await this.saveRenderTo();
return this;
}
}
63 changes: 33 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,57 +25,60 @@ export class Contreebutors {
}

async add(args: { username: string }) {
const contributorsListFile = new ContributorsJsonFile({
path: path.join(this.config.cwd, this.config.contributorsListPath)
});

await contributorsListFile.load();

if (contributorsListFile.isAdded(args.username)) {
throw new ContreebutorsError({
type: "warning",
message: `User "${args.username}" already added to the contributors list. Skipping...`
});
}

let user;

try {
const response = await got.get(`https://api.github.com/users/${args.username}`, {
responseType: "json"
});
user = response.body;
user = {
username: user.login,
name: user.name,
profileUrl: user.html_url,
avatarUrl: user.avatar_url
};
} catch (e) {
throw new ContreebutorsError({
message: `The following error occurred while trying to fetch data for user "${args.username}": ${e.message}`
});
}

const contributorsListFile = new ContributorsJsonFile({
path: path.join(this.config.cwd, this.config.contributorsListPath)
});

await contributorsListFile.loadContributorsList();

if (contributorsListFile.isAdded(user)) {
throw new ContreebutorsError({
type: "warning",
message: `User "${user.username}" already added to the contributors list. Skipping...`
});
}

await contributorsListFile.add({
username: user.login,
name: user.name,
profileUrl: user.html_url,
avatarUrl: user.avatar_url
});
await contributorsListFile.add(user).save();

const renderToFile = new RenderToFile({
path: path.join(this.config.cwd, this.config.renderToPath)
});

await renderToFile.generate({ contributorsListFile, renderer: this.config.renderer });
await this.render({ contributorsListFile });
}

async render() {
async render(args: { contributorsListFile?: ContributorsJsonFile } = {}) {
const renderToFile = new RenderToFile({
path: path.join(this.config.cwd, this.config.renderToPath)
});

const contributorsListFile = new ContributorsJsonFile({
path: path.join(this.config.cwd, this.config.contributorsListPath)
});
await renderToFile.load();

await contributorsListFile.loadContributorsList();
let contributorsListFile = args.contributorsListFile;
if (!contributorsListFile) {
contributorsListFile = new ContributorsJsonFile({
path: path.join(this.config.cwd, this.config.contributorsListPath)
});
await contributorsListFile.load();
}

await renderToFile.generate({ contributorsListFile, renderer: this.config.renderer });
await renderToFile
.generate({ contributorsListFile, renderer: this.config.renderer })
.save();
}
}

0 comments on commit bfa739c

Please sign in to comment.