diff --git a/src/bin/denoify.ts b/src/bin/denoify.ts index 75cd2e4..e9327c7 100644 --- a/src/bin/denoify.ts +++ b/src/bin/denoify.ts @@ -12,6 +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`) ; commanderStatic.parse(process.argv); @@ -20,5 +21,6 @@ const options = commanderStatic.opts(); denoify({ "projectPath": options.project, - "srcDirPath": options.src + "srcDirPath": options.src, + "denoDirPath": options.out, }); diff --git a/src/lib/denoify.ts b/src/lib/denoify.ts index 07db206..eba8e42 100644 --- a/src/lib/denoify.ts +++ b/src/lib/denoify.ts @@ -19,6 +19,7 @@ export async function denoify( params: { projectPath: string; srcDirPath?: string; + denoDirPath?: string; } ) { @@ -58,23 +59,6 @@ export async function denoify( })(); - if (!("main" in packageJsonParsed)) { - //TODO: We shouldn't force users to specify a default export. - throw new Error([ - "A main field in package.json need to be specified", - "otherwise we don't know what file the mod.ts should export." - ].join(" ")); - } - - if ( - !isInsideOrIsDir({ - "dirPath": tsconfigOutDir, - "fileOrDirPath": path.normalize(packageJsonParsed["main"]) - }) - ) { - throw new Error(`The package.json main should point to a file inside ${tsconfigOutDir}`) - } - const denoifyParamsFromPackageJson: { replacer?: string; ports?: { [portName: string]: string; } @@ -122,7 +106,7 @@ export async function denoify( } - const denoDistPath = path.join( + const denoDistPath = params.denoDirPath || path.join( path.dirname(tsconfigOutDir), `deno_${path.basename(tsconfigOutDir)}` ); // ./deno_dist @@ -220,29 +204,7 @@ export async function denoify( } }); - { - - const modFilePath = path.join(denoDistPath, "mod.ts"); - - if (!fs.existsSync(modFilePath)) { - - fs.writeFileSync( - path.join(modFilePath), - Buffer.from( - `export * from "${toPosix( - path.relative( - tsconfigOutDir, - path.normalize(packageJsonParsed["main"]) // ./dist/lib/index.js - ) // ./lib/index.js - .replace(/\.js$/i, ".ts"), // ./deno_dist/lib/index.ts - ).replace(/^(:?\.\/)?/, "./")}";`, - "utf8" - ) - ); - - } - - } + generateModFile(packageJsonParsed, tsconfigOutDir, denoDistPath, srcDirPath); { @@ -297,3 +259,47 @@ export async function denoify( } +function generateModFile(packageJsonParsed: any, tsconfigOutDir: string, denoDistPath: string, srcDir: string) { + const mainFileRelativePath = getMainFilePath(packageJsonParsed, tsconfigOutDir); + const modFilePath = path.join(denoDistPath, "mod.ts"); + if (!mainFileRelativePath) { + if (!fs.existsSync(modFilePath)) { + 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)) { + fs.writeFileSync( + path.join(modFilePath), + Buffer.from( + `export * from "${mainFileRelativePath}";`, + "utf8" + ) + ); + } +} + +function getMainFilePath(packageJsonParsed: any, tsconfigOutDir: string): string | undefined { + if (!("main" in packageJsonParsed)) { + return; + } + + if (!isInsideOrIsDir({ + "dirPath": tsconfigOutDir, + "fileOrDirPath": path.normalize(packageJsonParsed["main"]) + })) { + return; + } + + const indexFileRelativePath = toPosix( + path.relative( + tsconfigOutDir, + path.normalize(packageJsonParsed["main"]) // ./dist/lib/index.js + ) // ./lib/index.js + .replace(/\.js$/i, ".ts") + ).replace(/^(:?\.\/)?/, "./"); + return indexFileRelativePath; +} +