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

feat: output dir config & remove dep on pkg 'main' #31

Merged
merged 3 commits into from
Aug 29, 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: 3 additions & 1 deletion src/bin/denoify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tsconfig.outDir>)/deno_lib' -- Path to the output directory`)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it

;

commanderStatic.parse(process.argv);
Expand All @@ -20,5 +21,6 @@ const options = commanderStatic.opts();

denoify({
"projectPath": options.project,
"srcDirPath": options.src
"srcDirPath": options.src,
"denoDirPath": options.out,
});
88 changes: 47 additions & 41 deletions src/lib/denoify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function denoify(
params: {
projectPath: string;
srcDirPath?: string;
denoDirPath?: string;
}
) {

Expand Down Expand Up @@ -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}`)
}

Comment on lines -61 to -77
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I no longer have all the context in mind to remember why I deemed important to have this fail early strategy here.
I am willing to trust you on the fact that it can be safely removed.

const denoifyParamsFromPackageJson: {
replacer?: string;
ports?: { [portName: string]: string; }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

{

Expand Down Expand Up @@ -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;
}