Skip to content

Commit

Permalink
Merge pull request #32 from zxch3n/fix-remove-tsout-requirement
Browse files Browse the repository at this point in the history
tsconfig.json can be absent if outputDir is specified
  • Loading branch information
garronej authored Sep 2, 2021
2 parents aa914d8 + df23227 commit 70a37a3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/bin/denoify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tsconfig.outDir>)/deno_lib' -- Path to the output directory`)
.option("-o --out [outputDirPath]", `Default: '$(dirname <tsconfig.outDir>)/deno_lib' -- Path to the output directory`)
;

commanderStatic.parse(process.argv);
Expand All @@ -22,5 +22,5 @@ const options = commanderStatic.opts();
denoify({
"projectPath": options.project,
"srcDirPath": options.src,
"denoDirPath": options.out,
"denoDistPath": options.out,
});
74 changes: 42 additions & 32 deletions src/lib/denoify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function denoify(
params: {
projectPath: string;
srcDirPath?: string;
denoDirPath?: string;
denoDistPath?: string;
}
) {

Expand All @@ -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; }
Expand Down Expand Up @@ -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((() => {

Expand Down Expand Up @@ -259,18 +248,35 @@ export async function denoify(

}

function generateModFile(packageJsonParsed: any, tsconfigOutDir: string, denoDistPath: string, srcDir: string) {
const mainFileRelativePath = getMainFilePath(packageJsonParsed, tsconfigOutDir);
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 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(
Expand All @@ -281,7 +287,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;
}
Expand Down

0 comments on commit 70a37a3

Please sign in to comment.