Skip to content

Commit 633d006

Browse files
committed
feat: cleanup unused fpm versions
1 parent 3ab0e57 commit 633d006

File tree

8 files changed

+83
-27
lines changed

8 files changed

+83
-27
lines changed

.idea/dictionaries/develar.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ script:
4949
- npm run test
5050

5151
after_success:
52-
- if [[ "$TRAVIS_BRANCH" == "master" && "$TRAVIS_PULL_REQUEST" == "false" && "$AUTO_PUBLISH" != "false" ]]; then npm run semantic-release ; fi
52+
- node out/cleanup.js
53+
- if [[ "$TRAVIS_BRANCH" == "master" && "$TRAVIS_PULL_REQUEST" == "false" && "$AUTO_PUBLISH" != "false" ]]; then npm run semantic-release ; fi
5354

5455
branches:
5556
except:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
],
1111
"bin": {
1212
"build": "./out/build-cli.js",
13+
"cleanup": "./out/cleanup.js",
1314
"install-app-deps": "./out/install-app-deps.js"
1415
},
1516
"scripts": {

src/cleanup.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#! /usr/bin/env node
2+
3+
import { homedir } from "os"
4+
import { readdir, lstat, Stats, remove, readFile } from "fs-extra-p"
5+
import { Promise as BluebirdPromise } from "bluebird"
6+
import * as path from "path"
7+
8+
//noinspection JSUnusedLocalSymbols
9+
const __awaiter = require("./awaiter")
10+
11+
async function main() {
12+
const dir = path.join(homedir(), ".cache", "fpm")
13+
let items: string[] | null = null
14+
try {
15+
items = await readdir(dir)
16+
}
17+
catch (e) {
18+
if (e.code !== "ENOENT") {
19+
throw e
20+
}
21+
return
22+
}
23+
24+
await BluebirdPromise.map(items, <(item: string) => BluebirdPromise<any>> (async (it) => {
25+
let stat: Stats | null = null
26+
const itemPath = path.join(dir, it)
27+
try {
28+
stat = await lstat(itemPath)
29+
}
30+
catch (e) {
31+
if (e.code !== "ENOENT") {
32+
throw e
33+
}
34+
return
35+
}
36+
37+
if (!stat!.isDirectory() || !(await isRecentlyUsed(itemPath))) {
38+
console.log(`remove unused ${itemPath}`)
39+
await remove(itemPath)
40+
}
41+
}))
42+
43+
await BluebirdPromise.map(items, remove)
44+
}
45+
46+
async function isRecentlyUsed(dir: string) {
47+
try {
48+
const lastUsed = parseInt(await readFile(path.join(dir, ".lastUsed"), "utf8"), 10)
49+
if (!isNaN(lastUsed) && (Date.now() - lastUsed) < 3600000) {
50+
return true
51+
}
52+
}
53+
catch (e) {
54+
if (e.code !== "ENOENT") {
55+
throw e
56+
}
57+
}
58+
59+
return false
60+
}
61+
62+
main()

src/codeSign.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function downloadUrlOrBase64(urlOrBase64: string, destination: string): Bluebird
3030
return download(urlOrBase64, destination)
3131
}
3232
else {
33-
return outputFile(destination, Buffer.from(urlOrBase64, "base64"))
33+
return outputFile(destination, new Buffer(urlOrBase64, "base64"))
3434
}
3535
}
3636

src/fpmDownload.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { statOrNull, spawn, debug, debug7z } from "./util"
2-
import { readdir, mkdirs, move, remove } from "fs-extra-p"
2+
import { rename, remove } from "fs-extra-p"
33
import { download } from "./httpRequest"
44
import { path7za } from "7zip-bin"
55
import * as path from "path"
66
import { homedir } from "os"
77
import { Promise as BluebirdPromise } from "bluebird"
8+
import { writeFile } from "fs"
89

910
//noinspection JSUnusedLocalSymbols
1011
const __awaiter = require("./awaiter")
@@ -47,9 +48,6 @@ async function doDownloadFpm(version: string, osAndArch: string): Promise<string
4748
return path.join(fpmDir, "fpm")
4849
}
4950

50-
// the only version currently supported (i.e. all clients are consumed the same version
51-
await emptyDir(cacheDir, dirName)
52-
5351
// 7z cannot be extracted from the input stream, temp file is required
5452
const tempName = getTempName()
5553
const archiveName = path.join(cacheDir, tempName + ".7z")
@@ -68,27 +66,18 @@ async function doDownloadFpm(version: string, osAndArch: string): Promise<string
6866
stdio: ["ignore", debug.enabled ? "inherit" : "ignore", "inherit"],
6967
})
7068

71-
await BluebirdPromise.all([move(path.join(tempUnpackDir, dirName), fpmDir, {clobber: true}), remove(archiveName)])
72-
await remove(tempUnpackDir)
69+
await BluebirdPromise.all<any>([
70+
rename(path.join(tempUnpackDir, dirName), fpmDir)
71+
.catch(e => {
72+
console.warn("Cannot move downloaded fpm into final location (another process downloaded faster?): " + e)
73+
}),
74+
remove(archiveName),
75+
])
76+
await BluebirdPromise.all([
77+
remove(tempUnpackDir),
78+
writeFile(path.join(fpmDir, ".lastUsed"), Date.now().toString())
79+
])
7380

7481
debug(`fpm downloaded to ${fpmDir}`)
7582
return path.join(fpmDir, "fpm")
76-
}
77-
78-
// prefix to not delete dir or archived dir (.7z)
79-
async function emptyDir(dir: string, excludeNamePrefix: string) {
80-
let items: string[] | null = null
81-
try {
82-
items = await readdir(dir)
83-
}
84-
catch (e) {
85-
await mkdirs(dir)
86-
return
87-
}
88-
89-
items = items!
90-
.filter(it => !it.startsWith(excludeNamePrefix))
91-
.map(it => path.join(dir, it))
92-
93-
await BluebirdPromise.map(items, remove)
9483
}

test/src/ArtifactPublisherTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function versionNumber() {
1414
return getRandomInt(0, 99) + "." + Date.now() + "." + getRandomInt(0, 9)
1515
}
1616

17-
const token = Buffer.from("Y2Y5NDdhZDJhYzJlMzg1OGNiNzQzYzcwOWZhNGI0OTk2NWQ4ZDg3Yg==", "base64").toString()
17+
const token = new Buffer("Y2Y5NDdhZDJhYzJlMzg1OGNiNzQzYzcwOWZhNGI0OTk2NWQ4ZDg3Yg==", "base64").toString()
1818
const iconPath = join(__dirname, "..", "fixtures", "test-app", "build", "icon.icns")
1919

2020
//test("GitHub unauthorized", async (t) => {

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"src/awaiter.ts",
6060
"src/build-cli.ts",
6161
"src/builder.ts",
62+
"src/cleanup.ts",
6263
"src/codeSign.ts",
6364
"src/errorMessages.ts",
6465
"src/fpmDownload.ts",

0 commit comments

Comments
 (0)