Skip to content

Commit

Permalink
feat: cleanup unused fpm versions
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed May 9, 2016
1 parent 3ab0e57 commit 633d006
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .idea/dictionaries/develar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ script:
- npm run test

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

branches:
except:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"bin": {
"build": "./out/build-cli.js",
"cleanup": "./out/cleanup.js",
"install-app-deps": "./out/install-app-deps.js"
},
"scripts": {
Expand Down
62 changes: 62 additions & 0 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#! /usr/bin/env node

import { homedir } from "os"
import { readdir, lstat, Stats, remove, readFile } from "fs-extra-p"
import { Promise as BluebirdPromise } from "bluebird"
import * as path from "path"

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")

async function main() {
const dir = path.join(homedir(), ".cache", "fpm")
let items: string[] | null = null
try {
items = await readdir(dir)
}
catch (e) {
if (e.code !== "ENOENT") {
throw e
}
return
}

await BluebirdPromise.map(items, <(item: string) => BluebirdPromise<any>> (async (it) => {
let stat: Stats | null = null
const itemPath = path.join(dir, it)
try {
stat = await lstat(itemPath)
}
catch (e) {
if (e.code !== "ENOENT") {
throw e
}
return
}

if (!stat!.isDirectory() || !(await isRecentlyUsed(itemPath))) {
console.log(`remove unused ${itemPath}`)
await remove(itemPath)
}
}))

await BluebirdPromise.map(items, remove)
}

async function isRecentlyUsed(dir: string) {
try {
const lastUsed = parseInt(await readFile(path.join(dir, ".lastUsed"), "utf8"), 10)
if (!isNaN(lastUsed) && (Date.now() - lastUsed) < 3600000) {
return true
}
}
catch (e) {
if (e.code !== "ENOENT") {
throw e
}
}

return false
}

main()
2 changes: 1 addition & 1 deletion src/codeSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function downloadUrlOrBase64(urlOrBase64: string, destination: string): Bluebird
return download(urlOrBase64, destination)
}
else {
return outputFile(destination, Buffer.from(urlOrBase64, "base64"))
return outputFile(destination, new Buffer(urlOrBase64, "base64"))
}
}

Expand Down
37 changes: 13 additions & 24 deletions src/fpmDownload.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { statOrNull, spawn, debug, debug7z } from "./util"
import { readdir, mkdirs, move, remove } from "fs-extra-p"
import { rename, remove } from "fs-extra-p"
import { download } from "./httpRequest"
import { path7za } from "7zip-bin"
import * as path from "path"
import { homedir } from "os"
import { Promise as BluebirdPromise } from "bluebird"
import { writeFile } from "fs"

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

// the only version currently supported (i.e. all clients are consumed the same version
await emptyDir(cacheDir, dirName)

// 7z cannot be extracted from the input stream, temp file is required
const tempName = getTempName()
const archiveName = path.join(cacheDir, tempName + ".7z")
Expand All @@ -68,27 +66,18 @@ async function doDownloadFpm(version: string, osAndArch: string): Promise<string
stdio: ["ignore", debug.enabled ? "inherit" : "ignore", "inherit"],
})

await BluebirdPromise.all([move(path.join(tempUnpackDir, dirName), fpmDir, {clobber: true}), remove(archiveName)])
await remove(tempUnpackDir)
await BluebirdPromise.all<any>([
rename(path.join(tempUnpackDir, dirName), fpmDir)
.catch(e => {
console.warn("Cannot move downloaded fpm into final location (another process downloaded faster?): " + e)
}),
remove(archiveName),
])
await BluebirdPromise.all([
remove(tempUnpackDir),
writeFile(path.join(fpmDir, ".lastUsed"), Date.now().toString())
])

debug(`fpm downloaded to ${fpmDir}`)
return path.join(fpmDir, "fpm")
}

// prefix to not delete dir or archived dir (.7z)
async function emptyDir(dir: string, excludeNamePrefix: string) {
let items: string[] | null = null
try {
items = await readdir(dir)
}
catch (e) {
await mkdirs(dir)
return
}

items = items!
.filter(it => !it.startsWith(excludeNamePrefix))
.map(it => path.join(dir, it))

await BluebirdPromise.map(items, remove)
}
2 changes: 1 addition & 1 deletion test/src/ArtifactPublisherTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function versionNumber() {
return getRandomInt(0, 99) + "." + Date.now() + "." + getRandomInt(0, 9)
}

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

//test("GitHub unauthorized", async (t) => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"src/awaiter.ts",
"src/build-cli.ts",
"src/builder.ts",
"src/cleanup.ts",
"src/codeSign.ts",
"src/errorMessages.ts",
"src/fpmDownload.ts",
Expand Down

0 comments on commit 633d006

Please sign in to comment.