-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}`) | ||
} | ||
|
||
Comment on lines
-61
to
-77
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it