Skip to content

Commit a8e7f1f

Browse files
committed
feat: move javascript files to native typescript
1 parent 7905ec5 commit a8e7f1f

File tree

15 files changed

+208
-171
lines changed

15 files changed

+208
-171
lines changed

CHANGELOG.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@
99
> **Note**: These changes only apply if you use `sentry-cli` through the npm package [`@sentry/cli`](https://www.npmjs.com/package/@sentry/cli). If you use the standalone binary, these changes do not affect you.
1010

1111
- Drop support for Node.js <18. The minimum required Node.js version is now 18.0.0 ([#2985](https://github.com/getsentry/sentry-cli/issues/2985)).
12+
- The JavaScript wrapper now uses named exports instead of default exports ([#2989](https://github.com/getsentry/sentry-cli/pull/2989))
13+
. You need to update your imports:
14+
```js
15+
// Old (default import)
16+
const SentryCli = require('@sentry/cli');
17+
18+
// New (named import)
19+
const { SentryCli } = require('@sentry/cli');
20+
```
21+
22+
For ESM imports:
23+
```js
24+
// Old
25+
import SentryCli from '@sentry/cli';
26+
27+
// New
28+
import { SentryCli } from '@sentry/cli';
29+
```
30+
1231

1332
## 2.58.2
1433

@@ -517,7 +536,7 @@ We made several refactors and added several tests in this release. These changes
517536

518537
<details>
519538
<summary><h3>Changes to tests</h3></summary>
520-
539+
521540
- ref(test): Broaden `with_header_matcher` types (#2261) by @szokeasaurusrex
522541
- ref(test): Accept `impl Into<Matcher>` for `with_matcher` (#2260) by @szokeasaurusrex
523542
- ref(test): Align `with_reponse_body` parameter to `mockito` (#2259) by @szokeasaurusrex

bin/sentry-cli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'use strict';
44

55
const childProcess = require('child_process');
6-
const SentryCli = require('../js');
6+
const { SentryCli } = require('../js');
77

88
const child = childProcess
99
.spawn(SentryCli.getPath(), process.argv.slice(2), {

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module.exports = {
22
setupFiles: ['<rootDir>/setupTests.js'],
33
testPathIgnorePatterns: ['<rootDir>/src/'],
4+
transform: {
5+
'^.+\\.ts$': 'ts-jest',
6+
},
47
};

lib/__tests__/helper.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const os = require('os');
22

33
const helper = require('../helper');
44

5-
const SOURCEMAPS_OPTIONS = require('../releases/options/uploadSourcemaps');
5+
const { SOURCEMAPS_OPTIONS } = require('../releases/options/uploadSourcemaps');
66

77
describe('SentryCli helper', () => {
88
test('call sentry-cli --version', () => {

lib/helper.js renamed to lib/helper.ts

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
22

3-
const os = require('os');
4-
const path = require('path');
5-
const fs = require('fs');
6-
const childProcess = require('child_process');
3+
import * as os from 'node:os';
4+
import * as path from 'node:path';
5+
import * as fs from 'node:fs';
6+
import * as childProcess from 'node:child_process';
7+
import { SentryCliOptions } from './types';
78

89
const BINARY_DISTRIBUTIONS = [
910
{ packageName: '@sentry/cli-darwin', subpath: 'bin/sentry-cli' },
@@ -23,9 +24,9 @@ const BINARY_DISTRIBUTIONS = [
2324
* Without this, the binary can be detected as an asset and included by bundlers
2425
* that use @vercel/nft.
2526
*
26-
* @returns {string} The path to the sentry-cli binary
27+
* @returns The path to the sentry-cli binary
2728
*/
28-
function getFallbackBinaryPath() {
29+
function getFallbackBinaryPath(): string {
2930
const parts = [];
3031
parts.push(__dirname);
3132
parts.push('..');
@@ -93,9 +94,9 @@ function getDistributionForThisPlatform() {
9394
/**
9495
* Throws an error with a message stating that Sentry CLI doesn't support the current platform.
9596
*
96-
* @returns {never} nothing. It throws.
97+
* @returns nothing. It throws.
9798
*/
98-
function throwUnsupportedPlatformError() {
99+
function throwUnsupportedPlatformError(): void {
99100
throw new Error(
100101
`Unsupported operating system or architecture! Sentry CLI does not work on this architecture.
101102
@@ -110,9 +111,9 @@ Sentry CLI supports:
110111
* Tries to find the installed Sentry CLI binary - either by looking into the relevant
111112
* optional dependencies or by trying to resolve the fallback binary.
112113
*
113-
* @returns {string} The path to the sentry-cli binary
114+
* @returns The path to the sentry-cli binary
114115
*/
115-
function getBinaryPath() {
116+
function getBinaryPath(): string {
116117
if (process.env.SENTRY_BINARY_PATH) {
117118
return process.env.SENTRY_BINARY_PATH;
118119
}
@@ -161,47 +162,41 @@ It seems like none of the "@sentry/cli" package's optional dependencies got inst
161162

162163
/**
163164
* Will be used as the binary path when defined with `mockBinaryPath`.
164-
* @type {string | undefined}
165165
*/
166-
let mockedBinaryPath;
166+
let mockedBinaryPath: string | undefined;
167167

168168
/**
169169
* Overrides the default binary path with a mock value, useful for testing.
170170
*
171-
* @param {string} mockPath The new path to the mock sentry-cli binary
171+
* @param mockPath The new path to the mock sentry-cli binary
172172
* @deprecated This was used in tests internally and will be removed in the next major version.
173173
*/
174174
// TODO(v3): Remove this function
175-
function mockBinaryPath(mockPath) {
175+
function mockBinaryPath(mockPath: string) {
176176
mockedBinaryPath = mockPath;
177177
}
178178

179-
/**
180-
* The javascript type of a command line option.
181-
* @typedef {'array'|'string'|'boolean'|'inverted-boolean'} OptionType
182-
*/
183-
184-
/**
185-
* Schema definition of a command line option.
186-
* @typedef {object} OptionSchema
187-
* @prop {string} param The flag of the command line option including dashes.
188-
* @prop {OptionType} type The value type of the command line option.
189-
* @prop {string} [invertedParam] The flag of the command line option including dashes (optional).
190-
*/
191-
192-
/**
193-
* Schema definition for a command.
194-
* @typedef {Object.<string, OptionSchema>} OptionsSchema
195-
*/
179+
export type OptionsSchema = Record<
180+
string,
181+
| {
182+
param: string;
183+
type: 'array' | 'string' | 'number' | 'boolean' | 'inverted-boolean';
184+
invertedParam?: string;
185+
}
186+
| {
187+
param?: never;
188+
type: 'array' | 'string' | 'number' | 'boolean' | 'inverted-boolean';
189+
invertedParam: string;
190+
}
191+
>;
196192

197193
/**
198194
* Serializes command line options into an arguments array.
199195
*
200-
* @param {OptionsSchema} schema An options schema required by the command.
201-
* @param {object} options An options object according to the schema.
202-
* @returns {string[]} An arguments array that can be passed via command line.
196+
* @param schema An options schema required by the command.
197+
* @param options An options object according to the schema.
203198
*/
204-
function serializeOptions(schema, options) {
199+
function serializeOptions(schema: OptionsSchema, options: Record<string, unknown>): string[] {
205200
return Object.keys(schema).reduce((newOptions, option) => {
206201
const paramValue = options[option];
207202
if (paramValue === undefined || paramValue === null) {
@@ -246,20 +241,23 @@ function serializeOptions(schema, options) {
246241
/**
247242
* Serializes the command and its options into an arguments array.
248243
*
249-
* @param {string[]} command The literal name of the command.
250-
* @param {OptionsSchema} [schema] An options schema required by the command.
251-
* @param {object} [options] An options object according to the schema.
252-
* @returns {string[]} An arguments array that can be passed via command line.
244+
* @param command The literal name of the command.
245+
* @param schema An options schema required by the command.
246+
* @param options An options object according to the schema.
247+
* @returns An arguments array that can be passed via command line.
253248
*/
254-
function prepareCommand(command, schema, options) {
249+
function prepareCommand(
250+
command: string[],
251+
schema: OptionsSchema,
252+
options: Record<string, unknown>
253+
): string[] {
255254
return command.concat(serializeOptions(schema || {}, options || {}));
256255
}
257256

258257
/**
259258
* Returns the absolute path to the `sentry-cli` binary.
260-
* @returns {string}
261259
*/
262-
function getPath() {
260+
function getPath(): string {
263261
return mockedBinaryPath !== undefined ? mockedBinaryPath : getBinaryPath();
264262
}
265263

@@ -280,18 +278,24 @@ function getPath() {
280278
* const output = await execute(['--version']);
281279
* expect(output.trim()).toBe('sentry-cli x.y.z');
282280
*
283-
* @param {string[]} args Command line arguments passed to `sentry-cli`.
284-
* @param {boolean | 'rejectOnError'} live can be set to:
281+
* @param args Command line arguments passed to `sentry-cli`.
282+
* @param live can be set to:
285283
* - `true` to inherit stdio to display `sentry-cli` output directly.
286284
* - `false` to not inherit stdio and return the output as a string.
287285
* - `'rejectOnError'` to inherit stdio and reject the promise if the command
288286
* exits with a non-zero exit code.
289-
* @param {boolean} silent Disable stdout for silents build (CI/Webpack Stats, ...)
290-
* @param {string} [configFile] Relative or absolute path to the configuration file.
291-
* @param {import('./index').SentryCliOptions} [config] More configuration to pass to the CLI
292-
* @returns {Promise<string>} A promise that resolves to the standard output.
287+
* @param silent Disable stdout for silents build (CI/Webpack Stats, ...)
288+
* @param configFile Relative or absolute path to the configuration file.
289+
* @param config More configuration to pass to the CLI
290+
* @returns A promise that resolves to the standard output.
293291
*/
294-
async function execute(args, live, silent, configFile, config = {}) {
292+
async function execute(
293+
args: string[],
294+
live: boolean | 'rejectOnError',
295+
silent: boolean,
296+
configFile: string | undefined,
297+
config: SentryCliOptions = {}
298+
): Promise<string> {
295299
const env = { ...process.env };
296300
if (configFile) {
297301
env.SENTRY_PROPERTIES = configFile;
@@ -365,7 +369,7 @@ function getProjectFlagsFromOptions({ projects = [] } = {}) {
365369
return projects.reduce((flags, project) => flags.concat('-p', project), []);
366370
}
367371

368-
module.exports = {
372+
export {
369373
execute,
370374
getPath,
371375
getProjectFlagsFromOptions,
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
'use strict';
22

3-
const pkgInfo = require('../package.json');
4-
const helper = require('./helper');
5-
const Releases = require('./releases');
3+
import * as pkgInfo from '../package.json';
4+
import * as helper from './helper';
5+
import Releases from './releases';
6+
import type { SentryCliOptions } from './types';
67

7-
/**
8-
* @typedef {import('./types').SentryCliOptions} SentryCliOptions
9-
* @typedef {import('./types').SentryCliUploadSourceMapsOptions} SentryCliUploadSourceMapsOptions
10-
* @typedef {import('./types').SourceMapsPathDescriptor} SourceMapsPathDescriptor
11-
* @typedef {import('./types').SentryCliNewDeployOptions} SentryCliNewDeployOptions
12-
* @typedef {import('./types').SentryCliCommitsOptions} SentryCliCommitsOptions
13-
* @typedef {import('./types').SentryCliReleases} SentryCliReleases
14-
*/
8+
export type {
9+
SentryCliOptions,
10+
SentryCliUploadSourceMapsOptions,
11+
SourceMapsPathDescriptor,
12+
SentryCliNewDeployOptions,
13+
SentryCliCommitsOptions,
14+
SentryCliReleases,
15+
} from './types';
1516

1617
/**
1718
* Interface to and wrapper around the `sentry-cli` executable.
@@ -28,56 +29,55 @@ const Releases = require('./releases');
2829
* const release = await cli.releases.proposeVersion());
2930
* console.log(release);
3031
*/
31-
class SentryCli {
32+
export class SentryCli {
33+
public releases: Releases;
34+
3235
/**
3336
* Creates a new `SentryCli` instance.
3437
*
3538
* If the `configFile` parameter is specified, configuration located in the default
3639
* location and the value specified in the `SENTRY_PROPERTIES` environment variable is
3740
* overridden.
3841
*
39-
* @param {string | null} [configFile] - Path to Sentry CLI config properties, as described in https://docs.sentry.io/learn/cli/configuration/#properties-files.
42+
* @param configFile Path to Sentry CLI config properties, as described in https://docs.sentry.io/learn/cli/configuration/#properties-files.
4043
* By default, the config file is looked for upwards from the current path and defaults from ~/.sentryclirc are always loaded.
4144
* This value will update `SENTRY_PROPERTIES` env variable.
42-
* @param {SentryCliOptions} [options] - More options to pass to the CLI
45+
* @param options More options to pass to the CLI
4346
*/
44-
constructor(configFile, options) {
47+
constructor(public configFile: string | null, public options: SentryCliOptions) {
4548
if (typeof configFile === 'string') {
4649
this.configFile = configFile;
4750
}
4851
this.options = options || { silent: false };
49-
this.releases = new Releases({ ...this.options, configFile });
52+
this.releases = new Releases(this.options, configFile);
5053
}
5154

5255
/**
5356
* Returns the version of the installed `sentry-cli` binary.
54-
* @returns {string}
5557
*/
56-
static getVersion() {
58+
static getVersion(): string {
5759
return pkgInfo.version;
5860
}
5961

6062
/**
6163
* Returns an absolute path to the `sentry-cli` binary.
62-
* @returns {string}
6364
*/
64-
static getPath() {
65+
static getPath(): string {
6566
return helper.getPath();
6667
}
6768

6869
/**
6970
* See {helper.execute} docs.
70-
* @param {string[]} args Command line arguments passed to `sentry-cli`.
71-
* @param {boolean | 'rejectOnError'} live can be set to:
71+
*
72+
* @param args Command line arguments passed to `sentry-cli`.
73+
* @param live can be set to:
7274
* - `true` to inherit stdio to display `sentry-cli` output directly.
7375
* - `false` to not inherit stdio and return the output as a string.
7476
* - `'rejectOnError'` to inherit stdio and reject the promise if the command
7577
* exits with a non-zero exit code.
76-
* @returns {Promise<string>} A promise that resolves to the standard output.
78+
* @returns A promise that resolves to the standard output.
7779
*/
78-
execute(args, live) {
80+
execute(args: string[], live: boolean | 'rejectOnError'): Promise<string> {
7981
return helper.execute(args, live, this.options.silent, this.configFile, this.options);
8082
}
8183
}
82-
83-
module.exports = SentryCli;

lib/logger.js renamed to lib/logger.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
'use strict';
22

3-
const format = require('util').format;
3+
import { format } from 'node:util';
44

5-
module.exports = class Logger {
6-
constructor(stream) {
7-
this.stream = stream;
8-
}
5+
export class Logger {
6+
constructor(public stream: NodeJS.WriteStream) {}
97

108
log() {
119
const message = format(...arguments);

lib/releases/__tests__/index.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const SentryCli = require('../..');
1+
const { SentryCli } = require('../..');
22

33
describe('SentryCli releases', () => {
44
afterEach(() => {
@@ -23,7 +23,7 @@ describe('SentryCli releases', () => {
2323
beforeEach(() => {
2424
mockExecute.mockClear();
2525
// eslint-disable-next-line global-require
26-
const SentryCliLocal = require('../..');
26+
const { SentryCli: SentryCliLocal } = require('../..');
2727
cli = new SentryCliLocal();
2828
});
2929
describe('new', () => {

0 commit comments

Comments
 (0)