added flow to babel cli (#10244)
* added flow to babel cli * added 'SourceMapGenerator' as a argument to 'fromObject'
This commit is contained in:
parent
4506590557
commit
4d12c8971b
@ -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;
|
||||
@ -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,
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
// @flow
|
||||
|
||||
import defaults from "lodash/defaults";
|
||||
import outputFileSync from "output-file-sync";
|
||||
import { sync as mkdirpSync } from "mkdirp";
|
||||
@ -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)) {
|
||||
@ -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) {
|
||||
@ -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);
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
// @flow
|
||||
|
||||
import convertSourceMap from "convert-source-map";
|
||||
import defaults from "lodash/defaults";
|
||||
import sourceMap from "source-map";
|
||||
@ -6,9 +8,18 @@ import path from "path";
|
||||
import fs from "fs";
|
||||
|
||||
import * as util from "./util";
|
||||
import { type CmdOptions } from "./options";
|
||||
|
||||
export default async function({ cliOptions, babelOptions }) {
|
||||
function buildResult(fileResults) {
|
||||
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 ||
|
||||
@ -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) {
|
||||
@ -91,14 +102,16 @@ export default async function({ cliOptions, babelOptions }) {
|
||||
}
|
||||
}
|
||||
|
||||
function readStdin() {
|
||||
return new Promise((resolve, reject) => {
|
||||
function readStdin(): Promise<string> {
|
||||
return new Promise(
|
||||
(resolve: Function, reject: Function): void => {
|
||||
let code = "";
|
||||
|
||||
process.stdin.setEncoding("utf8");
|
||||
|
||||
process.stdin.on("readable", function() {
|
||||
const chunk = process.stdin.read();
|
||||
// $FlowIgnore
|
||||
if (chunk !== null) code += chunk;
|
||||
});
|
||||
|
||||
@ -106,10 +119,11 @@ export default async function({ cliOptions, babelOptions }) {
|
||||
resolve(code);
|
||||
});
|
||||
process.stdin.on("error", reject);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async function stdin() {
|
||||
async function stdin(): Promise<void> {
|
||||
const code = await readStdin();
|
||||
|
||||
const res = await util.transform(
|
||||
@ -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) {
|
||||
@ -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(
|
||||
@ -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"
|
||||
@ -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);
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
// @flow
|
||||
|
||||
import fs from "fs";
|
||||
|
||||
import commander from "commander";
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -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);
|
||||
});
|
||||
@ -48,7 +50,7 @@ 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);
|
||||
}
|
||||
|
||||
@ -56,7 +58,11 @@ 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,
|
||||
@ -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,
|
||||
@ -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;
|
||||
@ -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) {
|
||||
@ -118,7 +127,10 @@ export function requireChokidar() {
|
||||
}
|
||||
}
|
||||
|
||||
export function adjustRelative(relative, keepFileExtension) {
|
||||
export function adjustRelative(
|
||||
relative: string,
|
||||
keepFileExtension: boolean,
|
||||
): string {
|
||||
if (keepFileExtension) {
|
||||
return relative;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user