feat: migrated packages to ts

This commit is contained in:
Haoyu Xu
2025-05-02 02:27:42 +08:00
parent 0af0c785d4
commit 8f6f537c81
111 changed files with 3166 additions and 1155 deletions

View File

@@ -1,7 +1,19 @@
import sharp from 'sharp'
import path from 'node:path'
export const process = async (filename, maskFilename, extractedDir) => {
export type Rect = {
x: number
y: number
w: number
h: number
rotate: number
}
export const process = async (
filename: string,
maskFilename: string,
extractedDir: string
) => {
const image = sharp(path.join(extractedDir, filename)).removeAlpha()
const imageMeta = await image.metadata()
const imageBuffer = await image.toBuffer()
@@ -13,7 +25,22 @@ export const process = async (filename, maskFilename, extractedDir) => {
return sharp(imageBuffer).joinChannel(mask).toBuffer()
}
export const crop = async (buffer, rect) => {
export const crop = async (
buffer:
| Buffer
| ArrayBuffer
| Uint8Array
| Uint8ClampedArray
| Int8Array
| Uint16Array
| Int16Array
| Uint32Array
| Int32Array
| Float32Array
| Float64Array
| string,
rect: Rect
) => {
const left = rect.y
const top = rect.x
const width = rect.h
@@ -28,7 +55,7 @@ export const crop = async (buffer, rect) => {
return await sharp(newImage).rotate(rotate).toBuffer()
}
export const toBuffer = async (filename, extractedDir) => {
export const toBuffer = async (filename: string, extractedDir: string) => {
const file = path.join(extractedDir, filename)
const { data, info } = await sharp(file)
.raw()
@@ -36,7 +63,7 @@ export const toBuffer = async (filename, extractedDir) => {
const { width, height, channels } = info
const pixelArray = new Uint8ClampedArray(data.buffer)
for (let i = 0; i < pixelArray.length; i += 4) {
let alpha = pixelArray[i + 3] / 255
const alpha = pixelArray[i + 3] / 255
pixelArray[i + 0] = pixelArray[i + 0] * alpha
pixelArray[i + 1] = pixelArray[i + 1] * alpha
pixelArray[i + 2] = pixelArray[i + 2] * alpha

View File

@@ -1,4 +1,9 @@
export function generate(values) {
export function generate(
values: {
key: string
value: string
}[]
) {
return values
.map((value) => {
return `VITE_${value.key.toUpperCase()}=${value.value}`

View File

@@ -1,28 +1,40 @@
import process from 'node:process'
export const parse = (args) => {
type Args = {
[name: string]: {
type?: string
default?: unknown
multiple?: boolean
short?: string
}
}
export const parse = (args: Args) => {
const envVars = process.env
const argKeys = Object.keys(args)
const values = {}
const values: Record<string, unknown | unknown[]> = {}
argKeys.map((key) => {
let noInput = false
let value,
type = args[key].type || 'string',
let value: unknown
const type = args[key].type || 'string',
defaultVal = args[key].default,
multiple = args[key].multiple || false,
short = args[key].short
value = envVars[key] || envVars[short]
value = short
? envVars[short]
? envVars[short]
: envVars[key]
: envVars[key]
if (!value) noInput = true
value = noInput ? defaultVal : value
if (noInput) {
values[key] = value
} else {
value = multiple ? value.split(',') : value
const cValue = multiple ? (value as string).split(',') : value
if (multiple) {
values[key] = []
value.map((item) => {
values[key].push(typeCast(type, item))
})
values[key] = (cValue as string[]).map((item: string) =>
typeCast(type, item)
)
} else {
values[key] = typeCast(type, value)
}
@@ -31,7 +43,7 @@ export const parse = (args) => {
return values
}
const typeCast = (type, value) => {
const typeCast = (type: 'number' | 'boolean' | string, value: unknown) => {
switch (type) {
case 'number':
return Number(value)

View File

@@ -1,4 +1,4 @@
export const handle = (err) => {
export const handle = (err: string[]) => {
if (err.length > 0) {
const str = `${err.length} error${err.length > 1 ? 's were' : ' was'} found:\n${err.join('\n')}`
throw new Error(str)

View File

@@ -3,52 +3,61 @@ import path from 'node:path'
import yauzl from 'yauzl-promise'
import yazl from 'yazl'
export async function write(content, filePath) {
export async function write(
content: string | NodeJS.ArrayBufferView,
filePath: string
) {
mkdir(path.dirname(filePath))
return await fsP.writeFile(filePath, content, { flag: 'w' })
}
export function writeSync(content, filePath) {
export function writeSync(
content: string | NodeJS.ArrayBufferView,
filePath: string
) {
mkdir(path.dirname(filePath))
return fs.writeFileSync(filePath, content, { flag: 'w' })
}
export async function read(filePath, encoding = 'utf8') {
return await fsP.readFile(filePath, encoding, { flag: 'r' })
export async function read(
filePath: string,
encoding: BufferEncoding = 'utf8'
) {
return await fsP.readFile(filePath, { encoding, flag: 'r' })
}
export function readSync(filePath, encoding = 'utf8') {
export function readSync(filePath: string, encoding: BufferEncoding = 'utf8') {
if (exists(filePath)) {
return fs.readFileSync(filePath, encoding, { flag: 'r' })
return fs.readFileSync(filePath, { encoding, flag: 'r' })
}
return null
}
export function exists(filePath) {
export function exists(filePath: string) {
return fs.existsSync(filePath)
}
export function rmdir(dir) {
export function rmdir(dir: string) {
if (exists(dir)) {
fs.rmSync(dir, { recursive: true })
}
}
export function rm(dir) {
export function rm(dir: string) {
if (exists(dir)) {
fs.rmSync(dir, { recursive: true })
}
}
export function mkdir(dir) {
export function mkdir(dir: string) {
if (!exists(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
export async function copy(
sourcePath,
targetPath,
sourcePath: string,
targetPath: string,
mode = fs.constants.COPYFILE_FICLONE
) {
if (!exists(sourcePath)) {
@@ -60,8 +69,8 @@ export async function copy(
}
export async function copyDir(
sourcePath,
targetPath,
sourcePath: string,
targetPath: string,
mode = fs.constants.COPYFILE_FICLONE
) {
if (!exists(sourcePath)) {
@@ -72,11 +81,11 @@ export async function copyDir(
return await fsP.cp(sourcePath, targetPath, { recursive: true, mode })
}
export function appendSync(content, filePath) {
export function appendSync(content: string | Uint8Array, filePath: string) {
return fs.appendFileSync(filePath, content, 'utf8')
}
export function readdirSync(dir) {
export function readdirSync(dir: string) {
if (!exists(dir)) {
console.warn(`Source ${dir} does not exist.`)
return []
@@ -84,7 +93,7 @@ export function readdirSync(dir) {
return fs.readdirSync(dir)
}
export function fileTypeSync(dir) {
export function fileTypeSync(dir: string) {
if (!exists(dir)) {
console.warn(`Source ${dir} does not exist.`)
return null
@@ -92,7 +101,7 @@ export function fileTypeSync(dir) {
return fs.statSync(dir).isDirectory() ? 'dir' : 'file'
}
export const symlink = (source, target) => {
export const symlink = (source: string, target: string) => {
if (!exists(source)) {
console.warn(`Source ${source} does not exist.`)
return
@@ -105,14 +114,14 @@ export const symlink = (source, target) => {
fs.symlinkSync(relative, target)
}
export const symlinkAll = (source, target) => {
export const symlinkAll = (source: string, target: string) => {
const files = readdirSync(source)
files.map((file) => {
symlink(path.join(source, file), path.join(target, file))
})
}
export const mv = (source, target) => {
export const mv = (source: string, target: string) => {
if (!exists(source)) {
console.warn(`Source file ${source} does not exist.`)
return
@@ -125,8 +134,8 @@ export const mv = (source, target) => {
}
export const cpSync = (
source,
target,
source: string,
target: string,
opts = {
dereference: false,
}
@@ -146,7 +155,7 @@ export const cpSync = (
})
}
export const relative = (source, target) => {
export const relative = (source: string, target: string) => {
if (!exists(source)) {
console.warn(`Source file ${source} does not exist.`)
return
@@ -154,7 +163,7 @@ export const relative = (source, target) => {
return path.relative(source, target)
}
export const size = (source) => {
export const size = (source: string) => {
if (!exists(source)) {
console.warn(`Source file ${source} does not exist.`)
return

View File

@@ -1,12 +1,17 @@
import fs from 'node:fs'
import path from 'node:path'
import { parse } from 'yaml'
import type { Tags, ScalarTag, SchemaOptions, CollectionTag } from 'yaml'
export function read(file_dir, customTags = []) {
const include = {
identify: (value) => value.startsWith('!include'),
export function read(
file_dir: string,
customTags: ScalarTag[] | CollectionTag[] = []
) {
const include: ScalarTag = {
identify: (value: unknown) =>
typeof value === 'string' && value.startsWith('!include'),
tag: '!include',
resolve(str) {
resolve(str: string) {
const dir = path.resolve(path.dirname(file_dir), str)
const data = read(dir)
return data
@@ -15,5 +20,5 @@ export function read(file_dir, customTags = []) {
const file = fs.readFileSync(file_dir, 'utf8')
return parse(file, {
customTags: [include, ...customTags],
})
} as SchemaOptions)
}