Skip to content

Commit

Permalink
Enable TS strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
alexesprit committed Sep 6, 2020
1 parent 5dc420d commit 7e10c2a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ function main(): void {
const updater = new Updater(options);
updater
.updateFiles(pathsToUpdate)
.then(({ commitSha, branch }) => {
if (commitSha === null) {
.then((updateResult) => {
if (updateResult === null) {
info('No files to update');
return;
}

const { commitSha, branch } = updateResult;

setOutput('commit-sha', commitSha);

const shortSha = commitSha.slice(0, 7);
Expand Down
44 changes: 20 additions & 24 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { promisify } from 'util';

import { getOctokit, context } from '@actions/github';

import { UpdaterOptions } from './util';
import { UpdaterOptions, isNotNull } from './util';

const readFileAsync = promisify(readFile);

Expand Down Expand Up @@ -34,7 +34,7 @@ interface UpdateResult {
export class Updater {
private octokit: ReturnType<typeof getOctokit>;
private message: string;
private defaultBranch: string;
private defaultBranch: string | null;

constructor(options: UpdaterOptions) {
this.octokit = getOctokit(options.token);
Expand All @@ -43,7 +43,7 @@ export class Updater {
this.defaultBranch = options.branch || null;
}

async updateFiles(paths: string[]): Promise<UpdateResult> {
async updateFiles(paths: string[]): Promise<UpdateResult | null> {
const branch = await this.getBranch();
const lastRef = await this.getLastRef(branch);

Expand Down Expand Up @@ -78,16 +78,14 @@ export class Updater {
branch: string,
filePaths: string[],
base_tree: string
): Promise<string> {
const promises = Promise.all(
filePaths.map((filePath) => {
return this.createTreeItem(filePath, branch);
})
);

const tree = (await promises).filter((change) => {
return change !== null;
});
): Promise<string | null> {
const tree = (
await Promise.all(
filePaths.map((filePath) => {
return this.createTreeItem(filePath, branch);
})
)
).filter(isNotNull);

if (tree.length === 0) {
return null;
Expand All @@ -105,10 +103,10 @@ export class Updater {
private async createTreeItem(
filePath: string,
branch: string
): Promise<TreeItem> {
): Promise<TreeItem | null> {
const remoteFile = await this.getRemoteContents(filePath, branch);
const localContents = await this.getLocalContents(filePath);
const remoteContents = remoteFile.content;
const remoteContents = remoteFile?.content || null;

const mode = '100644';

Expand All @@ -121,7 +119,6 @@ export class Updater {
return {
mode,
path: filePath,
sha: null,
};
}

Expand Down Expand Up @@ -150,7 +147,7 @@ export class Updater {
return { treeSha, commitSha };
}

private async getLocalContents(filePath: string): Promise<string> {
private async getLocalContents(filePath: string): Promise<string | null> {
if (existsSync(filePath)) {
return (await readFileAsync(filePath)).toString();
}
Expand All @@ -161,25 +158,24 @@ export class Updater {
private async getRemoteContents(
filePath: string,
branch: string
): Promise<RemoteFile> {
let content: string = null;
let sha: string = null;

): Promise<RemoteFile | null> {
try {
const { data } = await this.octokit.repos.getContent({
...context.repo,
path: filePath,
ref: branch,
});

content = Buffer.from(data['content'], 'base64').toString();
const content = Buffer.from(data['content'], 'base64').toString();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sha = data['sha'];
const sha = data['sha'];

return { content, sha };
} catch (err) {
// Do nothing
}

return { content, sha };
return null;
}

private async updateRef(sha: string, branch: string): Promise<string> {
Expand Down
4 changes: 4 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ export function getActionOptions(): UpdaterOptions {

return { token, message, branch };
}

export function isNotNull<T>(arg: T): arg is Exclude<T, null> {
return arg !== null;
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"target": "ES2017",
"module": "CommonJS",
"outDir": "./build",
"rootDir": "./src"
"rootDir": "./src",
"strict": true
},
"include": ["./src/**/*"]
}

0 comments on commit 7e10c2a

Please sign in to comment.