Skip to content

Commit

Permalink
Add gitHead to package.json
Browse files Browse the repository at this point in the history
This is a field normally added automatically by npm publish.

Closes microsoft#458
  • Loading branch information
felixfbecker committed Dec 7, 2018
1 parent 5526ac3 commit d876269
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 33 deletions.
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 } };
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
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",
},
});
}

0 comments on commit d876269

Please sign in to comment.