Skip to content

Commit bed9952

Browse files
Patil, OmkarPatil, Omkar
Patil, Omkar
authored and
Patil, Omkar
committed
Better tooling and tsd integration
1 parent 52f50d2 commit bed9952

File tree

11 files changed

+259
-137
lines changed

11 files changed

+259
-137
lines changed

README.md

+56-13
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@
44

55
I'm a [Yeoman](http://yeoman.io) generator that allows you to create NodeJS modules using TypeScript. I let you quickly setup a project with latest available tools and best practices.
66
Tools and libraries I use -
7-
- tsc - for compiling TypeScript.
8-
- tsconfig.json - for providing compiler options.
9-
- tslint - for linting TypeScript source files.
10-
- [gulp](http://gulpjs.com/) - as a build system. You can carry out above tasks through gulp.
11-
- [Jasmine 2](http://jasmine.github.io/2.3/introduction.html) - for writing tests.
7+
- _tsc_ - for compiling TypeScript.
8+
- _tsd_ - for TypeScript definition files management.
9+
- _tsconfig.json_ - for providing compiler options.
10+
- _tslint_ - for linting TypeScript source files.
11+
- [_gulp_](http://gulpjs.com/) - as a build system. You can carry out above tasks through gulp.
12+
- [_Jasmine 2_](http://jasmine.github.io/2.3/introduction.html) - for writing tests.
1213

1314
>You want to know if you can change any of these? Of course, why not? It is your module after all. I simply get down to business of generating, no questions asked. Once done, I get out of the way as if I were not there at all and you can do as you please!
1415
1516
## Usage
1617

17-
Install `generator-node-typescript` globally.
18+
Install `typescript tsd tslint generator-node-typescript` globally.
1819

1920
```
20-
npm install -g generator-node-typescript
21+
npm install -g typescript tsd tslint generator-node-typescript
2122
```
2223

23-
>I don't need `typescript-tools` and `tsd` but these are great tools to have for working with TypeScript in editors like sublime and atom and I heartily recommend them.
24-
>Though I install `typescript` and `tslint` locally to avoid global dependencies, installing them globally wouldn't be a bad idea at all.
25-
26-
Make a new directory and `cd` into it.
24+
Create a new directory and `cd` into it.
2725

2826
```
2927
mkdir my-new-project && cd $_
@@ -36,10 +34,55 @@ Run `gulp --help` for information on available tasks.
3634
```
3735
gulp --help
3836
```
37+
Here is the list of tasks available out of the box -
38+
```sh
39+
_build INTERNAL TASK - Compiles all TypeScript source files
40+
build Compiles all TypeScript source files and updates module references
41+
clean Cleans the generated js files from lib directory
42+
gen_tsrefs Generates the app.d.ts references file dynamically for all application *.ts files
43+
help Display this help text.
44+
test Runs the Jasmine test specs [build]
45+
tsconfig_files Update files section in tsconfig.json
46+
tslint Lints all TypeScript source files
47+
```
3948

40-
## Quick notes
49+
## Additional notes
4150
- I provide nice integration with [VSCode editor](https://code.visualstudio.com/). I configure the `gulp build` task as the default VSCode build task.
42-
- The `gulp build` task also updates _tsconfig/files_ section using _tsconfig/filesGlob_ before build so you don't have to do it manually.
51+
- The `gulp build` task also updates _tsconfig/files_ section using _tsconfig/filesGlob_ and generates _typings/app.d.ts_ references file dynamically
52+
for all the application *.ts files before build so you don't have to do it manually.
53+
54+
## Development tips
55+
- The _tsd_ tool dynamically updates _typings/tsd.d.ts_ file for all type definitions you install through it. Also, the gulp *gen_tsrefs* task dynamically
56+
updates _typings/app.d.ts_ references file for all application *.ts. Therefore you just need to add references to these two files in any _.ts_ file and you
57+
are good to go with *tsc* compiler.
58+
- To keep on using the typical CommonJS exports and imports use the `export` and `inport` systax mentioned in the example below.
59+
60+
```ts
61+
class Greeter {
62+
greeting: string;
63+
constructor(message: string) {
64+
this.greeting = message;
65+
}
66+
greet() {
67+
return 'Bonjour, ' + this.greeting + '!';
68+
}
69+
}
70+
//The following line is important, export like this allows you to use the old-style CommonJS export/import.
71+
export = Greeter;
72+
73+
```
74+
This is how the above class can be used in some other file.
75+
76+
```ts
77+
/// <reference path="../typings/tsd.d.ts" />
78+
/// <reference path="../typings/app.d.ts" />
79+
80+
import Greeter = require('./greeter');
81+
82+
const greeter = new Greeter('friend');
83+
greeter.greet();
84+
85+
```
4386

4487
## License
4588

generators/app/index.js

+27-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22
var yeoman = require('yeoman-generator');
3-
var chalk = require('chalk');
4-
var yosay = require('yosay');
5-
var path = require('path');
6-
var _ = require('lodash');
3+
var chalk = require('chalk');
4+
var yosay = require('yosay');
5+
var path = require('path');
6+
var _ = require('lodash');
77

88
module.exports = yeoman.generators.Base.extend({
99
initializing: function () {
@@ -12,15 +12,15 @@ module.exports = yeoman.generators.Base.extend({
1212
// Have Yeoman greet the user.
1313
this.log(yosay(
1414
'Welcome to the amazing ' + chalk.red('Node TypeScript') + ' generator!'
15-
));
15+
));
1616

1717
this.log(
1818
chalk.cyan('I simply get down to business of generating, no questions asked!')
1919
+ '\n'
20-
+ chalk.yellow('Libraries you ask? I use gulp, jasmine, tslint and I compile TypeScript to CommonJS using tsconfig.json.')
20+
+ chalk.yellow('Libraries you ask? I use gulp, jasmine, tslint and I compile TypeScript to CommonJS modules using tsconfig.json.')
2121
+ '\n'
2222
+ chalk.gray('Can you change these? Of course, it\'s your library. I get out of the way after scaffolding.')
23-
);
23+
);
2424

2525
done();
2626
},
@@ -30,43 +30,52 @@ module.exports = yeoman.generators.Base.extend({
3030
this.directory('src', 'src');
3131
this.directory('test', 'test');
3232
this.directory('_vscode', '.vscode');
33+
this.directory('typings', 'typings');
3334
},
3435

3536
app: function () {
3637
this.fs.copyTpl(
3738
this.templatePath('_package.json'),
3839
this.destinationPath('package.json'),
39-
{appname: _.kebabCase(path.basename(process.cwd()))}
40-
);
40+
{ appname: _.kebabCase(path.basename(process.cwd())) }
41+
);
4142
},
4243

4344
projectfiles: function () {
4445
this.fs.copy(
4546
this.templatePath('_gulpfile.js'),
4647
this.destinationPath('gulpfile.js')
47-
);
48+
);
4849
this.fs.copy(
4950
this.templatePath('_tsconfig.json'),
5051
this.destinationPath('tsconfig.json')
51-
);
52+
);
5253
this.fs.copy(
5354
this.templatePath('_tslint.json'),
5455
this.destinationPath('tslint.json')
55-
);
56+
);
5657
this.fs.copy(
5758
this.templatePath('editorconfig'),
5859
this.destinationPath('.editorconfig')
59-
);
60+
);
6061
this.fs.copy(
6162
this.templatePath('gitignore'),
6263
this.destinationPath('.gitignore')
63-
);
64+
);
65+
this.fs.copy(
66+
this.templatePath('jshintrc'),
67+
this.destinationPath('.jshintrc')
68+
);
6469
}
6570
},
6671

67-
install: function () {
68-
this.npmInstall(null, {
69-
skipInstall: this.options['skip-install']
70-
});
72+
install: {
73+
npmInstall: function () {
74+
var generator = this;
75+
generator.npmInstall(null, { skipInstall: this.options['skip-install'] }, function () {
76+
generator.spawnCommandSync('tsd', ['init']); //tsd init
77+
generator.spawnCommandSync('tsd', ['install', 'node', '--save']) //tsd install --save node
78+
});
79+
}
7180
}
7281
});

generators/app/templates/_gulpfile.js

+44-20
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,62 @@
1-
var gulp = require('gulp');
2-
var tslint = require('gulp-tslint');
3-
var exec = require('child_process').exec;
1+
var gulp = require('gulp');
2+
var tslint = require('gulp-tslint');
3+
var exec = require('child_process').exec;
44
var jasmine = require('gulp-jasmine');
5-
var gulp = require('gulp-help')(gulp);
5+
var gulp = require('gulp-help')(gulp);
66
var tsconfig = require('gulp-tsconfig-files');
7-
var dotbin = require('dotbin');
7+
var path = require('path');
8+
var inject = require('gulp-inject');
9+
var gulpSequence = require('gulp-sequence');
10+
var del = require('del');
811

9-
var tsFilesGlob = (function(c) {
12+
var typeDefsPath = (function (tsd) {
13+
return tsd.path || 'typings';
14+
})(require('./tsd.json'));
15+
16+
var tsFilesGlob = (function (c) {
1017
return c.filesGlob || c.files || '**/*.ts';
1118
})(require('./tsconfig.json'));
1219

13-
gulp.task('tslint', 'Lints all TypeScript source files', function(){
20+
gulp.task('tsconfig_files', 'Update files section in tsconfig.json', function () {
21+
gulp.src(tsFilesGlob).pipe(tsconfig());
22+
});
23+
24+
gulp.task('gen_tsrefs', 'Generates the app.d.ts references file dynamically for all application *.ts files', function () {
25+
var target = gulp.src(path.join('.', typeDefsPath, 'app.d.ts'));
26+
var sources = gulp.src([path.join('.', 'src', '**', '*.ts')], { read: false });
27+
return target.pipe(inject(sources, {
28+
starttag: '//{',
29+
endtag: '//}',
30+
transform: function (filepath) {
31+
return '/// <reference path="..' + filepath + '" />';
32+
}
33+
})).pipe(gulp.dest(path.join('.', typeDefsPath)));
34+
});
35+
36+
gulp.task('clean', 'Cleans the generated js files from lib directory', function () {
37+
return del([
38+
'lib/**/*'
39+
]);
40+
});
41+
42+
gulp.task('tslint', 'Lints all TypeScript source files', function () {
1443
return gulp.src(tsFilesGlob)
15-
.pipe(tslint())
16-
.pipe(tslint.report('verbose'));
44+
.pipe(tslint())
45+
.pipe(tslint.report('verbose'));
1746
});
1847

19-
gulp.task('build', 'Compiles all TypeScript source files', ['tsconfig_files'], function (cb) {
48+
gulp.task('_build', 'INTERNAL TASK - Compiles all TypeScript source files', function (cb) {
2049
exec('tsc', function (err, stdout, stderr) {
2150
console.log(stdout);
2251
console.log(stderr);
2352
cb(err);
2453
});
2554
});
2655

27-
gulp.task('test', 'Runs the Jasmine test specs', ['build'], function () {
28-
return gulp.src('test/*.js')
29-
.pipe(jasmine());
30-
});
56+
//run tslint task, then run _tsconfig_files and _gen_tsrefs in parallel, then run _build
57+
gulp.task('build', 'Compiles all TypeScript source files and updates module references', gulpSequence('tslint', ['tsconfig_files', 'gen_tsrefs'], '_build'));
3158

32-
gulp.task('tsconfig_files', 'Update files section in tsconfig.json', function () {
33-
var src = require('./tsconfig.json').filesGlob;
34-
if (typeof src !== 'undefined') {
35-
gulp.src(src).pipe(tsconfig());
36-
}
59+
gulp.task('test', 'Runs the Jasmine test specs', ['build'], function () {
60+
return gulp.src('test/*.js')
61+
.pipe(jasmine());
3762
});
38-

generators/app/templates/_package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
},
1717
"dependencies": {},
1818
"devDependencies": {
19+
"del": "2.0.2",
1920
"gulp": "^3.9.0",
20-
"gulp-tslint": "^3.1",
2121
"gulp-help": "^1.6.0",
22-
"tslint": "2.*",
22+
"gulp-inject": "3.0.0",
2323
"gulp-jasmine": "^2.0.1",
24+
"gulp-sequence": "0.4.1",
2425
"gulp-tsconfig-files": "0.0.2",
25-
"typescript": "^1.5",
26-
"dotbin": "^1.0.1",
26+
"gulp-tslint": "^3.1",
2727
"jasmine": "^2.3.1"
2828
},
2929
"engines": {
30-
"node": ">=0.10.0"
30+
"node": ">=0.12.0"
3131
}
3232
}
+15-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"compilerOptions": {
3-
"module": "commonjs",
4-
"noImplicitAny": true,
5-
"removeComments": true,
6-
"preserveConstEnums": true,
7-
"sourceMap": false,
8-
"declaration": true,
9-
"outDir": "./lib"
10-
},
11-
"filesGlob": [
12-
"src/**/*.ts"
13-
],
14-
"files": [
15-
"src/greeter.ts"
16-
]
2+
"compilerOptions": {
3+
"declaration": true,
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"noImplicitAny": true,
7+
"outDir": "./lib",
8+
"preserveConstEnums": true,
9+
"removeComments": true
10+
},
11+
"filesGlob": [
12+
"src/**/*.ts"
13+
],
14+
"files": [
15+
"src/greeter.ts"
16+
]
1717
}

0 commit comments

Comments
 (0)