dts-generator
Advanced tools
Comparing version
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var index_1 = require('../index'); | ||
@@ -38,3 +39,3 @@ module.exports = function main(argv) { | ||
} | ||
['name', 'out'].forEach(function (key) { | ||
['out'].forEach(function (key) { | ||
if (!kwArgs[key]) { | ||
@@ -41,0 +42,0 @@ console.error("Missing required argument \"" + key + "\""); |
84
index.js
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var fs = require('fs'); | ||
@@ -15,8 +16,13 @@ var glob = require('glob'); | ||
else { | ||
var separatorExpression = new RegExp(pathUtil.sep.replace('\\', '\\\\'), 'g'); | ||
var separatorExpression_1 = new RegExp(pathUtil.sep.replace('\\', '\\\\'), 'g'); | ||
return function (filename) { | ||
return filename.replace(separatorExpression, '/'); | ||
return filename.replace(separatorExpression_1, '/'); | ||
}; | ||
} | ||
})(); | ||
/** | ||
* A helper function that takes TypeScript diagnostic errors and returns an error | ||
* object. | ||
* @param diagnostics The array of TypeScript Diagnostic objects | ||
*/ | ||
function getError(diagnostics) { | ||
@@ -68,7 +74,18 @@ var message = 'Declaration generation failed'; | ||
} | ||
/** | ||
* Load and parse a TSConfig File | ||
* @param options The dts-generator options to load config into | ||
* @param fileName The path to the file | ||
*/ | ||
function getTSConfig(options, fileName) { | ||
var configText = fs.readFileSync(fileName, { encoding: 'utf8' }); | ||
var result = ts.parseConfigFileTextToJson(fileName, configText); | ||
if (result.error) { | ||
throw getError([result.error]); | ||
} | ||
var configObject = result.config; | ||
var configParseResult = ts.parseJsonConfigFileContent(configObject, ts.sys, pathUtil.dirname(fileName)); | ||
if (configParseResult.errors && configParseResult.errors.length) { | ||
throw getError(configParseResult.errors); | ||
} | ||
options.target = configParseResult.options.target; | ||
@@ -81,5 +98,23 @@ if (configParseResult.options.outDir) { | ||
} | ||
if (configParseResult.options.rootDir) { | ||
options.rootDir = configParseResult.options.rootDir; | ||
} | ||
options.files = configParseResult.fileNames; | ||
return; | ||
return options; | ||
} | ||
function isNodeKindImportDeclaration(value) { | ||
return value && value.kind === ts.SyntaxKind.ImportDeclaration; | ||
} | ||
function isNodeKindExternalModuleReference(value) { | ||
return value && value.kind === ts.SyntaxKind.ExternalModuleReference; | ||
} | ||
function isNodeKindStringLiteral(value) { | ||
return value && value.kind === ts.SyntaxKind.StringLiteral; | ||
} | ||
function isNodeKindExportDeclaration(value) { | ||
return value && value.kind === ts.SyntaxKind.ExportDeclaration; | ||
} | ||
function isNodeKindExportAssignment(value) { | ||
return value && value.kind === ts.SyntaxKind.ExportAssignment; | ||
} | ||
function generate(options) { | ||
@@ -110,3 +145,3 @@ var noop = function (message) { | ||
} | ||
var baseDir = pathUtil.resolve(options.project || options.baseDir); | ||
var baseDir = pathUtil.resolve(options.rootDir || options.project || options.baseDir); | ||
verboseMessage("baseDir = \"" + baseDir + "\""); | ||
@@ -116,7 +151,7 @@ var eol = options.eol || os.EOL; | ||
var indent = options.indent === undefined ? '\t' : options.indent; | ||
var target = options.target || 2 /* Latest */; | ||
var target = typeof options.target !== 'undefined' ? options.target : ts.ScriptTarget.Latest; | ||
verboseMessage("taget = " + target); | ||
var compilerOptions = { | ||
declaration: true, | ||
module: 1 /* CommonJS */, | ||
module: ts.ModuleKind.CommonJS, | ||
target: target | ||
@@ -169,2 +204,4 @@ }; | ||
sendMessage('processing:'); | ||
var mainExportDeclaration = false; | ||
var mainExportAssignment = false; | ||
program.getSourceFiles().some(function (sourceFile) { | ||
@@ -185,2 +222,9 @@ // Source file is a default library, or other dependency from another project, that should not be included in | ||
} | ||
// We can optionally output the main module if there's something to export. | ||
if (options.main && options.main === (options.name + filenameToMid(sourceFile.fileName.slice(baseDir.length, -3)))) { | ||
ts.forEachChild(sourceFile, function (node) { | ||
mainExportDeclaration = mainExportDeclaration || isNodeKindExportDeclaration(node); | ||
mainExportAssignment = mainExportAssignment || isNodeKindExportAssignment(node); | ||
}); | ||
} | ||
var emitOutput = program.emit(sourceFile, writeFile); | ||
@@ -195,6 +239,16 @@ if (emitOutput.emitSkipped || emitOutput.diagnostics.length > 0) { | ||
}); | ||
if (options.main) { | ||
if (options.main && options.name) { | ||
output.write(("declare module '" + options.name + "' {") + eol + indent); | ||
output.write(("import main = require('" + options.main + "');") + eol + indent); | ||
output.write('export = main;' + eol); | ||
if (compilerOptions.target >= ts.ScriptTarget.ES6) { | ||
if (mainExportAssignment) { | ||
output.write(("export {default} from '" + options.main + "';") + eol + indent); | ||
} | ||
if (mainExportDeclaration) { | ||
output.write(("export * from '" + options.main + "';") + eol); | ||
} | ||
} | ||
else { | ||
output.write(("import main = require('" + options.main + "');") + eol + indent); | ||
output.write('export = main;' + eol); | ||
} | ||
output.write('}' + eol); | ||
@@ -208,4 +262,4 @@ sendMessage("Aliased main module " + options.name + " to " + options.main); | ||
var filename = declarationFile.fileName; | ||
var sourceModuleId = options.name + filenameToMid(filename.slice(baseDir.length, -5)); | ||
/* For some reason, SourceFile.externalModuleIndicator is missing from 1.6-beta, so having | ||
var sourceModuleId = options.name ? options.name + filenameToMid(filename.slice(baseDir.length, -5)) : filenameToMid(filename.slice(baseDir.length + 1, -5)); | ||
/* For some reason, SourceFile.externalModuleIndicator is missing from 1.6+, so having | ||
* to use a sledgehammer on the nut */ | ||
@@ -215,3 +269,3 @@ if (declarationFile.externalModuleIndicator) { | ||
var content = processTree(declarationFile, function (node) { | ||
if (node.kind === 232 /* ExternalModuleReference */) { | ||
if (isNodeKindExternalModuleReference(node)) { | ||
var expression = node.expression; | ||
@@ -222,7 +276,7 @@ if (expression.text.charAt(0) === '.') { | ||
} | ||
else if (node.kind === 122 /* DeclareKeyword */) { | ||
else if (node.kind === ts.SyntaxKind.DeclareKeyword) { | ||
return ''; | ||
} | ||
else if (node.kind === 9 /* StringLiteral */ && | ||
(node.parent.kind === 228 /* ExportDeclaration */ || node.parent.kind === 222 /* ImportDeclaration */)) { | ||
else if (isNodeKindStringLiteral(node) && node.parent && | ||
(isNodeKindExportDeclaration(node.parent) || isNodeKindImportDeclaration(node.parent))) { | ||
var text = node.text; | ||
@@ -229,0 +283,0 @@ if (text.charAt(0) === '.') { |
{ | ||
"name": "dts-generator", | ||
"description": ".d.ts generator. Generates a single d.ts bundle containing external modules from TypeScript files.", | ||
"version": "1.6.3", | ||
"version": "1.7.0", | ||
"bugs": { | ||
@@ -17,10 +17,13 @@ "url": "https://github.com/SitePen/dts-generator/issues" | ||
"dependencies": { | ||
"bluebird": "2.10.2", | ||
"glob": "5.0.15", | ||
"mkdirp": "0.5.1", | ||
"typescript": "1.7.3" | ||
"bluebird": "3.3.3", | ||
"glob": "7.0.0", | ||
"mkdirp": "0.5.1" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^1.6.0" | ||
}, | ||
"devDependencies": { | ||
"intern": "~3.0.0", | ||
"tsd": "0.6.5", | ||
"intern": "~3.0.0" | ||
"tslint": "3.5.0" | ||
}, | ||
@@ -27,0 +30,0 @@ "scripts": { |
@@ -27,3 +27,3 @@ .d.ts generator | ||
```js | ||
require('dts-generator')({ | ||
require('dts-generator').default({ | ||
name: 'package-name', | ||
@@ -30,0 +30,0 @@ project: '/path/to/package-directory', |
@@ -9,2 +9,3 @@ (function (factory) { | ||
})(function (require, exports) { | ||
"use strict"; | ||
exports.loaderOptions = { | ||
@@ -11,0 +12,0 @@ packages: [ |
@@ -9,4 +9,5 @@ (function (factory) { | ||
})(function (require, exports) { | ||
"use strict"; | ||
require('./bin/dts-generator'); | ||
require('./index'); | ||
}); |
@@ -9,2 +9,3 @@ (function (factory) { | ||
})(function (require, exports) { | ||
"use strict"; | ||
var registerSuite = require('intern!object'); | ||
@@ -11,0 +12,0 @@ var assert = require('intern/chai!assert'); |
@@ -9,2 +9,3 @@ (function (factory) { | ||
})(function (require, exports) { | ||
"use strict"; | ||
var registerSuite = require('intern!object'); | ||
@@ -55,4 +56,17 @@ var assert = require('intern/chai!assert'); | ||
}); | ||
}, | ||
'es6 main module': function () { | ||
return index_1.default({ | ||
name: 'foo', | ||
project: 'tests/support/foo-es6', | ||
out: 'tmp/foo.es6.d.ts', | ||
main: 'index.ts' | ||
}).then(function () { | ||
var contents = fs.readFileSync('tmp/foo.es6.d.ts', { encoding: 'utf8' }); | ||
assert(contents, 'foo.es6.d.ts should exist and have contents'); | ||
// assert.include(contents, `module 'foo/index'`); | ||
// assert.include(contents, `module 'foo/Bar'`); | ||
}); | ||
} | ||
}); | ||
}); |
{ | ||
"rules": { | ||
"align": false, | ||
"ban": [], | ||
@@ -9,2 +10,3 @@ "class-name": true, | ||
"forin": false, | ||
"indent": [ true, "tabs" ], | ||
"interface-name": false, | ||
@@ -15,7 +17,9 @@ "jsdoc-format": true, | ||
"max-line-length": false, | ||
"member-access": false, | ||
"member-ordering": false, | ||
"no-any": false, | ||
"no-arg": true, | ||
"no-bitwise": false, | ||
"no-consecutive-blank-lines": true, | ||
"no-console": false, | ||
"no-consecutive-blank-lines": true, | ||
"no-construct": false, | ||
@@ -26,8 +30,7 @@ "no-constructor-vars": true, | ||
"no-duplicate-variable": true, | ||
"no-shadowed-variable": false, | ||
"no-empty": false, | ||
"no-eval": true, | ||
"no-shadowed-variable": false, | ||
"no-string-literal": false, | ||
"no-switch-case-fall-through": false, | ||
"no-trailing-comma": true, | ||
"no-trailing-whitespace": true, | ||
@@ -40,2 +43,3 @@ "no-unreachable": true, | ||
"no-var-requires": false, | ||
"object-literal-sort-keys": false, | ||
"one-line": [ true, "check-open-brace", "check-whitespace" ], | ||
@@ -45,5 +49,10 @@ "quotemark": [ true, "single" ], | ||
"semicolon": true, | ||
"trailing-comma": [ true, { | ||
"multiline": "never", | ||
"singleline": "never" | ||
} ], | ||
"triple-equals": [ true, "allow-null-check" ], | ||
"typedef": false, | ||
"typedef-whitespace": [ true, { | ||
"call-signature": "nospace", | ||
"index-signature": "nospace", | ||
@@ -50,0 +59,0 @@ "parameter": "nospace", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
41755
10.02%550
15.06%3
50%16
-5.88%+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
Updated
Updated