Skip to content

Commit

Permalink
refactor: migrate helpers.js/s3-endpoints.js/signing.js to TypeScript (
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 authored May 12, 2023
1 parent 3ec0a68 commit 52e214e
Show file tree
Hide file tree
Showing 26 changed files with 1,627 additions and 1,520 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ module.exports = {
'import/no-commonjs': 'error',
},
},
{
files: ['./src/**/*.ts'],
rules: {
'prefer-const': 'error',
},
},
{
files: ['./tests/**/*'],
rules: {
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/nodejs-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
name: Test on node ${{ matrix.node_version }} and ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 3
max-parallel: 2
matrix:
node_version: [12.x, 14.x, 16.x, 17.x, 18.x, 20.x]
node_version: [12.x, 14.x, 16.x, 18.x, 20.x]
os: [windows-latest]
steps:
- uses: actions/checkout@v3
Expand All @@ -28,6 +28,7 @@ jobs:
- run: npm i

- name: Start MinIO Server -> Run Unit and Functional Tests
timeout-minutes: 10
env:
CI: true
MINIO_CI_CD: true
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
name: Test on node ${{ matrix.node_version }} and ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 3
max-parallel: 2
matrix:
node_version: [12.x, 14.x, 16.x, 17.x, 18.x, 20.x]
node_version: [12.x, 14.x, 16.x, 18.x, 20.x]
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v3
Expand All @@ -28,6 +28,7 @@ jobs:
- run: npm i

- name: Start Server -> Run Unit and Functional Tests
timeout-minutes: 10
env:
CI: true
MINIO_CI_CD: true
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ node_modules
yarn.lock
.yarn/
.yarnrc.yml
pnpm-lock.yaml
39 changes: 17 additions & 22 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable no-console */
import { execSync } from 'node:child_process'
import { exec } from 'node:child_process'
import * as fs from 'node:fs'
import * as fsp from 'node:fs/promises'
import * as path from 'node:path'
import { promisify } from 'node:util'

import * as babel from '@babel/core'
import * as fsWalk from '@nodelib/fs.walk'
Expand Down Expand Up @@ -48,40 +49,40 @@ function options(module) {
constantReexports: true,
},
plugins: module === 'esm' ? plugins.splice(1) : plugins,
presets: [['@babel/env', { targets: { node: '8' }, modules: false }], ['@babel/preset-typescript']],
presets: [['@babel/env', { targets: { node: '14' }, modules: false }], ['@babel/preset-typescript']],
}
}

const extMap = { cjs: '.js', esm: '.mjs' }

async function buildFiles({ files, module, outDir }) {
console.log(`building for ${module}`)
execSync(`npx tsc --outDir ${outDir}`, { stdio: 'inherit' })
await promisify(exec)(`npx tsc --outDir ${outDir}`, { stdio: 'inherit' })

const opt = options(module)
for (const file of files) {
if (!file.dirent.isFile()) {
continue
}

if (file.path.endsWith('.d.ts')) {
continue
}

const outFilePath = path.join(outDir, path.relative('src/', file.path))
const outDirPath = path.dirname(outFilePath)

await fsp.mkdir(outDirPath, { recursive: true })
const distCodePath = outFilePath.replace(/\.[tj]s$/g, extMap[module])

if (file.path.endsWith('.d.ts')) {
await fsp.copyFile(file.path, outFilePath)
continue
}

try {
const result = await babel.transformAsync(fs.readFileSync(file.path).toString(), {
const result = await babel.transformAsync(await fsp.readFile(file.path, 'utf-8'), {
filename: file.path,
...opt,
})

const distCodePath = outFilePath.replace(/\.[tj]s$/g, extMap[module])

fs.writeFileSync(distCodePath, result.code)
await fsp.writeFile(distCodePath, result.code)
} catch (e) {
console.error(`failed to transpile ${file.path}`)
throw e
Expand All @@ -93,17 +94,11 @@ async function main() {
await fsp.rm('dist', { recursive: true, force: true })

const entries = fsWalk.walkSync('src/')
await buildFiles({
files: entries,
module: 'cjs',
outDir: './dist/main/',
})

await buildFiles({
files: entries,
module: 'esm',
outDir: './dist/esm/',
})

await Promise.all([
buildFiles({ files: entries, module: 'cjs', outDir: './dist/main/' }),
buildFiles({ files: entries, module: 'esm', outDir: './dist/esm/' }),
])

for (const file of fsWalk.walkSync('dist/esm/')) {
if (file.dirent.isDirectory()) {
Expand Down
Loading

0 comments on commit 52e214e

Please sign in to comment.