Skip to content

Commit

Permalink
Lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Takeshi Iwana committed Dec 19, 2020
1 parent a104d58 commit 9c3e217
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 26 deletions.
5 changes: 2 additions & 3 deletions src/utils/download.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import GitlyOptions from '../interfaces/options'

import fetch from './fetch'
import execute from './execute'
import exists from './exists'
import fetch from './fetch'
import { isOffline } from './offline'
import parse from './parse'
import { getFile, getUrl } from './tar'
Expand All @@ -11,7 +11,6 @@ import { getFile, getUrl } from './tar'
* Download the tar file from the repository
* and store it in a temporary directory
* @param repository The repository to download
* @param options
*
* @example
* ```js
Expand All @@ -23,7 +22,7 @@ import { getFile, getUrl } from './tar'
export default async function download(
repository: string,
options: GitlyOptions = {}
) {
): Promise<string> {
const info = parse(repository, options)
const file = getFile(info, options)
const url = getUrl(info, options)
Expand Down
8 changes: 6 additions & 2 deletions src/utils/exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import GitlyOptions from '../interfaces/options'
import parse from './parse'
import { getFile } from './tar'

export default async (path: string, options: GitlyOptions = {}) => {
export default async function exists(
path: string,
options: GitlyOptions = {}
): Promise<boolean> {
if (!isAbsolute(path)) {
path = getFile(parse(path), options)
}
try {
await fs.access(path, constants.F_OK)
return true
} catch (_) { }
// eslint-disable-next-line no-empty
} catch (_) {}
return false
}
16 changes: 11 additions & 5 deletions src/utils/extract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { promises as fs } from 'fs'
import { resolve } from 'path'
import { FileStat } from 'tar'

import GitlyOptions from '../interfaces/options'

Expand All @@ -16,16 +15,23 @@ const { mkdir } = fs
* @param options
*
*/
export default async (source: string, destination: string, options: GitlyOptions = {}) => {
export default async (
source: string,
destination: string,
options: GitlyOptions = {}
): Promise<string> => {
destination = resolve(destination)
if (await exists(source, options)) {
try {
let filter = (options.extract && options.extract.filter) ? options.extract.filter :
(_path: string, _stat: FileStat) => true
const filter =
options.extract && options.extract.filter
? options.extract.filter
: () => true
await mkdir(destination, { recursive: true })
await extract({ strip: 1, filter, file: source, cwd: destination })
return destination
} catch (_) { }
// eslint-disable-next-line no-empty
} catch (_) {}
}
return ''
}
2 changes: 1 addition & 1 deletion src/utils/gitly.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import GitlyOptions from '../interfaces/options'

import download from './download'
import extract from './extract'

/**
* Downloads and extracts the repository
* @param repository The repository to download
* @param destination The destination to extract
* @param options
* @returns A tuple with the source and destination respectively
*/
export default async function gitly(
Expand Down
3 changes: 2 additions & 1 deletion src/utils/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export async function isOffline(): Promise<boolean> {
try {
await lookup('google.com')
return false
} catch (_) { }
// eslint-disable-next-line no-empty
} catch (_) {}
return true
}
20 changes: 10 additions & 10 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,39 @@ import URLInfo from '../interfaces/url'
* ```
*/
export default (url: string, options: GitlyOptions = {}): URLInfo => {
let { url: normalized, host } = normalizeURL(url, options)
const { url: normalized, host } = normalizeURL(url, options)

// Parse the url
const result = parse(normalized)
const paths = (result.path || '').split('/').filter(p => !!p)
const paths = (result.path || '').split('/').filter((p) => !!p)
const owner = paths.shift() || ''
const repository = paths.shift() || ''
return {
protocol: (result.protocol || 'https').replace(/\:/g, ''),
protocol: (result.protocol || 'https').replace(/:/g, ''),
host: result.host || host || 'github.com',
hostname: (result.hostname || host || 'github').replace(/\.(\S+)/, ''),
hash: result.hash || '',
href: result.href || '',
path: result.path || '',
repository,
owner,
type: (result.hash || '#master').substr(1)
type: (result.hash || '#master').substr(1),
}
}

function normalizeURL(url: string, options: any) {
function normalizeURL(url: string, options: GitlyOptions) {
// Remove 'www.'
url = url.replace('www.', '')
// Remove '.git'
url = url.replace('.git', '')
const httpRegex = /http(s)?\:\/\//
const httpRegex = /http(s)?:\/\//
const tldRegex = /[\S]+\.([\D]+)/
let host = options.host || ''
if (/([\S]+)\:.+/.test(url) && !httpRegex.test(url)) {
if (/([\S]+):.+/.test(url) && !httpRegex.test(url)) {
/**
* Matches host:owner/repo
*/
const matches = url.match(/([\S]+)\:.+/)
const matches = url.match(/([\S]+):.+/)
// Get the host
host = matches ? matches[1] : ''
// Remove the host from the url
Expand All @@ -68,10 +68,10 @@ function normalizeURL(url: string, options: any) {
*/

// Get the TLD if any
const matches = (options.host as string || '').match(tldRegex)
const matches = ((options.host as string) || '').match(tldRegex)
let match = 'com'
if (matches) match = matches[1]
let domain = (options.host || 'github')
let domain = options.host || 'github'
domain = domain.replace(`.${match}`, '')

url = `https://${domain}.${match}/${url}`
Expand Down
11 changes: 8 additions & 3 deletions src/utils/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tar = require('tar')
import GitlyOptions from '../interfaces/options'
import URLInfo from '../interfaces/url'

export function getUrl(info: URLInfo, options: GitlyOptions = {}) {
export function getUrl(info: URLInfo, options: GitlyOptions = {}): string {
const { path: repo, type } = info

if (options.url && options.url.filter) {
Expand All @@ -22,9 +22,14 @@ export function getUrl(info: URLInfo, options: GitlyOptions = {}) {
}
}

export function getFile(info: URLInfo, options: GitlyOptions = {}) {
export function getFile(info: URLInfo, options: GitlyOptions = {}): string {
const { path, type, hostname: site } = info
return join(options.temp || join(os.homedir(), '.gitly'), site, path, `${type}.tar.gz`)
return join(
options.temp || join(os.homedir(), '.gitly'),
site,
path,
`${type}.tar.gz`
)
}

export const extract = tar.extract
3 changes: 2 additions & 1 deletion src/utils/write.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WriteStream } from 'fs'
import { createWriteStream, promises as fs } from 'fs'
import { dirname, normalize } from 'path'

Expand All @@ -6,7 +7,7 @@ const { mkdir } = fs
* Create a folder and return a writable stream.
* @param path The path to write a file
*/
export default async (path: string) => {
export default async function write(path: string): Promise<WriteStream> {
path = normalize(path)
await mkdir(dirname(path), { recursive: true })
return createWriteStream(path)
Expand Down

0 comments on commit 9c3e217

Please sign in to comment.