Skip to content

Commit

Permalink
Revision 0.32.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Dec 2, 2023
1 parent ecc71e8 commit 8f65f40
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 43 deletions.
36 changes: 16 additions & 20 deletions hammer.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { compression, measurement } from './task/benchmark'
import * as Benchmark from './task/benchmark'
import * as Build from './task/build'
import * as Fs from 'fs'

// -------------------------------------------------------------------------------
Expand All @@ -18,20 +19,14 @@ export async function format() {
// Start
// -------------------------------------------------------------------------------
export async function start() {
await shell(`hammer run example/index.ts --dist target/example`)
await shell(`hammer run example/index.mts --dist target/example`)
}
// -------------------------------------------------------------------------------
// Benchmark
// -------------------------------------------------------------------------------
export async function benchmark_compression() {
await compression()
}
export async function benchmark_measurement() {
await measurement()
}
export async function benchmark() {
await benchmark_compression()
await benchmark_measurement()
await Benchmark.compression()
await Benchmark.measurement()
}
// -------------------------------------------------------------------------------
// Test
Expand All @@ -58,25 +53,26 @@ export async function test(filter = '') {
// Build
// -------------------------------------------------------------------------------
export async function build(target = 'target/build') {
await folder(target).delete()
await test()
await shell(`tsc -p ./src/tsconfig.json --outDir ${target} --target ES2020 --module CommonJS --declaration --removeComments`)
await folder(target).add('package.json')
await clean()
await Promise.all([
Build.buildDefault(target),
Build.buildImport(target),
Build.buildTypes(target),
Build.buildRedirect(target),
Build.buildPackageJson(target)
])
await folder(target).add('readme.md')
await folder(target).add('license')
await shell(`cd ${target} && npm pack`)
}
// -------------------------------------------------------------------------------
// Install
// -------------------------------------------------------------------------------
export async function install_local(target = 'target/typebox') {
export async function install() {
await clean()
await build(target)
await folder('node_modules').add(target)
const packagePath = `${target}/package.json`
const packageJson = JSON.parse(Fs.readFileSync(packagePath, 'utf-8'))
packageJson.name = 'typebox'
Fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2))
await build('target/typebox')
await folder('node_modules').add('target/typebox')
}
// -------------------------------------------------------------
// Publish
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 4 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sinclair/typebox",
"version": "0.32.0-dev-4",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"version": "0.32.0-dev-5",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
"json-schema",
Expand All @@ -10,28 +10,16 @@
],
"author": "sinclairzx81",
"license": "MIT",
"main": "./index.js",
"types": "./index.d.ts",
"exports": {
"./compiler": "./compiler/index.js",
"./errors": "./errors/index.js",
"./system": "./system/index.js",
"./type": "./type/index.js",
"./value": "./value/index.js",
".": "./index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/sinclairzx81/typebox"
},
"scripts": {
"benchmark:compression": "hammer task benchmark_compression",
"benchmark:measurement": "hammer task benchmark_measurement",
"benchmark": "hammer task benchmark",
"install:local": "hammer task install_local",
"test:typescript": "hammer task test_typescript",
"test:static": "hammer task test_static",
"test:runtime": "hammer task test_runtime",
"benchmark": "hammer task benchmark",
"install": "hammer task install",
"build": "hammer task build",
"test": "hammer task test",
"clean": "hammer task clean",
Expand Down
5 changes: 5 additions & 0 deletions task/build/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare function shell(command: string): Promise<void>

export async function buildDefault(target: string) {
await shell(`tsc -p ./src/tsconfig.json --outDir ${target}/build/default --target ES2020 --module CommonJS --removeComments`)
}
15 changes: 11 additions & 4 deletions task/build/esm.ts → task/build/import.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// ------------------------------------------------------------------
// Node-ESM-Transform
// ------------------------------------------------------------------
declare function shell(command: string): Promise<void>
import * as Path from 'node:path'
import * as Fs from 'node:fs'

// ------------------------------------------------------------------
// Build
// ------------------------------------------------------------------
export async function buildImport(target: string) {
await shell(`tsc -p ./src/tsconfig.json --outDir ${target}/build/import --target ESNext --module ESNext --removeComments`)
convertToEsm(`${target}/build/import`)
}
// ------------------------------------------------------------------
// Node-ESM-Transform
// ------------------------------------------------------------------
// prettier-ignore
function shouldProcess(sourcePath: string) {
const extname = Path.extname(sourcePath)
Expand Down Expand Up @@ -50,7 +58,6 @@ function readDirectory(sourceDirectory: string) {
processSourcePath(sourcePath)
}
}
/** Transforms modules within sourceDirectory to Node ESM */
// prettier-ignore
export function convertToEsm(sourceDirectory: string) {
readDirectory(sourceDirectory)
Expand Down
6 changes: 5 additions & 1 deletion task/build/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export * from './esm'
export * from './default'
export * from './import'
export * from './package'
export * from './redirect'
export * from './types'
56 changes: 56 additions & 0 deletions task/build/package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { submodules } from './submodules'
import * as Fs from 'node:fs'

// prettier-ignore
export function buildPackageJson(target: string) {
const content = JSON.stringify(resolvePackageJson(), null, 2)
Fs.writeFileSync(`${target}/package.json`, content, 'utf-8')
}
// prettier-ignore
function resolveSubmoduleExports(submodule: string) {
return {
types: `./build/types/${submodule}/index.d.ts`,
default: `./build/default/${submodule}/index.js`,
import: `./build/import/${submodule}/index.mjs`
}
}
// prettier-ignore
function resolveIndexExport() {
return {
"types": "./build/types/index.d.ts",
"default": "./build/default/index.js",
"import": "./build/import/index.mjs"
}
}
// prettier-ignore
function resolveExports() {
const exports = submodules.reduce((acc, submodule) => {
return { ...acc, [`./${submodule}`]: resolveSubmoduleExports(submodule) }
}, {
".": resolveIndexExport()
})
return { exports }
}
// prettier-ignore
function resolveMetadata() {
const packageJson = JSON.parse(Fs.readFileSync('package.json', 'utf-8'))
return {
name: packageJson.name,
version: packageJson.version,
description: packageJson.description,
keywords: packageJson.keywords,
author: packageJson.author,
license: packageJson.license,
repository: packageJson.repository,
types: "./build/types/index.d.ts",
module: "./build/import/index.mjs",
main: "./build/default/index.js"
}
}
// prettier-ignore
function resolvePackageJson() {
return {
...resolveMetadata(),
...resolveExports()
}
}
20 changes: 20 additions & 0 deletions task/build/redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { submodules } from './submodules'
import * as Fs from 'node:fs'

function writeRedirect(target: string, submodule: string) {
Fs.mkdirSync(`${target}/${submodule}`, { recursive: true })
Fs.writeFileSync(
`${target}/${submodule}/package.json`,
JSON.stringify(
{
main: `../build/default/${submodule}/index.js`,
types: `../build/types/${submodule}/index.d.ts`,
},
null,
2,
),
)
}
export function buildRedirect(target: string) {
submodules.forEach((submodule) => writeRedirect(target, submodule))
}
1 change: 1 addition & 0 deletions task/build/submodules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const submodules = ['compiler', 'errors', 'system', 'type', 'value']
5 changes: 5 additions & 0 deletions task/build/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare function shell(command: string): Promise<void>

export async function buildTypes(target: string) {
await shell(`tsc -p ./src/tsconfig.json --outDir ${target}/build/types --target ES2020 --module CommonJS --removeComments --declaration --emitDeclarationOnly`)
}

0 comments on commit 8f65f40

Please sign in to comment.