Skip to content

Commit

Permalink
feat: complete the initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
liuweiGL committed Mar 31, 2021
1 parent 6256424 commit 6316831
Show file tree
Hide file tree
Showing 11 changed files with 1,030 additions and 3,517 deletions.
3,858 changes: 570 additions & 3,288 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
"rollup": "^2.44.0",
"ts-node": "^9.1.1",
"typescript": "^4.2.3",
"vite": "^2.1.4"
"vite": "^2.1.5"
}
}
6 changes: 4 additions & 2 deletions src/lib/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from 'path'

import pkg from '../../package.json'

export const PLUGIN_NAME = pkg.name
export const PKG_NAME = pkg.name

export const PLUGIN_DATA_DIR = path.join(os.homedir(), `.${PLUGIN_NAME}`)
export const PLUGIN_NAME = PKG_NAME.replace('-', ':')

export const PLUGIN_DATA_DIR = path.join(os.homedir(), `.${PKG_NAME}`)
80 changes: 48 additions & 32 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import fs from 'fs'
import path from 'path'

import util from 'util'
import child_process from 'child_process'
import os from 'os'
import { PLUGIN_DATA_DIR } from './constant'

/**
* Check if file exists
*
* @param filePath file path
* @returns does the file exist
*/
export const exists = async (filePath: string) => {
try {
await fs.promises.access(filePath)
return true
} catch (error) {
return false
}
}

/**
* Resolve file path with `PLUGIN_DATA_DIR`
*
Expand All @@ -13,42 +30,41 @@ export const resolvePath = (fileName: string) => {
return path.resolve(PLUGIN_DATA_DIR, fileName)
}

/**
* Make sure `PLUGIN_DATA_DIR` exists
*/
export const ensureDataDir = async () => {
try {
await fs.promises.access(PLUGIN_DATA_DIR, fs.constants.F_OK)
} catch {
await fs.promises.mkdir(PLUGIN_DATA_DIR, { recursive: true })
}
export const exec = async (cmd: string) => {
return await util.promisify(child_process.exec)(cmd)
}

export const readFile = async (filePath: string) => {
return (await fs.promises.readFile(filePath)).toString()
}

/**
* Write data to the file under `PLUGIN_DATA_DIR`
*
* @param fileName file name
* @param data file content
* @returns file path
*/
export const writeFile = async (
fileName: string,
filePath: string,
data: string | Uint8Array
) => {
const filePath = resolvePath(fileName)
await ensureDataDir()
await fs.promises.writeFile(filePath, data)
return filePath
const dirname = path.dirname(filePath)
const exist = await exists(dirname)

if (!exists) {
await fs.promises.mkdir(dirname, { recursive: true })
}

return await fs.promises.writeFile(filePath, data)
}

/**
* Read content from the file under `PLUGIN_DATA_DIR`
*
* @param fileName file name
* @returns file content
*/
export const readFile = async (fileName: string) => {
const filePath = resolvePath(fileName)
await ensureDataDir()
return await fs.promises.readFile(filePath)
export const getLocalV4Ips = () => {
const interfaceDict = os.networkInterfaces()
const addresses: string[] = []
for (const key in interfaceDict) {
const interfaces = interfaceDict[key]
if (interfaces) {
for (const item of interfaces) {
if (item.family === 'IPv4') {
addresses.push(item.address)
}
}
}
}

return addresses
}
106 changes: 106 additions & 0 deletions src/mkcert/Source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Octokit } from '@octokit/rest'
import log from '../lib/log'
import request from '../lib/request'

export type SourceInfo = {
version: string
downloadUrl: string
}

export abstract class BaseSource {
abstract getSourceInfo(): Promise<SourceInfo | undefined>

protected getPlatformIdentifier() {
switch (process.platform) {
case 'win32':
return 'windows-amd64.exe'
case 'linux':
return process.arch === 'arm64'
? 'linux-arm64'
: process.arch === 'arm'
? 'linux-arm'
: 'linux-amd64'
case 'darwin':
return 'darwin-amd64'
default:
throw new Error('Unsupported platform')
}
}
}

/**
* Download mkcert from github repo
*/
export class GithubSource extends BaseSource {
public static create() {
return new GithubSource()
}

private constructor() {
super()
}

public async getSourceInfo(): Promise<SourceInfo | undefined> {
let version: string | undefined
let downloadUrl: string | undefined

const octokit = new Octokit()
const { data } = await octokit.repos.getLatestRelease({
owner: 'FiloSottile',
repo: 'mkcert'
})
const platformIdentifier = this.getPlatformIdentifier()

version = data.tag_name
downloadUrl = data.assets.find(item =>
item.name.includes(platformIdentifier)
)?.browser_download_url

if (!(version && downloadUrl)) {
log('Github assets do not match')
return
}

return {
downloadUrl,
version
}
}
}

/**
* Download mkcert from coding repo
*
* @see {https://help.coding.net/openapi}
*/
export class CodingSource extends BaseSource {
private static CODING_OPEN_API = 'https://liuweigl.coding.net/open-api'

public static create() {
return new CodingSource()
}

private constructor() {
super()
}

async getSourceInfo(): Promise<SourceInfo | undefined> {
const { data } = await request({
method: 'POST',
url: CodingSource.CODING_OPEN_API,
params: {
Action: 'DescribeArtifactRepositoryFileList'
},
data: {
Action: 'DescribeArtifactRepositoryFileList',
Repository: 'mkcert',
Project: 'github',
PageSize: 10
}
})

console.log(data)

return data
}
}
Loading

0 comments on commit 6316831

Please sign in to comment.