Skip to content

Commit 37c8573

Browse files
authored
format code (#20)
1 parent 322661c commit 37c8573

12 files changed

+138
-106
lines changed

.github/workflows/ci.yml

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ on:
77
branches: [main]
88

99
jobs:
10+
lint-and-format:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- uses: denoland/setup-deno@v1
15+
- run: deno fmt --check
16+
# TODO
17+
# - run: deno lint
18+
1019
test:
1120
strategy:
1221
matrix:

README.md

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# JSR npm command line tool
22

3-
The JSR npm CLI integrates JSR (JavaScript Registry) packages with npm-based projects, facilitating the use of JSR packages in environments that traditionally rely on npm. Learn more about JSR at [jsr.io](https://jsr.io).
3+
The JSR npm CLI integrates JSR (JavaScript Registry) packages with npm-based
4+
projects, facilitating the use of JSR packages in environments that
5+
traditionally rely on npm. Learn more about JSR at [jsr.io](https://jsr.io).
46

57
## Quick Start
68

@@ -10,7 +12,8 @@ Add a JSR package to your project:
1012
npx jsr add @package/name # 'install' and 'i' are also supported
1113
```
1214

13-
This command auto-updates your `package.json` and installs the package, automatically detecting and using your project's package manager.
15+
This command auto-updates your `package.json` and installs the package,
16+
automatically detecting and using your project's package manager.
1417

1518
## How It Works
1619

@@ -20,9 +23,11 @@ The CLI creates or updates a `.npmrc` file in your project with:
2023
@jsr:registry=https://npm.jsr.io
2124
```
2225

23-
This line redirects npm to fetch JSR packages from the JSR registry instead of the default npm registry.
26+
This line redirects npm to fetch JSR packages from the JSR registry instead of
27+
the default npm registry.
2428

25-
Packages are added to `package.json` with an alias, mapping the JSR package name to the npm registry URL hosted by JSR, like so:
29+
Packages are added to `package.json` with an alias, mapping the JSR package name
30+
to the npm registry URL hosted by JSR, like so:
2631

2732
```json
2833
{
@@ -32,7 +37,8 @@ Packages are added to `package.json` with an alias, mapping the JSR package name
3237
}
3338
```
3439

35-
This ensures that the package is fetched from JSR when you run npm install commands.
40+
This ensures that the package is fetched from JSR when you run npm install
41+
commands.
3642

3743
## Commands
3844

@@ -43,14 +49,18 @@ This ensures that the package is fetched from JSR when you run npm install comma
4349
## Limitations
4450

4551
- `jsr:` import specifiers are not supported.
46-
- Due to transpilation, the developer experience in editors might differ from native JSR usage.
52+
- Due to transpilation, the developer experience in editors might differ from
53+
native JSR usage.
4754

48-
For the best developer experience and to fully leverage JSR's capabilities, consider environments with native JSR support like Deno.
55+
For the best developer experience and to fully leverage JSR's capabilities,
56+
consider environments with native JSR support like Deno.
4957

5058
## Contributing
5159

52-
We welcome contributions and feedback. Visit our GitHub repository to contribute or report issues.
60+
We welcome contributions and feedback. Visit our GitHub repository to contribute
61+
or report issues.
5362

5463
## License
5564

56-
This CLI is available under the [MIT License](https://opensource.org/licenses/MIT).
65+
This CLI is available under the
66+
[MIT License](https://opensource.org/licenses/MIT).

src/bin.ts

+49-39
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,62 @@ function printHelp() {
2626
console.log(`jsr.io cli for node
2727
2828
Usage:
29-
${prettyPrintRow([
30-
["jsr add @std/log", 'Install the "@std/log" package from jsr.io'],
31-
["jsr remove @std/log", 'Remove the "@std/log" package from the project'],
32-
])}
29+
${
30+
prettyPrintRow([
31+
["jsr add @std/log", 'Install the "@std/log" package from jsr.io'],
32+
["jsr remove @std/log", 'Remove the "@std/log" package from the project'],
33+
])
34+
}
3335
3436
Commands:
35-
${prettyPrintRow([
36-
["i, install, add", "Install one or more jsr packages"],
37-
["r, uninstall, remove", "Remove one or more jsr packages"],
38-
["publish", "Publish a package to the JSR registry."],
39-
])}
37+
${
38+
prettyPrintRow([
39+
["i, install, add", "Install one or more jsr packages"],
40+
["r, uninstall, remove", "Remove one or more jsr packages"],
41+
["publish", "Publish a package to the JSR registry."],
42+
])
43+
}
4044
4145
Options:
42-
${prettyPrintRow([
43-
[
44-
"-P, --save-prod",
45-
"Package will be added to dependencies. This is the default.",
46-
],
47-
["-D, --save-dev", "Package will be added to devDependencies."],
48-
["-O, --save-optional", "Package will be added to optionalDependencies."],
49-
["--npm", "Use npm to remove and install packages."],
50-
["--yarn", "Use yarn to remove and install packages."],
51-
["--pnpm", "Use pnpm to remove and install packages."],
52-
["--bun", "Use bun to remove and install packages."],
53-
["--verbose", "Show additional debugging information."],
54-
["-h, --help", "Show this help text."],
55-
["--version", "Print the version number."],
56-
])}
46+
${
47+
prettyPrintRow([
48+
[
49+
"-P, --save-prod",
50+
"Package will be added to dependencies. This is the default.",
51+
],
52+
["-D, --save-dev", "Package will be added to devDependencies."],
53+
["-O, --save-optional", "Package will be added to optionalDependencies."],
54+
["--npm", "Use npm to remove and install packages."],
55+
["--yarn", "Use yarn to remove and install packages."],
56+
["--pnpm", "Use pnpm to remove and install packages."],
57+
["--bun", "Use bun to remove and install packages."],
58+
["--verbose", "Show additional debugging information."],
59+
["-h, --help", "Show this help text."],
60+
["--version", "Print the version number."],
61+
])
62+
}
5763
5864
Publish Options:
59-
${prettyPrintRow([
60-
[
61-
"--token <Token>",
62-
"The API token to use when publishing. If unset, interactive authentication is be used.",
63-
],
64-
[
65-
"--dry-run",
66-
"Prepare the package for publishing performing all checks and validations without uploading.",
67-
],
68-
["--allow-slow-types", "Allow publishing with slow types."],
69-
])}
65+
${
66+
prettyPrintRow([
67+
[
68+
"--token <Token>",
69+
"The API token to use when publishing. If unset, interactive authentication is be used.",
70+
],
71+
[
72+
"--dry-run",
73+
"Prepare the package for publishing performing all checks and validations without uploading.",
74+
],
75+
["--allow-slow-types", "Allow publishing with slow types."],
76+
])
77+
}
7078
7179
Environment variables:
72-
${prettyPrintRow([
73-
["JSR_URL", "Use a different registry url for the publish command"],
74-
])}
80+
${
81+
prettyPrintRow([
82+
["JSR_URL", "Use a different registry url for the publish command"],
83+
])
84+
}
7585
`);
7686
}
7787

@@ -122,7 +132,7 @@ if (args.length === 0) {
122132
process.exit(0);
123133
} else if (options.values.version) {
124134
const version = JSON.parse(
125-
fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf-8")
135+
fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf-8"),
126136
).version as string;
127137
console.log(version);
128138
process.exit(0);

src/commands.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import * as path from "node:path";
33
import * as fs from "node:fs";
44
import * as kl from "kolorist";
5-
import { JsrPackage, exec, fileExists } from "./utils";
6-
import { Bun, PkgManagerName, getPkgManager } from "./pkg_manager";
5+
import { exec, fileExists, JsrPackage } from "./utils";
6+
import { Bun, getPkgManager, PkgManagerName } from "./pkg_manager";
77
import { downloadDeno, getDenoDownloadUrl } from "./download";
88

99
const NPMRC_FILE = ".npmrc";
@@ -112,7 +112,7 @@ export async function publish(cwd: string, options: PublishOptions) {
112112
// in case jsr gets added to a project as a dependency where
113113
// developers use multiple OSes
114114
process.platform,
115-
process.platform === "win32" ? "deno.exe" : "deno"
115+
process.platform === "win32" ? "deno.exe" : "deno",
116116
);
117117

118118
// Check if deno executable is available, download it if not.

src/download.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function getDenoDownloadUrl(): Promise<DownloadInfo> {
3939
if (!res.ok) {
4040
await res.body?.cancel();
4141
throw new Error(
42-
`${res.status}: Unable to retrieve canary version information from ${DENO_CANARY_INFO_URL}.`
42+
`${res.status}: Unable to retrieve canary version information from ${DENO_CANARY_INFO_URL}.`,
4343
);
4444
}
4545
const sha = (await res.text()).trim();
@@ -54,7 +54,7 @@ export async function getDenoDownloadUrl(): Promise<DownloadInfo> {
5454

5555
export async function downloadDeno(
5656
binPath: string,
57-
info: DownloadInfo
57+
info: DownloadInfo,
5858
): Promise<void> {
5959
const binFolder = path.dirname(binPath);
6060

@@ -93,13 +93,13 @@ export async function downloadDeno(
9393
// Delete downloaded file
9494
await fs.promises.rm(file);
9595
},
96-
{ max: contentLen }
96+
{ max: contentLen },
9797
);
9898
}
9999

100100
async function withProgressBar<T>(
101101
fn: (tick: (n: number) => void) => Promise<T>,
102-
options: { max: number }
102+
options: { max: number },
103103
): Promise<T> {
104104
let current = 0;
105105
let start = Date.now();
@@ -123,7 +123,7 @@ async function withProgressBar<T>(
123123

124124
const bar = "#".repeat((barLength / 100) * percent) + ">";
125125
const remaining = kl.blue(
126-
"-".repeat(Math.max(barLength - bar.length, 0))
126+
"-".repeat(Math.max(barLength - bar.length, 0)),
127127
);
128128
s += ` [${kl.cyan(bar)}${remaining}] `;
129129
}
@@ -152,7 +152,7 @@ async function withProgressBar<T>(
152152
}
153153

154154
async function* streamToAsyncIterable<T>(
155-
stream: ReadableStream<T>
155+
stream: ReadableStream<T>,
156156
): AsyncIterable<T> {
157157
const reader = stream.getReader();
158158
try {

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright 2024 the JSR authors. MIT license.
22
export {
33
install,
4-
remove,
54
type InstallOptions,
65
publish,
76
type PublishOptions,
7+
remove,
88
} from "./commands";
99
export { JsrPackage, JsrPackageNameError } from "./utils";

src/pkg_manager.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2024 the JSR authors. MIT license.
22
import { InstallOptions } from "./commands";
3-
import { JsrPackage, exec, findProjectDir } from "./utils";
3+
import { exec, findProjectDir, JsrPackage } from "./utils";
44
import * as kl from "kolorist";
55

66
async function execWithLog(cmd: string, args: string[], cwd: string) {
@@ -18,7 +18,7 @@ function modeToFlag(mode: InstallOptions["mode"]): string {
1818

1919
function toPackageArgs(pkgs: JsrPackage[]): string[] {
2020
return pkgs.map(
21-
(pkg) => `@${pkg.scope}/${pkg.name}@npm:${pkg.toNpmPackage()}`
21+
(pkg) => `@${pkg.scope}/${pkg.name}@npm:${pkg.toNpmPackage()}`,
2222
);
2323
}
2424

@@ -46,7 +46,7 @@ class Npm implements PackageManager {
4646
await execWithLog(
4747
"npm",
4848
["remove", ...packages.map((pkg) => pkg.toString())],
49-
this.cwd
49+
this.cwd,
5050
);
5151
}
5252
}
@@ -68,7 +68,7 @@ class Yarn implements PackageManager {
6868
await execWithLog(
6969
"yarn",
7070
["remove", ...packages.map((pkg) => pkg.toString())],
71-
this.cwd
71+
this.cwd,
7272
);
7373
}
7474
}
@@ -90,7 +90,7 @@ class Pnpm implements PackageManager {
9090
await execWithLog(
9191
"yarn",
9292
["remove", ...packages.map((pkg) => pkg.toString())],
93-
this.cwd
93+
this.cwd,
9494
);
9595
}
9696
}
@@ -112,7 +112,7 @@ export class Bun implements PackageManager {
112112
await execWithLog(
113113
"bun",
114114
["remove", ...packages.map((pkg) => pkg.toString())],
115-
this.cwd
115+
this.cwd,
116116
);
117117
}
118118
}
@@ -129,14 +129,15 @@ function getPkgManagerFromEnv(value: string): PkgManagerName | null {
129129

130130
export async function getPkgManager(
131131
cwd: string,
132-
pkgManagerName: PkgManagerName | null
132+
pkgManagerName: PkgManagerName | null,
133133
) {
134134
const envPkgManager = process.env.npm_config_user_agent;
135-
const fromEnv =
136-
envPkgManager !== undefined ? getPkgManagerFromEnv(envPkgManager) : null;
135+
const fromEnv = envPkgManager !== undefined
136+
? getPkgManagerFromEnv(envPkgManager)
137+
: null;
137138

138139
const { projectDir, pkgManagerName: fromLockfile } = await findProjectDir(
139-
cwd
140+
cwd,
140141
);
141142

142143
const result = pkgManagerName || fromEnv || fromLockfile || "npm";

src/utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ export class JsrPackage {
3838
}
3939

4040
throw new JsrPackageNameError(
41-
`Invalid jsr package name: A jsr package name must have the format @<scope>/<name>, but got "${input}"`
41+
`Invalid jsr package name: A jsr package name must have the format @<scope>/<name>, but got "${input}"`,
4242
);
4343
}
4444

4545
private constructor(
4646
public scope: string,
4747
public name: string,
48-
public version: string | null
48+
public version: string | null,
4949
) {}
5050

5151
toNpmPackage(): string {
@@ -80,7 +80,7 @@ export async function findProjectDir(
8080
projectDir: cwd,
8181
pkgManagerName: null,
8282
pkgJsonPath: null,
83-
}
83+
},
8484
): Promise<ProjectInfo> {
8585
const npmLockfile = path.join(dir, "package-lock.json");
8686
if (await fileExists(npmLockfile)) {
@@ -154,7 +154,7 @@ export async function exec(
154154
cmd: string,
155155
args: string[],
156156
cwd: string,
157-
env?: Record<string, string | undefined>
157+
env?: Record<string, string | undefined>,
158158
) {
159159
const cp = spawn(cmd, args, { stdio: "inherit", cwd, shell: true, env });
160160

0 commit comments

Comments
 (0)