Skip to content

Commit

Permalink
refactor: use modern TS and JS syntax
Browse files Browse the repository at this point in the history
JS: optional chaining, logical assigment operators
TS: `Awaited` instead of custom `ThenArg`
  • Loading branch information
sidvishnoi committed Dec 16, 2021
1 parent bcf67a4 commit d2e9997
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/deploy-w3c-echidna.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function showErrors(jobs: EchidnaJobs) {
type AvailableErrorLoggers = keyof ErrorLoggers;
for (const type of Object.keys(loggers) as AvailableErrorLoggers[]) {
const logger = loggers[type]!;
if (jobs[type] && jobs[type].errors.length) {
if (jobs[type]?.errors.length) {
console.log();
logger(jobs[type].errors);
console.log();
Expand Down
11 changes: 5 additions & 6 deletions src/prepare-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import * as puppeteer from "puppeteer";
import { PUPPETEER_ENV } from "./constants.js";
import { exit } from "./utils.js";

import { ThenArg } from "./utils.js";
import { Inputs } from "./prepare.js";
export type BuildOptions = ThenArg<ReturnType<typeof buildOptions>>;
export type BuildOptions = Awaited<ReturnType<typeof buildOptions>>;

export async function buildOptions(inputs: Inputs) {
const { toolchain, source, destination } = getBasicBuildOptions(inputs);
Expand Down Expand Up @@ -44,10 +43,10 @@ function getBasicBuildOptions(inputs: Inputs): BasicBuildOptions {
if (toolchain) {
switch (toolchain) {
case "respec":
source = source || "index.html";
source ||= "index.html";
break;
case "bikeshed":
source = source || "index.bs";
source ||= "index.bs";
break;
default:
exit(`Invalid input "TOOLCHAIN": ${toolchain}`);
Expand Down Expand Up @@ -263,7 +262,7 @@ async function getPreviousVersionInfo(shortName: string, publishDate: string) {
);
if (thisVersion) {
const dd = thisVersion.nextElementSibling as HTMLElement | null;
if (dd && dd.localName === "dd") {
if (dd?.localName === "dd") {
const link = dd.querySelector("a");
if (link) return link.href;
}
Expand All @@ -278,7 +277,7 @@ async function getPreviousVersionInfo(shortName: string, publishDate: string) {
);
if (thisVersion) {
const dd = thisVersion.nextElementSibling as HTMLElement | null;
if (dd && dd.localName === "dd") {
if (dd?.localName === "dd") {
const link = dd.querySelector("a");
if (link) return link.href;
}
Expand Down
3 changes: 1 addition & 2 deletions src/prepare-deploy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { exit, yesOrNo } from "./utils.js";

import { ThenArg } from "./utils.js";
import { Inputs, GitHubContext } from "./prepare.js";
export type W3CDeployOptions = ThenArg<ReturnType<typeof w3cEchidnaDeployment>>;
export type W3CDeployOptions = Awaited<ReturnType<typeof w3cEchidnaDeployment>>;
export type GithubPagesDeployOptions = ReturnType<typeof githubPagesDeployment>;

export function githubPagesDeployment(
Expand Down
11 changes: 2 additions & 9 deletions src/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
env,
exit,
formatAsHeading,
pprint,
setOutput,
ThenArg,
} from "./utils.js";
import { env, exit, formatAsHeading, pprint, setOutput } from "./utils.js";

import { buildOptions } from "./prepare-build.js";
import {
Expand Down Expand Up @@ -73,7 +66,7 @@ export default async function main(
};
}

export type ProcessedInput = ThenArg<ReturnType<typeof processInputs>>;
export type ProcessedInput = Awaited<ReturnType<typeof processInputs>>;
async function processInputs(inputs: Inputs, githubContext: GitHubContext) {
return {
build: await buildOptions(inputs),
Expand Down
2 changes: 0 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,3 @@ export class StaticServer {
return new URL(`http://localhost:${this._port}`);
}
}

export type ThenArg<T> = T extends PromiseLike<infer U> ? U : T;
8 changes: 4 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Run this file in a local GitHub repo, and change inputs as needed.
*/
import { platform } from "os";
import { formatAsHeading, pprint, ThenArg } from "../src/utils.js";
import { formatAsHeading, pprint } from "../src/utils.js";

let SILENT_CHILD = !true;

Expand All @@ -30,9 +30,9 @@ if (SILENT_CHILD) {
}

export interface Outputs {
prepare: ThenArg<ReturnType<typeof import("../src/prepare.js").default>>;
setup: ThenArg<ReturnType<typeof import("../src/setup.js").default>>;
build: ThenArg<ReturnType<typeof import("../src/build.js").default>>;
prepare: Awaited<ReturnType<typeof import("../src/prepare.js").default>>;
setup: Awaited<ReturnType<typeof import("../src/setup.js").default>>;
build: Awaited<ReturnType<typeof import("../src/build.js").default>>;
}
const outputs: Partial<Outputs> = {};
type AsyncFn = (outputs: Partial<Outputs>) => Promise<object | undefined>;
Expand Down

0 comments on commit d2e9997

Please sign in to comment.