Skip to content

Commit

Permalink
Add a -q/--quiet option in the CLI and use it in the build script (#284)
Browse files Browse the repository at this point in the history
Fixes #240
  • Loading branch information
alangpierce authored Jun 29, 2018
1 parent 1ed9130 commit 33a8c12
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
12 changes: 6 additions & 6 deletions script/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ async function main(): Promise<void> {
async function buildSucrase(): Promise<void> {
console.log("Building Sucrase");
await run(`rm -rf ./dist`);
await run(`${SUCRASE} ./src -d ./dist --transforms imports,typescript`);
await run(`${SUCRASE} ./src -d ./dist --transforms imports,typescript -q`);
if (!fast) {
await run(`rm -rf ./dist-self-build`);
// The installed Sucrase version is always the previous version, but released versions of
// Sucrase should be self-compiled, so we do a multi-phase compilation. We compile Sucrase with
// the previous version, then use it to compile the current code, then use that to compile the
// code again. The second and third outputs should be exactly identical; otherwise we may have a
// problem where it miscompiled itself.
await run(`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms imports,typescript`);
await run(`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms imports,typescript -q`);
await run(
`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms typescript --out-extension mjs`,
`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms typescript --out-extension mjs -q`,
);
await run("rm -rf ./dist");
await run("mv ./dist-self-build ./dist");
await run(`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms imports,typescript`);
await run(`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms imports,typescript -q`);
await run(
`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms typescript --out-extension mjs`,
`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms typescript --out-extension mjs -q`,
);
await run("diff -r ./dist ./dist-self-build");
// Also add in .d.ts files from tsc, which only need to be compiled once.
Expand All @@ -67,7 +67,7 @@ async function buildIntegration(path: string): Promise<void> {
}

await run(`rm -rf ${path}/dist`);
await run(`${SUCRASE} ${path}/src -d ${path}/dist --transforms imports,typescript`);
await run(`${SUCRASE} ${path}/src -d ${path}/dist --transforms imports,typescript -q`);

if (!fast) {
await run(`${TSC} --emitDeclarationOnly --project ${path} --outDir ${path}/dist`);
Expand Down
50 changes: 30 additions & 20 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import {join} from "path";

import {Options, transform} from "./index";

interface CLIOptions {
outExtension: string;
excludeDirs: Array<string>;
quiet: boolean;
sucraseOptions: Options;
}

export default function run(): void {
commander
.description(`Sucrase: super-fast Babel alternative.`)
Expand All @@ -16,6 +23,7 @@ export default function run(): void {
.option("--out-extension <extension>", "File extension to use for all output files.", "js")
.option("--exclude-dirs <paths>", "Names of directories that should not be traversed.")
.option("-t, --transforms <transforms>", "Comma-separated list of transforms to run.")
.option("-q, --quiet", "Don't print the names of converted files.")
.option(
"--enable-legacy-typescript-module-interop",
"Use default TypeScript ESM/CJS interop strategy.",
Expand All @@ -39,17 +47,20 @@ export default function run(): void {
}

const outDir = commander.outDir;
const outExtension = commander.outExtension;
const srcDir = commander.args[0];
const excludeDirs = commander.excludeDirs ? commander.excludeDirs.split(",") : [];

const options: Options = {
transforms: commander.transforms.split(","),
enableLegacyTypeScriptModuleInterop: commander.enableLegacyTypescriptModuleInterop,
enableLegacyBabel5ModuleInterop: commander.enableLegacyBabel5ModuleInterop,
const options: CLIOptions = {
outExtension: commander.outExtension,
excludeDirs: commander.excludeDirs ? commander.excludeDirs.split(",") : [],
quiet: commander.quiet,
sucraseOptions: {
transforms: commander.transforms.split(","),
enableLegacyTypeScriptModuleInterop: commander.enableLegacyTypescriptModuleInterop,
enableLegacyBabel5ModuleInterop: commander.enableLegacyBabel5ModuleInterop,
},
};

buildDirectory(srcDir, outDir, outExtension, excludeDirs, options).catch((e) => {
buildDirectory(srcDir, outDir, options).catch((e) => {
process.exitCode = 1;
console.error(e);
});
Expand All @@ -58,35 +69,34 @@ export default function run(): void {
async function buildDirectory(
srcDirPath: string,
outDirPath: string,
outExtension: string,
excludeDirs: Array<string>,
options: Options,
options: CLIOptions,
): Promise<void> {
const extension = options.transforms.includes("typescript") ? ".ts" : ".js";
const extension = options.sucraseOptions.transforms.includes("typescript") ? ".ts" : ".js";
if (!(await exists(outDirPath))) {
await mkdir(outDirPath);
}
for (const child of await readdir(srcDirPath)) {
if (["node_modules", ".git"].includes(child) || excludeDirs.includes(child)) {
if (["node_modules", ".git"].includes(child) || options.excludeDirs.includes(child)) {
continue;
}
const srcChildPath = join(srcDirPath, child);
const outChildPath = join(outDirPath, child);
if ((await stat(srcChildPath)).isDirectory()) {
await buildDirectory(srcChildPath, outChildPath, outExtension, excludeDirs, options);
await buildDirectory(srcChildPath, outChildPath, options);
} else if (srcChildPath.endsWith(extension)) {
const outPath = `${outChildPath.substr(
0,
outChildPath.length - extension.length,
)}.${outExtension}`;
const outPath = `${outChildPath.substr(0, outChildPath.length - extension.length)}.${
options.outExtension
}`;
await buildFile(srcChildPath, outPath, options);
}
}
}

async function buildFile(srcPath: string, outPath: string, options: Options): Promise<void> {
console.log(`${srcPath} -> ${outPath}`);
async function buildFile(srcPath: string, outPath: string, options: CLIOptions): Promise<void> {
if (!options.quiet) {
console.log(`${srcPath} -> ${outPath}`);
}
const code = (await readFile(srcPath)).toString();
const transformedCode = transform(code, {...options, filePath: srcPath}).code;
const transformedCode = transform(code, {...options.sucraseOptions, filePath: srcPath}).code;
await writeFile(outPath, transformedCode);
}

0 comments on commit 33a8c12

Please sign in to comment.