add prettier config

This commit is contained in:
Ryan Cao 2023-07-19 10:43:43 +08:00
parent 5f106d5966
commit 16bd754a82
No known key found for this signature in database
7 changed files with 75 additions and 79 deletions

4
.prettierrc Normal file
View file

@ -0,0 +1,4 @@
{
"useTabs": true,
"printWidth": 120
}

View file

@ -2,8 +2,8 @@ import { install } from "./stages/install";
import { configure } from "./stages/configure"; import { configure } from "./stages/configure";
const main = async () => { const main = async () => {
await install(); await install();
await configure(); await configure();
}; };
main(); main();

View file

@ -1,7 +1,7 @@
import { push } from "./stages/push"; import { push } from "./stages/push";
const main = async () => { const main = async () => {
await push(); await push();
}; };
main(); main();

View file

@ -3,22 +3,22 @@ import { exec } from "@actions/exec";
import { getStorePaths } from "../utils"; import { getStorePaths } from "../utils";
export const configure = async () => { export const configure = async () => {
core.startGroup("Configure attic"); core.startGroup("Configure attic");
try { try {
const endpoint = core.getInput("endpoint"); const endpoint = core.getInput("endpoint");
const cache = core.getInput("cache"); const cache = core.getInput("cache");
const token = core.getInput("token"); const token = core.getInput("token");
core.info("Logging in to attic cache"); core.info("Logging in to attic cache");
await exec("attic", ["login", "--set-default", cache, endpoint, token]); await exec("attic", ["login", "--set-default", cache, endpoint, token]);
core.info("Collecting store paths before build"); core.info("Collecting store paths before build");
const paths = await getStorePaths(); const paths = await getStorePaths();
core.saveState("initial-paths", JSON.stringify(paths)); core.saveState("initial-paths", JSON.stringify(paths));
} catch (e) { } catch (e) {
core.setFailed(`Action failed with error: ${e}`); core.setFailed(`Action failed with error: ${e}`);
} }
core.endGroup(); core.endGroup();
}; };

View file

@ -7,31 +7,31 @@ import { tmpdir } from "node:os";
import { join } from "node:path"; import { join } from "node:path";
export const install = async () => { export const install = async () => {
core.startGroup("Install attic"); core.startGroup("Install attic");
core.info("Installing attic"); core.info("Installing attic");
const installScript = await fetch( const installScript = await fetch(
"https://raw.githubusercontent.com/zhaofengli/attic/main/.github/install-attic-ci.sh" "https://raw.githubusercontent.com/zhaofengli/attic/main/.github/install-attic-ci.sh",
).then((r) => { ).then((r) => {
if (!r.ok) { if (!r.ok) {
core.setFailed(`Action failed with error: ${r.statusText}`); core.setFailed(`Action failed with error: ${r.statusText}`);
core.endGroup(); core.endGroup();
process.exit(1); process.exit(1);
} }
return r.text(); return r.text();
}); });
try { try {
const installScriptPath = join(tmpdir(), "install-attic-ci.sh"); const installScriptPath = join(tmpdir(), "install-attic-ci.sh");
await writeFile(installScriptPath, installScript); await writeFile(installScriptPath, installScript);
core.info("Running install script"); core.info("Running install script");
await exec("bash", [installScriptPath]); await exec("bash", [installScriptPath]);
} catch (e) { } catch (e) {
core.setFailed(`Action failed with error: ${e}`); core.setFailed(`Action failed with error: ${e}`);
} }
core.endGroup(); core.endGroup();
}; };

View file

@ -3,33 +3,29 @@ import { exec } from "@actions/exec";
import { getStorePaths } from "../utils"; import { getStorePaths } from "../utils";
export const push = async () => { export const push = async () => {
core.startGroup("Push to Attic"); core.startGroup("Push to Attic");
try { try {
const skipPush = core.getInput("skip-push"); const skipPush = core.getInput("skip-push");
if (skipPush === "true") { if (skipPush === "true") {
core.info("Pushing to cache is disabled by skip-push"); core.info("Pushing to cache is disabled by skip-push");
} else { } else {
const cache = core.getInput("cache"); const cache = core.getInput("cache");
core.info("Pushing to cache"); core.info("Pushing to cache");
const oldPaths = JSON.parse(core.getState("initial-paths")) as string[]; const oldPaths = JSON.parse(core.getState("initial-paths")) as string[];
const newPaths = await getStorePaths(); const newPaths = await getStorePaths();
const addedPaths = newPaths const addedPaths = newPaths
.filter((p) => !oldPaths.includes(p)) .filter((p) => !oldPaths.includes(p))
.filter( .filter(
(p) => (p) => !p.endsWith(".drv") && !p.endsWith(".drv.chroot") && !p.endsWith(".check") && !p.endsWith(".lock"),
!p.endsWith(".drv") && );
!p.endsWith(".drv.chroot") &&
!p.endsWith(".check") &&
!p.endsWith(".lock")
);
await exec("attic", ["push", cache, ...addedPaths]); await exec("attic", ["push", cache, ...addedPaths]);
} }
} catch (e) { } catch (e) {
core.setFailed(`Action failed with error: ${e}`); core.setFailed(`Action failed with error: ${e}`);
} }
core.endGroup(); core.endGroup();
}; };

View file

@ -2,26 +2,22 @@ import { exec } from "@actions/exec";
import { Writable } from "node:stream"; import { Writable } from "node:stream";
class StringStream extends Writable { class StringStream extends Writable {
chunks: Buffer[] = []; chunks: Buffer[] = [];
_write( _write(chunk: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>, _enc: unknown, next: () => unknown) {
chunk: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>, this.chunks.push(Buffer.from(chunk));
_enc: unknown, next();
next: () => unknown }
) {
this.chunks.push(Buffer.from(chunk));
next();
}
string() { string() {
return Buffer.concat(this.chunks).toString("utf-8"); return Buffer.concat(this.chunks).toString("utf-8");
} }
} }
export const getStorePaths = async () => { export const getStorePaths = async () => {
const outStream = new StringStream(); const outStream = new StringStream();
await exec("nix", ["path-info", "--all"], { outStream }); await exec("nix", ["path-info", "--all"], { outStream });
const paths = outStream.string().split("\n").filter(Boolean); const paths = outStream.string().split("\n").filter(Boolean);
return paths; return paths;
}; };