Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
add JSON Schema tool scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Sep 11, 2024
1 parent 129e3b3 commit 7232a9e
Show file tree
Hide file tree
Showing 22 changed files with 490 additions and 69 deletions.
76 changes: 17 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Interweb Build
# Interweb Build Tools

<p align="center" width="100%">
<img height="90" src="https://user-images.githubusercontent.com/545047/190171432-5526db8f-9952-45ce-a745-bea4302f912b.svg" />
Expand All @@ -10,82 +10,40 @@
</a>
<br />
<a href="https://github.com/cosmology-tech/interweb-build/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
<a href="https://www.npmjs.com/package/@interweb/build"><img height="20" src="https://img.shields.io/github/package-json/v/cosmology-tech/interweb-build?filename=packages%2Fbuild%2Fpackage.json"></a>
</p>

Interweb Build is a powerful wrapper around esbuild, designed to simplify and streamline your build process for Interweb projects.
Interweb is a collection of powerful tools designed to simplify and streamline your development process for web and TypeScript projects.

## Packages

## Features
This monorepo contains the following packages:

- Simple API for building TypeScript projects
- Customizable build options
- Built-in support for common Interweb project configurations
- Easy integration with existing projects
### [@interweb/build](./packages/build)

[![npm version](https://img.shields.io/npm/v/@interweb/build.svg)](https://www.npmjs.com/package/@interweb/build)

## Installation
Interweb Build is a powerful wrapper around esbuild, designed to simplify and streamline your build process for Interweb projects.

#### Installation
```sh
npm install @interweb/build
```

## Usage
[Read more about @interweb/build](./packages/build)

Here's a basic example of how to use Interweb Build:
### [@interweb/ts-json-schema](./packages/ts-json-schema)

```js
import { InterwebBuild, defaultOptions } from '@interweb/build';
[![npm version](https://img.shields.io/npm/v/@interweb/ts-json-schema.svg)](https://www.npmjs.com/package/@interweb/ts-json-schema)

// Use default options
InterwebBuild.build();
Interweb TS JSON Schema is a powerful wrapper around ts-json-schema-generator, designed to simplify the process of generating JSON schemas from TypeScript files in Interweb projects.

// Customize options
InterwebBuild.build({
entryPoints: ['src/custom-entry.ts'],
outfile: 'dist/custom-bundle.js',
minify: true,
});

// Use default options as a base for a custom configuration
const myConfig = {
...defaultOptions,
minify: true,
target: 'es2018',
};
InterwebBuild.build(myConfig);
#### Installation
```sh
npm install @interweb/ts-json-schema
```

## API Reference

### `InterwebBuild.build(options)`

Builds your project using the provided options.

- `options` (optional): An object containing build options. If not provided, default options will be used.

Returns a Promise that resolves to the build result.

### `defaultOptions`

An object containing the default build options. You can spread these into your own configuration for easy customization.

## Default Configuration

Interweb Build comes with the following default configuration:

```ts
{
bundle: true,
minify: false,
outfile: 'dist/bundle.js',
platform: 'node',
sourcemap: true,
target: 'ESNext',
logLevel: 'info',
}
```
[Read more about @interweb/ts-json-schema](./packages/ts-json-schema)

## License

Interweb Build is MIT licensed.
Interweb Build is [MIT licensed](./LICENSE).
12 changes: 12 additions & 0 deletions __fixtures__/schema-test/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface User {
id: number;
name: string;
email: string;
}

export interface Post {
id: number;
title: string;
content: string;
author: User;
}
11 changes: 11 additions & 0 deletions __fixtures__/schema-test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"]
}

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

49 changes: 49 additions & 0 deletions __output__/schema-test/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"email": {
"type": "string"
}
},
"required": [
"id",
"name",
"email"
],
"additionalProperties": false
},
"Post": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"title": {
"type": "string"
},
"content": {
"type": "string"
},
"author": {
"$ref": "#/definitions/User"
}
},
"required": [
"id",
"title",
"content",
"author"
],
"additionalProperties": false
}
}
}
26 changes: 26 additions & 0 deletions __output__/schema-test/specific-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/User",
"definitions": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"email": {
"type": "string"
}
},
"required": [
"id",
"name",
"email"
],
"additionalProperties": false
}
}
}
4 changes: 2 additions & 2 deletions packages/build/__tests__/first.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { join, resolve } from 'path';

import { InterwebBuild, InterwebBuildOptions } from '../src/index';

const fixtureDir = resolve(join(__dirname, '/../__fixtures__/', 'first'));
const outputDir = resolve(join(__dirname, '/../__output__/', 'first'));
const fixtureDir = resolve(join(__dirname, '/../../../__fixtures__/', 'first'));
const outputDir = resolve(join(__dirname, '/../../../__output__/', 'first'));

describe('InterwebBuild', () => {
it('builds the fixture project successfully', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/build/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ module.exports = {
transformIgnorePatterns: [`/node_modules/*`],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
modulePathIgnorePatterns: ['dist/*', '__output__/*']
modulePathIgnorePatterns: ['dist/*']
};
2 changes: 1 addition & 1 deletion packages/build/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"rootDir": "src/"
},
"include": ["src/**/*.ts"],
"exclude": ["dist", "node_modules", "**/*.spec.*", "**/*.test.*", "__output__"]
"exclude": ["dist", "node_modules", "**/*.spec.*", "**/*.test.*"]
}
111 changes: 111 additions & 0 deletions packages/ts-json-schema/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# @interweb/ts-json-schema

<p align="center" width="100%">
<img height="90" src="https://user-images.githubusercontent.com/545047/190171432-5526db8f-9952-45ce-a745-bea4302f912b.svg" />
</p>

<p align="center" width="100%">
<a href="https://github.com/cosmology-tech/interweb-build/actions/workflows/run-tests.yml">
<img height="20" src="https://github.com/cosmology-tech/interweb-build/actions/workflows/run-tests.yml/badge.svg" />
</a>
<br />
<a href="https://github.com/cosmology-tech/interweb-build/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
<a href="https://www.npmjs.com/package/@interweb/ts-json-schema"><img height="20" src="https://img.shields.io/github/package-json/v/cosmology-tech/interweb-build?filename=packages%2Fts-json-schema%2Fpackage.json"></a>
</p>

`@interweb/ts-json-schema` is a wrapper around ts-json-schema-generator, designed to simplify the process of generating JSON schemas from TypeScript files in Interweb projects.

## Features

- Simple API for generating JSON schemas from TypeScript
- Customizable schema generation options
- Built-in support for common Interweb project configurations

## Installation

```sh
npm install @interweb/ts-json-schema
```

## Usage

Here's a basic example of how to use Interweb TS JSON Schema:

```ts
import { generateSchema } from '@interweb/ts-json-schema';

// Generate schema for all types in a file
await generateSchema({
sourcePath: 'path/to/source/file.ts',
tsconfigPath: 'path/to/tsconfig.json',
outputPath: 'path/to/output/schema.json'
});

// Generate schema for a specific type
await generateSchema({
sourcePath: 'path/to/source/file.ts',
tsconfigPath: 'path/to/tsconfig.json',
outputPath: 'path/to/output/schema.json',
type: 'User'
});
```

## API Reference

### `generateSchema(options)`

Generates a JSON schema from TypeScript files using the provided options.

- `options`: An object containing the following properties:
- `sourcePath` (required): Path to the source TypeScript file.
- `tsconfigPath` (required): Path to the tsconfig.json file.
- `outputPath` (required): Path where the generated schema will be saved.
- `type` (optional): Specific type to generate schema for. If not provided, generates schema for all types.

Returns a Promise that resolves when the schema has been generated and saved.

## Configuration

Interweb TS JSON Schema uses the following configuration by default:

```ts
{
path: sourcePath,
tsconfig: tsconfigPath,
type: '*', // Or the specified type if provided
}
```

These options are passed to the underlying `ts-json-schema-generator`. You can customize these by modifying the `generateSchema` function or by creating your own wrapper around it.

## Example

Here's an example of how you might use Interweb TS JSON Schema in a project:

```ts
import { generateSchema } from '@interweb/ts-json-schema';
import { join } from 'path';

const sourcePath = join(__dirname, 'src/types.ts');
const tsconfigPath = join(__dirname, 'tsconfig.json');
const outputPath = join(__dirname, 'schema.json');

async function generateProjectSchema() {
try {
await generateSchema({
sourcePath,
tsconfigPath,
outputPath
});
console.log('Schema generated successfully!');
} catch (error) {
console.error('Error generating schema:', error);
}
}

generateProjectSchema();
```

## License

Interweb TS JSON Schema is MIT licensed.
Loading

0 comments on commit 7232a9e

Please sign in to comment.