-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #104
- Loading branch information
Showing
13 changed files
with
383 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
fedify-cli-*.tar.xz | ||
fedify-cli-*.tgz | ||
fedify-cli-*.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bin/ | ||
*.tgz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import { execFile } from "node:child_process"; | ||
import { createWriteStream } from "node:fs"; | ||
import { | ||
access, | ||
chmod, | ||
constants, | ||
copyFile, | ||
mkdir, | ||
mkdtemp, | ||
readFile, | ||
realpath, | ||
} from "node:fs/promises"; | ||
import { tmpdir } from "node:os"; | ||
import { dirname, join } from "node:path"; | ||
import { Readable } from "node:stream"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
const platforms = { | ||
darwin: { | ||
arm64: "macos-aarch64.tar.xz", | ||
x64: "macos-x86_64.tar.xz", | ||
}, | ||
linux: { | ||
arm64: "linux-aarch64.tar.xz", | ||
x64: "linux-x86_64.tar.xz", | ||
}, | ||
win32: { | ||
x64: "windows-x86_64.zip", | ||
}, | ||
}; | ||
|
||
export async function main(version) { | ||
const filename = fileURLToPath(import.meta.url); | ||
const dirName = dirname(filename); | ||
const packageJson = await readFile(join(dirName, "package.json"), { | ||
encoding: "utf8", | ||
}); | ||
const pkg = JSON.parse(packageJson); | ||
const binDir = join(dirName, "bin"); | ||
await mkdir(binDir, { recursive: true }); | ||
await install(version ?? pkg.version, binDir); | ||
} | ||
|
||
async function install(version, targetDir) { | ||
const downloadUrl = getDownloadUrl(version); | ||
const downloadPath = await download(downloadUrl); | ||
let extractPath; | ||
if (downloadPath.endsWith(".zip")) { | ||
extractPath = await extractZip(downloadPath); | ||
} else { | ||
extractPath = await extractTar(downloadPath); | ||
} | ||
const exePath = join(extractPath, "fedify.exe"); | ||
if (await isFile(exePath)) { | ||
const targetPath = join(targetDir, "fedify.exe"); | ||
await copyFile(exePath, targetPath); | ||
return targetPath; | ||
} | ||
const binPath = join(extractPath, "fedify"); | ||
if (await isFile(binPath)) { | ||
const targetPath = join(targetDir, "fedify"); | ||
await copyFile(binPath, targetPath); | ||
await chmod(targetPath, 0o755); | ||
return targetPath; | ||
} | ||
throw new Error("Executable not found in the archive"); | ||
} | ||
|
||
function getDownloadUrl(version) { | ||
const platform = platforms[process.platform]; | ||
if (!platform) { | ||
console.error("Unsupported platform:", process.platform); | ||
return; | ||
} | ||
const suffix = platform[process.arch]; | ||
if (!suffix) { | ||
console.error("Unsupported architecture:", process.arch); | ||
return; | ||
} | ||
const filename = `fedify-cli-${version}-${suffix}`; | ||
const url = | ||
`https://github.com/dahlia/fedify/releases/download/${version}/${filename}`; | ||
return url; | ||
} | ||
|
||
async function download(url) { | ||
const response = await fetch(url, { redirect: "follow" }); | ||
if (!response.ok) { | ||
console.error("Download failed:", response.statusText); | ||
return; | ||
} | ||
const tmpDir = await mkdtemp(join(tmpdir(), `fedify-`)); | ||
const filename = url.substring(url.lastIndexOf("/") + 1); | ||
const downloadPath = join(tmpDir, filename); | ||
const fileStream = createWriteStream(downloadPath); | ||
const readable = Readable.fromWeb(response.body); | ||
await new Promise((resolve, reject) => { | ||
readable.pipe(fileStream); | ||
readable.on("error", reject); | ||
fileStream.on("finish", resolve); | ||
}); | ||
return downloadPath; | ||
} | ||
|
||
async function extractZip(path) { | ||
const dir = await mkdtemp(join(tmpdir(), "fedify-")); | ||
await new Promise((resolve, reject) => { | ||
execFile("powershell", [ | ||
"-NoProfile", | ||
"-ExecutionPolicy", | ||
"Bypass", | ||
"-Command", | ||
`Import-Module Microsoft.PowerShell.Archive;\ | ||
Expand-Archive -LiteralPath '${path}' -DestinationPath '${dir}'`, | ||
], (error, _, stderr) => { | ||
if (error) { | ||
console.error("Extraction failed:", error); | ||
reject(error); | ||
return; | ||
} | ||
if (stderr) console.warn(stderr); | ||
resolve(); | ||
}); | ||
}); | ||
return dir; | ||
} | ||
|
||
async function extractTar(path) { | ||
const switches = { | ||
".tar": "", | ||
".tar.gz": "z", | ||
".tgz": "z", | ||
".tar.bz2": "j", | ||
".tbz2": "j", | ||
".tar.xz": "J", | ||
".txz": "J", | ||
}; | ||
let switch_ = ""; | ||
for (const ext in switches) { | ||
if (path.endsWith(ext)) { | ||
switch_ = switches[ext]; | ||
break; | ||
} | ||
} | ||
path = await realpath(path); | ||
const dir = await mkdtemp(join(tmpdir(), "fedify-")); | ||
await execTar(`xvf${switch_}`, dir, path); | ||
return dir; | ||
} | ||
|
||
function execTar(switch_, dir, path) { | ||
return new Promise((resolve, reject) => { | ||
execFile("tar", [switch_, path], { cwd: dir }, (error, _, stderr) => { | ||
if (error) { | ||
console.error("Extraction failed:", error); | ||
reject(error); | ||
return; | ||
} | ||
if (stderr) console.warn(stderr); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
export async function isFile(path) { | ||
try { | ||
await access(path, constants.R_OK); | ||
return true; | ||
} catch (error) { | ||
if (error.code === "ENOENT") return false; | ||
throw error; | ||
} | ||
} | ||
|
||
if (fileURLToPath(import.meta.url) === process.argv[1]) { | ||
await main(process.argv[2]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { which } from "jsr:@david/[email protected]"; | ||
import { dirname, join } from "jsr:@std/[email protected]"; | ||
import denoJson from "../deno.json" with { type: "json" }; | ||
import metadataTemplate from "./package.json" with { type: "json" }; | ||
|
||
async function main() { | ||
const metadata = { | ||
...metadataTemplate, | ||
name: denoJson.name, | ||
version: Deno.args.length < 1 ? denoJson.version : Deno.args[0], | ||
private: false, | ||
}; | ||
const tempDir = await Deno.makeTempDir(); | ||
console.debug("Working directory:", tempDir); | ||
await Deno.writeTextFile( | ||
join(tempDir, "package.json"), | ||
JSON.stringify(metadata), | ||
); | ||
await Deno.copyFile( | ||
join(import.meta.dirname!, "install.mjs"), | ||
join(tempDir, "install.mjs"), | ||
); | ||
await Deno.copyFile( | ||
join(import.meta.dirname!, "run.mjs"), | ||
join(tempDir, "run.mjs"), | ||
); | ||
await Deno.copyFile( | ||
join(dirname(import.meta.dirname!), "README.md"), | ||
join(tempDir, "README.md"), | ||
); | ||
const command = new Deno.Command(await which("npm") ?? "npm", { | ||
args: ["pack"], | ||
stdin: "inherit", | ||
stdout: "inherit", | ||
stderr: "inherit", | ||
cwd: tempDir, | ||
}); | ||
const result = await command.output(); | ||
if (!result.success) Deno.exit(result.code); | ||
for await (const entry of Deno.readDir(tempDir)) { | ||
if (entry.isFile && entry.name.endsWith(".tgz")) { | ||
await Deno.copyFile(join(tempDir, entry.name), entry.name); | ||
console.log(entry.name); | ||
} | ||
} | ||
} | ||
|
||
if (import.meta.main) await main(); |
Oops, something went wrong.