Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Add gitHead to package.json #543

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions src/full.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as yargs from "yargs";

import appInsights = require("applicationinsights");
import { Fetcher } from "./util/io";
import calculateVersions from "./calculate-versions";
import clean from "./clean";
import createSearchIndex from "./create-search-index";
import generatePackages from "./generate-packages";
import { getDefinitelyTyped } from "./get-definitely-typed";
import { getDefinitelyTyped, resolveDefinitelyTypedMaster } from "./get-definitely-typed";
import { Options } from "./lib/common";
import { UncachedNpmInfoClient } from "./lib/npm-client";
import parseDefinitions from "./parse-definitions";
import publishPackages from "./publish-packages";
import publishRegistry from "./publish-registry";
import uploadBlobsAndUpdateIssue from "./upload-blobs";
import { Fetcher } from "./util/io";
import { assertDefined, currentTimeStamp, logUncaughtErrors, numberOfOsProcesses } from "./util/util";
import validate from "./validate";

Expand All @@ -26,12 +26,13 @@ if (!module.parent) {
export default async function full(dry: boolean, timeStamp: string, githubAccessToken: string, fetcher: Fetcher, options: Options): Promise<void> {
const infoClient = new UncachedNpmInfoClient();
await clean();
const dt = await getDefinitelyTyped(options);
const commit = await resolveDefinitelyTypedMaster(githubAccessToken, fetcher);
const dt = await getDefinitelyTyped(options, commit);
const allPackages = await parseDefinitions(dt, options.parseInParallel
? { nProcesses: numberOfOsProcesses, definitelyTypedPath: assertDefined(options.definitelyTypedPath) }
: undefined);
const changedPackages = await calculateVersions(dt, infoClient);
await generatePackages(dt, allPackages, changedPackages);
await generatePackages(dt, commit, allPackages, changedPackages);
await createSearchIndex(allPackages, infoClient);
await publishPackages(changedPackages, dry, githubAccessToken, fetcher);
await publishRegistry(dt, allPackages, dry, infoClient);
Expand Down
14 changes: 9 additions & 5 deletions src/generate-packages.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import { emptyDir } from "fs-extra";
import * as yargs from "yargs";

import { FS, getDefinitelyTyped } from "./get-definitely-typed";
import { FS, getDefinitelyTyped, resolveDefinitelyTypedMaster } from "./get-definitely-typed";
import { Options } from "./lib/common";
import { generateNotNeededPackage, generateTypingPackage } from "./lib/package-generator";
import { AllPackages } from "./lib/packages";
import { outputDirPath } from "./lib/settings";
import { ChangedPackages, readChangedPackages } from "./lib/versions";
import { Fetcher } from "./util/io";
import { logger, writeLog } from "./util/logging";
import { writeTgz } from "./util/tgz";
import { logUncaughtErrors } from "./util/util";

if (!module.parent) {
const tgz = !!yargs.argv.tgz;
logUncaughtErrors(async () => {
const dt = await getDefinitelyTyped(Options.defaults);
const githubAccessToken = process.env["GH_API_TOKEN"] || "";
const fetcher = new Fetcher();
const commit = await resolveDefinitelyTypedMaster(githubAccessToken, fetcher);
const dt = await getDefinitelyTyped(Options.defaults, commit);
const allPackages = await AllPackages.read(dt);
await generatePackages(dt, allPackages, await readChangedPackages(allPackages), tgz);
await generatePackages(dt, commit, allPackages, await readChangedPackages(allPackages), tgz);
});
}

export default async function generatePackages(dt: FS, allPackages: AllPackages, changedPackages: ChangedPackages, tgz = false): Promise<void> {
export default async function generatePackages(dt: FS, gitHead: string, allPackages: AllPackages, changedPackages: ChangedPackages, tgz = false): Promise<void> {
const [log, logResult] = logger();
log("\n## Generating packages\n");

await emptyDir(outputDirPath);

for (const { pkg, version } of changedPackages.changedTypings) {
await generateTypingPackage(pkg, allPackages, version, dt);
await generateTypingPackage(pkg, allPackages, version, dt, gitHead);
if (tgz) {
await writeTgz(pkg.outputDirectory, `${pkg.outputDirectory}.tgz`);
}
Expand Down
17 changes: 13 additions & 4 deletions src/get-definitely-typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import * as zlib from "zlib";

import { Options } from "./lib/common";
import { dataDirPath, definitelyTypedZipUrl } from "./lib/settings";
import { readFile, readJson, stringOfStream } from "./util/io";
import { assertDefined, assertSorted, Awaitable, exec, joinPaths, withoutStart, logUncaughtErrors } from "./util/util";
import { Fetcher, readFile, readJson, stringOfStream } from "./util/io";
import { assertDefined, assertSorted, Awaitable, exec, joinPaths, logUncaughtErrors, withoutStart } from "./util/util";
import { queryGithub } from "./util/github";

/**
* Readonly filesystem.
Expand Down Expand Up @@ -43,10 +44,18 @@ if (!module.parent) {
});
}

export async function getDefinitelyTyped(options: Options): Promise<FS> {
/**
* Return the commit SHA1 of the master tip of DefinitelyTyped
*/
export async function resolveDefinitelyTypedMaster(githubAccessToken: string, fetcher: Fetcher): Promise<string> {
const masterRef = await queryGithub("repos/DefinitelyTyped/DefinitelyTyped/git/refs/heads/master", githubAccessToken, fetcher) as { object: { sha: string } };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is master the correct SHA to use here? In particular, it's possible that it may have moved between the time that this package was merged. Another package may have been merged in between.

I think it would be better if you use the latest sha just for the package's directory.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really matter - this SHA will be used to download the zip, so it is guaranteed that gitHead points to the exact revision that is being published. That is all that should matter.

It would be nice, but I wouldn't know where to get that commit from. Can you point to where?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here’s the code I used, although that was using octokit. The rest call shouldn’t be too different though.

DefinitelyTyped/types-publisher-watchdog@f647115#diff-168726dbe96b3ce427e7fedce31bb0bcL76

Look at the part I deleted starting with const oops

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so you would get the latest commit from the git log of that folder. I thought you were suggesting to use the commit ID from the triggering event, e.g. the commit hash of the merge commit of a PR that was merged which triggered the new publish

return masterRef.object.sha
}

export async function getDefinitelyTyped(options: Options, revision: string = 'master'): Promise<FS> {
if (options.definitelyTypedPath === undefined) {
await ensureDir(dataDirPath);
return downloadAndExtractFile(definitelyTypedZipUrl);
return downloadAndExtractFile(definitelyTypedZipUrl + "/" + revision);
} else {
const { error, stderr, stdout } = await exec("git diff --name-only", options.definitelyTypedPath);
if (error) { throw error; }
Expand Down
7 changes: 4 additions & 3 deletions src/lib/package-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import { sourceBranch } from "./settings";

const mitLicense = readFileSync(joinPaths(__dirname, "..", "..", "LICENSE"), "utf-8");

export async function generateTypingPackage(typing: TypingsData, packages: AllPackages, version: string, dt: FS): Promise<void> {
export async function generateTypingPackage(typing: TypingsData, packages: AllPackages, version: string, dt: FS, gitHead: string): Promise<void> {
const typesDirectory = dt.subDir("types").subDir(typing.name);
const packageFS = typing.isLatest ? typesDirectory : typesDirectory.subDir(`v${typing.major}`);

const packageJson = await createPackageJSON(typing, version, packages);
const packageJson = await createPackageJSON(typing, version, gitHead, packages);
await writeCommonOutputs(typing, packageJson, createReadme(typing));
await Promise.all(typing.files.map(async file => writeFile(await outputFilePath(typing, file), await packageFS.readFile(file))));
}
Expand Down Expand Up @@ -52,11 +52,12 @@ async function outputFilePath(pkg: AnyPackage, filename: string): Promise<string

interface Dependencies { [name: string]: string; }

async function createPackageJSON(typing: TypingsData, version: string, packages: AllPackages): Promise<string> {
async function createPackageJSON(typing: TypingsData, version: string, gitHead: string, packages: AllPackages): Promise<string> {
// Use the ordering of fields from https://docs.npmjs.com/files/package.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the documentation doesn't mention gitHead, so I suspect this comment isn't true for gitHead. I'm not sure whether that matters though.

Any idea how to find out where npm's package.json puts gitHead? I tried looking at unpgk's version but it doesn't appear to be there either.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can find it in the registry API response: http://registry.npmjs.org/commander/latest
And npm should also write it out in node_modules when installing.
npm info commander gitHead also returns it (or npm info commander --json).

But the order of keys shouldn't really matter in an object.

It gets added by npm here: https://github.com/npm/read-package-json/blob/1b85cf87f5875774e7fbc617f606923d8cbda6a4/read-json.js#L345

const out: {} = {
name: typing.fullNpmName,
version,
gitHead,
description: `TypeScript definitions for ${typing.libraryName}`,
// keywords,
// homepage,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const outputDirPath = joinPaths(root, "output");
export const validateOutputPath = joinPaths(root, "validateOutput");
export const logDir = joinPaths(root, "logs");

/** URL to download the repository from. */
export const definitelyTypedZipUrl = "https://codeload.github.com/DefinitelyTyped/DefinitelyTyped/tar.gz/master";
/** URL to download the repository from. Needs to be appended with a revision. */
export const definitelyTypedZipUrl = "https://codeload.github.com/DefinitelyTyped/DefinitelyTyped/tar.gz";
/** The branch that DefinitelyTyped is sourced from. */
export const sourceBranch = "master";

Expand Down
17 changes: 2 additions & 15 deletions src/publish-packages.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as yargs from "yargs";

import appInsights = require("applicationinsights");
import { Fetcher } from "./util/io";
import { getDefinitelyTyped } from "./get-definitely-typed";
import { Options } from "./lib/common";
import { NpmPublishClient } from "./lib/npm-client";
import { deprecateNotNeededPackage, publishNotNeededPackage, publishTypingsPackage } from "./lib/package-publisher";
import { AllPackages } from "./lib/packages";
import { ChangedPackages, readChangedPackages } from "./lib/versions";
import { queryGithub } from "./util/github";
import { Fetcher } from "./util/io";
import { logger, writeLog } from "./util/logging";
import { logUncaughtErrors } from "./util/util";

Expand Down Expand Up @@ -86,17 +87,3 @@ export default async function publishPackages(changedPackages: ChangedPackages,
await writeLog("publishing.md", logResult());
console.log("Done!");
}

async function queryGithub(path: string, githubToken: string, fetcher: Fetcher) {
const [log] = logger();
log("Requesting from github: " + path);
return await fetcher.fetchJson({
hostname: "api.github.com",
path: path + "&access_token=" + githubToken,
method: "GET",
headers: {
// arbitrary string, but something must be provided
"User-Agent": "types-publisher",
},
});
}
16 changes: 16 additions & 0 deletions src/util/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Fetcher } from "./io";
import { logger } from "./logging";

export async function queryGithub(path: string, githubToken: string, fetcher: Fetcher) {
const [log] = logger();
log("Requesting from github: " + path);
return fetcher.fetchJson({
hostname: "api.github.com",
path: path + "&access_token=" + githubToken,
method: "GET",
headers: {
// arbitrary string, but something must be provided
"User-Agent": "types-publisher",
},
});
}