Skip to content

Commit 9e056e6

Browse files
committed
CLI support, closes TypeStrong#13
1 parent 5ae0a27 commit 9e056e6

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/typings
88

99
/lib/**/*.js
10+
!/lib/dts-bundle.js
1011

1112
/test/tmp
1213
/test/build

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export interface BundleResult {
206206
// list of files not found that shouldn`t be no included in the bundle.
207207
noIncludeFilesNotFound: string[];
208208
// true if dts-bunlde wrote the result, false otherwise.
209-
emited?: boolean;
209+
emitted?: boolean;
210210
// original options passed to the function.
211211
options: Options;
212212
}

lib/dts-bundle.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env node
2+
3+
var pkg = require('../package');
4+
var program = require('commander');
5+
var dts = require("./index");
6+
var path = require('path');
7+
var os = require('os');
8+
9+
function mapOptions(argObj) {
10+
var result = argObj.configJson ? require(path.resolve(argObj.configJson)) : {};
11+
12+
var optList = [
13+
"main",
14+
"name",
15+
"baseDir",
16+
"out",
17+
//"newline", // Manual
18+
//"indent", // not implemented
19+
"prefix",
20+
"separator",
21+
"externals",
22+
//"exclude", // not implemented
23+
"removeSource",
24+
"verbose",
25+
"referenceExternals",
26+
"emitOnIncludedFileNotFound",
27+
"emitOnNoIncludedFileNotFound"
28+
];
29+
30+
optList.forEach(function (optName) {
31+
if (argObj.hasOwnProperty(optName))
32+
result[optName] = argObj[optName];
33+
}, this);
34+
35+
if (argObj.hasOwnProperty("newline")) {
36+
switch (argObj.newline) {
37+
case "unix":
38+
result.newline = "\n";
39+
break;
40+
case "windows":
41+
result.newline = "\r\n";
42+
break;
43+
case "currentOsDefault":
44+
result.newline = os.EOL;
45+
break;
46+
}
47+
}
48+
return result;
49+
}
50+
51+
function callBundle(options) {
52+
if (!options.name || !options.main) {
53+
console.log("'name' and 'main' parameters are required. --help for get option list.")
54+
process.exit(1);
55+
}
56+
return dts.bundle(options);
57+
}
58+
59+
program
60+
.version(pkg.version)
61+
.option('--configJson <value>', "path to json config file. Load it first and override options with additional parameters")
62+
.option('--name <value>', 'name of module likein package.json *required')
63+
.option('--main <value>', 'path to entry-point (see documentation) *required')
64+
.option('--baseDir [value]', 'base directory to be used for discovering type declarations')
65+
.option('--out [value]', 'path of output file. Is relative from baseDir but you can use absolute paths. ')
66+
.option('--externals', 'include typings outside of the "baseDir" (i.e. like node.d.ts)')
67+
.option('--referenceExternals', 'reference external modules as <reference path="..." /> tags *** Experimental, TEST NEEDED')
68+
//.option('--exclude ', 'filter to exclude typings, either a RegExp or a callback. match path relative to opts.baseDir')
69+
.option('--removeSource', 'delete all source typings (i.e. "<baseDir>/**/*.d.ts")')
70+
.option('--newline [style]', 'newline style to use in output file => unix|windows|currentOsDefault', /^(unix|windows|currentOsDefault)$/i)
71+
//.option('--indent', 'indentation to use in output file')
72+
.option('--prefix [value]', 'prefix for rewriting module names')
73+
.option('--separator [value]', 'separator for rewriting module "path" names')
74+
.option('--verbose', 'enable verbose mode, prints detailed info about all references and includes/excludes')
75+
.option('--emitOnIncludedFileNotFound', 'emit although included files not found. See readme "Files not found" section. ')
76+
.option('--emitOnNoIncludedFileNotFound', 'emit although no included files not found. See readme "Files not found" section. ')
77+
.parse(process.argv);
78+
79+
console.log("%s version %s\n%s\n", pkg.name, pkg.version, pkg.description);
80+
81+
var options = mapOptions(program);
82+
83+
var result = callBundle(options);
84+
85+
if (!result.emitted) {
86+
console.log("Result no emitted - use verbose to see details.");
87+
process.exit(1);
88+
}
89+

lib/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export interface BundleResult {
6565
fileMap: { [name: string]: Result; };
6666
includeFilesNotFound: string[];
6767
noIncludeFilesNotFound: string[];
68-
emited?: boolean;
68+
emitted?: boolean;
6969
options: Options;
7070
}
7171

@@ -381,11 +381,11 @@ export function bundle(options: Options): BundleResult {
381381
}
382382

383383
fs.writeFileSync(outFile, content, 'utf8');
384-
bundleResult.emited = true;
384+
bundleResult.emitted = true;
385385
} else {
386386
warning(" XXX Not emit due to exist files not found.")
387387
trace("See documentation for emitOnIncludedFileNotFound and emitOnNoIncludedFileNotFound options.")
388-
bundleResult.emited = false;
388+
bundleResult.emitted = false;
389389
}
390390

391391
// print some debug info

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"export",
1010
"d.ts"
1111
],
12+
"bin": {
13+
"dts-bundle": "./lib/dts-bundle.js"
14+
},
1215
"homepage": "https://github.com/grunt-ts/dts-bundle",
1316
"repository": {
1417
"type": "git",
@@ -35,6 +38,7 @@
3538
},
3639
"main": "./index.js",
3740
"dependencies": {
41+
"commander": "^2.9.0",
3842
"detect-indent": "^0.2.0",
3943
"glob": "^4.0.2",
4044
"mkdirp": "^0.5.0"

0 commit comments

Comments
 (0)