-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.ts
83 lines (70 loc) · 2.22 KB
/
archive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {z} from "zod";
import * as fs from "fs";
import dotenv from "dotenv";
import prompt from "prompt";
import * as path from "path";
import {execSync} from "child_process";
dotenv.config({path: path.join(__dirname, ".env")});
const envSchema = z.object({
ARCHIVE_SERVER: z.string(),
ARCHIVE_DIR: z.string()
});
const inputSchema = z.strictObject({
filename: z.string(),
cleanup: z.boolean()
});
const env = envSchema.parse(process.env);
type Details = z.infer<typeof inputSchema>
const getDetails = async () => {
prompt.start();
prompt.message = 'Enter file details:\n';
prompt.delimiter = '';
return prompt.get({
properties: {
filename: {
message: "Enter file name:",
type: 'string',
required: true
},
cleanup: {
message: "Clean up?",
type: "boolean",
required: false,
default: false
}
}
});
};
const formatFilename = (filename: string) => filename.replace(" ", "_");
const getTarFilename = (filename: string) => `${filename}.tar.gz`;
const formatCwd = formatFilename(process.cwd()).substring(1);
const scaffoldArchive = (tar: string, cwd: string) => {
execSync(`ssh ${env.ARCHIVE_SERVER} "cd '${env.ARCHIVE_DIR}' && mkdir -p '${cwd}'"`);
const dir = path.join(env.ARCHIVE_DIR, formatCwd);
return {
dir,
exists: execSync(`ssh ${env.ARCHIVE_SERVER} "cd '${dir}' && ls"`).toString().split("\n").includes(tar)
};
};
const createTar = (details: Details) => {
execSync(`tar -czvf "${getTarFilename(formatFilename(details.filename))}" "${details.filename}"`);
};
const copy = (tar: string, dir: string) => {
execSync(`scp "${tar}" "${env.ARCHIVE_SERVER}:${dir}"`);
};
const cleanup = (tar: string, filename: string) => {
const isDir = fs.lstatSync(filename).isDirectory();
fs.rmSync(filename, {recursive: isDir});
fs.rmSync(tar);
};
const run = async () => {
const details = inputSchema.parse(await getDetails());
const tar = getTarFilename(formatFilename(details.filename));
const {dir, exists} = scaffoldArchive(tar, formatCwd);
if (exists) throw new Error("File already exists");
createTar(details);
copy(tar, dir);
if (!details.cleanup) return;
cleanup(tar, details.filename);
};
run().catch(console.error);