From 2b041eefcedd35bdf061e9504ef3112746ab9fd5 Mon Sep 17 00:00:00 2001 From: Ihor Chulinda Date: Fri, 26 Jan 2018 15:54:38 +0100 Subject: [PATCH] feat(plugin-ts): initial implementation of baset-plugin-ts --- packages/baset-plugin-ts/.npmignore | 5 ++++ packages/baset-plugin-ts/LICENSE | 22 +++++++++++++++ packages/baset-plugin-ts/README.md | 5 ++++ packages/baset-plugin-ts/package.json | 38 ++++++++++++++++++++++++++ packages/baset-plugin-ts/src/index.ts | 38 ++++++++++++++++++++++++++ packages/baset-plugin-ts/tsconfig.json | 28 +++++++++++++++++++ packages/baset-plugin-ts/tslint.json | 8 ++++++ 7 files changed, 144 insertions(+) create mode 100644 packages/baset-plugin-ts/.npmignore create mode 100644 packages/baset-plugin-ts/LICENSE create mode 100644 packages/baset-plugin-ts/README.md create mode 100644 packages/baset-plugin-ts/package.json create mode 100644 packages/baset-plugin-ts/src/index.ts create mode 100644 packages/baset-plugin-ts/tsconfig.json create mode 100644 packages/baset-plugin-ts/tslint.json diff --git a/packages/baset-plugin-ts/.npmignore b/packages/baset-plugin-ts/.npmignore new file mode 100644 index 00000000..d0fdcb37 --- /dev/null +++ b/packages/baset-plugin-ts/.npmignore @@ -0,0 +1,5 @@ +!/bin/**/* +!/dist/**/* +/src/ +tsconfig.json +tslint.json diff --git a/packages/baset-plugin-ts/LICENSE b/packages/baset-plugin-ts/LICENSE new file mode 100644 index 00000000..c8e488bc --- /dev/null +++ b/packages/baset-plugin-ts/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2018 Ihor Chulinda + +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. + diff --git a/packages/baset-plugin-ts/README.md b/packages/baset-plugin-ts/README.md new file mode 100644 index 00000000..37a22e95 --- /dev/null +++ b/packages/baset-plugin-ts/README.md @@ -0,0 +1,5 @@ +# BaseT Core +> TypeScript reader plugin for [BaseT](https://github.com/Igmat/baset) project. + + + diff --git a/packages/baset-plugin-ts/package.json b/packages/baset-plugin-ts/package.json new file mode 100644 index 00000000..686d0d29 --- /dev/null +++ b/packages/baset-plugin-ts/package.json @@ -0,0 +1,38 @@ +{ + "name": "baset-plugin-ts", + "version": "0.4.1", + "description": "TypeScript plugin for BaseT project.", + "keywords": [ + "baset-plugin-ts", + "typescript", + "baseline", + "unit-test", + "test", + "testing", + "e2e-test" + ], + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "npm run tslint && tsc", + "watch": "npm run tslint && tsc -w", + "tslint": "tslint -c tslint.json -p tsconfig.json", + "test": "baset", + "accept": "baset accept", + "doctoc": "doctoc README.md", + "prepublish": "npm run doctoc" + }, + "author": "", + "license": "MIT", + "devDependencies": { + "@types/node": "^9.3.0", + "baset": "^0.4.1", + "doctoc": "^1.3.0", + "tslint": "^5.9.1", + "typescript": "^2.6.2" + }, + "dependencies": { + "typescript": "^2.6.2", + "find-up": "^2.1.0" + } +} diff --git a/packages/baset-plugin-ts/src/index.ts b/packages/baset-plugin-ts/src/index.ts new file mode 100644 index 00000000..173ecdfa --- /dev/null +++ b/packages/baset-plugin-ts/src/index.ts @@ -0,0 +1,38 @@ +import { sync } from 'find-up'; +import * as fs from 'fs'; +import * as path from 'path'; +import { CompilerOptions, transpileModule } from 'typescript'; +import { isPrimitive, promisify } from 'util'; + +const readFile = promisify(fs.readFile); + +export interface ITypeScriptReaderOptions { + config: string | { compilerOptions: CompilerOptions }; +} +export default class TypeScriptReader { + private compilerOptions: CompilerOptions; + constructor(private options?: ITypeScriptReaderOptions) { + const config = (!options) + ? (() => { + const configPath = sync('tsconfig.json'); + if (!configPath) throw new Error("We can't find TS config file for your tests"); + + return configPath; + })() + : options.config; + + this.compilerOptions = (typeof config === 'string') + ? require(path.resolve(config)) + : config; + } + + read = async (filePath: string, spec: string | Promise) => { + if (typeof spec !== 'string' || spec.length) throw new Error('TypeScript plugin have to be first in plugins chain'); + const src = await readFile(filePath, { encoding: 'utf8' }); + + return transpileModule(src, { + compilerOptions: this.compilerOptions, + fileName: filePath, + }).outputText; + } +} diff --git a/packages/baset-plugin-ts/tsconfig.json b/packages/baset-plugin-ts/tsconfig.json new file mode 100644 index 00000000..9af3c65e --- /dev/null +++ b/packages/baset-plugin-ts/tsconfig.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + "target": "es2015", + "module": "commonjs", + "sourceMap": true, + "declaration": true, + "outDir": "dist", + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "noImplicitThis": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "moduleResolution": "node", + "baseUrl": ".", + "paths": { + "*": [ + "node_modules/*", + "src/types/*" + ] + }, + "experimentalDecorators": true, + "emitDecoratorMetadata": true + }, + "include": [ + "src/**/*" + ] +} diff --git a/packages/baset-plugin-ts/tslint.json b/packages/baset-plugin-ts/tslint.json new file mode 100644 index 00000000..7d3f8305 --- /dev/null +++ b/packages/baset-plugin-ts/tslint.json @@ -0,0 +1,8 @@ +{ + "extends": [ + "../../tslint.json" + ], + "rules": { + "no-default-export": false + } +}