Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/two-books-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@definitelytyped/definitions-parser": patch
"@definitelytyped/utils": patch
---

Switch back to official npm libs
2 changes: 1 addition & 1 deletion packages/definitions-parser/src/data-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs";
import { writeJson, joinPaths, readFileAndWarn } from "@definitelytyped/utils";
import { dataDirPath } from "./lib/settings";

export function readDataFile(generatedBy: string, fileName: string): Promise<object> {
export function readDataFile(generatedBy: string, fileName: string): Promise<Record<string, unknown>> {
return readFileAndWarn(generatedBy, dataFilePath(fileName));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/publisher/src/lib/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface ChangedPackages {
}

export async function readChangedPackages(allPackages: AllPackages): Promise<ChangedPackages> {
const json = (await readDataFile("calculate-versions", versionsFilename)) as ChangedPackagesJson;
const json = (await readDataFile("calculate-versions", versionsFilename)) as unknown as ChangedPackagesJson;
return {
changedTypings: await Promise.all(
json.changedTypings.map(async ({ id, version, latestVersion }) => ({
Expand Down
10 changes: 7 additions & 3 deletions packages/publisher/src/publish-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default async function publishRegistry(dt: FS, allPackages: AllPackages,
}
}

async function generate(registry: string, packageJson: {}): Promise<void> {
async function generate(registry: string, packageJson: Record<string, unknown>): Promise<void> {
await fs.promises.rm(registryOutputPath, { recursive: true, force: true });
await fs.promises.mkdir(registryOutputPath, { recursive: true });
await writeOutputJson("package.json", packageJson);
Expand All @@ -118,7 +118,7 @@ async function generate(registry: string, packageJson: {}): Promise<void> {
async function publish(
client: NpmPublishClient,
packageName: string,
packageJson: {},
packageJson: Record<string, unknown>,
version: string,
dry: boolean,
log: Logger,
Expand Down Expand Up @@ -211,7 +211,11 @@ function assertJsonNewer(newer: { [s: string]: any }, older: { [s: string]: any
}
}

function generatePackageJson(name: string, version: string, typesPublisherContentHash: string): object {
function generatePackageJson(
name: string,
version: string,
typesPublisherContentHash: string,
): Record<string, unknown> {
const json = {
name,
version,
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@
"url": "https://github.com/microsoft/DefinitelyTyped-tools/issues"
},
"dependencies": {
"@qiwi/npm-registry-client": "^8.9.1",
"@types/node": "^25.1.0",
"cachedir": "^2.4.0",
"charm": "^1.0.2",
"libnpmpublish": "^11.1.3",
"minimatch": "^10.1.1",
"npm-registry-fetch": "^19.1.1",
"tar": "^7.5.7",
"tar-stream": "^3.1.7",
"which": "^6.0.0"
},
"devDependencies": {
"@qiwi/npm-types": "^1.0.3",
"@types/charm": "^1.0.6",
"@types/libnpmpublish": "^9.0.1",
"@types/npm-registry-fetch": "^8.0.8",
"@types/tar-stream": "^3.1.4",
"@types/which": "^3.0.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function readFileSync(path: string): string {
}

/** If a file doesn't exist, warn and tell the step it should have been generated by. */
export async function readFileAndWarn(generatedBy: string, filePath: string): Promise<object> {
export async function readFileAndWarn(generatedBy: string, filePath: string): Promise<Record<string, unknown>> {
try {
return await readJson(filePath, isObject);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/miscellany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function identity<T>(t: T): T {
return t;
}

export function isObject(value: unknown): value is object {
export function isObject(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === "object";
}

Expand Down
115 changes: 56 additions & 59 deletions packages/utils/src/npm.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,86 @@
import * as os from "os";
import process from "process";
import fs from "fs";
import RegClient from "@qiwi/npm-registry-client";
import { resolve as resolveUrl } from "url";
import { publish } from "libnpmpublish";
import npmFetch from "npm-registry-fetch";
import { joinPaths } from "./fs";
import { Logger } from "./logging";
import { createTgz } from "./io";
import { createTgz, streamToBuffer } from "./io";

export const npmRegistryHostName = "registry.npmjs.org";
export const npmRegistry = `https://${npmRegistryHostName}/`;

export const cacheDir = joinPaths(process.env.GITHUB_ACTIONS ? joinPaths(__dirname, "../../..") : os.tmpdir(), "cache");

type NeedToFixNpmRegistryClientTypings = any;

export class NpmPublishClient {
static async create(token: string, config?: NeedToFixNpmRegistryClientTypings): Promise<NpmPublishClient> {
return new NpmPublishClient(new RegClient(config), { token }, npmRegistry);
static async create(token: string, _config?: {}): Promise<NpmPublishClient> {
Comment thread
jakebailey marked this conversation as resolved.
Outdated
return new NpmPublishClient(token, npmRegistry);
}

private constructor(
private readonly client: RegClient,
private readonly auth: NeedToFixNpmRegistryClientTypings,
private readonly token: string,
private readonly registry: string,
) {}

async publish(publishedDirectory: string, packageJson: {}, dry: boolean, log: Logger): Promise<void> {
const readme = await fs.promises.readFile(joinPaths(publishedDirectory, "README.md"));
async publish(
publishedDirectory: string,
packageJson: Record<string, unknown>,
dry: boolean,
log: Logger,
): Promise<void> {
if (dry) {
log(`(dry) Skip publish of ${publishedDirectory} to ${this.registry}`);
return;
}

const tarballBuffer = await streamToBuffer(
createTgz(publishedDirectory, (err) => {
throw err;
}),
);

return new Promise<void>((resolve, reject) => {
const body = createTgz(publishedDirectory, reject);
const metadata = { readme, ...packageJson };
if (dry) {
log(`(dry) Skip publish of ${publishedDirectory} to ${this.registry}`);
}
resolve(
dry
? undefined
: promisifyVoid((cb) => {
this.client.publish(
this.registry,
{
access: "public",
auth: this.auth,
metadata: metadata as NeedToFixNpmRegistryClientTypings,
body: body as NeedToFixNpmRegistryClientTypings,
},
cb,
);
}),
);
await publish(packageJson as { name: string; version: string }, tarballBuffer, {
registry: this.registry,
token: this.token,
access: "public",
});
}

tag(packageName: string, version: string, distTag: string, dry: boolean, log: Logger): Promise<void> {
async tag(packageName: string, version: string, distTag: string, dry: boolean, log: Logger): Promise<void> {
if (dry) {
log(`(dry) Skip tag of ${packageName}@${distTag} as ${version}`);
return Promise.resolve();
return;
}
return promisifyVoid((cb) => {
this.client.distTags.add(this.registry, { package: packageName, version, distTag, auth: this.auth }, cb);
});
}

deprecate(packageName: string, version: string, message: string): Promise<void> {
const url = resolveUrl(npmRegistry, packageName);
const params = {
message,
version,
auth: this.auth,
};
return promisifyVoid((cb) => {
this.client.deprecate(url, params, cb);
await npmFetch(`/-/package/${encodeURIComponent(packageName)}/dist-tags/${encodeURIComponent(distTag)}`, {
method: "PUT",
registry: this.registry,
token: this.token,
body: JSON.stringify(version),
headers: {
"content-type": "application/json",
},
});
}
}

function promisifyVoid(callsBack: (cb: (error: Error | undefined) => void) => void): Promise<void> {
return new Promise<void>((resolve, reject) => {
callsBack((error) => {
if (error) {
reject(error);
} else {
resolve();
}
async deprecate(packageName: string, version: string, message: string): Promise<void> {
// Fetch the current package metadata
const packument = (await npmFetch.json(`/${encodeURIComponent(packageName)}`, {
registry: this.registry,
token: this.token,
})) as { versions: Record<string, { deprecated?: string }> };

// Update the deprecated field for the specified version
if (!packument.versions[version]) {
throw new Error(`Version ${version} not found in ${packageName}`);
}
packument.versions[version].deprecated = message;

// PUT the updated packument back
await npmFetch(`/${encodeURIComponent(packageName)}`, {
method: "PUT",
registry: this.registry,
token: this.token,
body: packument,
});
});
}
}
Loading
Loading