From 8ad016346b9fb35acdaa21f72134073fd5273e7b Mon Sep 17 00:00:00 2001 From: CarreraPHP Date: Sat, 24 Dec 2016 15:16:16 +0530 Subject: [PATCH] Base files with Typescript Base files with Typescript --- .editorconfig | 12 ++++++++++++ .gitignore | 3 +++ .vscode/settings.json | 3 +++ .vscode/tasks.json | 23 +++++++++++++++++++++++ LICENSE | 21 +++++++++++++++++++++ README.md | 25 +++++++++++++++++++++++++ lib/greeter.d.ts | 5 +++++ lib/greeter.js | 12 ++++++++++++ lib/index.d.ts | 2 ++ lib/index.js | 3 +++ package.json | 42 ++++++++++++++++++++++++++++++++++++++++++ src/greeter.ts | 11 +++++++++++ src/index.ts | 4 ++++ test/greeter-spec.ts | 12 ++++++++++++ tsconfig.json | 18 ++++++++++++++++++ tslint.json | 3 +++ 16 files changed, 199 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 LICENSE create mode 100644 README.md create mode 100644 lib/greeter.d.ts create mode 100644 lib/greeter.js create mode 100644 lib/index.d.ts create mode 100644 lib/index.js create mode 100644 package.json create mode 100644 src/greeter.ts create mode 100644 src/index.ts create mode 100644 test/greeter-spec.ts create mode 100644 tsconfig.json create mode 100644 tslint.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5760be5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# http://editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3cd27af --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +coverage/ +node_modules/ +npm-debug.log diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0053ecd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules/typescript/lib/" +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..49501fa --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,23 @@ +{ + "version": "0.1.0", + "command": "npm", + "isShellCommand": true, + "args": ["run"], + "showOutput": "always", + "tasks": [ + { + "taskName": "build", + "isBuildCommand": true + }, + { + "taskName": "clean" + }, + { + "taskName": "lint" + }, + { + "taskName": "test", + "isTestCommand": true + } + ] +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0d1c38e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 + +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/README.md b/README.md new file mode 100644 index 0000000..3a476a0 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# Using this module in other modules + +Here is a quick example of how this module can be used in other modules. The [TypeScript Module Resolution Logic](https://www.typescriptlang.org/docs/handbook/module-resolution.html) makes it quite easy. The file `src/index.ts` acts as an aggregator of all the functionality in this module. It imports from other files and re-exports to provide a unified interface for this module. The _package.json_ file contains `main` attribute that points to the generated `lib/index.js` file and `typings` attribute that points to the generated `lib/index.d.ts` file. + +> If you are planning to have code in multiple files (which is quite natural for a NodeJS module) that users can import, make sure you update `src/index.ts` file appropriately. + +Now assuming you have published this amazing module to _npm_ with the name `my-amazing-lib`, and installed it in the module in which you need it - + +- To use the `Greeter` class in a TypeScript file - + +```ts +import { Greeter } from "my-amazing-lib"; + +const greeter = new Greeter("World!"); +greeter.greet(); +``` + +- To use the `Greeter` class in a JavaScript file - + +```js +const Greeter = require('my-amazing-lib').Greeter; + +const greeter = new Greeter('World!'); +greeter.greet(); +``` diff --git a/lib/greeter.d.ts b/lib/greeter.d.ts new file mode 100644 index 0000000..2dee4d6 --- /dev/null +++ b/lib/greeter.d.ts @@ -0,0 +1,5 @@ +export declare class Greeter { + private greeting; + constructor(message: string); + greet(): string; +} diff --git a/lib/greeter.js b/lib/greeter.js new file mode 100644 index 0000000..5a45f46 --- /dev/null +++ b/lib/greeter.js @@ -0,0 +1,12 @@ +"use strict"; +var Greeter = (function () { + function Greeter(message) { + this.greeting = message; + } + Greeter.prototype.greet = function () { + return "Bonjour, " + this.greeting + "!"; + }; + return Greeter; +}()); +exports.Greeter = Greeter; +; diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..c4cc4b5 --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,2 @@ +import { Greeter } from "./greeter"; +export { Greeter }; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..1ec6f8a --- /dev/null +++ b/lib/index.js @@ -0,0 +1,3 @@ +"use strict"; +var greeter_1 = require("./greeter"); +exports.Greeter = greeter_1.Greeter; diff --git a/package.json b/package.json new file mode 100644 index 0000000..afd26f9 --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "scaffolding-all", + "version": "0.0.0", + "description": "Scaffold your application based on an documentation", + "license": "MIT", + "repository": "", + "files": [ + "lib" + ], + "main": "lib/index.js", + "typings": "lib/index.d.ts", + "scripts": { + "clean": "rimraf lib", + "lint": "tslint --force --format verbose \"src/**/*.ts\"", + "build": "npm run clean && npm run lint && echo Using TypeScript && tsc --version && tsc --pretty", + "test": "npm run build && mocha --compilers ts:ts-node/register --recursive test/**/*-spec.ts", + "watch": "npm run build -- --watch", + "watch:test": "npm run test -- --watch" + }, + "keywords": [ + "scaffold", + "yeoman-generator" + ], + "author": "carreraphp@gmail.com", + "dependencies": {}, + "devDependencies": { + "@types/chai": "^3.0.0", + "@types/json5": "0.0.29", + "@types/mocha": "^2.0.0", + "@types/node": "6.0.31", + "chai": "^3.0.0", + "json5": "^0.5.1", + "mocha": "^3.0.0", + "rimraf": "^2.0.0", + "ts-node": "^1.0.0", + "tslint": "^4.0.0", + "typescript": "^2.0.0" + }, + "engines": { + "node": ">=4.0.0" + } +} diff --git a/src/greeter.ts b/src/greeter.ts new file mode 100644 index 0000000..3f67c61 --- /dev/null +++ b/src/greeter.ts @@ -0,0 +1,11 @@ +export class Greeter { + private greeting: string; + + constructor(message: string) { + this.greeting = message; + } + + public greet() { + return "Bonjour, " + this.greeting + "!"; + } +}; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..ca2e150 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,4 @@ +import { json5 } from "json5"; +import { Greeter } from "./greeter"; + +export { Greeter }; diff --git a/test/greeter-spec.ts b/test/greeter-spec.ts new file mode 100644 index 0000000..b062736 --- /dev/null +++ b/test/greeter-spec.ts @@ -0,0 +1,12 @@ + +import { Greeter } from "../src/greeter"; +import * as chai from "chai"; + +const expect = chai.expect; + +describe("greeter", () => { + it("should greet with message", () => { + const greeter = new Greeter("friend"); + expect(greeter.greet()).to.equal("Bonjour, friend!"); + }); +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..b9e61a5 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "declaration": true, + "module": "commonjs", + "moduleResolution": "node", + "noImplicitAny": true, + "outDir": "./lib", + "preserveConstEnums": true, + "removeComments": true + }, + "include": [ + "src/**/*" + ], + "exclude": [ + "node_modules", + "**/*-spec.ts" + ] +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..dfe4c4a --- /dev/null +++ b/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "tslint:latest" +}