Skip to content

Commit

Permalink
fix(inception): initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
djMax committed Oct 19, 2023
0 parents commit 5675b53
Show file tree
Hide file tree
Showing 12 changed files with 7,260 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Node.js Package

on:
push:
branches:
- main

permissions: read-all

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
cache: yarn
- run: yarn install --immutable
- run: yarn build
- run: yarn lint
- run: yarn test


publish-npm:
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
pull-requests: write
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.MRSNUFFLES_SEMANTIC_RELEASES }}
- uses: actions/setup-node@v3
with:
node-version: 20
cache: yarn
- run: yarn install --immutable
- run: yarn build

- name: Release
env:
GITHUB_TOKEN: ${{ secrets.MRSNUFFLES_SEMANTIC_RELEASES }}
NODE_AUTH_TOKEN: ${{ secrets.SESAMECARE_OSS_NPM_TOKEN }}
run: |
yarn dlx semantic-release
24 changes: 24 additions & 0 deletions .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Node CI

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Use Node.js 18
uses: actions/setup-node@v3
with:
node-version: 20
cache: yarn
- name: npm install, lint, build, and test
run: |
yarn install --immutable
yarn lint
yarn build
yarn test
env:
CI: true
67 changes: 67 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Custom
src/generated
.transpiled
.nyc_output
.eslintcache

# TypeScript incremental compilation cache
*.tsbuildinfo

# Stock
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.orig

work
/build
pids
logs
results
coverage
lib-cov
html-report
xunit.xml
node_modules
npm-debug.log

.project
.idea
.settings
.iml
*.sublime-workspace
*.sublime-project

.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
.AppleDouble
.LSOverride
.Spotlight-V100
.Trashes

.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

.node_repl_history

# TypeScript incremental compilation cache
*.tsbuildinfo
# Added by coconfig
.eslintignore
.npmignore
tsconfig.json
tsconfig.build.json
.prettierrc.js
.eslintrc.js
.commitlintrc.json
vitest.config.ts
541 changes: 541 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

874 changes: 874 additions & 0 deletions .yarn/releases/yarn-3.6.4.cjs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"

yarnPath: .yarn/releases/yarn-3.6.4.cjs
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# typia-standalone-validator

This package creates a bundle standalone validator for a Typescript type using Typia. It basically just runs npx a couple times to do the dance and generate a single js file that will validate a given Javascript object against a Typescript type at runtime.

```
npx @sesamecare-oss/typia-standalone-validator <source file with TS type decl> <type> --project <your tsconfig.json> --o <output file>
```
74 changes: 74 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"name": "@sesamecare-oss/typia-standalone-validator",
"version": "1.0.0",
"description": "Build a standalone validator using Typia and tsup",
"main": "build/index.js",
"types": "build/index.d.ts",
"author": "Developers <[email protected]>",
"license": "UNLICENSED",
"packageManager": "[email protected]",
"scripts": {
"build": "tsc -p tsconfig.build.json && yarn dlx glob-chmod 755 build/bin/*",
"clean": "yarn dlx rimraf ./dist",
"lint": "eslint .",
"postinstall": "coconfig",
"test": "vitest"
},
"bin": "build/bin/cli.js",
"keywords": [
"typescript",
"sesame"
],
"repository": {
"type": "git",
"url": "git+https://github.com/sesamecare/typia-standalone-validator.git"
},
"publishConfig": {
"access": "public"
},
"release": {
"branches": [
"main"
],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
[
"@semantic-release/exec",
{
"publishCmd": "yarn dlx pinst --disable"
}
],
"@semantic-release/npm",
"@semantic-release/git"
]
},
"config": {
"coconfig": "@openapi-typescript-infra/coconfig"
},
"devDependencies": {
"@openapi-typescript-infra/coconfig": "^4.2.2",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/exec": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@types/minimist": "^1.2.4",
"@types/node": "^20.8.7",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0",
"coconfig": "^1.0.0",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"typescript": "^5.2.2",
"vitest": "^0.34.6"
},
"dependencies": {
"minimist": "^1.2.8",
"tsup": "^7.2.0"
},
"peerDependencies": {
"typia": "*"
}
}
24 changes: 24 additions & 0 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import minimist from 'minimist';

import { generateValidator } from '..';

const argv = minimist(process.argv.slice(2), {
alias: {
out: ['o', 'output'],
},
});

async function run() {
await generateValidator({
...argv,
source: argv._[0],
tsType: argv._[1],
project: argv.project,
outputFile: argv.out,
});
}

run().catch((error) => {
console.error(error);
process.exit(-1);
});
9 changes: 9 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, test } from 'vitest';

import { generateValidator } from './index';

describe('Module exports', () => {
test('should export expected elements', () => {
expect(typeof generateValidator).toBe('function');
});
});
103 changes: 103 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import fs from 'fs';
import path from 'path';
import { spawn } from 'child_process';

async function runCommand(command: string, args: string[] = []) {
return new Promise((resolve, reject) => {
const child = spawn(command, args);

let stdout = '';
let stderr = '';

child.stdout.on('data', (data) => {
stdout += data.toString();
});

child.stderr.on('data', (data) => {
stderr += data.toString();
});

child.on('close', (code) => {
if (code !== 0) {
reject(new Error(`Command failed with exit code ${code}: ${stderr}`));
} else {
resolve(stdout);
}
});
});
}

async function prepend(file: string, content: string) {
const currentContent = await fs.promises.readFile(file, 'utf8');
const newContent = `${content}${currentContent}`;
await fs.promises.writeFile(file, newContent, 'utf8');
}

export async function generateValidator({
source,
tsType,
project,
outputFile,
}: {
project: string;
outputFile: string;
source: string;
tsType: string;
}) {
const template = `import typia from 'typia';
import { ${tsType} } from '../${path.relative(process.cwd(), source)}';
export const validate${tsType} = typia.createValidate<${tsType}>();
`;

try {
// Typia only runs on dirs, so make one.
if (!fs.existsSync('__typia__')) {
await fs.promises.mkdir('__typia__');
}
await fs.promises.writeFile('__typia__/validator.ts', template);

await fs.promises.writeFile(
'__typia__/tsconfig.json',
`{
"extends": "${path.relative(path.join(process.cwd(), '__typia__'), project)}",
"strict": true,
"strictNullChecks":true,
"compilerOptions": {
"plugins": [{
"transform": "typia/lib/transform"
}]
}
}`,
);

await runCommand('npx', [
'typia',
'generate',
'--input',
'__typia__',
'--output',
'__typia_out__',
'--project',
'__typia__/tsconfig.json',
]);

await runCommand('npx', [
'tsup',
'__typia_out__/validator.ts',
'--target',
'node18',
'-d',
path.dirname(outputFile),
'--format',
'esm',
]);

const tmpOutput = path.join(path.dirname(outputFile), 'validator.mjs');
await prepend(tmpOutput, '// @ts-nocheck\n');
await fs.promises.rename(tmpOutput, outputFile);
} finally {
fs.rmSync('__typia__', { recursive: true });
fs.rmSync('__typia_out__', { recursive: true });
}
}
Loading

0 comments on commit 5675b53

Please sign in to comment.