From e0307a8e54c882776fc5aed8d3f78704fb943cf8 Mon Sep 17 00:00:00 2001 From: Adam Shaw Date: Mon, 24 Jun 2019 14:09:25 -0400 Subject: [PATCH 1/2] fix incorrect args to readFileSync/writeFileSync --- lib/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 6e744c4..4474d39 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -183,7 +183,7 @@ export function bundle(options: Options): BundleResult { mainFileContent += generatedLine + "\n"; }); mainFile = path.resolve(baseDir, "dts-bundle.tmp." + exportName + ".d.ts"); - fs.writeFileSync(mainFile, mainFileContent, 'utf8'); + fs.writeFileSync(mainFile, mainFileContent, { encoding: 'utf8' }); } trace('\n### find typings ###'); @@ -424,7 +424,7 @@ export function bundle(options: Options): BundleResult { } } - fs.writeFileSync(outFile, content, 'utf8'); + fs.writeFileSync(outFile, content, { encoding: 'utf8' }); bundleResult.emitted = true; } else { warning(" XXX Not emit due to exist files not found.") @@ -587,7 +587,7 @@ export function bundle(options: Options): BundleResult { if (fs.lstatSync(file).isDirectory()) { // if file is a directory then lets assume commonjs convention of an index file in the given folder file = path.join(file, 'index.d.ts'); } - const code = fs.readFileSync(file, 'utf8').replace(bomOptExp, '').replace(/\s*$/, ''); + const code = fs.readFileSync(file, { encoding: 'utf8' }).replace(bomOptExp, '').replace(/\s*$/, ''); res.indent = detectIndent(code) || indent; // buffer multi-line comments, handle JSDoc From 2e95a4f37ed7ba9b91993eaa37d70e163953077c Mon Sep 17 00:00:00 2001 From: Adam Shaw Date: Tue, 25 Jun 2019 10:14:28 -0400 Subject: [PATCH 2/2] transformModuleBody hook --- lib/index.ts | 26 +++++++++---- test/expected/transformModuleBody/foo-mx.d.ts | 38 +++++++++++++++++++ test/test.js | 25 ++++++++++++ 3 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 test/expected/transformModuleBody/foo-mx.d.ts diff --git a/lib/index.ts b/lib/index.ts index 4474d39..0a777c8 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -42,6 +42,7 @@ export interface Options { emitOnNoIncludedFileNotFound?: boolean; headerPath: string; headerText: string; + transformModuleBody: (moduleBody: string, moduleName?: string) => string } export interface ModLine { @@ -544,16 +545,27 @@ export function bundle(options: Options): BundleResult { return (lines.length === 0 ? '' : i + lines.join(newline + i)) + newline; } + function transformModuleBody(moduleBody: string, moduleName?: string) { + if (typeof options.transformModuleBody === 'function') { + moduleBody = options.transformModuleBody(moduleBody, moduleName); + } + return moduleBody + } + function formatModule(file: string, lines: string[]) { - let out = ''; + let moduleBody = mergeModulesLines(lines); + if (outputAsModuleFolder) { - return mergeModulesLines(lines); - } + moduleBody = transformModuleBody(moduleBody); + return moduleBody; + + } else { + let moduleName = getExpName(file); + moduleBody = transformModuleBody(moduleBody, moduleName); - out += 'declare module \'' + getExpName(file) + '\' {' + newline; - out += mergeModulesLines(lines); - out += '}' + newline; - return out; + return 'declare module \'' + moduleName + '\' {' + newline + + moduleBody + '}' + newline; + } } // main info extractor diff --git a/test/expected/transformModuleBody/foo-mx.d.ts b/test/expected/transformModuleBody/foo-mx.d.ts new file mode 100644 index 0000000..1e03485 --- /dev/null +++ b/test/expected/transformModuleBody/foo-mx.d.ts @@ -0,0 +1,38 @@ +// Dependencies for this module: +// ../../src/typings/external.d.ts + +declare module 'foo-mx' { + import exp = require('foo-mx/lib/exported-sub'); + import mod1 = require('external1'); + export import Foo = require('foo-mx/Foo'); + export function run(foo?: Foo): Foo; + export function flep(): exp.ExternalContainer; + export function bar(): mod1.SomeType; +/* INJECTED VIA transformModuleBody */ +} + +declare module 'foo-mx/lib/exported-sub' { + import Foo = require('foo-mx/Foo'); + import mod2 = require('external2'); + export class ExternalContainer { + something: mod2.AnotherType; + } + export function bar(foo: Foo): string; + export function bazz(value: string, option?: boolean): string; +/* INJECTED VIA transformModuleBody */ +} + +declare module 'foo-mx/Foo' { + class Foo { + foo: string; + constructor(secret?: string); + /** + * Bars the foo. + */ + barFoo(): void; + /** Foos the baz. */ + fooBaz(): void; + } + export = Foo; +/* INJECTED VIA transformModuleBody */ +} diff --git a/test/test.js b/test/test.js index 25f95dc..5846a18 100644 --- a/test/test.js +++ b/test/test.js @@ -332,6 +332,31 @@ describe('dts bundle', function () { assert.strictEqual(getFile(actualFile), getFile(expectedFile)); }); + testit('transformModuleBody', function (actDir, expDir) { + var result = dts.bundle({ + name: 'foo-mx', + main: path.join(actDir, 'index.d.ts'), + newline: '\n', + verbose: true, + headerPath: "none", + transformModuleBody(moduleBody) { + return moduleBody + '/* INJECTED VIA transformModuleBody */\n'; + } + }); + var name = 'foo-mx.d.ts'; + var actualFile = path.join(actDir, name); + assert.isTrue(result.emitted, "not emit " + actualFile); + var expectedFile = path.join(expDir, name); + assertFiles(actDir, [ + name, + 'index.d.ts', + 'Foo.d.ts', + 'lib/exported-sub.d.ts', + 'lib/only-internal.d.ts' + ]); + assert.strictEqual(getFile(actualFile), getFile(expectedFile)); + }); + //testit('includeExclude_cli', function (actDir, expDir) { }); // No exclude options available from CLI. (function testit(name, assertion, run) {