convert @babel/cli to typescript (#13213)

* babel-cli flowts rename

* babel-cli flowts convert

* babel-cli

* yarn install
This commit is contained in:
Bogdan Savluk 2021-05-14 09:32:38 +02:00 committed by GitHub
parent 5def29d1ca
commit bb70ea47f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 30 deletions

View File

@ -1,5 +1,3 @@
// @flow
import commander from "commander"; import commander from "commander";
import { buildExternalHelpers } from "@babel/core"; import { buildExternalHelpers } from "@babel/core";

View File

@ -1,18 +1,16 @@
// @flow
import slash from "slash"; import slash from "slash";
import path from "path"; import path from "path";
import fs from "fs"; import fs from "fs";
import * as util from "./util"; import * as util from "./util";
import { type CmdOptions } from "./options"; import type { CmdOptions } from "./options";
const FILE_TYPE = Object.freeze({ const FILE_TYPE = Object.freeze({
NON_COMPILABLE: "NON_COMPILABLE", NON_COMPILABLE: "NON_COMPILABLE",
COMPILED: "COMPILED", COMPILED: "COMPILED",
IGNORED: "IGNORED", IGNORED: "IGNORED",
ERR_COMPILATION: "ERR_COMPILATION", ERR_COMPILATION: "ERR_COMPILATION",
}); } as const);
function outputFileSync(filePath: string, data: string | Buffer): void { function outputFileSync(filePath: string, data: string | Buffer): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true }); fs.mkdirSync(path.dirname(filePath), { recursive: true });
@ -28,7 +26,7 @@ export default async function ({
async function write( async function write(
src: string, src: string,
base: string, base: string,
): Promise<$Keys<typeof FILE_TYPE>> { ): Promise<keyof typeof FILE_TYPE> {
let relative = path.relative(base, src); let relative = path.relative(base, src);
if (!util.isCompilableExtension(relative, cliOptions.extensions)) { if (!util.isCompilableExtension(relative, cliOptions.extensions)) {

View File

@ -1,5 +1,3 @@
// @flow
import convertSourceMap from "convert-source-map"; import convertSourceMap from "convert-source-map";
import sourceMap from "source-map"; import sourceMap from "source-map";
import slash from "slash"; import slash from "slash";
@ -7,18 +5,18 @@ import path from "path";
import fs from "fs"; import fs from "fs";
import * as util from "./util"; import * as util from "./util";
import { type CmdOptions } from "./options"; import type { CmdOptions } from "./options";
type CompilationOutput = { type CompilationOutput = {
code: string, code: string;
map: Object, map: any;
}; };
export default async function ({ export default async function ({
cliOptions, cliOptions,
babelOptions, babelOptions,
}: CmdOptions): Promise<void> { }: CmdOptions): Promise<void> {
function buildResult(fileResults: Array<Object>): CompilationOutput { function buildResult(fileResults: Array<any>): CompilationOutput {
const map = new sourceMap.SourceMapGenerator({ const map = new sourceMap.SourceMapGenerator({
file: file:
cliOptions.sourceMapTarget || cliOptions.sourceMapTarget ||
@ -37,7 +35,7 @@ export default async function ({
if (result.map) { if (result.map) {
const consumer = new sourceMap.SourceMapConsumer(result.map); const consumer = new sourceMap.SourceMapConsumer(result.map);
const sources = new Set(); const sources = new Set<string>();
consumer.eachMapping(function (mapping) { consumer.eachMapping(function (mapping) {
if (mapping.source != null) sources.add(mapping.source); if (mapping.source != null) sources.add(mapping.source);
@ -104,7 +102,7 @@ export default async function ({
} }
function readStdin(): Promise<string> { function readStdin(): Promise<string> {
return new Promise((resolve: Function, reject: Function): void => { return new Promise((resolve, reject): void => {
let code = ""; let code = "";
process.stdin.setEncoding("utf8"); process.stdin.setEncoding("utf8");
@ -158,7 +156,7 @@ export default async function ({
}); });
const results = await Promise.all( const results = await Promise.all(
_filenames.map(async function (filename: string): Promise<Object> { _filenames.map(async function (filename: string): Promise<any> {
let sourceFilename = filename; let sourceFilename = filename;
if (cliOptions.outFile) { if (cliOptions.outFile) {
sourceFilename = path.relative( sourceFilename = path.relative(

View File

@ -1,5 +1,3 @@
// @flow
import fs from "fs"; import fs from "fs";
import commander from "commander"; import commander from "commander";
@ -170,6 +168,7 @@ commander.option(
"Use a specific extension for the output files", "Use a specific extension for the output files",
); );
declare const PACKAGE_JSON: { name: string; version: string };
commander.version(PACKAGE_JSON.version + " (@babel/core " + version + ")"); commander.version(PACKAGE_JSON.version + " (@babel/core " + version + ")");
commander.usage("[options] <files ...>"); commander.usage("[options] <files ...>");
// register an empty action handler so that commander.js can throw on // register an empty action handler so that commander.js can throw on
@ -178,8 +177,8 @@ commander.usage("[options] <files ...>");
commander.action(() => {}); commander.action(() => {});
export type CmdOptions = { export type CmdOptions = {
babelOptions: Object, babelOptions: any;
cliOptions: Object, cliOptions: any;
}; };
export default function parseArgv(args: Array<string>): CmdOptions | null { export default function parseArgv(args: Array<string>): CmdOptions | null {

View File

@ -1,5 +1,3 @@
// @flow
import readdirRecursive from "fs-readdir-recursive"; import readdirRecursive from "fs-readdir-recursive";
import * as babel from "@babel/core"; import * as babel from "@babel/core";
import path from "path"; import path from "path";
@ -47,7 +45,7 @@ export function readdirForCompilable(
*/ */
export function isCompilableExtension( export function isCompilableExtension(
filename: string, filename: string,
altExts?: Array<string>, altExts?: readonly string[],
): boolean { ): boolean {
const exts = altExts || babel.DEFAULT_EXTENSIONS; const exts = altExts || babel.DEFAULT_EXTENSIONS;
const ext = path.extname(filename); const ext = path.extname(filename);
@ -65,8 +63,8 @@ const CALLER = {
export function transform( export function transform(
filename: string, filename: string,
code: string, code: string,
opts: Object, opts: any,
): Promise<Object> { ): Promise<any> {
opts = { opts = {
...opts, ...opts,
caller: CALLER, caller: CALLER,
@ -81,10 +79,7 @@ export function transform(
}); });
} }
export function compile( export function compile(filename: string, opts: any | Function): Promise<any> {
filename: string,
opts: Object | Function,
): Promise<Object> {
opts = { opts = {
...opts, ...opts,
caller: CALLER, caller: CALLER,
@ -119,7 +114,7 @@ process.on("uncaughtException", function (err) {
process.exitCode = 1; process.exitCode = 1;
}); });
export function requireChokidar(): Object { export function requireChokidar(): any {
// $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504 // $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504
const require = createRequire(import /*::("")*/.meta.url); const require = createRequire(import /*::("")*/.meta.url);