Skip to content

Commit

Permalink
flow gen-flow-files
Browse files Browse the repository at this point in the history
Summary:
This adds a new command, `flow gen-flow-files path/to/file.js`, which generates the minimal `.js.flow` interface file that can be used for publishing compiled Flow source. Specifically: It generates only `import`, `declare`, and `declare export` statements in order to omit all the implementation text.

Background: Currently the best practice for publishing Flow source is (roughly):

1. `cp src/foo.js dist/foo.js.flow`
2. `babel src/foo.js > dist/foo.js`

This mostly works, but has a few downsides:

1. The published contents of `dist/*` are much larger than they need to be -- they contain a pre-compiled copy of all the original source. This is basically just crufty bytes that need to be downloaded every time the package is `npm install`-ed.
2. The published `.js.flow` files contain implementation details -- which are much more susceptible to breaking changes across Flow versions. While breaking changes across Flow version are still possible even in `declare` statements, this happens *far* less often.
3. We also want to re-use this type -> code codegen infrastructure to generate libdefs for flow-typed at some point. This particular diff is only about `.js.flow` files (i.e. no `declare module ...` statements) -- but that can be added on in a follow up diff.

This diff includes a new `codegen.ml` file which exposes an API intended for generating code from various things (types for now, possibly ASTs at some point if that's useful/performant/worth the effort). It's minimal in terms of feature-set right now but we can expand/improve it later. I didn't use `type_printer.ml` because it only handles some types and many of the types it will print aren't actual code or easily composable with other bits of code. It's also used by lots of stuff that I didn't really want to investigate breaking changes for while building out this feature.

At some point it probably makes sense to either improve `Type_printer` enough to subsume `Codegen` or the other way around. I suspect the `Codegen` is going to be easier to generalize, but we'll leave that for a later time to look into.

Closes facebook/flow#2184

Reviewed By: gabelevi

Differential Revision: D3663107

Pulled By: jeffmo

fbshipit-source-id: a791f85235f978fc9e5e46639e0dec37b71fad60
  • Loading branch information
jeffmo authored and Hhvm Bot committed Sep 1, 2016
1 parent e27dc56 commit 2a404b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions hphp/hack/src/utils/tty.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ let style_num = function
| NormalWithBG (text, bg) -> (text_num text) ^ ";" ^ (background_num bg)
| BoldWithBG (text, bg) -> (text_num text) ^ ";" ^ (background_num bg) ^ ";1"

let print_one ?(color_mode=Color_Auto) c s =
let print_one ?(color_mode=Color_Auto) ?(out_channel=stdout) c s =
let should_color = match color_mode with
| Color_Always -> true
| Color_Never -> false
Expand All @@ -84,14 +84,14 @@ let print_one ?(color_mode=Color_Auto) c s =
Unix.isatty Unix.stdout && term <> "dumb"
end in
if should_color
then Printf.printf "\x1b[%sm%s\x1b[0m" (style_num c) (s)
else Printf.printf "%s" s
then Printf.fprintf out_channel "\x1b[%sm%s\x1b[0m" (style_num c) (s)
else Printf.fprintf out_channel "%s" s

let cprint ?(color_mode=Color_Auto) strs =
List.iter strs (fun (c, s) -> print_one ~color_mode c s)
let cprint ?(color_mode=Color_Auto) ?(out_channel=stdout) strs =
List.iter strs (fun (c, s) -> print_one ~color_mode ~out_channel c s)

let cprintf ?(color_mode=Color_Auto) c =
Printf.ksprintf (print_one ~color_mode c)
let cprintf ?(color_mode=Color_Auto) ?(out_channel=stdout) c =
Printf.ksprintf (print_one ~color_mode ~out_channel c)

let (spinner, spinner_used) =
let state = ref 0 in
Expand Down
8 changes: 4 additions & 4 deletions hphp/hack/src/utils/tty.mli
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ type color_mode =
| Color_Auto

(*
* Print a sequence of colorized strings to stdout, using ANSI color escapes
* codes.
* Print a sequence of colorized strings to stdout/stderr, using ANSI color
* escapes codes.
*)
val cprint : ?color_mode:color_mode -> (style * string) list -> unit
val cprintf : ?color_mode:color_mode -> style ->
val cprint : ?color_mode:color_mode -> ?out_channel:out_channel -> (style * string) list -> unit
val cprintf : ?color_mode:color_mode -> ?out_channel:out_channel -> style ->
('a, unit, string, unit) format4 -> 'a

(* These two functions provide a four-state TTY-friendly spinner that
Expand Down

0 comments on commit 2a404b0

Please sign in to comment.