From 2e7291127255f5003c29cec980345bd0e8ca62ac Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Thu, 2 Sep 2021 14:08:42 +0800 Subject: [PATCH 1/2] fix: remove dep that tsconfig must have outDir --- src/bin/denoify.ts | 4 +-- src/lib/denoify.ts | 62 ++++++++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/bin/denoify.ts b/src/bin/denoify.ts index e9327c7..d0e10ed 100644 --- a/src/bin/denoify.ts +++ b/src/bin/denoify.ts @@ -12,7 +12,7 @@ commanderStatic `) .option("-p, --project [projectPath]", `Default: './' -- Root path of the project to denoify, target directory is supposed to contain a 'package.json' and 'tsconfig.json'.`) .option("--src [srcDirPath]", `Default: '[projectPath]/src' | '[projectPath]/lib' -- Path to the directory containing the source .ts files. If the provided path is not absolute it is assumed relative to [projectPath]`) - .option("--out [outputDirPath]", `Default: '$(dirname )/deno_lib' -- Path to the output directory`) + .option("-o --out [outputDirPath]", `Default: '$(dirname )/deno_lib' -- Path to the output directory`) ; commanderStatic.parse(process.argv); @@ -22,5 +22,5 @@ const options = commanderStatic.opts(); denoify({ "projectPath": options.project, "srcDirPath": options.src, - "denoDirPath": options.out, + "denoDistPath": options.out, }); diff --git a/src/lib/denoify.ts b/src/lib/denoify.ts index eba8e42..32d8065 100644 --- a/src/lib/denoify.ts +++ b/src/lib/denoify.ts @@ -19,7 +19,7 @@ export async function denoify( params: { projectPath: string; srcDirPath?: string; - denoDirPath?: string; + denoDistPath?: string; } ) { @@ -39,26 +39,6 @@ export async function denoify( .toString("utf8") ); - const { tsconfigOutDir } = (() => { - - const parsedTsCompile = commentJson.parse( - fs.readFileSync("tsconfig.json") - .toString("utf8") - ); - - const { outDir } = parsedTsCompile["compilerOptions"]; - - if (!outDir) { - throw new Error("tsconfig.json must specify an outDir"); - } - - return { - "tsconfigOutDir": path.normalize(outDir) // dist/ - }; - - })(); - - const denoifyParamsFromPackageJson: { replacer?: string; ports?: { [portName: string]: string; } @@ -106,10 +86,19 @@ export async function denoify( } - const denoDistPath = params.denoDirPath || path.join( - path.dirname(tsconfigOutDir), - `deno_${path.basename(tsconfigOutDir)}` - ); // ./deno_dist + const tsconfigOutDir = getTsConfigOutDir(); + let denoDistPath: string; + if (params.denoDistPath != null) { + denoDistPath = params.denoDistPath + } else if ( tsconfigOutDir != null) { + denoDistPath = path.join( + path.dirname(tsconfigOutDir), + `deno_${path.basename(tsconfigOutDir)}` + ); + } else { + throw new Error(`You should specify output directory by --out option or specify "outDir" in tsconfig.json`); + } + const { denoifySingleFile } = denoifySingleFileFactory((() => { @@ -259,7 +248,22 @@ export async function denoify( } -function generateModFile(packageJsonParsed: any, tsconfigOutDir: string, denoDistPath: string, srcDir: string) { +function getTsConfigOutDir(): string | undefined { + const parsedTsCompile = commentJson.parse( + fs.readFileSync("tsconfig.json") + .toString("utf8") + ); + + const { outDir } = parsedTsCompile["compilerOptions"]; + + if (!outDir) { + return; + } + + return path.normalize(outDir) +} + +function generateModFile(packageJsonParsed: any, tsconfigOutDir: string | undefined, denoDistPath: string, srcDir: string) { const mainFileRelativePath = getMainFilePath(packageJsonParsed, tsconfigOutDir); const modFilePath = path.join(denoDistPath, "mod.ts"); if (!mainFileRelativePath) { @@ -281,7 +285,11 @@ function generateModFile(packageJsonParsed: any, tsconfigOutDir: string, denoDis } } -function getMainFilePath(packageJsonParsed: any, tsconfigOutDir: string): string | undefined { +function getMainFilePath(packageJsonParsed: any, tsconfigOutDir: string | undefined): string | undefined { + if (tsconfigOutDir == null) { + return; + } + if (!("main" in packageJsonParsed)) { return; } From df23227252bb40e215a342b59927c94da8f2cb1c Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Thu, 2 Sep 2021 14:21:43 +0800 Subject: [PATCH 2/2] refactor: simplify code in genModFile --- src/lib/denoify.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/denoify.ts b/src/lib/denoify.ts index 32d8065..0b2e722 100644 --- a/src/lib/denoify.ts +++ b/src/lib/denoify.ts @@ -264,17 +264,19 @@ function getTsConfigOutDir(): string | undefined { } function generateModFile(packageJsonParsed: any, tsconfigOutDir: string | undefined, denoDistPath: string, srcDir: string) { - const mainFileRelativePath = getMainFilePath(packageJsonParsed, tsconfigOutDir); const modFilePath = path.join(denoDistPath, "mod.ts"); + if (fs.existsSync(modFilePath)) { + return; + } + + const mainFileRelativePath = getMainFilePath(packageJsonParsed, tsconfigOutDir); if (!mainFileRelativePath) { - if (!fs.existsSync(modFilePath)) { - console.warn(`Did not generate "mod.ts" file. You may create "mod.ts" file to export in ${srcDir}`); - } + console.warn(`Did not generate "mod.ts" file. You may create "mod.ts" file to export in ${srcDir}`); return; } const indexFilePath = path.resolve(denoDistPath, mainFileRelativePath); - if (!fs.existsSync(modFilePath) && fs.existsSync(indexFilePath)) { + if (fs.existsSync(indexFilePath)) { fs.writeFileSync( path.join(modFilePath), Buffer.from(