Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tsconfig.json can be absent if outputDir is specified #32

Merged
merged 3 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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