-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.ts
115 lines (100 loc) · 3.84 KB
/
deploy.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Run this with:
// deno run --unstable --allow-read --allow-write .\deploy.ts
import * as flags from "https://deno.land/[email protected]/flags/mod.ts";
import * as path from "https://deno.land/[email protected]/path/mod.ts";
import * as fs from "https://deno.land/[email protected]/fs/mod.ts";
import { walk } from "./walk.ts";
const __filename = path.fromFileUrl(import.meta.url);
const __dirname = path.dirname(__filename);
const args = flags.parse(Deno.args, {
boolean: ["force-overwrite", "help", "silent"],
alias: {
"force-overwrite": "f",
help: "h",
silent: "s",
},
});
if (args.help) {
console.log([
"OPTIONS",
"--help, -h Prints this help.",
"--force-overwrite, -f Deletes and recreates the output folder without notice.",
"--silent, -s Do not prompt for anything. Succeed or fail silently.",
].join("\n"));
Deno.exit();
}
async function _isDirEmpty(dir: string): Promise<boolean> {
try {
for await (const _ of Deno.readDir(dir)) {
return false;
}
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
return true;
}
try {
const currentDirName = path.basename(__dirname);
const outputDir = path.resolve(__dirname, "..", currentDirName + "-output");
if (await fs.exists(outputDir)) {
if (args["force-overwrite"]) {
console.log(
'Automatically cleaning up directory at "' + outputDir + '"',
);
await fs.emptyDir(outputDir);
} else if (!await _isDirEmpty(outputDir)) {
if (args.silent) {
console.error(
'Directory "' + outputDir + '" exists and is not empty.',
);
Deno.exit(1);
} else {
console.log('Output directory "' + outputDir + '" exists.');
const response = prompt("Delete and rewrite? (y/n)");
if (response?.toLowerCase() === "y") {
await fs.emptyDir(outputDir);
} else {
console.error(
'Directory "' + outputDir +
'" exists and is not empty.',
);
Deno.exit(1);
}
}
}
}
const fsWalker = walk(__dirname, {
includeFiles: true,
includeDirs: false,
followSymlinks: true,
skip: [
path.globToRegExp(path.resolve(__dirname, ".editorconfig")),
path.globToRegExp(path.resolve(__dirname, "*.ts")),
path.globToRegExp(path.resolve(__dirname, "*.bat")),
path.globToRegExp(path.resolve(__dirname, "README.md")),
path.globToRegExp(path.resolve(__dirname, "deno.jsonc")),
path.globToRegExp(path.resolve(__dirname, "images/**")),
path.globToRegExp("**/*.pdb"),
path.globToRegExp("**/.gitignore"),
path.globToRegExp("**/.gitattributes"),
path.globToRegExp("**/meta.ini"),
path.globToRegExp("**/*.7z"),
path.globToRegExp("**/.git/**/*"),
path.globToRegExp("**/.vscode/**/*"),
],
});
console.log("Copying files:");
for await (const walkEntry of fsWalker) {
const subpath = path.relative(__dirname, walkEntry.path);
const sourcePath = walkEntry.path;
const targetPath = path.join(outputDir, subpath);
const targetDir = path.dirname(targetPath);
console.log('"' + sourcePath + '" => "' + targetPath + '"');
await Deno.mkdir(targetDir, { recursive: true });
await Deno.copyFile(sourcePath, targetPath);
}
} catch (e) {
console.error(e);
}