Skip to content

Commit

Permalink
added flow to babel cli (#10244)
Browse files Browse the repository at this point in the history
* added flow to babel cli

* added 'SourceMapGenerator' as a argument to 'fromObject'
  • Loading branch information
letladi authored and nicolo-ribaudo committed Jul 25, 2019
1 parent 4506590 commit 4d12c89
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 42 deletions.
4 changes: 2 additions & 2 deletions lib/third-party-libs.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ declare module "source-map" {
}

declare module "convert-source-map" {
import type { SourceMap } from "source-map";
import type { SourceMap, SourceMapGenerator } from "source-map";

declare class Converter {
toJSON(): string;
Expand All @@ -177,7 +177,7 @@ declare module "convert-source-map" {
declare module.exports: {
SourceMap: SourceMap,
Converter: Converter,
fromObject(obj: SourceMap): Converter,
fromObject(obj: SourceMap | SourceMapGenerator): Converter,
fromJSON(str: string): Converter,
fromBase64(str: string): Converter,
fromComment(str: string): Converter,
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-cli/src/babel-external-helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// @flow

import commander from "commander";
import { buildExternalHelpers } from "@babel/core";

function collect(value, previousValue): Array<string> {
function collect(
value: string | any,
previousValue: Array<string>,
): Array<string> {
// If the user passed the option with no value, like "babel-external-helpers --whitelist", do nothing.
if (typeof value !== "string") return previousValue;

Expand Down
16 changes: 11 additions & 5 deletions packages/babel-cli/src/babel/dir.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

import defaults from "lodash/defaults";
import outputFileSync from "output-file-sync";
import { sync as mkdirpSync } from "mkdirp";
Expand All @@ -6,11 +8,15 @@ import path from "path";
import fs from "fs";

import * as util from "./util";
import { type CmdOptions } from "./options";

export default async function({ cliOptions, babelOptions }) {
export default async function({
cliOptions,
babelOptions,
}: CmdOptions): Promise<void> {
const filenames = cliOptions.filenames;

async function write(src, base) {
async function write(src: string, base: string): Promise<boolean> {
let relative = path.relative(base, src);

if (!util.isCompilableExtension(relative, cliOptions.extensions)) {
Expand Down Expand Up @@ -65,14 +71,14 @@ export default async function({ cliOptions, babelOptions }) {
}
}

function getDest(filename, base) {
function getDest(filename: string, base: string): string {
if (cliOptions.relative) {
return path.join(base, cliOptions.outDir, filename);
}
return path.join(cliOptions.outDir, filename);
}

async function handleFile(src, base) {
async function handleFile(src: string, base: string): Promise<boolean> {
const written = await write(src, base);

if (!written && cliOptions.copyFiles) {
Expand All @@ -84,7 +90,7 @@ export default async function({ cliOptions, babelOptions }) {
return written;
}

async function handle(filenameOrDir) {
async function handle(filenameOrDir: string): Promise<number> {
if (!fs.existsSync(filenameOrDir)) return 0;

const stat = fs.statSync(filenameOrDir);
Expand Down
60 changes: 37 additions & 23 deletions packages/babel-cli/src/babel/file.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

import convertSourceMap from "convert-source-map";
import defaults from "lodash/defaults";
import sourceMap from "source-map";
Expand All @@ -6,9 +8,18 @@ import path from "path";
import fs from "fs";

import * as util from "./util";

export default async function({ cliOptions, babelOptions }) {
function buildResult(fileResults) {
import { type CmdOptions } from "./options";

type CompilationOutput = {
code: string,
map: Object,
};

export default async function({
cliOptions,
babelOptions,
}: CmdOptions): Promise<void> {
function buildResult(fileResults: Array<Object>): CompilationOutput {
const map = new sourceMap.SourceMapGenerator({
file:
cliOptions.sourceMapTarget ||
Expand Down Expand Up @@ -74,7 +85,7 @@ export default async function({ cliOptions, babelOptions }) {
};
}

function output(fileResults) {
function output(fileResults: Array<string>): void {
const result = buildResult(fileResults);

if (cliOptions.outFile) {
Expand All @@ -91,25 +102,28 @@ export default async function({ cliOptions, babelOptions }) {
}
}

function readStdin() {
return new Promise((resolve, reject) => {
let code = "";
function readStdin(): Promise<string> {
return new Promise(
(resolve: Function, reject: Function): void => {
let code = "";

process.stdin.setEncoding("utf8");
process.stdin.setEncoding("utf8");

process.stdin.on("readable", function() {
const chunk = process.stdin.read();
if (chunk !== null) code += chunk;
});
process.stdin.on("readable", function() {
const chunk = process.stdin.read();
// $FlowIgnore
if (chunk !== null) code += chunk;
});

process.stdin.on("end", function() {
resolve(code);
});
process.stdin.on("error", reject);
});
process.stdin.on("end", function() {
resolve(code);
});
process.stdin.on("error", reject);
},
);
}

async function stdin() {
async function stdin(): Promise<void> {
const code = await readStdin();

const res = await util.transform(
Expand All @@ -126,7 +140,7 @@ export default async function({ cliOptions, babelOptions }) {
output([res]);
}

async function walk(filenames) {
async function walk(filenames: Array<string>): Promise<void> {
const _filenames = [];

filenames.forEach(function(filename) {
Expand All @@ -151,7 +165,7 @@ export default async function({ cliOptions, babelOptions }) {
});

const results = await Promise.all(
_filenames.map(async function(filename) {
_filenames.map(async function(filename: string): Promise<Object> {
let sourceFilename = filename;
if (cliOptions.outFile) {
sourceFilename = path.relative(
Expand All @@ -168,7 +182,7 @@ export default async function({ cliOptions, babelOptions }) {
{
sourceFileName: sourceFilename,
// Since we're compiling everything to be merged together,
// "inline" applies to the final output file, but to the individual
// "inline" applies to the final output file, but not to the individual
// files being concatenated.
sourceMaps:
babelOptions.sourceMaps === "inline"
Expand All @@ -192,7 +206,7 @@ export default async function({ cliOptions, babelOptions }) {
output(results);
}

async function files(filenames) {
async function files(filenames: Array<string>): Promise<void> {
if (!cliOptions.skipInitialBuild) {
await walk(filenames);
}
Expand All @@ -208,7 +222,7 @@ export default async function({ cliOptions, babelOptions }) {
pollInterval: 10,
},
})
.on("all", function(type, filename) {
.on("all", function(type: string, filename: string) {
if (!util.isCompilableExtension(filename, cliOptions.extensions)) {
return;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/babel-cli/src/babel/options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

import fs from "fs";

import commander from "commander";
Expand Down Expand Up @@ -151,7 +153,12 @@ commander.option(
commander.version(pkg.version + " (@babel/core " + version + ")");
commander.usage("[options] <files ...>");

export default function parseArgv(args: Array<string>) {
export type CmdOptions = {
babelOptions: Object,
cliOptions: Object,
};

export default function parseArgv(args: Array<string>): CmdOptions {
//
commander.parse(args);

Expand Down
32 changes: 22 additions & 10 deletions packages/babel-cli/src/babel/util.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// @flow

import readdirRecursive from "fs-readdir-recursive";
import * as babel from "@babel/core";
import includes from "lodash/includes";
import path from "path";
import fs from "fs";

export function chmod(src, dest) {
export function chmod(src: string, dest: string): void {
fs.chmodSync(dest, fs.statSync(src).mode);
}

Expand All @@ -13,8 +15,8 @@ type ReaddirFilter = (filename: string) => boolean;
export function readdir(
dirname: string,
includeDotfiles: boolean,
filter: ReaddirFilter,
) {
filter?: ReaddirFilter,
): Array<string> {
return readdirRecursive(dirname, (filename, _index, currentDirectory) => {
const stat = fs.statSync(path.join(currentDirectory, filename));

Expand All @@ -30,7 +32,7 @@ export function readdirForCompilable(
dirname: string,
includeDotfiles: boolean,
altExts?: Array<string>,
) {
): Array<string> {
return readdir(dirname, includeDotfiles, function(filename) {
return isCompilableExtension(filename, altExts);
});
Expand All @@ -48,15 +50,19 @@ export function isCompilableExtension(
return includes(exts, ext);
}

export function addSourceMappingUrl(code, loc) {
export function addSourceMappingUrl(code: string, loc: string): string {
return code + "\n//# sourceMappingURL=" + path.basename(loc);
}

const CALLER = {
name: "@babel/cli",
};

export function transform(filename, code, opts) {
export function transform(
filename: string,
code: string,
opts: Object,
): Promise<Object> {
opts = {
...opts,
caller: CALLER,
Expand All @@ -71,7 +77,10 @@ export function transform(filename, code, opts) {
});
}

export function compile(filename, opts) {
export function compile(
filename: string,
opts: Object | Function,
): Promise<Object> {
opts = {
...opts,
caller: CALLER,
Expand All @@ -85,7 +94,7 @@ export function compile(filename, opts) {
});
}

export function deleteDir(path) {
export function deleteDir(path: string): void {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file) {
const curPath = path + "/" + file;
Expand All @@ -106,7 +115,7 @@ process.on("uncaughtException", function(err) {
process.exit(1);
});

export function requireChokidar() {
export function requireChokidar(): Object {
try {
return require("chokidar");
} catch (err) {
Expand All @@ -118,7 +127,10 @@ export function requireChokidar() {
}
}

export function adjustRelative(relative, keepFileExtension) {
export function adjustRelative(
relative: string,
keepFileExtension: boolean,
): string {
if (keepFileExtension) {
return relative;
}
Expand Down

0 comments on commit 4d12c89

Please sign in to comment.