-
Notifications
You must be signed in to change notification settings - Fork 21
Option to write exports to jsr.json #57
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
Changes from 4 commits
dfffcf7
fd15061
3027393
5a0cf51
8c955e6
2be37a1
fceaddc
96c00e2
b65aead
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ import { table } from "table"; | |
| import * as ts from "typescript"; | ||
| import { type BuildContext, compileProject } from "./compile.js"; | ||
| import { | ||
| detectConfigIndention, | ||
| findConfigPath, | ||
| formatForLog, | ||
| isSourceFile, | ||
| isTestFile, | ||
|
|
@@ -24,6 +26,7 @@ interface RawConfig { | |
| conditions?: Record<string, "esm" | "cjs" | "src">; | ||
| tsconfig?: string; // optional path to tsconfig.json file | ||
| noEdit?: boolean; | ||
| jsr?: boolean; | ||
|
Owner
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. cc @pullfrog drop this. zshy should write to jsr.json if it exists. the existence of the file is the way to enable this.
Contributor
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. Removed the
Contributor
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. Done! Removed the |
||
| } | ||
|
|
||
| interface NormalizedConfig { | ||
|
|
@@ -33,6 +36,7 @@ interface NormalizedConfig { | |
| cjs: boolean; | ||
| tsconfig: string; | ||
| noEdit: boolean; | ||
| jsr: boolean; | ||
| } | ||
|
|
||
| export async function main(): Promise<void> { | ||
|
|
@@ -163,36 +167,15 @@ Examples: | |
| /////////////////////////////////// | ||
|
|
||
| // Find package.json by scanning up the file system | ||
| let packageJsonPath = "./package.json"; | ||
| let currentDir = process.cwd(); | ||
|
|
||
| while (currentDir !== path.dirname(currentDir)) { | ||
| const candidatePath = path.join(currentDir, "package.json"); | ||
| if (fs.existsSync(candidatePath)) { | ||
| packageJsonPath = candidatePath; | ||
| break; | ||
| } | ||
| currentDir = path.dirname(currentDir); | ||
| } | ||
|
|
||
| if (!fs.existsSync(packageJsonPath)) { | ||
| log.error("❌ package.json not found in current directory or any parent directories"); | ||
| process.exit(1); | ||
| } | ||
| const packageJsonPath = findConfigPath("package.json"); | ||
|
|
||
| // read package.json and extract the "zshy" exports config | ||
| const pkgJsonRaw = fs.readFileSync(packageJsonPath, "utf-8"); | ||
| // console.log("📦 Extracting entry points from package.json exports..."); | ||
| const pkgJson = JSON.parse(pkgJsonRaw); | ||
|
|
||
| // Detect indentation from package.json to preserve it. | ||
| let indent: string | number = 2; // Default to 2 spaces | ||
| const indentMatch = pkgJsonRaw.match(/^([ \t]+)/m); | ||
| if (indentMatch?.[1]) { | ||
| indent = indentMatch[1]; | ||
| } else if (!pkgJsonRaw.includes("\n")) { | ||
| indent = 0; // minified | ||
| } | ||
| const pkgJsonIndent = detectConfigIndention(pkgJsonRaw); | ||
|
|
||
| const pkgJsonDir = path.dirname(packageJsonPath); | ||
| const pkgJsonRelPath = relativePosix(pkgJsonDir, packageJsonPath); | ||
|
|
@@ -285,11 +268,14 @@ Examples: | |
|
|
||
| const config = { ...rawConfig } as NormalizedConfig; | ||
|
|
||
| // Normalize boolean options | ||
| config.noEdit ??= false; | ||
| config.jsr ??= false; | ||
|
Contributor
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. Consider moving the |
||
|
|
||
| // Normalize cjs property | ||
| if (config.cjs === undefined) { | ||
| config.cjs = true; // Default to true if not specified | ||
| } | ||
| config.noEdit ??= false; | ||
|
|
||
| // Validate that if cjs is disabled, no conditions are set to "cjs" | ||
| if (config.cjs === false && config.conditions) { | ||
|
|
@@ -1045,7 +1031,48 @@ Examples: | |
| /////////////////////////////// | ||
| log.info("[dryrun] Skipping package.json modification"); | ||
| } else { | ||
| fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, indent) + "\n"); | ||
| fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, pkgJsonIndent) + "\n"); | ||
| } | ||
| } | ||
|
|
||
| ////////////////////////////////// | ||
| /// write jsr exports /// | ||
| ////////////////////////////////// | ||
|
|
||
| if (config.jsr) { | ||
| if (!isSilent) { | ||
| log.info(`${prefix}Updating jsr.json...`); | ||
| } | ||
|
|
||
| // Find jsr.json by scanning up the file system | ||
| const jsrJsonPath = findConfigPath("jsr.json"); | ||
|
|
||
| // read jsr.json | ||
| const jsrJsonRaw = fs.readFileSync(jsrJsonPath, "utf-8"); | ||
| const jsrJson = JSON.parse(jsrJsonRaw); | ||
|
|
||
| // Detect indentation from jsr.json to preserve it. | ||
| const jsrJsonIndent = detectConfigIndention(jsrJsonRaw); | ||
|
|
||
| const jsrJsonDir = path.dirname(jsrJsonPath); | ||
| const jsrJsonRelPath = relativePosix(jsrJsonDir, jsrJsonPath); | ||
|
|
||
| if (!isSilent) { | ||
| log.info(`Reading jsr.json from ./${jsrJsonRelPath}`); | ||
| } | ||
|
|
||
| // Copy exports from zshy config to jsr.json exports | ||
| const jsrExports = config.exports; | ||
| jsrJson.exports = jsrExports; | ||
| if (isVerbose) { | ||
| log.info(`Setting "exports": ${formatForLog(jsrExports)}`); | ||
| } | ||
|
|
||
| // Write jsr json | ||
| if (isDryRun) { | ||
| log.info("[dryrun] Skipping jsr.json modification"); | ||
| } else { | ||
| fs.writeFileSync(jsrJsonPath, JSON.stringify(jsrJson, null, jsrJsonIndent) + "\n"); | ||
| } | ||
| } | ||
|
DallasHoff marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| "use strict"; | ||
|
colinhacks marked this conversation as resolved.
|
||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.hi = hi; | ||
| /** | ||
| * Main entry point for the test library | ||
| */ | ||
| function hi() { | ||
| console.log("hi"); | ||
| } | ||
| //# sourceMappingURL=index.js.map | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /** | ||
| * Main entry point for the test library | ||
| */ | ||
| export declare function hi(): void; | ||
| //# sourceMappingURL=index.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /** | ||
| * Main entry point for the test library | ||
| */ | ||
| export declare function hi(): void; | ||
| //# sourceMappingURL=index.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "$schema": "https://jsr.io/schema/config-file.v1.json", | ||
| "name": "@jsr/my-pkg", | ||
| "version": "1.0.0", | ||
| "exports": { | ||
| ".": "./src/index.ts" | ||
| } | ||
| } |

Uh oh!
There was an error while loading. Please reload this page.