From 5fc77dcfdf8a5da66a34cdc11947826750d520a1 Mon Sep 17 00:00:00 2001 From: kanno <812137533@qq.com> Date: Thu, 18 Jul 2024 11:30:54 +0800 Subject: [PATCH] feat: add publish workflow --- .github/workflows/publish.yaml | 25 +++++++++++++++++++++++++ __tests__/options.spec.ts | 2 +- __tests__/plugin.spec.ts | 2 +- __tests__/tarball.spec.ts | 2 +- src/compress.ts | 2 +- src/index.ts | 2 +- src/{utils.ts => shared.ts} | 11 ++--------- src/tar.ts | 2 +- src/task.ts | 2 +- 9 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/publish.yaml rename src/{utils.ts => shared.ts} (89%) diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml new file mode 100644 index 0000000..443fd2b --- /dev/null +++ b/.github/workflows/publish.yaml @@ -0,0 +1,25 @@ +name: publish +on: + push: + tags: ['v*'] + +jobs: + publish: + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '22.x' + registry-url: 'https://registry.npmjs.org' + - name: Install Dependices + run: make + - name: Pack and Publish + run: | + make build + npm publish --provenance --access public + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} \ No newline at end of file diff --git a/__tests__/options.spec.ts b/__tests__/options.spec.ts index 58099f1..7d485b5 100644 --- a/__tests__/options.spec.ts +++ b/__tests__/options.spec.ts @@ -4,7 +4,7 @@ import { afterAll, expect, test } from 'vitest' import type { Pretty, ViteCompressionPluginConfigAlgorithm } from '../src/interface' import { compression } from '../src' -import { readAll } from '../src/utils' +import { readAll } from '../src/shared' import type { Algorithm } from '../src' const getId = () => Math.random().toString(32).slice(2, 10) diff --git a/__tests__/plugin.spec.ts b/__tests__/plugin.spec.ts index 32765c0..fd3b8a5 100644 --- a/__tests__/plugin.spec.ts +++ b/__tests__/plugin.spec.ts @@ -5,7 +5,7 @@ import fsp from 'fs/promises' import util from 'util' import type { ZlibOptions } from 'zlib' import { afterAll, expect, test } from 'vitest' -import { len, readAll } from '../src/utils' +import { len, readAll } from '../src/shared' import { type Algorithm, type ViteCompressionPluginConfig, compression } from '../src' const getId = () => Math.random().toString(32).slice(2, 10) diff --git a/__tests__/tarball.spec.ts b/__tests__/tarball.spec.ts index f12aad9..14eb438 100644 --- a/__tests__/tarball.spec.ts +++ b/__tests__/tarball.spec.ts @@ -5,7 +5,7 @@ import fsp from 'fs/promises' import { build } from 'vite' import { afterAll, expect, test } from 'vitest' import tar from 'tar-stream' -import { readAll } from '../src/utils' +import { readAll } from '../src/shared' import type { ViteCompressionPluginConfig, ViteTarballPluginOptions } from '../src' import { compression, tarball } from '../src' diff --git a/src/compress.ts b/src/compress.ts index fa82034..7037cdb 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -5,7 +5,7 @@ import path from 'path' import type { BrotliOptions, InputType, ZlibOptions } from 'zlib' import { Pack } from './tar' import type { Algorithm, AlgorithmFunction, UserCompressionOptions } from './interface' -import { slash, stringToBytes } from './utils' +import { slash, stringToBytes } from './shared' export function ensureAlgorithm(userAlgorithm: Algorithm) { const algorithm = userAlgorithm in zlib ? userAlgorithm : 'gzip' diff --git a/src/index.ts b/src/index.ts index a9a412d..52ddd06 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import os from 'os' import path from 'path' import { createFilter } from '@rollup/pluginutils' import type { Plugin, ResolvedConfig } from 'vite' -import { len, readAll, replaceFileName, slash, stringToBytes } from './utils' +import { len, readAll, replaceFileName, slash, stringToBytes } from './shared' import { compress, createTarBall, defaultCompressionOptions, ensureAlgorithm } from './compress' import { createConcurrentQueue } from './task' import type { diff --git a/src/utils.ts b/src/shared.ts similarity index 89% rename from src/utils.ts rename to src/shared.ts index 2f04940..418414d 100644 --- a/src/utils.ts +++ b/src/shared.ts @@ -1,5 +1,6 @@ import fsp from 'fs/promises' import path from 'path' +import { u8 } from './tar' export function len>(source: T) { return source.length @@ -40,14 +41,6 @@ export async function readAll(entry: string) { return result } -let _encoder: TextEncoder - export function stringToBytes(b: string | Uint8Array) { - if (typeof b === 'string') { - if (!_encoder) { - _encoder = new TextEncoder() - } - return _encoder.encode(b) - } - return b + return typeof b === 'string' ? u8.encode(b) : b } diff --git a/src/tar.ts b/src/tar.ts index 2a7585b..2b68006 100644 --- a/src/tar.ts +++ b/src/tar.ts @@ -3,7 +3,7 @@ // No gnu or old gnu or v7 and oters etc. // POSIX.1-1988 // https://www.gnu.org/software/tar/manual/tar.html#Blocking-Factor -import { len } from './utils' +import { len } from './shared' export interface Header { name: Uint8Array[100] diff --git a/src/task.ts b/src/task.ts index 4cad109..75b50ef 100644 --- a/src/task.ts +++ b/src/task.ts @@ -1,4 +1,4 @@ -import { len } from './utils' +import { len } from './shared' class Queue { maxConcurrent: number