Skip to content

Commit

Permalink
convert @babel/cli to typescript (#13213)
Browse files Browse the repository at this point in the history
* babel-cli flowts rename

* babel-cli flowts convert

* babel-cli

* yarn install
  • Loading branch information
zxbodya committed May 14, 2021
1 parent 5def29d commit bb70ea4
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// @flow

import slash from "slash";
import path from "path";
import fs from "fs";

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

const FILE_TYPE = Object.freeze({
NON_COMPILABLE: "NON_COMPILABLE",
COMPILED: "COMPILED",
IGNORED: "IGNORED",
ERR_COMPILATION: "ERR_COMPILATION",
});
} as const);

function outputFileSync(filePath: string, data: string | Buffer): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
Expand All @@ -28,7 +26,7 @@ export default async function ({
async function write(
src: string,
base: string,
): Promise<$Keys<typeof FILE_TYPE>> {
): Promise<keyof typeof FILE_TYPE> {
let relative = path.relative(base, src);

if (!util.isCompilableExtension(relative, cliOptions.extensions)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
// @flow

import convertSourceMap from "convert-source-map";
import sourceMap from "source-map";
import slash from "slash";
import path from "path";
import fs from "fs";

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

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

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

if (result.map) {
const consumer = new sourceMap.SourceMapConsumer(result.map);
const sources = new Set();
const sources = new Set<string>();

consumer.eachMapping(function (mapping) {
if (mapping.source != null) sources.add(mapping.source);
Expand Down Expand Up @@ -104,7 +102,7 @@ export default async function ({
}

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

process.stdin.setEncoding("utf8");
Expand Down Expand Up @@ -158,7 +156,7 @@ export default async function ({
});

const results = await Promise.all(
_filenames.map(async function (filename: string): Promise<Object> {
_filenames.map(async function (filename: string): Promise<any> {
let sourceFilename = filename;
if (cliOptions.outFile) {
sourceFilename = path.relative(
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import fs from "fs";

import commander from "commander";
Expand Down Expand Up @@ -170,6 +168,7 @@ commander.option(
"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.usage("[options] <files ...>");
// register an empty action handler so that commander.js can throw on
Expand All @@ -178,8 +177,8 @@ commander.usage("[options] <files ...>");
commander.action(() => {});

export type CmdOptions = {
babelOptions: Object,
cliOptions: Object,
babelOptions: any;
cliOptions: any;
};

export default function parseArgv(args: Array<string>): CmdOptions | null {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import readdirRecursive from "fs-readdir-recursive";
import * as babel from "@babel/core";
import path from "path";
Expand Down Expand Up @@ -47,7 +45,7 @@ export function readdirForCompilable(
*/
export function isCompilableExtension(
filename: string,
altExts?: Array<string>,
altExts?: readonly string[],
): boolean {
const exts = altExts || babel.DEFAULT_EXTENSIONS;
const ext = path.extname(filename);
Expand All @@ -65,8 +63,8 @@ const CALLER = {
export function transform(
filename: string,
code: string,
opts: Object,
): Promise<Object> {
opts: any,
): Promise<any> {
opts = {
...opts,
caller: CALLER,
Expand All @@ -81,10 +79,7 @@ export function transform(
});
}

export function compile(
filename: string,
opts: Object | Function,
): Promise<Object> {
export function compile(filename: string, opts: any | Function): Promise<any> {
opts = {
...opts,
caller: CALLER,
Expand Down Expand Up @@ -119,7 +114,7 @@ process.on("uncaughtException", function (err) {
process.exitCode = 1;
});

export function requireChokidar(): Object {
export function requireChokidar(): any {
// $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504
const require = createRequire(import /*::("")*/.meta.url);

Expand Down

0 comments on commit bb70ea4

Please sign in to comment.