project initialized
This commit is contained in:
commit
0762bde89e
22
.editorconfig
Normal file
22
.editorconfig
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
# top-most EditorConfig file
|
||||||
|
root = true
|
||||||
|
|
||||||
|
# all files
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
max_line_length = 80
|
||||||
|
|
||||||
|
[*.{js,ts}]
|
||||||
|
quote_type = single
|
||||||
|
curly_bracket_next_line = false
|
||||||
|
spaces_around_brackets = inside
|
||||||
|
indent_brace_style = BSD KNF
|
||||||
|
|
||||||
|
# HTML
|
||||||
|
[*.html]
|
||||||
|
quote_type = double
|
23
.gitignore
vendored
Normal file
23
.gitignore
vendored
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
*.log
|
||||||
|
.idea
|
||||||
|
.DS_Store
|
||||||
|
.cache
|
||||||
|
node_modules
|
||||||
|
|
||||||
|
coverage
|
||||||
|
lib
|
||||||
|
esm5
|
||||||
|
lib-esm
|
||||||
|
esm2015
|
||||||
|
lib-fesm
|
||||||
|
fesm
|
||||||
|
umd
|
||||||
|
bundles
|
||||||
|
typings
|
||||||
|
types
|
||||||
|
docs
|
||||||
|
dist
|
||||||
|
|
||||||
|
## this is generated by `npm pack`
|
||||||
|
*.tgz
|
||||||
|
package
|
32
.npmignore
Normal file
32
.npmignore
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
.*
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# tools configs
|
||||||
|
**/tsconfig.json
|
||||||
|
tsconfig.*.json
|
||||||
|
tslint.json
|
||||||
|
**/webpack.config.js
|
||||||
|
**/jest.config.js
|
||||||
|
**/prettier.config.js
|
||||||
|
|
||||||
|
# build scripts
|
||||||
|
config/
|
||||||
|
scripts/
|
||||||
|
|
||||||
|
# Test files
|
||||||
|
**/*.spec.js
|
||||||
|
**/*.test.js
|
||||||
|
**/*.test.d.ts
|
||||||
|
**/*.spec.d.ts
|
||||||
|
__tests__
|
||||||
|
coverage
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
node_modules
|
||||||
|
src
|
||||||
|
docs
|
||||||
|
examples
|
||||||
|
|
||||||
|
## this is generated by `npm pack`
|
||||||
|
*.tgz
|
||||||
|
package
|
7
.prettierignore
Normal file
7
.prettierignore
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
bundles
|
||||||
|
esm5
|
||||||
|
esm2015
|
||||||
|
fesm
|
||||||
|
types
|
||||||
|
typings
|
||||||
|
dist
|
9
.travis.yml
Normal file
9
.travis.yml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
language: node_js
|
||||||
|
node_js: '8'
|
||||||
|
cache: yarn
|
||||||
|
notifications:
|
||||||
|
email: false
|
||||||
|
install:
|
||||||
|
- yarn
|
||||||
|
script:
|
||||||
|
- yarn build
|
11
.vscode/extensions.json
vendored
Normal file
11
.vscode/extensions.json
vendored
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||||
|
// for the documentation about the extensions.json format
|
||||||
|
"recommendations": [
|
||||||
|
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
|
||||||
|
"eg2.tslint",
|
||||||
|
"esbenp.prettier-vscode",
|
||||||
|
"codezombiech.gitignore",
|
||||||
|
"EditorConfig.EditorConfig"
|
||||||
|
]
|
||||||
|
}
|
13
.vscode/settings.json
vendored
Normal file
13
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
// Place your settings in this file to overwrite default and user settings.
|
||||||
|
{
|
||||||
|
"tslint.autoFixOnSave": true,
|
||||||
|
"tslint.enable": true,
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"typescript.format.enable": false,
|
||||||
|
"javascript.format.enable": false,
|
||||||
|
"typescript.referencesCodeLens.enabled": true,
|
||||||
|
"javascript.referencesCodeLens.enabled": true,
|
||||||
|
"editor.rulers": [
|
||||||
|
80,100
|
||||||
|
],
|
||||||
|
}
|
0
CHANGELOG.md
Normal file
0
CHANGELOG.md
Normal file
21
LICENSE.md
Normal file
21
LICENSE.md
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2018 Martin Hochel
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
236
README.md
Normal file
236
README.md
Normal file
|
@ -0,0 +1,236 @@
|
||||||
|
# Typescript lib starter
|
||||||
|
|
||||||
|
[![Greenkeeper badge](https://badges.greenkeeper.io/Hotell/typescript-lib-starter.svg)](https://greenkeeper.io/)
|
||||||
|
|
||||||
|
[![Build Status](https://travis-ci.org/Hotell/typescript-lib-starter.svg?branch=master)](https://travis-ci.org/Hotell/typescript-lib-starter)
|
||||||
|
[![NPM version](https://img.shields.io/npm/v/%40martin_hotell%2Ftypescript-lib-starter.svg)](https://www.npmjs.com/package/@martin_hotell/typescript-lib-starter)
|
||||||
|
![Downloads](https://img.shields.io/npm/dm/@martin_hotell/typescript-lib-starter.svg)
|
||||||
|
[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
|
||||||
|
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
|
||||||
|
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
|
||||||
|
|
||||||
|
This npm library starter:
|
||||||
|
|
||||||
|
- creates package for both Node and Browser
|
||||||
|
- build will creates 4 standard "package" formats:
|
||||||
|
- `umd` 👉 UMD bundle for Node and Browser
|
||||||
|
> `main` field in package.json
|
||||||
|
- `esm5` 👉 transpiled files to ES5 + es2015 modules for tree shaking
|
||||||
|
> `module` field in package.json
|
||||||
|
- `esm2015` 👉 raw javascript files transpiled from typescript to latest ES standard ( es2018 )
|
||||||
|
> `es2015` field in package.json
|
||||||
|
>
|
||||||
|
> this is useful if you wanna transpile everything or just wanna ship untranspiled esNext code for evergreen browsers)
|
||||||
|
- `fesm` 👉 experimental bundle type introduced by Angular team (TL;DR: it's an es2015 flattened bundle, like UMD but with latest ECMAscript and JS modules)
|
||||||
|
- type definitions are automatically generated and shipped with your package
|
||||||
|
- > `types` field in package.json
|
||||||
|
- `sideEffects` 👉 [support proper tree-shaking](https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free) for whole library ( Webpack >= 4). Turn this off or adjust as needed if your modules are not pure!
|
||||||
|
|
||||||
|
## Start coding in 4 steps !
|
||||||
|
|
||||||
|
1. `git clone https://github.com/Hotell/typescript-lib-starter <your-libary-folder-name> && cd $_`
|
||||||
|
|
||||||
|
2. `rm -rf .git && git init`
|
||||||
|
|
||||||
|
3. in `package.json` reset following fields:
|
||||||
|
|
||||||
|
```diff
|
||||||
|
{
|
||||||
|
- "name": "@next-gen/typescript-lib-starter",
|
||||||
|
+ "name": "{yourLibraryPackageName}",
|
||||||
|
- "version": "1.7.0",
|
||||||
|
+ "version": "1.0.0",
|
||||||
|
- "description": "TypeScript library setup for multiple compilation targets using tsc and webpack",
|
||||||
|
+ "description": "What is your library all about...",
|
||||||
|
- "author": "Martin Hochel",
|
||||||
|
+ "author": "{yourName}",
|
||||||
|
- "license": "MIT",
|
||||||
|
+ "license": "{yourLicense}",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
- "url": "https://www.github.com/Hotell/typescript-lib-starter"
|
||||||
|
+ "url": "https://www.github.com/{yourAccountName}/{yourLibraryPackageName}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Install all dependencies `yarn install`
|
||||||
|
|
||||||
|
Happy coding ! 🖖
|
||||||
|
|
||||||
|
## Consumption of published library:
|
||||||
|
|
||||||
|
1. install it 🤖
|
||||||
|
|
||||||
|
```sh
|
||||||
|
yarn add my-new-library
|
||||||
|
# OR
|
||||||
|
npm install my-new-library
|
||||||
|
```
|
||||||
|
|
||||||
|
1. use it 💪
|
||||||
|
|
||||||
|
### Webpack
|
||||||
|
|
||||||
|
> #### NOTE:
|
||||||
|
>
|
||||||
|
> Don't forget to turn off ES modules transpilation to enable tree-shaking!
|
||||||
|
>
|
||||||
|
> - babel: `{"modules": false}`
|
||||||
|
> - typescript: `{"module": "esnext"}`
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// main.ts or main.js
|
||||||
|
import { Greeter } from 'my-new-library'
|
||||||
|
|
||||||
|
const mountPoint = document.getElementById('app')
|
||||||
|
const App = () => {
|
||||||
|
const greeter = new Greeter('Stranger')
|
||||||
|
return `<h1>${greeter.greet()}</h1>`
|
||||||
|
}
|
||||||
|
const render = (Root: Function, where: HTMLElement) => {
|
||||||
|
where.innerHTML = Root()
|
||||||
|
}
|
||||||
|
|
||||||
|
render(App, mountPoint)
|
||||||
|
```
|
||||||
|
|
||||||
|
```html
|
||||||
|
<!-- index.htm -->
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script src="bundle.js" async></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
### UMD/ES2015 module aware browsers ( no bundler )
|
||||||
|
|
||||||
|
```html
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script type="module">
|
||||||
|
import {Greeter} from './node_modules/my-lib/esm2015/index.js'
|
||||||
|
|
||||||
|
const App = () => {
|
||||||
|
const greeter = new Greeter('Stranger');
|
||||||
|
return `<h1>${greeter.greet()}</h1>`
|
||||||
|
}
|
||||||
|
const render = (Root, where) => {
|
||||||
|
where.innerHTML = Root();
|
||||||
|
}
|
||||||
|
|
||||||
|
render(App, mountPoint);
|
||||||
|
</script>
|
||||||
|
<script nomodule src="node_modules/my-lib/bundles/my-new-library.umd.min.js"></script>
|
||||||
|
<script nomodule async>
|
||||||
|
var Greeter = MyLib.Greeter;
|
||||||
|
|
||||||
|
var App = function() {
|
||||||
|
var greeter = new Greeter('Stranger');
|
||||||
|
return '<h1>'+greeter.greet()+'</h1>'
|
||||||
|
}
|
||||||
|
var render = function(Root, where) {
|
||||||
|
where.innerHTML = Root();
|
||||||
|
}
|
||||||
|
|
||||||
|
render(App, mountPoint);
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Publish your library
|
||||||
|
|
||||||
|
> #### NOTE:
|
||||||
|
>
|
||||||
|
> you have to create npm account and register token on your machine
|
||||||
|
> 👉 `npm adduser`
|
||||||
|
>
|
||||||
|
> If you are using scope ( you definitely should 👌) don't forget to [`--scope`](https://docs.npmjs.com/cli/adduser#scope)
|
||||||
|
|
||||||
|
Execute `yarn release` which will handle following tasks:
|
||||||
|
|
||||||
|
- bump package version and git tag
|
||||||
|
- update/(create if it doesn't exist) CHANGELOG.md
|
||||||
|
- push to github master branch + push tags
|
||||||
|
- publish build packages to npm
|
||||||
|
|
||||||
|
> **NOTE:**
|
||||||
|
>
|
||||||
|
> all package files are gonna be within `/dist` folder from where `npm publish` will be executed
|
||||||
|
|
||||||
|
> releases are handled by awesome [standard-version](https://github.com/conventional-changelog/standard-version)
|
||||||
|
|
||||||
|
### Initial Release (without bumping package.json version):
|
||||||
|
|
||||||
|
`yarn release --first-release`
|
||||||
|
|
||||||
|
### Pre-release
|
||||||
|
|
||||||
|
- To get from `1.1.2` to `1.1.2-0`:
|
||||||
|
|
||||||
|
`yarn release --prerelease`
|
||||||
|
|
||||||
|
- **Alpha**: To get from `1.1.2` to `1.1.2-alpha.0`:
|
||||||
|
|
||||||
|
`yarn release --prerelease alpha`
|
||||||
|
|
||||||
|
- **Beta**: To get from `1.1.2` to `1.1.2-beta.0`:
|
||||||
|
|
||||||
|
`yarn release --prerelease beta`
|
||||||
|
|
||||||
|
### Dry run mode
|
||||||
|
|
||||||
|
See what commands would be run, without committing to git or updating files
|
||||||
|
|
||||||
|
`yarn release --dry-run`
|
||||||
|
|
||||||
|
## Check what files are gonna be published to npm
|
||||||
|
|
||||||
|
- `cd dist && yarn pack` OR `yarn release:preflight` which will create a tarball with everything that would get published to NPM
|
||||||
|
|
||||||
|
## Check size of your published NPM bundle
|
||||||
|
|
||||||
|
`yarn size`
|
||||||
|
|
||||||
|
## Format and fix lint errors
|
||||||
|
|
||||||
|
`yarn ts:style:fix`
|
||||||
|
|
||||||
|
## Generate documentation
|
||||||
|
|
||||||
|
`yarn docs`
|
||||||
|
|
||||||
|
## Commit ( via commitizen )
|
||||||
|
|
||||||
|
- this is preferred way how to create conventional-changelog valid commits
|
||||||
|
- if you prefer your custom tool we provide a commit hook linter which will error out, it you provide invalid commit message
|
||||||
|
- if you are in rush and just wanna skip commit message validation just prefix your message with `WIP: something done` ( if you do this please squash your work when you're done with proper commit message so standard-version can create Changelog and bump version of your library appropriately )
|
||||||
|
|
||||||
|
`yarn commit` - will invoke [commitizen CLI](https://github.com/commitizen/cz-cli)
|
||||||
|
|
||||||
|
### Troubleshooting
|
||||||
|
|
||||||
|
#### dynamic `import()`
|
||||||
|
|
||||||
|
This starter uses latest **TypeScript >=2.9** which has support for lazy loading chunks/modules via `import()` and also definition acquisition via [`import('../path-to-module').TypeFoo`](http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html#import-types)
|
||||||
|
|
||||||
|
Before TS 2.9, it wasn't possible to properly generate ambient definitions if you used dynamic `import()`. This works now as expected without any hacks ❤️ !
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
> ### Before TS 2.9
|
||||||
|
>
|
||||||
|
> Please note that if you wanna use that feature, compiler will complain because declaration generation is turned on, and currently TS can't handle type generation with types that will be loaded in the future ( lazily )
|
||||||
|
>
|
||||||
|
> **How to solve this:**
|
||||||
|
>
|
||||||
|
> - turn of type checking and don't generate types for that lazy import: `import('./components/button') as any`
|
||||||
|
> - or you can use this [temporary workaround](https://github.com/Microsoft/TypeScript/issues/16603#issuecomment-310208259)
|
66
config/global.d.ts
vendored
Normal file
66
config/global.d.ts
vendored
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
// ts-jest types require 'babel-core'
|
||||||
|
declare module 'babel-core' {
|
||||||
|
interface TransformOptions {}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare module 'jest-config' {
|
||||||
|
const defaults: jest.DefaultOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
declare module 'sort-object-keys' {
|
||||||
|
const sortPackageJson: <T extends {}>(
|
||||||
|
object: T,
|
||||||
|
sortWith?: (...args: any[]) => any
|
||||||
|
) => T
|
||||||
|
export = sortPackageJson
|
||||||
|
}
|
||||||
|
|
||||||
|
type RollupPluginFn<O extends object = {}> = (
|
||||||
|
options?: O
|
||||||
|
) => import('rollup').Plugin
|
||||||
|
|
||||||
|
declare module 'rollup-plugin-json' {
|
||||||
|
export interface Options {
|
||||||
|
/**
|
||||||
|
* All JSON files will be parsed by default, but you can also specifically include/exclude files
|
||||||
|
*/
|
||||||
|
include?: string | string[]
|
||||||
|
exclude?: string | string[]
|
||||||
|
/**
|
||||||
|
* for tree-shaking, properties will be declared as variables, using either `var` or `const`
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
preferConst?: boolean
|
||||||
|
/**
|
||||||
|
* specify indentation for the generated default export — defaults to '\t'
|
||||||
|
* @default '\t'
|
||||||
|
*/
|
||||||
|
indent?: string
|
||||||
|
}
|
||||||
|
const plugin: RollupPluginFn<Options>
|
||||||
|
export default plugin
|
||||||
|
}
|
||||||
|
declare module 'rollup-plugin-sourcemaps' {
|
||||||
|
const plugin: RollupPluginFn
|
||||||
|
export default plugin
|
||||||
|
}
|
||||||
|
declare module 'rollup-plugin-node-resolve' {
|
||||||
|
const plugin: RollupPluginFn
|
||||||
|
export default plugin
|
||||||
|
}
|
||||||
|
declare module 'rollup-plugin-commonjs' {
|
||||||
|
const plugin: RollupPluginFn
|
||||||
|
export default plugin
|
||||||
|
}
|
||||||
|
declare module 'rollup-plugin-replace' {
|
||||||
|
const plugin: RollupPluginFn
|
||||||
|
export default plugin
|
||||||
|
}
|
||||||
|
declare module 'rollup-plugin-uglify' {
|
||||||
|
const uglify: RollupPluginFn
|
||||||
|
export { uglify }
|
||||||
|
}
|
||||||
|
declare module 'rollup-plugin-terser' {
|
||||||
|
const terser: RollupPluginFn
|
||||||
|
export { terser }
|
||||||
|
}
|
60
config/helpers.js
Normal file
60
config/helpers.js
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
// helpers
|
||||||
|
module.exports = {
|
||||||
|
camelCaseToDash,
|
||||||
|
dashToCamelCase,
|
||||||
|
toUpperCase,
|
||||||
|
pascalCase,
|
||||||
|
normalizePackageName,
|
||||||
|
getOutputFileName,
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} myStr
|
||||||
|
*/
|
||||||
|
function camelCaseToDash(myStr) {
|
||||||
|
return myStr.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} myStr
|
||||||
|
*/
|
||||||
|
function dashToCamelCase(myStr) {
|
||||||
|
return myStr.replace(/-([a-z])/g, (g) => g[1].toUpperCase())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} myStr
|
||||||
|
*/
|
||||||
|
function toUpperCase(myStr) {
|
||||||
|
return `${myStr.charAt(0).toUpperCase()}${myStr.substr(1)}`
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} myStr
|
||||||
|
*/
|
||||||
|
function pascalCase(myStr) {
|
||||||
|
return toUpperCase(dashToCamelCase(myStr))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} rawPackageName
|
||||||
|
*/
|
||||||
|
function normalizePackageName(rawPackageName) {
|
||||||
|
const scopeEnd = rawPackageName.indexOf('/') + 1
|
||||||
|
|
||||||
|
return rawPackageName.substring(scopeEnd)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} fileName
|
||||||
|
* @param {boolean?} isProd
|
||||||
|
*/
|
||||||
|
function getOutputFileName(fileName, isProd = false) {
|
||||||
|
return isProd ? fileName.replace(/\.js$/, '.min.js') : fileName
|
||||||
|
}
|
43
config/jest.config.js
Normal file
43
config/jest.config.js
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
// @ts-check
|
||||||
|
|
||||||
|
const { defaults } = require('jest-config')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {import('./types').TsJestConfig}
|
||||||
|
*/
|
||||||
|
const tsJestConfig = {
|
||||||
|
skipBabel: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Partial<jest.InitialOptions>}
|
||||||
|
*/
|
||||||
|
const config = {
|
||||||
|
rootDir: '..',
|
||||||
|
transform: {
|
||||||
|
'^.+\\.(ts|tsx)$': 'ts-jest',
|
||||||
|
},
|
||||||
|
testMatch: [
|
||||||
|
'<rootDir>/src/**/__tests__/**/*.ts?(x)',
|
||||||
|
'<rootDir>/src/**/?(*.)+(spec|test).ts?(x)',
|
||||||
|
],
|
||||||
|
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'tsx'],
|
||||||
|
globals: {
|
||||||
|
'ts-jest': tsJestConfig,
|
||||||
|
},
|
||||||
|
coverageThreshold: {
|
||||||
|
global: {
|
||||||
|
branches: 80,
|
||||||
|
functions: 80,
|
||||||
|
lines: 80,
|
||||||
|
statements: 80,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
setupFiles: ['<rootDir>/config/setup-tests.js'],
|
||||||
|
watchPlugins: [
|
||||||
|
'jest-watch-typeahead/filename',
|
||||||
|
'jest-watch-typeahead/testname',
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = config
|
128
config/rollup.config.js
Normal file
128
config/rollup.config.js
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
import { resolve } from 'path'
|
||||||
|
import sourceMaps from 'rollup-plugin-sourcemaps'
|
||||||
|
import nodeResolve from 'rollup-plugin-node-resolve'
|
||||||
|
import json from 'rollup-plugin-json'
|
||||||
|
import commonjs from 'rollup-plugin-commonjs'
|
||||||
|
import replace from 'rollup-plugin-replace'
|
||||||
|
import { uglify } from 'rollup-plugin-uglify'
|
||||||
|
import { terser } from 'rollup-plugin-terser'
|
||||||
|
import { getIfUtils, removeEmpty } from 'webpack-config-utils'
|
||||||
|
|
||||||
|
import pkg from '../package.json'
|
||||||
|
const {
|
||||||
|
pascalCase,
|
||||||
|
normalizePackageName,
|
||||||
|
getOutputFileName,
|
||||||
|
} = require('./helpers')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {import('./types').RollupConfig} Config
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @typedef {import('./types').RollupPlugin} Plugin
|
||||||
|
*/
|
||||||
|
|
||||||
|
const env = process.env.NODE_ENV || 'development'
|
||||||
|
const { ifProduction } = getIfUtils(env)
|
||||||
|
|
||||||
|
const LIB_NAME = pascalCase(normalizePackageName(pkg.name))
|
||||||
|
const ROOT = resolve(__dirname, '..')
|
||||||
|
const DIST = resolve(ROOT, 'dist')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {{entry:{esm5: string, esm2015: string},bundles:string}}
|
||||||
|
*/
|
||||||
|
const PATHS = {
|
||||||
|
entry: {
|
||||||
|
esm5: resolve(DIST, 'esm5'),
|
||||||
|
esm2015: resolve(DIST, 'esm2015'),
|
||||||
|
},
|
||||||
|
bundles: resolve(DIST, 'bundles'),
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {string[]}
|
||||||
|
*/
|
||||||
|
const external = Object.keys(pkg.peerDependencies) || []
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Plugin[]}
|
||||||
|
*/
|
||||||
|
const plugins = /** @type {Plugin[]} */ ([
|
||||||
|
// Allow json resolution
|
||||||
|
json(),
|
||||||
|
|
||||||
|
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
|
||||||
|
commonjs(),
|
||||||
|
|
||||||
|
// Allow node_modules resolution, so you can use 'external' to control
|
||||||
|
// which external modules to include in the bundle
|
||||||
|
// https://github.com/rollup/rollup-plugin-node-resolve#usage
|
||||||
|
nodeResolve(),
|
||||||
|
|
||||||
|
// Resolve source maps to the original source
|
||||||
|
sourceMaps(),
|
||||||
|
|
||||||
|
// properly set process.env.NODE_ENV within `./environment.ts`
|
||||||
|
replace({
|
||||||
|
exclude: 'node_modules/**',
|
||||||
|
'process.env.NODE_ENV': JSON.stringify(env),
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Config}
|
||||||
|
*/
|
||||||
|
const CommonConfig = {
|
||||||
|
input: {},
|
||||||
|
output: {},
|
||||||
|
inlineDynamicImports: true,
|
||||||
|
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
|
||||||
|
external,
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Config}
|
||||||
|
*/
|
||||||
|
const UMDconfig = {
|
||||||
|
...CommonConfig,
|
||||||
|
input: resolve(PATHS.entry.esm5, 'index.js'),
|
||||||
|
output: {
|
||||||
|
file: getOutputFileName(
|
||||||
|
resolve(PATHS.bundles, 'index.umd.js'),
|
||||||
|
ifProduction()
|
||||||
|
),
|
||||||
|
// file: getOutputFileName('dist/bundles/index.umd.js', ifProduction()),
|
||||||
|
format: 'umd',
|
||||||
|
name: LIB_NAME,
|
||||||
|
sourcemap: true,
|
||||||
|
},
|
||||||
|
plugins: /** @type {Plugin[]} */ (removeEmpty([
|
||||||
|
...plugins,
|
||||||
|
ifProduction(uglify()),
|
||||||
|
])),
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Config}
|
||||||
|
*/
|
||||||
|
const FESMconfig = {
|
||||||
|
...CommonConfig,
|
||||||
|
input: resolve(PATHS.entry.esm2015, 'index.js'),
|
||||||
|
output: [
|
||||||
|
{
|
||||||
|
file: getOutputFileName(
|
||||||
|
resolve(PATHS.bundles, 'index.esm.js'),
|
||||||
|
ifProduction()
|
||||||
|
),
|
||||||
|
format: 'es',
|
||||||
|
sourcemap: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
plugins: /** @type {Plugin[]} */ (removeEmpty([
|
||||||
|
...plugins,
|
||||||
|
ifProduction(terser()),
|
||||||
|
])),
|
||||||
|
}
|
||||||
|
|
||||||
|
export default [UMDconfig, FESMconfig]
|
4
config/setup-tests.js
Normal file
4
config/setup-tests.js
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
// add here any code that you wanna execute before tests like
|
||||||
|
// - polyfills
|
||||||
|
// - some custom code
|
||||||
|
// for more docs check see https://jestjs.io/docs/en/configuration.html#setupfiles-array
|
14
config/tsconfig.json
Normal file
14
config/tsconfig.json
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"strict": true,
|
||||||
|
"allowJs": true,
|
||||||
|
"checkJs": true,
|
||||||
|
"target": "es2017",
|
||||||
|
"module": "commonjs",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"noEmit": true
|
||||||
|
}
|
||||||
|
}
|
30
config/types.js
Normal file
30
config/types.js
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
export {}
|
||||||
|
|
||||||
|
// ===== JEST ====
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {import('ts-jest/dist/jest-types').TsJestConfig} TsJestConfig
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @TODO https://github.com/Microsoft/TypeScript/issues/24916
|
||||||
|
/**
|
||||||
|
* @typedef {Partial<jest.ProjectConfig & jest.GlobalConfig>} JestConfig
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {typeof import('jest-config').defaults} JestDefaultConfig
|
||||||
|
*/
|
||||||
|
|
||||||
|
// ==== PRETTIER ====
|
||||||
|
/**
|
||||||
|
* @typedef {import('prettier').Options} PrettierConfig
|
||||||
|
*/
|
||||||
|
|
||||||
|
// ==== ROLLUP ====
|
||||||
|
/**
|
||||||
|
* @typedef {import('rollup').InputOptions & { output: import('rollup').OutputOptions | Array<import('rollup').OutputOptions | null> }} RollupConfig
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {import('rollup').Plugin} RollupPlugin
|
||||||
|
*/
|
100
package.json
Normal file
100
package.json
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
{
|
||||||
|
"name": "@overflow/core-js",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "TypeScript library setup for multiple compilation targets using tsc and webpack",
|
||||||
|
"main": "./bundles/index.umd.js",
|
||||||
|
"module": "./esm5/index.js",
|
||||||
|
"es2015": "./esm2015/index.js",
|
||||||
|
"typings": "./types/index.d.ts",
|
||||||
|
"sideEffects": false,
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://git.loafle.net/overflow/core-js.git"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"registry": "https://nexus.loafle.net/repository/npm-loafle/"
|
||||||
|
},
|
||||||
|
"author": "loafle.com",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=8.5"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"cleanup": "shx rm -rf dist",
|
||||||
|
"prebuild": "yarn cleanup && yarn verify",
|
||||||
|
"build": "tsc && tsc --target es2018 --outDir dist/esm2015 && rollup -c config/rollup.config.js && rollup -c config/rollup.config.js --environment NODE_ENV:production",
|
||||||
|
"postbuild": "node scripts/copy.js && yarn size",
|
||||||
|
"docs": "typedoc -p . --theme minimal --target 'es6' --excludeNotExported --excludePrivate --ignoreCompilerErrors --exclude \"**/src/**/__tests__/*.*\" --out docs src/",
|
||||||
|
"test": "jest -c ./config/jest.config.js",
|
||||||
|
"test:watch": "yarn test -- --watch",
|
||||||
|
"test:coverage": "yarn test -- --coverage",
|
||||||
|
"test:ci": "yarn test -- --ci",
|
||||||
|
"validate-js": "tsc -p ./config && tsc -p ./scripts",
|
||||||
|
"verify": "yarn validate-js && yarn style && yarn test:ci",
|
||||||
|
"commit": "git-cz",
|
||||||
|
"style": "yarn lint",
|
||||||
|
"style:fix": "yarn format:fix && yarn lint:fix",
|
||||||
|
"lint": "tslint --project tsconfig.json --format codeFrame",
|
||||||
|
"lint:fix": "yarn lint -- --fix",
|
||||||
|
"prerelease": "yarn build",
|
||||||
|
"release": "standard-version",
|
||||||
|
"postrelease": "node scripts/copy.js && yarn release:git && yarn release:npm",
|
||||||
|
"release:git": "git push --no-verify --follow-tags origin master",
|
||||||
|
"release:npm": "cd dist && yarn publish",
|
||||||
|
"release:preflight": "cd dist && yarn pack",
|
||||||
|
"size": "yarn size:umd && yarn size:fesm",
|
||||||
|
"size:umd": "shx echo \"Gzipped+minified UMD bundle Size:\" && cross-var strip-json-comments --no-whitespace \"./dist/bundles/index.umd.min.js\" | gzip-size",
|
||||||
|
"size:fesm": "shx echo \"Gzipped+minified FESM bundle Size:\" && strip-json-comments --no-whitespace \"./dist/bundles/index.esm.min.js\" | gzip-size"
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"commitizen": {
|
||||||
|
"path": "./node_modules/cz-conventional-changelog"
|
||||||
|
},
|
||||||
|
"validate-commit-msg": {
|
||||||
|
"types": "conventional-commit-types",
|
||||||
|
"maxSubjectLength": 120
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"tslib": ">=1.9.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"reflect-metadata": "^0.1.12"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/chokidar": "^1.7.5",
|
||||||
|
"@types/jest": "^23.3.1",
|
||||||
|
"@types/json5": "^0.0.30",
|
||||||
|
"@types/node": "^8.10.4",
|
||||||
|
"@types/webpack-config-utils": "^2.3.0",
|
||||||
|
"awesome-typescript-loader": "^5.2.0",
|
||||||
|
"commitizen": "^2.10.1",
|
||||||
|
"cross-var": "^1.1.0",
|
||||||
|
"cz-conventional-changelog": "^2.1.0",
|
||||||
|
"gzip-size-cli": "^3.0.0",
|
||||||
|
"jest": "^23.5.0",
|
||||||
|
"jest-watch-typeahead": "^0.2.0",
|
||||||
|
"json5": "^2.0.1",
|
||||||
|
"lint-staged": "^7.2.2",
|
||||||
|
"rollup": "^0.65.0",
|
||||||
|
"rollup-plugin-commonjs": "^9.1.6",
|
||||||
|
"rollup-plugin-json": "^3.0.0",
|
||||||
|
"rollup-plugin-node-resolve": "^3.3.0",
|
||||||
|
"rollup-plugin-replace": "^2.0.0",
|
||||||
|
"rollup-plugin-sourcemaps": "^0.4.2",
|
||||||
|
"rollup-plugin-terser": "^1.0.1",
|
||||||
|
"rollup-plugin-uglify": "^4.0.0",
|
||||||
|
"shx": "^0.3.2",
|
||||||
|
"sort-object-keys": "^1.1.2",
|
||||||
|
"standard-version": "^4.4.0",
|
||||||
|
"strip-json-comments-cli": "^1.0.1",
|
||||||
|
"ts-jest": "^23.1.4",
|
||||||
|
"tslib": "^1.9.3",
|
||||||
|
"tslint": "^5.11.0",
|
||||||
|
"tslint-config-standard": "^7.1.0",
|
||||||
|
"typedoc": "^0.12.0",
|
||||||
|
"typescript": "^2.9.2",
|
||||||
|
"validate-commit-msg": "^2.14.0",
|
||||||
|
"webpack-config-utils": "^2.3.0"
|
||||||
|
}
|
||||||
|
}
|
45
scripts/copy.js
Normal file
45
scripts/copy.js
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
const { writeFileSync, copyFileSync } = require('fs')
|
||||||
|
const { resolve } = require('path')
|
||||||
|
const packageJson = require('../package.json')
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
const projectRoot = resolve(__dirname, '..')
|
||||||
|
const distPath = resolve(projectRoot, 'dist')
|
||||||
|
const distPackageJson = createDistPackageJson(packageJson)
|
||||||
|
|
||||||
|
copyFileSync(
|
||||||
|
resolve(projectRoot, 'README.md'),
|
||||||
|
resolve(distPath, 'README.md')
|
||||||
|
)
|
||||||
|
copyFileSync(
|
||||||
|
resolve(projectRoot, 'CHANGELOG.md'),
|
||||||
|
resolve(distPath, 'CHANGELOG.md')
|
||||||
|
)
|
||||||
|
copyFileSync(
|
||||||
|
resolve(projectRoot, 'LICENSE.md'),
|
||||||
|
resolve(distPath, 'LICENSE.md')
|
||||||
|
)
|
||||||
|
copyFileSync(
|
||||||
|
resolve(projectRoot, '.npmignore'),
|
||||||
|
resolve(distPath, '.npmignore')
|
||||||
|
)
|
||||||
|
writeFileSync(resolve(distPath, 'package.json'), distPackageJson)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {typeof packageJson} packageConfig
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
|
function createDistPackageJson(packageConfig) {
|
||||||
|
const {
|
||||||
|
devDependencies,
|
||||||
|
scripts,
|
||||||
|
engines,
|
||||||
|
config,
|
||||||
|
...distPackageJson
|
||||||
|
} = packageConfig
|
||||||
|
|
||||||
|
return JSON.stringify(distPackageJson, null, 2)
|
||||||
|
}
|
221
scripts/migrate.js
Normal file
221
scripts/migrate.js
Normal file
|
@ -0,0 +1,221 @@
|
||||||
|
/**
|
||||||
|
* Migrate Typescript-library-starter from 3. -> 4.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const JSON5 = require('json5')
|
||||||
|
const sortObjectByKeyNameList = require('sort-object-keys')
|
||||||
|
const {
|
||||||
|
writeFileSync,
|
||||||
|
copyFileSync,
|
||||||
|
readFileSync,
|
||||||
|
existsSync,
|
||||||
|
unlinkSync,
|
||||||
|
} = require('fs')
|
||||||
|
const { resolve, join } = require('path')
|
||||||
|
const starterPkg = require('../package.json')
|
||||||
|
const args = process.argv.slice(2)
|
||||||
|
const pathToProject = args[0]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {typeof projectPkg} Pkg
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!pathToProject) {
|
||||||
|
throw new Error(
|
||||||
|
'you need provide relative path to package that uses ts-lib-starter!'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const ROOT = resolve(__dirname, '..')
|
||||||
|
const PACKAGE_ROOT = resolve(ROOT, pathToProject)
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
if (!existsSync(PACKAGE_ROOT)) {
|
||||||
|
throw new Error(`${PACKAGE_ROOT}, doesn't exists`)
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Migration initialized 👀')
|
||||||
|
|
||||||
|
console.log('path to Package:', PACKAGE_ROOT)
|
||||||
|
|
||||||
|
updatePackageJson()
|
||||||
|
updateTsConfig()
|
||||||
|
updateTsLintConfig()
|
||||||
|
updateConfigDir()
|
||||||
|
updateScriptsDir()
|
||||||
|
|
||||||
|
console.log('DONE ✅')
|
||||||
|
}
|
||||||
|
|
||||||
|
function updatePackageJson() {
|
||||||
|
const libPackagePkgPath = resolve(PACKAGE_ROOT, 'package.json')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {typeof starterPkg}
|
||||||
|
*/
|
||||||
|
const libPackagePkg = JSON5.parse(
|
||||||
|
readFileSync(libPackagePkgPath, { encoding: 'utf-8' })
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {typeof starterPkg}
|
||||||
|
*/
|
||||||
|
const updatePkg = {
|
||||||
|
...libPackagePkg,
|
||||||
|
main: starterPkg.main,
|
||||||
|
engines: { ...libPackagePkg.engines, ...starterPkg.engines },
|
||||||
|
scripts: { ...libPackagePkg.scripts, ...starterPkg.scripts },
|
||||||
|
peerDependencies: sortObjectByKeyNameList({
|
||||||
|
...libPackagePkg.peerDependencies,
|
||||||
|
...starterPkg.peerDependencies,
|
||||||
|
}),
|
||||||
|
devDependencies: sortObjectByKeyNameList({
|
||||||
|
...starterPkg.devDependencies,
|
||||||
|
...libPackagePkg.devDependencies,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
removePackages(updatePkg.devDependencies)
|
||||||
|
writePackage(updatePkg)
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {{[packageName:string]:string}} devDependencies
|
||||||
|
*/
|
||||||
|
function removePackages(devDependencies) {
|
||||||
|
const depsToRemove = [
|
||||||
|
'@types/uglifyjs-webpack-plugin',
|
||||||
|
'@types/webpack',
|
||||||
|
'uglifyjs-webpack-plugin',
|
||||||
|
'webpack',
|
||||||
|
'webpack-cli',
|
||||||
|
// packages needed for this script
|
||||||
|
'json5',
|
||||||
|
'@types/json5',
|
||||||
|
'sort-object-keys',
|
||||||
|
]
|
||||||
|
|
||||||
|
depsToRemove.forEach(
|
||||||
|
(dependencyName) => delete devDependencies[dependencyName]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {typeof starterPkg} pkg
|
||||||
|
*/
|
||||||
|
function writePackage(pkg) {
|
||||||
|
const updatedLibPkgToWrite = JSON.stringify(pkg, null, 2)
|
||||||
|
writeFileSync(join(PACKAGE_ROOT, 'package.json'), updatedLibPkgToWrite)
|
||||||
|
|
||||||
|
console.log('\n updated package.json:', updatedLibPkgToWrite, '\n')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateTsConfig() {
|
||||||
|
/**
|
||||||
|
* @typedef {typeof import('../tsconfig.json')} TsConfig
|
||||||
|
*/
|
||||||
|
|
||||||
|
const starterConfigPath = resolve(ROOT, 'tsconfig.json')
|
||||||
|
const libPackageConfigPath = resolve(PACKAGE_ROOT, 'tsconfig.json')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {TsConfig}
|
||||||
|
*/
|
||||||
|
const starterConfig = JSON5.parse(
|
||||||
|
readFileSync(starterConfigPath, { encoding: 'utf-8' })
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {TsConfig}
|
||||||
|
*/
|
||||||
|
const libConfig = JSON5.parse(
|
||||||
|
readFileSync(libPackageConfigPath, { encoding: 'utf-8' })
|
||||||
|
)
|
||||||
|
|
||||||
|
console.log('starter:', starterConfig)
|
||||||
|
console.log('library:', libConfig)
|
||||||
|
|
||||||
|
console.log('==TS-Config:nothing updated==\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateTsLintConfig() {
|
||||||
|
/**
|
||||||
|
* @typedef {typeof import('../tslint.json')} TsLintConfig
|
||||||
|
*/
|
||||||
|
|
||||||
|
const starterConfigPath = resolve(ROOT, 'tslint.json')
|
||||||
|
const libPackageConfigPath = resolve(PACKAGE_ROOT, 'tslint.json')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {TsLintConfig}
|
||||||
|
*/
|
||||||
|
const starterConfig = JSON5.parse(
|
||||||
|
readFileSync(starterConfigPath, { encoding: 'utf-8' })
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {TsLintConfig}
|
||||||
|
*/
|
||||||
|
const libConfig = JSON5.parse(
|
||||||
|
readFileSync(libPackageConfigPath, { encoding: 'utf-8' })
|
||||||
|
)
|
||||||
|
|
||||||
|
console.log('starter:', starterConfig)
|
||||||
|
console.log('library:', libConfig)
|
||||||
|
|
||||||
|
console.log('==TS-Lint:nothing updated==\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateConfigDir() {
|
||||||
|
const starterConfigPathDir = resolve(ROOT, 'config')
|
||||||
|
const libPackageConfigPathDir = resolve(PACKAGE_ROOT, 'config')
|
||||||
|
|
||||||
|
const filesToCopy = [
|
||||||
|
'global.d.ts',
|
||||||
|
'helpers.js',
|
||||||
|
'rollup.config.js',
|
||||||
|
'tsconfig.json',
|
||||||
|
'types.js',
|
||||||
|
]
|
||||||
|
const filesToRemove = ['webpack.config.js']
|
||||||
|
|
||||||
|
filesToCopy.forEach((file) => {
|
||||||
|
copyFileSync(
|
||||||
|
resolve(starterConfigPathDir, file),
|
||||||
|
join(libPackageConfigPathDir, file)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
filesToRemove.forEach((file) => {
|
||||||
|
unlinkSync(join(libPackageConfigPathDir, file))
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('==config/ updated==\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateScriptsDir() {
|
||||||
|
const starterScriptsPathDir = resolve(ROOT, 'scripts')
|
||||||
|
const libPackageScriptsPathDir = resolve(PACKAGE_ROOT, 'scripts')
|
||||||
|
|
||||||
|
const filesToCopy = ['copy.js', 'tsconfig.json']
|
||||||
|
/**
|
||||||
|
* @type {string[]}
|
||||||
|
*/
|
||||||
|
const filesToRemove = []
|
||||||
|
|
||||||
|
filesToCopy.forEach((file) => {
|
||||||
|
copyFileSync(
|
||||||
|
resolve(starterScriptsPathDir, file),
|
||||||
|
join(libPackageScriptsPathDir, file)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
filesToRemove.forEach((file) => {
|
||||||
|
unlinkSync(join(libPackageScriptsPathDir, file))
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('==scripts/ updated==\n')
|
||||||
|
}
|
8
scripts/tsconfig.json
Normal file
8
scripts/tsconfig.json
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"extends": "../config/tsconfig.json",
|
||||||
|
"compilerOptions": {},
|
||||||
|
"include": [
|
||||||
|
".",
|
||||||
|
"../config/global.d.ts"
|
||||||
|
]
|
||||||
|
}
|
16
src/__tests__/Greeter.spec.ts
Normal file
16
src/__tests__/Greeter.spec.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
jest.mock('../environment.ts', () => ({
|
||||||
|
IS_DEV: true,
|
||||||
|
IS_PROD: false,
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe(`Greeter`, () => {
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
console.log('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should greet`, () => {
|
||||||
|
console.log('');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
6
src/core/error.ts
Normal file
6
src/core/error.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
export class IllegalArgumentError extends Error {
|
||||||
|
public constructor(message?: string) {
|
||||||
|
super(message);
|
||||||
|
Object.setPrototypeOf(this, new.target.prototype);
|
||||||
|
}
|
||||||
|
}
|
2
src/core/index.ts
Normal file
2
src/core/index.ts
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export * from './error';
|
||||||
|
export * from './type';
|
16
src/core/type.ts
Normal file
16
src/core/type.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
export declare const Type: FunctionConstructor;
|
||||||
|
export declare function isType(v: any): v is Type<any>;
|
||||||
|
export interface Type<T> extends Function {
|
||||||
|
new (...args: any[]): T;
|
||||||
|
}
|
||||||
|
|
||||||
|
export declare type IdentityType<T> = T | symbol;
|
||||||
|
export declare type PropertyKeyType = IdentityType<string>;
|
||||||
|
export declare type MetadataKeyType = IdentityType<string>;
|
||||||
|
|
||||||
|
export enum PrimitiveType {
|
||||||
|
ANY = 'any',
|
||||||
|
STRING = 'string',
|
||||||
|
NUMBER = 'number',
|
||||||
|
BOOLEAN = 'boolean',
|
||||||
|
}
|
5
src/environment.ts
Normal file
5
src/environment.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
/** * @internal */
|
||||||
|
export const IS_DEV = process.env.NODE_ENV === 'development';
|
||||||
|
|
||||||
|
/** * @internal */
|
||||||
|
export const IS_PROD = process.env.NODE_ENV === 'production';
|
3
src/index.ts
Normal file
3
src/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export * from './core';
|
||||||
|
export * from './reflect';
|
||||||
|
export * from './util';
|
65
src/reflect/AccessibleObject.ts
Normal file
65
src/reflect/AccessibleObject.ts
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
import {
|
||||||
|
Type,
|
||||||
|
} from '../core';
|
||||||
|
|
||||||
|
import {
|
||||||
|
TypeUtil,
|
||||||
|
} from '../util/TypeUtil';
|
||||||
|
|
||||||
|
import { AnnotatedElement } from './AnnotatedElement';
|
||||||
|
import { Annotation } from './Annotation';
|
||||||
|
|
||||||
|
export abstract class AccessibleObject implements AnnotatedElement {
|
||||||
|
private _annotations: Map<Type<any>, Annotation>;
|
||||||
|
|
||||||
|
protected constructor() {
|
||||||
|
this._annotations = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
public _addAnnotation<AnnotationType extends Annotation>(annotation: AnnotationType): void {
|
||||||
|
this._annotations.set(TypeUtil.getType(annotation), annotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public isAnnotationPresent<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): boolean {
|
||||||
|
return null !== this.getAnnotation(annotationClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getOwnAnnotation<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): AnnotationType | undefined {
|
||||||
|
return this._annotations.get(annotationClass) as AnnotationType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getOwnAnnotations(): Map<Type<any>, Annotation> {
|
||||||
|
return this._annotations;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getOwnAnnotationsByType<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>)
|
||||||
|
: AnnotationType[] | undefined {
|
||||||
|
if (0 === this._annotations.size) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const results: AnnotationType[] = [];
|
||||||
|
for (const classType of Array.from(this._annotations.keys())) {
|
||||||
|
if (classType === annotationClass) {
|
||||||
|
results.push(this._annotations.get(classType) as AnnotationType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (0 === results.length) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAnnotation<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): AnnotationType | undefined {
|
||||||
|
return this.getOwnAnnotation(annotationClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAnnotations(): Map<Type<any>, Annotation> {
|
||||||
|
return this.getOwnAnnotations();
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAnnotationsByType<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>)
|
||||||
|
: AnnotationType[] | undefined {
|
||||||
|
return this.getOwnAnnotationsByType(annotationClass);
|
||||||
|
}
|
||||||
|
}
|
17
src/reflect/AnnotatedElement.ts
Normal file
17
src/reflect/AnnotatedElement.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import {
|
||||||
|
Type,
|
||||||
|
} from '../core';
|
||||||
|
|
||||||
|
import { Annotation } from './Annotation';
|
||||||
|
|
||||||
|
export interface AnnotatedElement {
|
||||||
|
_addAnnotation<AnnotationType extends Annotation>(annotation: AnnotationType): void;
|
||||||
|
|
||||||
|
isAnnotationPresent<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): boolean;
|
||||||
|
getOwnAnnotation<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): AnnotationType | undefined;
|
||||||
|
getOwnAnnotations(): Map<Type<any>, Annotation>;
|
||||||
|
getOwnAnnotationsByType<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): AnnotationType[] | undefined;
|
||||||
|
getAnnotation<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): AnnotationType | undefined;
|
||||||
|
getAnnotations(): Map<Type<any>, Annotation>;
|
||||||
|
getAnnotationsByType<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): AnnotationType[] | undefined;
|
||||||
|
}
|
7
src/reflect/Annotation.ts
Normal file
7
src/reflect/Annotation.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
export abstract class Annotation<Attribute = {}> {
|
||||||
|
public readonly attribute: Attribute | undefined;
|
||||||
|
|
||||||
|
public constructor(attribute?: Attribute) {
|
||||||
|
this.attribute = attribute;
|
||||||
|
}
|
||||||
|
}
|
231
src/reflect/Class.ts
Normal file
231
src/reflect/Class.ts
Normal file
|
@ -0,0 +1,231 @@
|
||||||
|
import {
|
||||||
|
PropertyKeyType,
|
||||||
|
Type,
|
||||||
|
} from '../core';
|
||||||
|
|
||||||
|
import {
|
||||||
|
TypeUtil,
|
||||||
|
} from '../util/TypeUtil';
|
||||||
|
|
||||||
|
import { AccessibleObject } from './AccessibleObject';
|
||||||
|
import { Annotation } from './Annotation';
|
||||||
|
import { SystemClassRegistry } from './ClassRegistry';
|
||||||
|
import { Constructor } from './Constructor';
|
||||||
|
import { Field } from './Field';
|
||||||
|
import { Metadata } from './Metadata';
|
||||||
|
import { Method } from './Method';
|
||||||
|
|
||||||
|
export class Class extends AccessibleObject {
|
||||||
|
private _type: Type<any>;
|
||||||
|
private _constructor: Constructor | undefined;
|
||||||
|
private _fields: Map<PropertyKeyType, Field>;
|
||||||
|
private _methods: Map<PropertyKeyType, Method>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* forClass
|
||||||
|
*/
|
||||||
|
public static forType(type: Type<any>): Class | undefined {
|
||||||
|
return SystemClassRegistry.get(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* _defineClass
|
||||||
|
*/
|
||||||
|
public static _defineClass(type: Type<any>): Class {
|
||||||
|
let clazz: Class | undefined = Class.forType(type);
|
||||||
|
if (undefined === clazz) {
|
||||||
|
clazz = new Class(type);
|
||||||
|
SystemClassRegistry.set(type, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === clazz._constructor) {
|
||||||
|
const parameterTypes = Metadata.getOwnParamTypes(type);
|
||||||
|
if (undefined !== parameterTypes) {
|
||||||
|
clazz._constructor = new Constructor(clazz, parameterTypes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
private constructor(type: Type<any>) {
|
||||||
|
super();
|
||||||
|
this._type = type;
|
||||||
|
this._fields = new Map();
|
||||||
|
this._methods = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* _defineField
|
||||||
|
*/
|
||||||
|
public _defineConstructor(parameterTypes: any[]): Constructor | null {
|
||||||
|
let cons: Constructor | undefined = this._constructor;
|
||||||
|
if (undefined === cons) {
|
||||||
|
cons = new Constructor(this, parameterTypes);
|
||||||
|
this._constructor = cons;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cons;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* _defineField
|
||||||
|
*/
|
||||||
|
public _defineField(propertyKey: PropertyKeyType, propertyType: any): Field {
|
||||||
|
let field: Field | undefined = this._fields.get(propertyKey);
|
||||||
|
if (undefined === field) {
|
||||||
|
field = new Field(this, propertyKey, propertyType);
|
||||||
|
this._fields.set(propertyKey, field);
|
||||||
|
}
|
||||||
|
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* _defineMethod
|
||||||
|
*/
|
||||||
|
public _defineMethod(propertyKey: PropertyKeyType, parameterTypes: any[], returnType: any): Method {
|
||||||
|
let method: Method | undefined = this._methods.get(propertyKey);
|
||||||
|
if (undefined === method) {
|
||||||
|
method = new Method(this, propertyKey, parameterTypes, returnType);
|
||||||
|
this._methods.set(propertyKey, method);
|
||||||
|
}
|
||||||
|
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getType(): Type<any> {
|
||||||
|
return this._type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getConstructor(): Constructor {
|
||||||
|
if (undefined === this._constructor) {
|
||||||
|
this._constructor = new Constructor(this, undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this._constructor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getOwnField(propertyKey: PropertyKeyType): Field | undefined {
|
||||||
|
return this._fields.get(propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getOwnFields(): Map<PropertyKeyType, Field> {
|
||||||
|
return this._fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getField(propertyKey: PropertyKeyType): Field | undefined {
|
||||||
|
const fields = this.getFields();
|
||||||
|
|
||||||
|
return fields.get(propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getFields(): Map<PropertyKeyType, Field> {
|
||||||
|
const fields: Map<PropertyKeyType, Field> = new Map();
|
||||||
|
|
||||||
|
const types = TypeUtil.ancestorsOf(this._type);
|
||||||
|
if (null === types || 0 === types.length) {
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
for (let i = 0; i < types.length; i++) {
|
||||||
|
const tType = types[i];
|
||||||
|
const tClazz = Class.forType(tType);
|
||||||
|
if (undefined === tClazz) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
tClazz.getOwnFields().forEach((value: Field, key: PropertyKeyType): void => {
|
||||||
|
fields.set(key, value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getOwnMethod(propertyKey: PropertyKeyType): Method | undefined {
|
||||||
|
return this._methods.get(propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getOwnMethods(): Map<PropertyKeyType, Method> {
|
||||||
|
return this._methods;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getMethod(propertyKey: PropertyKeyType): Method | undefined {
|
||||||
|
const methods = this.getMethods();
|
||||||
|
|
||||||
|
return methods.get(propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getMethods(): Map<PropertyKeyType, Method> {
|
||||||
|
const methods: Map<PropertyKeyType, Method> = new Map();
|
||||||
|
|
||||||
|
const types = TypeUtil.ancestorsOf(this._type);
|
||||||
|
if (null === types || 0 === types.length) {
|
||||||
|
return methods;
|
||||||
|
}
|
||||||
|
for (let i = 0; i < types.length; i++) {
|
||||||
|
const tClazzType = types[i];
|
||||||
|
const tClazz = Class.forType(tClazzType);
|
||||||
|
if (undefined === tClazz) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
tClazz.getOwnMethods().forEach((value: Method, key: PropertyKeyType): void => {
|
||||||
|
methods.set(key, value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return methods;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAnnotation<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): AnnotationType | undefined {
|
||||||
|
const annotations = this.getAnnotations();
|
||||||
|
|
||||||
|
return annotations.get(annotationClass) as AnnotationType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAnnotations(): Map<Type<any>, Annotation> {
|
||||||
|
const annotations: Map<Type<any>, Annotation> = new Map();
|
||||||
|
|
||||||
|
const types = TypeUtil.ancestorsOf(this._type);
|
||||||
|
if (null === types || 0 === types.length) {
|
||||||
|
return annotations;
|
||||||
|
}
|
||||||
|
for (let i = 0; i < types.length; i++) {
|
||||||
|
const tClazzType = types[i];
|
||||||
|
const tClazz = Class.forType(tClazzType);
|
||||||
|
if (undefined === tClazz) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
tClazz.getOwnAnnotations().forEach((value: Annotation, key: Type<any>): void => {
|
||||||
|
annotations.set(key, value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return annotations;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAnnotationsByType<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>)
|
||||||
|
: AnnotationType[] | undefined {
|
||||||
|
const annotations = this.getAnnotations();
|
||||||
|
if (0 === annotations.size) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const results: AnnotationType[] = [];
|
||||||
|
for (const classType of Array.from(annotations.keys())) {
|
||||||
|
if (classType === annotationClass) {
|
||||||
|
results.push(annotations.get(classType) as AnnotationType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (0 === results.length) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getName(): string {
|
||||||
|
return this._type.name;
|
||||||
|
}
|
||||||
|
}
|
12
src/reflect/ClassRegistry.ts
Normal file
12
src/reflect/ClassRegistry.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { Type } from '../core';
|
||||||
|
import { Registry } from '../util/Registry';
|
||||||
|
|
||||||
|
import { Class } from './Class';
|
||||||
|
|
||||||
|
export class ClassRegistry extends Registry<Type<any>, Class> {
|
||||||
|
public constructor(parent?: ClassRegistry) {
|
||||||
|
super(parent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const SystemClassRegistry = new ClassRegistry();
|
23
src/reflect/Constructor.ts
Normal file
23
src/reflect/Constructor.ts
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
// import {
|
||||||
|
// TypeUtil,
|
||||||
|
// } from '../util/TypeUtil';
|
||||||
|
|
||||||
|
import { Class } from './Class';
|
||||||
|
import { Executable } from './Executable';
|
||||||
|
|
||||||
|
export class Constructor extends Executable {
|
||||||
|
// private _rawConstructor: Function;
|
||||||
|
|
||||||
|
public constructor(declaringClazz: Class, parameterTypes?: any[]) {
|
||||||
|
super(declaringClazz, CONSTRUCTOR_NAME, parameterTypes);
|
||||||
|
// this._rawConstructor = TypeUtil.getPrototype(declaringClazz.getType())[CONSTRUCTOR_NAME];
|
||||||
|
}
|
||||||
|
|
||||||
|
public newInstance(...args: any[]): any {
|
||||||
|
const ctor = this.getDeclaringClass().getType();
|
||||||
|
|
||||||
|
return new (ctor.bind.apply(ctor, [null].concat(args)))();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const CONSTRUCTOR_NAME = 'constructor';
|
77
src/reflect/Executable.ts
Normal file
77
src/reflect/Executable.ts
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
import {
|
||||||
|
PropertyKeyType,
|
||||||
|
} from '../core';
|
||||||
|
|
||||||
|
import {
|
||||||
|
TypeUtil,
|
||||||
|
} from '../util/TypeUtil';
|
||||||
|
|
||||||
|
import { AccessibleObject } from './AccessibleObject';
|
||||||
|
import { Class } from './Class';
|
||||||
|
import { Member } from './Member';
|
||||||
|
import { Parameter } from './Parameter';
|
||||||
|
|
||||||
|
export abstract class Executable extends AccessibleObject implements Member {
|
||||||
|
private _clazz: Class;
|
||||||
|
private _name: PropertyKeyType;
|
||||||
|
private _parameters: Parameter[];
|
||||||
|
|
||||||
|
protected constructor(declaringClazz: Class, name: PropertyKeyType, parameterTypes?: any[]) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this._clazz = declaringClazz;
|
||||||
|
this._name = name;
|
||||||
|
this._parameters = [];
|
||||||
|
|
||||||
|
if (undefined === parameterTypes) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parameterNames = TypeUtil.getParameterNames(declaringClazz.getType(), name);
|
||||||
|
|
||||||
|
for (let i = 0; i < parameterTypes.length; i++) {
|
||||||
|
const parameterType = parameterTypes[i];
|
||||||
|
const parameterName = parameterNames[i];
|
||||||
|
const parameter: Parameter = new Parameter(this, parameterType, parameterName, i);
|
||||||
|
this._parameters.push(parameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getDeclaringClass(): Class {
|
||||||
|
return this._clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getName(): PropertyKeyType {
|
||||||
|
return this._name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getParameterCount
|
||||||
|
*/
|
||||||
|
public getParameterCount(): number {
|
||||||
|
if (null === this._parameters) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this._parameters.length;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* getParameters
|
||||||
|
*/
|
||||||
|
public getParameters(): Parameter[] | undefined {
|
||||||
|
return this._parameters;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* getParameter
|
||||||
|
*/
|
||||||
|
public getParameter(index: number): Parameter | undefined {
|
||||||
|
if (null === this._parameters) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
if (0 > index || this._parameters.length <= index) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this._parameters[index];
|
||||||
|
}
|
||||||
|
}
|
33
src/reflect/Field.ts
Normal file
33
src/reflect/Field.ts
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import {
|
||||||
|
PropertyKeyType,
|
||||||
|
} from '../core';
|
||||||
|
|
||||||
|
import { AccessibleObject } from './AccessibleObject';
|
||||||
|
import { Class } from './Class';
|
||||||
|
import { Member } from './Member';
|
||||||
|
|
||||||
|
export class Field extends AccessibleObject implements Member {
|
||||||
|
private _clazz: Class;
|
||||||
|
private _name: PropertyKeyType;
|
||||||
|
private _type: any;
|
||||||
|
|
||||||
|
public constructor(declaringClazz: Class, name: PropertyKeyType, fieldType: any) {
|
||||||
|
super();
|
||||||
|
this._clazz = declaringClazz;
|
||||||
|
this._name = name;
|
||||||
|
this._type = fieldType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getDeclaringClass(): Class {
|
||||||
|
return this._clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getName(): PropertyKeyType {
|
||||||
|
return this._name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getType(): any {
|
||||||
|
return this._type;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
src/reflect/Member.ts
Normal file
10
src/reflect/Member.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import {
|
||||||
|
PropertyKeyType,
|
||||||
|
} from '../core';
|
||||||
|
|
||||||
|
import { Class } from './Class';
|
||||||
|
|
||||||
|
export interface Member {
|
||||||
|
getDeclaringClass(): Class;
|
||||||
|
getName(): PropertyKeyType;
|
||||||
|
}
|
531
src/reflect/Metadata.ts
Normal file
531
src/reflect/Metadata.ts
Normal file
|
@ -0,0 +1,531 @@
|
||||||
|
import {
|
||||||
|
MetadataKeyType,
|
||||||
|
PropertyKeyType,
|
||||||
|
} from '../core';
|
||||||
|
|
||||||
|
import { TypeUtil } from '../util/TypeUtil';
|
||||||
|
|
||||||
|
export class Metadata {
|
||||||
|
/**
|
||||||
|
* Gets the metadata value for the provided metadata key on the target object or its prototype chain.
|
||||||
|
* @param key A key used to store and retrieve metadata.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns The metadata value for the metadata key if found; otherwise, `undefined`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // constructor
|
||||||
|
* result = Metadata.get("custom:annotation", Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.get("custom:annotation", Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // property (on prototype)
|
||||||
|
* result = Metadata.get("custom:annotation", Example.prototype, "property");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.get("custom:annotation", Example, "staticMethod");
|
||||||
|
*
|
||||||
|
* // method (on prototype)
|
||||||
|
* result = Metadata.get("custom:annotation", Example.prototype, "method");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static get(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): any;
|
||||||
|
public static get(key: MetadataKeyType, target: any, propertyKey: PropertyKeyType): any {
|
||||||
|
return Reflect.getMetadata(key, TypeUtil.getType(target), propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the metadata value for the provided metadata key on the target object or its prototype chain.
|
||||||
|
* @param key A key used to store and retrieve metadata.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns The metadata value for the metadata key if found; otherwise, `undefined`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // constructor
|
||||||
|
* result = Metadata.getOwn("custom:annotation", Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.getOwn("custom:annotation", Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // property (on prototype)
|
||||||
|
* result = Metadata.getOwn("custom:annotation", Example.prototype, "property");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.getOwn("custom:annotation", Example, "staticMethod");
|
||||||
|
*
|
||||||
|
* // method (on prototype)
|
||||||
|
* result = Metadata.getOwn("custom:annotation", Example.prototype, "method");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static getOwn(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): any;
|
||||||
|
public static getOwn(key: MetadataKeyType, target: any, propertyKey: PropertyKeyType): any {
|
||||||
|
return Reflect.getOwnMetadata(key, TypeUtil.getType(target), propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the metadata value for the provided metadata DESIGN_TYPE on the target object or its prototype chain.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns The metadata value for the metadata key if found; otherwise, `undefined`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // on contructor
|
||||||
|
* result = Metadata.getType(Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.getType(Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.getType(Example, "staticMethod");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static getType(target: any, propertyKey?: PropertyKeyType): any;
|
||||||
|
public static getType(target: any, propertyKey: PropertyKeyType): any {
|
||||||
|
return Reflect.getMetadata(DESIGN_TYPE, target, propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the metadata value for the provided metadata DESIGN_TYPE on the target object or its prototype chain.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns The metadata value for the metadata key if found; otherwise, `undefined`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // on contructor
|
||||||
|
* result = Metadata.getOwnType(Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.getOwnType(Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.getOwnType(Example, "staticMethod");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static getOwnType(target: any, propertyKey?: PropertyKeyType): any;
|
||||||
|
public static getOwnType(target: any, propertyKey: PropertyKeyType): any {
|
||||||
|
return Reflect.getMetadata(DESIGN_TYPE, target, propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the metadata value for the provided metadata DESIGN_RETURN_TYPE on the target object or its prototype chain.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns The metadata value for the metadata key if found; otherwise, `undefined`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // on contructor
|
||||||
|
* result = Metadata.getReturnType(Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.getReturnType(Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.getReturnType(Example, "staticMethod");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static getReturnType(target: any, propertyKey?: PropertyKeyType): any;
|
||||||
|
public static getReturnType(target: any, propertyKey: PropertyKeyType): any {
|
||||||
|
return Reflect.getMetadata(DESIGN_RETURN_TYPE, target, propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the metadata value for the provided metadata DESIGN_RETURN_TYPE on the target object or its prototype chain.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns The metadata value for the metadata key if found; otherwise, `undefined`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // on contructor
|
||||||
|
* result = Metadata.getOwnReturnType(Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.getOwnReturnType(Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.getOwnReturnType(Example, "staticMethod");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static getOwnReturnType(target: any, propertyKey?: PropertyKeyType): any;
|
||||||
|
public static getOwnReturnType(target: any, propertyKey: PropertyKeyType): any {
|
||||||
|
return Reflect.getOwnMetadata(DESIGN_RETURN_TYPE, target, propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined.
|
||||||
|
* @param key A key used to store and retrieve metadata.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // constructor
|
||||||
|
* result = Metadata.has("custom:annotation", Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.has("custom:annotation", Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // property (on prototype)
|
||||||
|
* result = Metadata.has("custom:annotation", Example.prototype, "property");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.has("custom:annotation", Example, "staticMethod");
|
||||||
|
*
|
||||||
|
* // method (on prototype)
|
||||||
|
* result = Metadata.has("custom:annotation", Example.prototype, "method");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static has(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): boolean;
|
||||||
|
public static has(key: MetadataKeyType, target: any, propertyKey: PropertyKeyType): boolean {
|
||||||
|
return Reflect.hasMetadata(key, TypeUtil.getType(target), propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined.
|
||||||
|
* @param key A key used to store and retrieve metadata.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // constructor
|
||||||
|
* result = Metadata.has("custom:annotation", Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.hasOwn("custom:annotation", Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // property (on prototype)
|
||||||
|
* result = Metadata.hasOwn("custom:annotation", Example.prototype, "property");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.hasOwn("custom:annotation", Example, "staticMethod");
|
||||||
|
*
|
||||||
|
* // method (on prototype)
|
||||||
|
* result = Metadata.hasOwn("custom:annotation", Example.prototype, "method");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static hasOwn(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): boolean;
|
||||||
|
public static hasOwn(key: MetadataKeyType, target: any, propertyKey: PropertyKeyType): boolean {
|
||||||
|
return Reflect.hasOwnMetadata(key, TypeUtil.getType(target), propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes the metadata entry from the target object with the provided key.
|
||||||
|
* @param key A key used to store and retrieve metadata.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns `true` if the metadata entry was found and deleted; otherwise, false.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // constructor
|
||||||
|
* result = Metadata.delete("custom:annotation", Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.delete("custom:annotation", Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // property (on prototype)
|
||||||
|
* result = Metadata.delete("custom:annotation", Example.prototype, "property");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.delete("custom:annotation", Example, "staticMethod");
|
||||||
|
*
|
||||||
|
* // method (on prototype)
|
||||||
|
* result = Metadata.delete("custom:annotation", Example.prototype, "method");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static delete(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): boolean;
|
||||||
|
public static delete(key: MetadataKeyType, target: any, propertyKey: PropertyKeyType): boolean {
|
||||||
|
return Reflect.deleteMetadata(key, TypeUtil.getType(target), propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the metadata value for the provided metadata DESIGN_PARAM_TYPES on the target object or its prototype chain.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @param value A value that contains attached metadata.
|
||||||
|
* @returns The metadata value for the metadata key if found; otherwise, `undefined`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // on contructor
|
||||||
|
* result = Metadata.setParamTypes(Example, undefined, [Object]);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.setParamTypes(Example, "staticProperty", [Object]);
|
||||||
|
*
|
||||||
|
* // property (on prototype)
|
||||||
|
* result = Metadata.setParamTypes(Example.prototype, "property", [Object]);
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.setParamTypes(Example, "staticMethod", [Object]);
|
||||||
|
*
|
||||||
|
* // method (on prototype)
|
||||||
|
* result = Metadata.setParamTypes(Example.prototype, "method", [Object]);
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static setParamTypes(target: any, propertyKey: PropertyKeyType, value: any): void {
|
||||||
|
return this.set(DESIGN_PARAM_TYPES, value, target.prototype, propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all metadata for a metadataKey.
|
||||||
|
* @param metadataKey
|
||||||
|
*/
|
||||||
|
public static getTargetsFromPropertyKey = (metadataKey: MetadataKeyType): any[] =>
|
||||||
|
PROPERTIES.has(metadataKey) ? PROPERTIES.get(metadataKey) || [] : []
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define a unique metadata entry on the target.
|
||||||
|
* @param key A key used to store and retrieve metadata.
|
||||||
|
* @param value A value that contains attached metadata.
|
||||||
|
* @param target The target object on which to define metadata.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // constructor
|
||||||
|
* Reflect.defineMetadata("custom:annotation", options, Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* Reflect.defineMetadata("custom:annotation", Number, Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // property (on prototype)
|
||||||
|
* Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "property");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* Reflect.defineMetadata("custom:annotation", Number, Example, "staticMethod");
|
||||||
|
*
|
||||||
|
* // method (on prototype)
|
||||||
|
* Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "method");
|
||||||
|
*
|
||||||
|
* // decorator factory as metadata-producing annotation.
|
||||||
|
* function MyAnnotation(options): PropertyDecorator {
|
||||||
|
* return (target, key) => Reflect.defineMetadata("custom:annotation", options, target, key);
|
||||||
|
* }
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static set(key: MetadataKeyType, value: any, target: any, propertyKey?: PropertyKeyType): void;
|
||||||
|
public static set(key: MetadataKeyType, value: any, target: any, propertyKey: PropertyKeyType): void {
|
||||||
|
|
||||||
|
const targets: any[] = PROPERTIES.has(key) ? PROPERTIES.get(key) || [] : [];
|
||||||
|
const classConstructor = TypeUtil.getType(target);
|
||||||
|
|
||||||
|
if (targets.indexOf(classConstructor) === -1) {
|
||||||
|
targets.push(classConstructor);
|
||||||
|
PROPERTIES.set(key, targets);
|
||||||
|
}
|
||||||
|
|
||||||
|
Reflect.defineMetadata(key, value, TypeUtil.getType(target), propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the metadata value for the provided metadata DESIGN_PARAM_TYPES on the target object or its prototype chain.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns The metadata value for the metadata key if found; otherwise, `undefined`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // on contructor
|
||||||
|
* result = Metadata.getParamTypes(Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.getParamTypes(Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.getParamTypes(Example, "staticMethod");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static getParamTypes(target: any, propertyKey?: PropertyKeyType): any[];
|
||||||
|
public static getParamTypes(target: any, propertyKey: PropertyKeyType): any[] {
|
||||||
|
return Reflect.getMetadata(DESIGN_PARAM_TYPES, target, propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the metadata value for the provided metadata DESIGN_PARAM_TYPES on the target object or its prototype chain.
|
||||||
|
* @param target The target object on which the metadata is defined.
|
||||||
|
* @param propertyKey The property key for the target.
|
||||||
|
* @returns The metadata value for the metadata key if found; otherwise, `undefined`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* ```typescript
|
||||||
|
* class Example {
|
||||||
|
* // property declarations are not part of ES6, though they are valid in TypeScript:
|
||||||
|
* // static staticProperty;
|
||||||
|
* // property;
|
||||||
|
*
|
||||||
|
* static staticMethod(p) { }
|
||||||
|
* method(p) { }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // on contructor
|
||||||
|
* result = Metadata.getParamTypes(Example);
|
||||||
|
*
|
||||||
|
* // property (on constructor)
|
||||||
|
* result = Metadata.getParamTypes(Example, "staticProperty");
|
||||||
|
*
|
||||||
|
* // method (on constructor)
|
||||||
|
* result = Metadata.getParamTypes(Example, "staticMethod");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static getOwnParamTypes(target: any, propertyKey?: PropertyKeyType): any[];
|
||||||
|
public static getOwnParamTypes(target: any, propertyKey: PropertyKeyType): any[] {
|
||||||
|
return Reflect.getOwnMetadata(DESIGN_PARAM_TYPES, target, propertyKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metadata key
|
||||||
|
* @private
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
const DESIGN_PARAM_TYPES = 'design:paramtypes';
|
||||||
|
/**
|
||||||
|
* Metadata key
|
||||||
|
* @private
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
const DESIGN_TYPE = 'design:type';
|
||||||
|
/**
|
||||||
|
* Metadata key
|
||||||
|
* @private
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
const DESIGN_RETURN_TYPE = 'design:returntype';
|
||||||
|
/**
|
||||||
|
* Properties collections
|
||||||
|
* @private
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
const PROPERTIES: Map<MetadataKeyType, any[]> = new Map<MetadataKeyType, any[]>();
|
29
src/reflect/Method.ts
Normal file
29
src/reflect/Method.ts
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import {
|
||||||
|
PropertyKeyType,
|
||||||
|
} from '../core';
|
||||||
|
|
||||||
|
import {
|
||||||
|
TypeUtil,
|
||||||
|
} from '../util/TypeUtil';
|
||||||
|
|
||||||
|
import { Class } from './Class';
|
||||||
|
import { Executable } from './Executable';
|
||||||
|
|
||||||
|
export class Method extends Executable {
|
||||||
|
private _returnType: any;
|
||||||
|
private _rawMethod: Function;
|
||||||
|
|
||||||
|
public constructor(declaringClazz: Class, name: PropertyKeyType, parameterTypes: any[], returnType: any) {
|
||||||
|
super(declaringClazz, name, parameterTypes);
|
||||||
|
this._returnType = returnType;
|
||||||
|
this._rawMethod = TypeUtil.getPrototype(declaringClazz.getType())[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
public getReturnType(): any {
|
||||||
|
return this._returnType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public invoke(instance: Object, ...args: any[]): any {
|
||||||
|
return this._rawMethod.apply(instance, args);
|
||||||
|
}
|
||||||
|
}
|
33
src/reflect/Parameter.ts
Normal file
33
src/reflect/Parameter.ts
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import { AccessibleObject } from './AccessibleObject';
|
||||||
|
import { Executable } from './Executable';
|
||||||
|
|
||||||
|
export class Parameter extends AccessibleObject {
|
||||||
|
private _executable: Executable;
|
||||||
|
private _type: any;
|
||||||
|
private _index: number;
|
||||||
|
private _name: string;
|
||||||
|
|
||||||
|
public constructor(executable: Executable, parameterType: any, name: string, index: number) {
|
||||||
|
super();
|
||||||
|
this._executable = executable;
|
||||||
|
this._type = parameterType;
|
||||||
|
this._name = name;
|
||||||
|
this._index = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getDeclaringExecutable(): Executable {
|
||||||
|
return this._executable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getType(): any {
|
||||||
|
return this._type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getName(): string {
|
||||||
|
return this._name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getIndex(): number {
|
||||||
|
return this._index;
|
||||||
|
}
|
||||||
|
}
|
12
src/reflect/index.ts
Normal file
12
src/reflect/index.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
export * from './AccessibleObject';
|
||||||
|
export * from './AnnotatedElement';
|
||||||
|
export * from './Annotation';
|
||||||
|
export * from './Class';
|
||||||
|
export * from './Constructor';
|
||||||
|
|
||||||
|
export * from './Executable';
|
||||||
|
export * from './Field';
|
||||||
|
export * from './Member';
|
||||||
|
export * from './Metadata';
|
||||||
|
export * from './Method';
|
||||||
|
export * from './Parameter';
|
38
src/util/AnnotationUtil.ts
Normal file
38
src/util/AnnotationUtil.ts
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import { Type } from '../core';
|
||||||
|
import { Annotation, Class } from '../reflect';
|
||||||
|
|
||||||
|
export abstract class AnnotationUtils {
|
||||||
|
public static hasAnnotation<T extends Annotation>(type: Type<any>, annotationClass: Type<T>): boolean {
|
||||||
|
const annotation = AnnotationUtils.getAnnotation(type, annotationClass);
|
||||||
|
if (undefined !== annotation) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getAnnotation<T extends Annotation>(type: Type<any>, annotationClass: Type<T>): T | undefined {
|
||||||
|
const clazz = Class.forType(type);
|
||||||
|
if (undefined === clazz) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const annotations = clazz.getAnnotations();
|
||||||
|
if (0 === annotations.size) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const annonClassType of Array.from(annotations.keys())) {
|
||||||
|
if (annonClassType === annotationClass) {
|
||||||
|
return annotations.get(annonClassType) as T;
|
||||||
|
}
|
||||||
|
const annotation = AnnotationUtils.getAnnotation(annonClassType, annotationClass);
|
||||||
|
if (undefined !== annotation) {
|
||||||
|
return annotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
68
src/util/Registry.ts
Normal file
68
src/util/Registry.ts
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
export abstract class Registry<K, V> {
|
||||||
|
private _parent: Registry<K, V> | undefined;
|
||||||
|
private _map: Map<K, V>;
|
||||||
|
|
||||||
|
protected constructor(parent?: Registry<K, V>) {
|
||||||
|
this._parent = parent;
|
||||||
|
this._map = new Map<K, V>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public get parent(): Registry<K, V> | undefined {
|
||||||
|
return this._parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get size(): number {
|
||||||
|
return this._map.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get(key: K): V | undefined {
|
||||||
|
let v = this._map.get(key);
|
||||||
|
if (undefined === v && undefined !== this._parent) {
|
||||||
|
v = this._parent.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public has(key: K): boolean {
|
||||||
|
let exist = this._map.has(key);
|
||||||
|
if (!exist && undefined !== this._parent) {
|
||||||
|
exist = this._parent.has(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return exist;
|
||||||
|
}
|
||||||
|
|
||||||
|
public set(key: K, value: V): void {
|
||||||
|
this._map.set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public entries(): IterableIterator<[K, V]> {
|
||||||
|
return this._map.entries();
|
||||||
|
}
|
||||||
|
|
||||||
|
public keys(): IterableIterator<K> {
|
||||||
|
return this._map.keys();
|
||||||
|
}
|
||||||
|
|
||||||
|
public values(): IterableIterator<V> {
|
||||||
|
return this._map.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
public clear(): void {
|
||||||
|
this._map.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public delete(key: K): boolean {
|
||||||
|
let result = this._map.delete(key);
|
||||||
|
if (!result && undefined !== this._parent) {
|
||||||
|
result = this._parent.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public forEach(callback: (vlaue: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
|
||||||
|
this._map.forEach(callback, thisArg);
|
||||||
|
}
|
||||||
|
}
|
414
src/util/TypeUtil.ts
Normal file
414
src/util/TypeUtil.ts
Normal file
|
@ -0,0 +1,414 @@
|
||||||
|
import {
|
||||||
|
PrimitiveType,
|
||||||
|
PropertyKeyType,
|
||||||
|
Type,
|
||||||
|
} from '../core';
|
||||||
|
|
||||||
|
export class TypeUtil {
|
||||||
|
/**
|
||||||
|
* Get the provide constructor.
|
||||||
|
* @param target
|
||||||
|
*/
|
||||||
|
public static getContructor<T>(target: any): Type<T> {
|
||||||
|
return typeof target === 'function' ? target : target.constructor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the provide constructor if target is an instance.
|
||||||
|
* @param target
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
public static getType<T>(target: any): Type<T> {
|
||||||
|
return target.prototype ? target : target.constructor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the provide prototype if target is an instance.
|
||||||
|
* @param target
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
public static getPrototype(target: any): Object {
|
||||||
|
return typeof target === 'function' ? target.prototype : target;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {symbol}
|
||||||
|
*/
|
||||||
|
public static getTypeOrSymbol(target: any): any {
|
||||||
|
return typeof target === 'symbol' ? target : TypeUtil.getType(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the given obj is a primitive.
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isPrimitiveOrPrimitiveType(target: any): boolean {
|
||||||
|
return TypeUtil.isString(target)
|
||||||
|
|| TypeUtil.isNumber(target)
|
||||||
|
|| TypeUtil.isBoolean(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {PrimitiveType}
|
||||||
|
*/
|
||||||
|
public static primitiveOf(target: any): PrimitiveType {
|
||||||
|
if (TypeUtil.isString(target)) {
|
||||||
|
return PrimitiveType.STRING;
|
||||||
|
}
|
||||||
|
if (TypeUtil.isNumber(target)) {
|
||||||
|
return PrimitiveType.NUMBER;
|
||||||
|
}
|
||||||
|
if (TypeUtil.isBoolean(target)) {
|
||||||
|
return PrimitiveType.BOOLEAN;
|
||||||
|
}
|
||||||
|
|
||||||
|
return PrimitiveType.ANY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isString(target: any): boolean {
|
||||||
|
return typeof target === 'string' || target instanceof String || target === String;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isNumber(target: any): boolean {
|
||||||
|
return typeof target === 'number' || target instanceof Number || target === Number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isBoolean(target: any): boolean {
|
||||||
|
return typeof target === 'boolean' || target instanceof Boolean || target === Boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {Boolean}
|
||||||
|
*/
|
||||||
|
public static isArray(target: any): boolean {
|
||||||
|
return Array.isArray(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the clazz is an array.
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isArrayOrArrayType(target: any): boolean {
|
||||||
|
if (target === Array) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TypeUtil.isArray(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the target.
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isCollection(target: any): boolean {
|
||||||
|
return TypeUtil.isArrayOrArrayType(target)
|
||||||
|
|| target === Map
|
||||||
|
|| target instanceof Map
|
||||||
|
|| target === Set
|
||||||
|
|| target instanceof Set
|
||||||
|
|| target === WeakMap
|
||||||
|
|| target instanceof WeakMap
|
||||||
|
|| target === WeakSet
|
||||||
|
|| target instanceof WeakSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isDate(target: any): boolean {
|
||||||
|
return target === Date || target instanceof Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isMethod(target: any, propertyKey: PropertyKeyType): boolean {
|
||||||
|
if (typeof(target[propertyKey]) === undefined) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return typeof target[propertyKey] === 'function';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isObject(target: any): boolean {
|
||||||
|
return target === Object;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isType(target: any): boolean {
|
||||||
|
return !TypeUtil.isPrimitiveOrPrimitiveType(target)
|
||||||
|
&& !TypeUtil.isObject(target)
|
||||||
|
&& !TypeUtil.isDate(target)
|
||||||
|
&& target !== undefined
|
||||||
|
&& !TypeUtil.isPromise(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the value is an empty string, null or undefined.
|
||||||
|
* @param value
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isEmpty(value: any): boolean {
|
||||||
|
return value === '' || value === null || value === undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get object name
|
||||||
|
*/
|
||||||
|
public static nameOf(obj: any): string {
|
||||||
|
switch (typeof obj) {
|
||||||
|
default:
|
||||||
|
return '' + obj;
|
||||||
|
case 'symbol':
|
||||||
|
return TypeUtil.nameOfSymbol(obj);
|
||||||
|
case 'function':
|
||||||
|
return TypeUtil.nameOfType(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the provide name.
|
||||||
|
* @param target
|
||||||
|
*/
|
||||||
|
public static nameOfType(target: any): string {
|
||||||
|
return typeof target === 'function'
|
||||||
|
? target.name
|
||||||
|
: target.constructor.name;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get symbol name.
|
||||||
|
* @param sym
|
||||||
|
*/
|
||||||
|
public static nameOfSymbol(sym: symbol): string {
|
||||||
|
return sym.toString().replace('Symbol(', '').replace(')', '');
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param out
|
||||||
|
* @param obj
|
||||||
|
* @param {{[p: string]: (collection: any[], value: any) => any}} reducers
|
||||||
|
* @returns {any}
|
||||||
|
*/
|
||||||
|
public static deepExtends(out: any, obj: any, reducers: { [key: string]: (collection: any[], value: any) => any } = {}): any {
|
||||||
|
|
||||||
|
if (obj === undefined || obj === null) {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TypeUtil.isPrimitiveOrPrimitiveType(obj) || typeof obj === 'symbol' || typeof obj === 'function') {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TypeUtil.isArrayOrArrayType(obj)) {
|
||||||
|
out = out || [];
|
||||||
|
} else {
|
||||||
|
out = out || {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultReducer = reducers.default ? reducers.default : (collection: any[], value: any) => {
|
||||||
|
collection.push(value);
|
||||||
|
|
||||||
|
return collection;
|
||||||
|
};
|
||||||
|
const set = (key: string | number, value: any) => {
|
||||||
|
if (TypeUtil.isArrayOrArrayType(obj)) {
|
||||||
|
out.push(value);
|
||||||
|
} else {
|
||||||
|
out[key] = value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.keys(obj).forEach(key => {
|
||||||
|
let value = obj[key];
|
||||||
|
|
||||||
|
if (value === undefined || value === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value === '' && out[key] !== '') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TypeUtil.isPrimitiveOrPrimitiveType(value) || typeof value === 'function') {
|
||||||
|
set(key, value);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TypeUtil.isArrayOrArrayType(value)) {
|
||||||
|
|
||||||
|
value = value.map((v: any) => TypeUtil.deepExtends(undefined, v));
|
||||||
|
|
||||||
|
set(key, []
|
||||||
|
.concat(out[key] || [], value)
|
||||||
|
.reduce((collection: any[], v: any) =>
|
||||||
|
reducers[key] ? reducers[key](collection, v) : defaultReducer(collection, v),
|
||||||
|
[]));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Object
|
||||||
|
if (TypeUtil.isArrayOrArrayType(obj)) {
|
||||||
|
set(key, TypeUtil.deepExtends(undefined, value, reducers));
|
||||||
|
} else {
|
||||||
|
set(key, TypeUtil.deepExtends(out[key], value, reducers));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (TypeUtil.isArrayOrArrayType(out)) {
|
||||||
|
out.reduce((collection: any[], value: any) => defaultReducer(collection, value), []);
|
||||||
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public static isPromise(target: any): boolean {
|
||||||
|
return target === Promise || target instanceof Promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {any}
|
||||||
|
*/
|
||||||
|
public static getInheritedType(target: Type<any>): Type<any> {
|
||||||
|
return Object.getPrototypeOf(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @param {PropertyKeyType} propertyKey
|
||||||
|
* @returns {PropertyDescriptor}
|
||||||
|
*/
|
||||||
|
public static descriptorOf(target: any, propertyKey: PropertyKeyType): PropertyDescriptor | undefined {
|
||||||
|
return Object.getOwnPropertyDescriptor(target && target.prototype || target, propertyKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @param {PropertyKeyType} propertyKey
|
||||||
|
* @returns {string[]}
|
||||||
|
*/
|
||||||
|
public static getParameterNames(target: any, propertyKey: PropertyKeyType): string[] {
|
||||||
|
const rawType = TypeUtil.getPrototype(target);
|
||||||
|
const fn: Function = rawType[propertyKey];
|
||||||
|
|
||||||
|
const code = fn.toString()
|
||||||
|
.replace(COMMENTS, '')
|
||||||
|
.replace(FAT_ARROWS, '')
|
||||||
|
.replace(DEFAULT_PARAMS, '');
|
||||||
|
|
||||||
|
const result = code.slice(code.indexOf('(') + 1, code.indexOf(')')).match(/([^\s,]+)/g);
|
||||||
|
|
||||||
|
return result === null
|
||||||
|
? []
|
||||||
|
: result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @returns {Array}
|
||||||
|
*/
|
||||||
|
public static ancestorsOf(target: Type<any>): Type<any>[] {
|
||||||
|
const classes: Type<any>[] = [];
|
||||||
|
|
||||||
|
let currentTarget = TypeUtil.getType(target);
|
||||||
|
|
||||||
|
while (TypeUtil.nameOf(currentTarget) !== '') {
|
||||||
|
classes.unshift(currentTarget);
|
||||||
|
currentTarget = TypeUtil.getInheritedType(currentTarget);
|
||||||
|
}
|
||||||
|
|
||||||
|
return classes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @param {string} name
|
||||||
|
* @param {Function} callback
|
||||||
|
*/
|
||||||
|
public static applyBefore(target: any, name: string, callback: Function): void {
|
||||||
|
const original = target[name];
|
||||||
|
target[name] = function (...args: any[]): any {
|
||||||
|
callback(...args);
|
||||||
|
|
||||||
|
return original.apply(target, args);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Promise<any>} promise
|
||||||
|
* @param {number} time
|
||||||
|
* @returns {Promise<any>}
|
||||||
|
*/
|
||||||
|
public static promiseTimeout(promise: Promise<any>, time = 1000): Promise<{ ok: boolean, response: any }> {
|
||||||
|
const timeout = (p: Promise<any>, t: number) => new Promise((resolve) => {
|
||||||
|
p.then((response) => {
|
||||||
|
resolve();
|
||||||
|
|
||||||
|
return response;
|
||||||
|
});
|
||||||
|
setTimeout(() => resolve({ ok: false }), t);
|
||||||
|
});
|
||||||
|
|
||||||
|
promise = promise.then((response) => ({ ok: true, response }));
|
||||||
|
|
||||||
|
return Promise.race([
|
||||||
|
promise,
|
||||||
|
timeout(promise, time),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
|
||||||
|
const DEFAULT_PARAMS = /=[^,]+/mg;
|
||||||
|
const FAT_ARROWS = /=>.*$/mg;
|
3
src/util/index.ts
Normal file
3
src/util/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export * from './AnnotationUtil';
|
||||||
|
export * from './Registry';
|
||||||
|
export * from './TypeUtil';
|
36
tsconfig.json
Normal file
36
tsconfig.json
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"module": "esnext",
|
||||||
|
"target": "es5",
|
||||||
|
"types": [
|
||||||
|
"reflect-metadata",
|
||||||
|
],
|
||||||
|
"lib": [
|
||||||
|
"dom",
|
||||||
|
"es2018"
|
||||||
|
],
|
||||||
|
"strict": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"suppressImplicitAnyIndexErrors": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"sourceMap": true,
|
||||||
|
"outDir": "dist/esm5",
|
||||||
|
"declaration": true,
|
||||||
|
"declarationDir": "dist/types",
|
||||||
|
"declarationMap": true,
|
||||||
|
"stripInternal": true,
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"importHelpers": true
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"./src"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"node_modules",
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"compileOnSave": false,
|
||||||
|
"buildOnSave": false
|
||||||
|
}
|
79
tslint.json
Normal file
79
tslint.json
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
{
|
||||||
|
"extends": [
|
||||||
|
"tslint-config-standard"
|
||||||
|
],
|
||||||
|
"rules": {
|
||||||
|
// core ts-lint rules
|
||||||
|
"await-promise": true,
|
||||||
|
"no-unused-variable": true,
|
||||||
|
"forin": true,
|
||||||
|
"no-bitwise": true,
|
||||||
|
"no-console": [
|
||||||
|
true,
|
||||||
|
"debug",
|
||||||
|
"info",
|
||||||
|
"time",
|
||||||
|
"timeEnd",
|
||||||
|
"trace"
|
||||||
|
],
|
||||||
|
"no-construct": true,
|
||||||
|
"no-debugger": true,
|
||||||
|
"no-shadowed-variable": true,
|
||||||
|
"no-string-literal": true,
|
||||||
|
"no-inferrable-types": [
|
||||||
|
true
|
||||||
|
],
|
||||||
|
"no-unnecessary-initializer": true,
|
||||||
|
"no-magic-numbers": false,
|
||||||
|
"no-require-imports": false,
|
||||||
|
"no-duplicate-super": true,
|
||||||
|
"no-boolean-literal-compare": true,
|
||||||
|
"no-namespace": [
|
||||||
|
true,
|
||||||
|
"allow-declarations"
|
||||||
|
],
|
||||||
|
"no-invalid-this": [
|
||||||
|
true,
|
||||||
|
"check-function-in-method"
|
||||||
|
],
|
||||||
|
"ordered-imports": [
|
||||||
|
true
|
||||||
|
],
|
||||||
|
"interface-name": [
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"newline-before-return": true,
|
||||||
|
"object-literal-shorthand": true,
|
||||||
|
"arrow-return-shorthand": [
|
||||||
|
true
|
||||||
|
],
|
||||||
|
"unified-signatures": true,
|
||||||
|
"prefer-for-of": false,
|
||||||
|
"match-default-export-name": true,
|
||||||
|
"prefer-const": true,
|
||||||
|
"semicolon": [
|
||||||
|
true,
|
||||||
|
"always"
|
||||||
|
],
|
||||||
|
"trailing-comma": [
|
||||||
|
true,
|
||||||
|
{
|
||||||
|
"multiline": {
|
||||||
|
"objects": "always",
|
||||||
|
"arrays": "always",
|
||||||
|
"functions": "always",
|
||||||
|
"typeLiterals": "ignore"
|
||||||
|
},
|
||||||
|
"esSpecCompliant": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"space-before-function-paren": [
|
||||||
|
true,
|
||||||
|
{
|
||||||
|
"anonymous": "always",
|
||||||
|
"named": "never",
|
||||||
|
"asyncArrow": "always"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user