Skip to content

Commit a395f36

Browse files
Distribute both GitHub Action entry point and lib to be imported by install-cli action
1 parent 707af61 commit a395f36

31 files changed

+322
-16
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
coverage/
22
dist/
3+
dist-lib/
34
node_modules/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export type SupportedPlatform = Extract<NodeJS.Platform, "linux" | "darwin" | "win32">;
2+
export declare const archMap: Record<string, string>;
3+
export declare const cliUrlBuilder: Record<SupportedPlatform, (version: string, arch?: string) => string>;
4+
export declare class CliInstaller {
5+
readonly version: string;
6+
readonly arch: string;
7+
constructor(version: string);
8+
install(url: string): Promise<void>;
9+
private getArch;
10+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import os from "os";
2+
import * as core from "@actions/core";
3+
import * as tc from "@actions/tool-cache";
4+
// maps OS architecture names to 1Password CLI installer architecture names
5+
export const archMap = {
6+
ia32: "386",
7+
x64: "amd64",
8+
arm: "arm",
9+
arm64: "arm64",
10+
};
11+
// Builds the download URL for the 1Password CLI based on the platform and version.
12+
export const cliUrlBuilder = {
13+
linux: (version, arch) => `https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_linux_${arch}_${version}.zip`,
14+
darwin: (version) => `https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_apple_universal_${version}.pkg`,
15+
win32: (version, arch) => `https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_windows_${arch}_${version}.zip`,
16+
};
17+
export class CliInstaller {
18+
version;
19+
arch;
20+
constructor(version) {
21+
this.version = version;
22+
this.arch = this.getArch();
23+
}
24+
async install(url) {
25+
console.info(`Downloading 1Password CLI from: ${url}`);
26+
const downloadPath = await tc.downloadTool(url);
27+
console.info("Installing 1Password CLI");
28+
const extractedPath = await tc.extractZip(downloadPath);
29+
core.addPath(extractedPath);
30+
core.info("1Password CLI installed");
31+
}
32+
getArch() {
33+
const arch = archMap[os.arch()];
34+
if (!arch) {
35+
throw new Error("Unsupported architecture");
36+
}
37+
return arch;
38+
}
39+
}

dist-lib/cli-installer/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { type Installer, newCliInstaller } from "./installer";

dist-lib/cli-installer/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { newCliInstaller } from "./installer";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Installer {
2+
installCli(): Promise<void>;
3+
}
4+
export declare const newCliInstaller: (version: string) => Installer;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os from "os";
2+
import { LinuxInstaller } from "./linux";
3+
import { MacOsInstaller } from "./macos";
4+
import { WindowsInstaller } from "./windows";
5+
export const newCliInstaller = (version) => {
6+
const platform = os.platform();
7+
switch (platform) {
8+
case "linux":
9+
return new LinuxInstaller(version);
10+
case "darwin":
11+
return new MacOsInstaller(version);
12+
case "win32":
13+
return new WindowsInstaller(version);
14+
default:
15+
throw new Error(`Unsupported platform: ${platform}`);
16+
}
17+
};

dist-lib/cli-installer/linux.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { CliInstaller } from "./cli-installer";
2+
import type { Installer } from "./installer";
3+
export declare class LinuxInstaller extends CliInstaller implements Installer {
4+
private readonly platform;
5+
constructor(version: string);
6+
installCli(): Promise<void>;
7+
}

dist-lib/cli-installer/linux.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { CliInstaller, cliUrlBuilder, } from "./cli-installer";
2+
export class LinuxInstaller extends CliInstaller {
3+
platform = "linux"; // Node.js platform identifier for Linux
4+
constructor(version) {
5+
super(version);
6+
}
7+
async installCli() {
8+
const urlBuilder = cliUrlBuilder[this.platform];
9+
await super.install(urlBuilder(this.version, this.arch));
10+
}
11+
}

dist-lib/cli-installer/macos.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CliInstaller } from "./cli-installer";
2+
import { type Installer } from "./installer";
3+
export declare class MacOsInstaller extends CliInstaller implements Installer {
4+
private readonly platform;
5+
constructor(version: string);
6+
installCli(): Promise<void>;
7+
install(downloadUrl: string): Promise<void>;
8+
}

0 commit comments

Comments
 (0)