-
Notifications
You must be signed in to change notification settings - Fork 18
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 all 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 { | ||
| detectConfigIndentation, | ||
| findConfigPath, | ||
| formatForLog, | ||
| isSourceFile, | ||
| isTestFile, | ||
|
|
@@ -158,24 +160,15 @@ Examples: | |
| log.info("Build will fail only on errors (default)"); | ||
| } | ||
| } | ||
|
|
||
| /////////////////////////////////// | ||
| /// find and read pkg json /// | ||
| /////////////////////////////////// | ||
|
|
||
| // 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); | ||
| } | ||
| const packageJsonPath = findConfigPath("package.json"); | ||
|
|
||
| if (!fs.existsSync(packageJsonPath)) { | ||
| if (!packageJsonPath) { | ||
| log.error("❌ package.json not found in current directory or any parent directories"); | ||
| process.exit(1); | ||
| } | ||
|
|
@@ -186,13 +179,7 @@ Examples: | |
| 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 = detectConfigIndentation(pkgJsonRaw); | ||
|
|
||
| const pkgJsonDir = path.dirname(packageJsonPath); | ||
| const pkgJsonRelPath = relativePosix(pkgJsonDir, packageJsonPath); | ||
|
|
@@ -285,11 +272,13 @@ Examples: | |
|
|
||
| const config = { ...rawConfig } as NormalizedConfig; | ||
|
|
||
| // Normalize boolean options | ||
| config.noEdit ??= false; | ||
|
|
||
| // 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 +1034,54 @@ 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 /// | ||
| ////////////////////////////////// | ||
|
|
||
| // Check if jsr.json exists in the project | ||
| const jsrJsonPath = findConfigPath("jsr.json"); | ||
|
|
||
| if (jsrJsonPath) { | ||
| if (config.noEdit) { | ||
|
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. The logic structure here mirrors the package.json modification section well. Good consistency. One minor suggestion: consider adding a check for the existence of |
||
| if (!isSilent) { | ||
| log.info("[noedit] Skipping modification of jsr.json"); | ||
| } | ||
| } else { | ||
| if (!isSilent) { | ||
| log.info(`${prefix}Updating 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 = detectConfigIndentation(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"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| "use strict"; | ||
colinhacks marked this conversation as resolved.
Show resolved
Hide 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.