Skip to content

Commit

Permalink
move filetype func to util and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Sep 6, 2021
1 parent cd44c24 commit 88ccc8e
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/app-builder-lib/src/appInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { prerelease, SemVer } from "semver"
import { PlatformSpecificBuildOptions } from "./options/PlatformSpecificBuildOptions"
import { Packager } from "./packager"
import { expandMacro } from "./util/macroExpander"
import { sanitizeFileName } from "./util/sanitizeFileName"
import { sanitizeFileName } from "./util/filename"

// fpm bug - rpm build --description is not escaped, well... decided to replace quite to smart quote
// http://leancrew.com/all-this/2010/11/smart-quotes-in-javascript/
Expand Down
2 changes: 1 addition & 1 deletion packages/app-builder-lib/src/linuxPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import FpmTarget from "./targets/fpm"
import { LinuxTargetHelper } from "./targets/LinuxTargetHelper"
import SnapTarget from "./targets/snap"
import { createCommonTarget } from "./targets/targetFactory"
import { sanitizeFileName } from "./util/sanitizeFileName"
import { sanitizeFileName } from "./util/filename"

export class LinuxPackager extends PlatformPackager<LinuxConfiguration> {
readonly executableName: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InvalidConfigurationError, isEmptyOrSpaces } from "builder-util"
import { sanitizeFileName } from "../util/sanitizeFileName"
import { sanitizeFileName } from "../util/filename"
import { WinPackager } from "../winPackager"

export interface CommonWindowsInstallerConfiguration {
Expand Down
35 changes: 2 additions & 33 deletions packages/app-builder-lib/src/publish/KeygenPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ClientRequest, RequestOptions } from "http"
import { HttpPublisher, PublishContext } from "electron-publish"
import { KeygenOptions } from "builder-util-runtime/out/publishOptions"
import { configureRequestOptions, HttpExecutor, parseJson } from "builder-util-runtime"
import * as path from "path"
import { getCompleteExtname } from "../util/filename"

export class KeygenPublisher extends HttpPublisher {
readonly providerName = "keygen"
Expand Down Expand Up @@ -79,7 +79,7 @@ export class KeygenPublisher extends HttpPublisher {
type: "release",
attributes: {
filename: fileName,
filetype: this.getFiletype(fileName),
filetype: getCompleteExtname(fileName),
filesize: dataLength,
version: this.version,
platform: this.info.platform,
Expand Down Expand Up @@ -114,35 +114,4 @@ export class KeygenPublisher extends HttpPublisher {
const { account, product, platform } = this.info
return `Keygen (account: ${account}, product: ${product}, platform: ${platform}, version: ${this.version})`
}

// Get the filetype from a filename. Returns a string of one or more file extensions,
// e.g. `.zip`, `.dmg`, `.tar.gz`, `.tar.bz2`, `.exe.blockmap`. We'd use `path.extname()`,
// but it doesn't support multiple extensions, e.g. `.dmg.blockmap`.
private getFiletype(filename: string): string {
let extname = path.extname(filename)

switch (extname) {
// Append leading extension for blockmap filetype
case '.blockmap': {
extname = path.extname(filename.replace(extname, '')) + extname

break
}
// Append leading extension for known compressed tar formats
case '.bz2':
case '.gz':
case '.lz':
case '.xz':
case '.7z': {
const ext = path.extname(filename.replace(extname, ''))
if (ext === '.tar') {
extname = ext + extname
}

break
}
}

return extname
}
}
39 changes: 39 additions & 0 deletions packages/app-builder-lib/src/util/filename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// @ts-ignore
import * as _sanitizeFileName from "sanitize-filename"
import * as path from 'path'

export function sanitizeFileName(s: string): string {
return _sanitizeFileName(s)
}

// Get the filetype from a filename. Returns a string of one or more file extensions,
// e.g. .zip, .dmg, .tar.gz, .tar.bz2, .exe.blockmap. We'd normally use `path.extname()`,
// but it doesn't support multiple extensions, e.g. Foo-1.0.0.dmg.blockmap should be
// .dmg.blockmap, not .blockmap.
export function getCompleteExtname(filename: string): string {
let extname = path.extname(filename)

switch (extname) {
// Append leading extension for blockmap filetype
case '.blockmap': {
extname = path.extname(filename.replace(extname, '')) + extname

break
}
// Append leading extension for known compressed tar formats
case '.bz2':
case '.gz':
case '.lz':
case '.xz':
case '.7z': {
const ext = path.extname(filename.replace(extname, ''))
if (ext === '.tar') {
extname = ext + extname
}

break
}
}

return extname
}
6 changes: 0 additions & 6 deletions packages/app-builder-lib/src/util/sanitizeFileName.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/dmg-builder/src/dmg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { findIdentity, isSignAllowed } from "app-builder-lib/out/codeSign/macCod
import MacPackager from "app-builder-lib/out/macPackager"
import { createBlockmap } from "app-builder-lib/out/targets/differentialUpdateInfoBuilder"
import { executeAppBuilderAsJson } from "app-builder-lib/out/util/appBuilder"
import { sanitizeFileName } from "app-builder-lib/out/util/sanitizeFileName"
import { sanitizeFileName } from "app-builder-lib/out/util/filename"
import { Arch, AsyncTaskManager, exec, getArchSuffix, InvalidConfigurationError, isEmptyOrSpaces, log, spawn, retry } from "builder-util"
import { CancellationToken } from "builder-util-runtime"
import { copyDir, copyFile, exists, statOrNull } from "builder-util/out/fs"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sanitizeFileName } from "app-builder-lib/out/util/sanitizeFileName"
import { sanitizeFileName } from "app-builder-lib/out/util/filename"
import { InvalidConfigurationError, log, isEmptyOrSpaces } from "builder-util"
import { getBinFromUrl } from "app-builder-lib/out/binDownload"
import { Arch, getArchSuffix, SquirrelWindowsOptions, Target } from "app-builder-lib"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sanitizeFileName } from "app-builder-lib/out/util/sanitizeFileName"
import { sanitizeFileName } from "app-builder-lib/out/util/filename"
import { exec, log, spawn, TmpDir } from "builder-util"
import { unlinkIfExists } from "builder-util/out/fs"
import * as chalk from "chalk"
Expand Down
39 changes: 39 additions & 0 deletions test/src/filenameUtilTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getCompleteExtname } from "app-builder-lib/out/util/filename"

// [inputFilename, expectedExtname]
const tests = [
["Foo-v1.exe.blockmap", ".exe.blockmap"],
["Foo-1.0.0.dmg.blockmap", ".dmg.blockmap"],
["Foo-v1.0.0.exe.blockmap", ".exe.blockmap"],
["Foo-1.0.0-mac.zip.blockmap", ".zip.blockmap"],
["Foo-1.0.0.exe", ".exe"],
["foo-1.0.0.exe", ".exe"],
["foo.bar-1.0.0.dmg", ".dmg"],
["Foo-2.0.0.rc1.dmg", ".dmg"],
["Foo-1.0.0-mac.dmg", ".dmg"],
["Foo-v1.0.0.zip", ".zip"],
["Foo-1.0.0.tar.gz", ".tar.gz"],
["Foo-1.0.0.tar.7z", ".tar.7z"],
["Foo-1.0.0.7z", ".7z"],
["Foo-1.0.0.test.7z", ".7z"],
["Foo-1.0.0.tar.xz", ".tar.xz"],
["Foo-1.0.0.tar.lz", ".tar.lz"],
["Foo-1.0.0.tar.bz2", ".tar.bz2"],
["Foo.v2.tar.bz2", ".tar.bz2"],
["Foo-v1.0.0.tar.bz2", ".tar.bz2"],
["Application.test.dmg", ".dmg"],
["Program.1.0.0.beta1.exe", ".exe"],
["application.dmg", ".dmg"],
["latest.yml", ".yml"],
[".gitignore", ""],
[".config.yml", ".yml"],
["code.h", ".h"],
]

for (const [filename, expected] of tests) {
test(`get complete extname for ${filename}`, () => {
const extname = getCompleteExtname(filename)

expect(extname).toBe(expected)
})
}

0 comments on commit 88ccc8e

Please sign in to comment.