From c4c840bdd36233327cc99427d0f9c1f9a7597fb5 Mon Sep 17 00:00:00 2001 From: Cameron Taggart Date: Sun, 25 Mar 2018 14:33:33 -0500 Subject: [PATCH] add exports option (#216) * add exports option * Fix test error * Add yargs debugging option --- .vscode/launch.json | 16 +++++++++++++ .vscode/tasks.json | 7 ++++++ build.fsx | 47 +++++++++++++++++++++++-------------- src/ts2fable.fs | 15 ++++++++++-- src/write.fs | 23 ++++++++++++++---- test/fsFileTests.fs | 8 +++---- tools/webpack.config.cli.js | 25 ++++++++++++++++++++ 7 files changed, 113 insertions(+), 28 deletions(-) create mode 100644 tools/webpack.config.cli.js diff --git a/.vscode/launch.json b/.vscode/launch.json index 5996462b..b236671c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,5 +13,21 @@ "sourceMaps": true, "outFiles": ["${workspaceRoot}/*/*.js"] }, + { + "type": "node", + "request": "launch", + "name": "Launch Cli", + "protocol": "inspector", + "program": "${workspaceRoot}/build/ts2fable.js", + "args":[ + "${workspaceRoot}/node_modules/office-ui-fabric-react/lib/index.d.ts", + "${workspaceRoot}/test-compile/OfficeReact.fs", + "-e", + "uifabric", + "office-ui-fabric-react" + ], + "sourceMaps": true, + "outFiles": ["${workspaceRoot}/*/*.js"] + }, ] } diff --git a/.vscode/tasks.json b/.vscode/tasks.json index e0f7efc4..09211e3d 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -16,6 +16,13 @@ "type": "shell", "label": "CliTest", "group": "build" + }, + { + "command": "fake", + "args": ["build","-t","WatchCli"], + "type": "shell", + "label": "WatchCli", + "group": "build" } ] } \ No newline at end of file diff --git a/build.fsx b/build.fsx index 8aa20e90..5ff643a7 100644 --- a/build.fsx +++ b/build.fsx @@ -89,36 +89,43 @@ Target.Create "BuildCli" (fun _ -> ) Target.Create "RunCli" (fun _ -> - let ts2fable definitionsFiles output = - let args = definitionsFiles @ [output] |> String.concat " " + let ts2fable args = + let args = args |> String.concat " " async { - node <| sprintf "%s %s" (distDir"ts2fable.js") args + node <| sprintf "%s %s" (distDir"ts2fable.js") args } [ // used by ts2fable - ts2fable ["node_modules/typescript/lib/typescript.d.ts"] "test-compile/TypeScript.fs" - ts2fable ["node_modules/@types/node/index.d.ts"] "test-compile/Node.fs" - ts2fable ["node_modules/@types/yargs/index.d.ts"] "test-compile/Yargs.fs" + ts2fable ["node_modules/typescript/lib/typescript.d.ts";"test-compile/TypeScript.fs"] + ts2fable ["node_modules/@types/node/index.d.ts";"test-compile/Node.fs"] + ts2fable ["node_modules/@types/yargs/index.d.ts";"test-compile/Yargs.fs"] // for test-compile - ts2fable ["node_modules/vscode/vscode.d.ts"] "test-compile/VSCode.fs" + ts2fable ["node_modules/vscode/vscode.d.ts";"test-compile/VSCode.fs"] // ts2fable ["node_modules/izitoast/dist/izitoast/izitoast.d.ts"] "test-compile/IziToast.fs" - ts2fable ["node_modules/izitoast/types/index.d.ts"] "test-compile/IziToast.fs" - ts2fable ["node_modules/electron/electron.d.ts"] "test-compile/Electron.fs" - ts2fable ["node_modules/@types/react/index.d.ts"] "test-compile/React.fs" - ts2fable ["node_modules/@types/mocha/index.d.ts"] "test-compile/Mocha.fs" - ts2fable ["node_modules/@types/chai/index.d.ts"] "test-compile/Chai.fs" - ts2fable ["node_modules/chalk/types/index.d.ts"] "test-compile/Chalk.fs" - ts2fable ["node_modules/monaco-editor/monaco.d.ts"] "test-compile/Monaco.fs" + ts2fable ["node_modules/izitoast/types/index.d.ts";"test-compile/IziToast.fs"] + ts2fable ["node_modules/electron/electron.d.ts";"test-compile/Electron.fs"] + ts2fable ["node_modules/@types/react/index.d.ts";"test-compile/React.fs"] + ts2fable ["node_modules/@types/mocha/index.d.ts";"test-compile/Mocha.fs"] + ts2fable ["node_modules/@types/chai/index.d.ts";"test-compile/Chai.fs"] + ts2fable ["node_modules/chalk/types/index.d.ts";"test-compile/Chalk.fs"] + ts2fable ["node_modules/monaco-editor/monaco.d.ts";"test-compile/Monaco.fs"] ts2fable [ "node_modules/@types/google-protobuf/index.d.ts" "node_modules/@types/google-protobuf/google/protobuf/empty_pb.d.ts" + "test-compile/Protobuf.fs" ] - "test-compile/Protobuf.fs" - ts2fable ["node_modules/synctasks/dist/SyncTasks.d.ts"] "test-compile/SyncTasks.fs" - ts2fable ["node_modules/subscribableevent/dist/SubscribableEvent.d.ts"] "test-compile/SubscribableEvent.fs" - ts2fable ["node_modules/office-ui-fabric-react/lib/index.d.ts"] "test-compile/OfficeUiFabricReact.fs" + ts2fable ["node_modules/synctasks/dist/SyncTasks.d.ts";"test-compile/SyncTasks.fs"] + ts2fable ["node_modules/subscribableevent/dist/SubscribableEvent.d.ts";"test-compile/SubscribableEvent.fs"] + ts2fable + [ + "node_modules/office-ui-fabric-react/lib/index.d.ts" + "test-compile/OfficeReact.fs" + "-e" + "uifabric" + "office-ui-fabric-react" + ] ] |> Async.Parallel |> Async.RunSynchronously @@ -239,6 +246,10 @@ Target.Create "WatchTest" (fun _ -> runDotnet toolDir "fable webpack -- --config webpack.config.test.js -w" ) +Target.Create "WatchCli" (fun _ -> + runDotnet toolDir "fable webpack -- --config webpack.config.cli.js -w" +) + Target.Create "CliTest" Target.DoNothing Target.Create "Deploy" DoNothing Target.Create "BuildAll" Target.DoNothing diff --git a/src/ts2fable.fs b/src/ts2fable.fs index 16b9022d..b81c8a47 100644 --- a/src/ts2fable.fs +++ b/src/ts2fable.fs @@ -11,13 +11,24 @@ let argv = yargs .usage("Usage: ts2fable some.d.ts src/Some.fs") .command(U2.Case1 "$0 [files..]", "") - .demandOption(U2.Case1 "files", "") + .option("files", jsOptions(fun o -> + o.alias <- Some !^"f" + o.description <- Some "input ts files and output fs file" + o.``type`` <- U5.Case1 "array" |> Some + )) + .option("exports", jsOptions(fun o -> + o.alias <- Some !^"e" + o.description <- Some "export types from files that contain the string" + o.``type`` <- U5.Case1 "array" |> Some + )) .help() .argv +//printfn "argv %A" argv let files = argv.["files"].Value :?> string array |> List.ofArray let tsfiles = files |> List.filter (fun s -> s.EndsWith ".ts") let fsfile = files |> List.tryFind (fun s -> s.EndsWith ".fs") +let exports = match argv.["exports"] with None -> [] | Some v -> v :?> string array |> List.ofArray match tsfiles.Length, fsfile with | 0, _ -> failwithf "Please provide the path to a TypeScript file" @@ -30,4 +41,4 @@ match tsfiles.Length, fsfile with if not <| fs.existsSync(!^ts) then failwithf "TypeScript file not found: %s" ts - writeFile tsfiles fsf + writeFile tsfiles fsf exports diff --git a/src/write.fs b/src/write.fs index e8bbfdfb..f61c795c 100644 --- a/src/write.fs +++ b/src/write.fs @@ -48,7 +48,10 @@ let transform (file: FsFile): FsFile = |> extractTypeLiterals // after fixEscapeWords |> addAliasUnionHelpers -let getFsFileOut (fsPath: string) (tsPaths: string list) = +let stringContainsAny (b: string list) (a: string): bool = + b |> List.exists a.Contains + +let getFsFileOut (fsPath: string) (tsPaths: string list) (exports: string list) = let options = jsOptions(fun o -> o.target <- Some ScriptTarget.ES2015 o.``module`` <- Some ModuleKind.CommonJS @@ -56,7 +59,19 @@ let getFsFileOut (fsPath: string) (tsPaths: string list) = let setParentNodes = true let host = ts.createCompilerHost(options, setParentNodes) let program = ts.createProgram(ResizeArray tsPaths, options, host) - let tsFiles = tsPaths |> List.map program.getSourceFile + + let exportFiles = + if exports.Length = 0 then tsPaths + else + program.getSourceFiles() + |> List.ofSeq + |> List.map (fun sf -> sf.fileName) + |> List.filter (stringContainsAny exports) + + for export in exportFiles do + printfn "export %s" export + + let tsFiles = exportFiles |> List.map program.getSourceFile let checker = program.getTypeChecker() let moduleNameMap = @@ -101,7 +116,7 @@ let emitFsFileOutAsLines (fsPath: string) (fsFileOut: FsFileOut) = file.``end``() lines |> List.ofSeq -let writeFile (tsPaths: string list) (fsPath: string): unit = +let writeFile (tsPaths: string list) (fsPath: string) exports: unit = // printfn "writeFile %A %s" tsPaths fsPath - let fsFileOut = getFsFileOut fsPath tsPaths + let fsFileOut = getFsFileOut fsPath tsPaths exports emitFsFileOut fsPath fsFileOut \ No newline at end of file diff --git a/test/fsFileTests.fs b/test/fsFileTests.fs index ea24df57..703ac1a2 100644 --- a/test/fsFileTests.fs +++ b/test/fsFileTests.fs @@ -27,14 +27,14 @@ let inline equal (expected: 'T) (actual: 'T): unit = let testFsFiles tsPaths fsPath (f: FsFile list -> unit) = - let fsFileOut = getFsFileOut fsPath tsPaths - emitFsFileOut fsPath fsFileOut + let fsFileOut = getFsFileOut fsPath tsPaths [] + emitFsFileOut fsPath fsFileOut f fsFileOut.Files let testFsFileLines tsPaths fsPath (f: string list -> unit) = - let fsFileOut = getFsFileOut fsPath tsPaths - emitFsFileOutAsLines fsPath fsFileOut + let fsFileOut = getFsFileOut fsPath tsPaths [] + emitFsFileOutAsLines fsPath fsFileOut |> f diff --git a/tools/webpack.config.cli.js b/tools/webpack.config.cli.js new file mode 100644 index 00000000..376b0c82 --- /dev/null +++ b/tools/webpack.config.cli.js @@ -0,0 +1,25 @@ +var path = require("path"); +var webpack = require("webpack"); +var common = require("./webpack.config.common"); +var fs = require('fs'); + +console.log("Bundling for cli..."); + +module.exports = { + devtool: "inline-source-map", + entry: common.config.cliEntry, + target: "node", + externals: common.config.nodeExternals, + output: { + filename: 'ts2fable.js', + path: common.config.buildDir, + devtoolModuleFilenameTemplate: info => + path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'), + }, + module: { + rules: common.getModuleRules() + }, + resolve: { + modules: [common.config.nodeModulesDir] + }, +}; \ No newline at end of file