Skip to content

Add the ability to upload to @types/x #1025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 33 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Deploy to npm

on:
push:
branches:
- master

workflow_dispatch:

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: "15.x"
registry-url: "https://registry.npmjs.org"

- run: npm install
- run: npm run build
- run: npm test

- name: Create packages for .d.ts files
run: node deploy/createTypesPackages.mjs

# Deploy anything which differs from the npm version of a tsconfig
- name: 'Deploy built packages to NPM'
run: node deploy/deployChangedPackages.mjs
env:
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,4 @@ inputfiles/browser.webidl.json
package-lock.json
yarn.lock
TypeScript
deploy/generated
3 changes: 3 additions & 0 deletions deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Deploys

We want to generate @types/xyz
122 changes: 122 additions & 0 deletions deploy/createTypesPackages.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// @ts-check

// node deploy/createTypesPackages.mjs

// prettier-ignore
const packages = [
{
name: "@types/web",
description: "Types for the DOM, and other web technologies in browsers",
files: [
{ from: "../generated/dom.generated.d.ts", to: "index.d.ts" }
],
},
];

// Note: You can add 'version: "1.0.0"' to a package above
// to set the major or minor, otherwise it will always bump
// the patch.

import { join, dirname } from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import pkg from "prettier";
const { format } = pkg;
import { execSync } from "child_process";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const go = async () => {
const gitSha = execSync("git rev-parse HEAD").toString().trim().slice(0, 7);

const generatedDir = join(__dirname, "generated");
const templateDir = join(__dirname, "template");

for (const pkg of packages) {
const folderName = pkg.name.replace("@", "").replace("/", "-");
const packagePath = join(generatedDir, folderName);

if (fs.existsSync(packagePath)) fs.rmSync(packagePath, { recursive: true });
fs.mkdirSync(packagePath, { recursive: true });

// Migrate in the template files
for (const templateFile of fs.readdirSync(templateDir)) {
if (templateFile.startsWith(".")) continue;

const templatedFile = join(templateDir, templateFile);
fs.copyFileSync(templatedFile, join(packagePath, templateFile));
}

// Add the reference files in the config above
pkg.files.forEach((fileRef) => {
fs.copyFileSync(
join(__filename, "..", fileRef.from),
join(packagePath, fileRef.to)
);
});

// Setup the files
await updatePackageJSON(packagePath, pkg, gitSha);

console.log("Built:", pkg.name);
}
};

go();

async function updatePackageJSON(packagePath, pkg, gitSha) {
const pkgJSONPath = join(packagePath, "package.json");
const packageText = fs.readFileSync(pkgJSONPath, "utf8");
const packageJSON = JSON.parse(packageText);
packageJSON.name = pkg.name;
packageJSON.description = pkg.description;

// Bump the last version of the number from npm,
// or use the _version in tsconfig if it's higher,
// or default to 0.0.1
let version = pkg.version || "0.0.1";
try {
const npmResponse = await fetch(
`https://registry.npmjs.org/${packageJSON.name}`
);
const npmPackage = await npmResponse.json();

const semverMarkers = npmPackage["dist-tags"].latest.split(".");
const bumpedVersion = `${semverMarkers[0]}.${semverMarkers[1]}.${
Number(semverMarkers[2]) + 1
}`;

if (isBumpedVersionHigher(version, bumpedVersion)) {
version = bumpedVersion;
}
} catch (error) {
// NOOP, this is for the first deploy, which will set it to 0.0.1
}

packageJSON.version = version;
packageJSON.domLibGeneratorSha = gitSha;

fs.writeFileSync(
pkgJSONPath,
format(JSON.stringify(packageJSON), { filepath: pkgJSONPath })
);
}

/**
* @param packageJSONVersion {string}
* @param bumpedVersion {string}
*/
function isBumpedVersionHigher(packageJSONVersion, bumpedVersion) {
const semverMarkersPackageJSON = packageJSONVersion.split(".");
const semverMarkersBumped = bumpedVersion.split(".");
for (let i = 0; i < 3; i++) {
if (Number(semverMarkersPackageJSON[i]) > Number(semverMarkersBumped[i])) {
return false;
}
}

return true;
}
84 changes: 84 additions & 0 deletions deploy/deployChangedPackages.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// @ts-check

// node deploy/deployChangedPackages.mjs
// Builds on the results of createTypesPackages.mjs and deploys the
// ones which have changed.

import * as fs from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import fetch from "node-fetch";
import { spawnSync } from "child_process";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const go = async () => {
const uploaded = [];

// Loop through generated packages, deploying versions for anything which has different
// .d.ts files from the version available on npm.
const generatedDir = join(__dirname, "generated");
Copy link
Contributor

@saschanaz saschanaz Jun 22, 2021

Choose a reason for hiding this comment

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

Passing URL value to fs methods can skip the path conversions.

Edit: (Like this) https://github.com/saschanaz/types-web/blob/7b2675fc37d568d2d99e288eb63125f8c55581af/src/version.ts#L7

for (const dirName of fs.readdirSync(generatedDir)) {
const localPackageJSONPath = join(generatedDir, dirName, "package.json");
const newTSConfig = fs.readFileSync(localPackageJSONPath, "utf-8");
const pkgJSON = JSON.parse(newTSConfig);

const dtsFiles = fs
.readdirSync(join(generatedDir, dirName))
.filter((f) => f.endsWith(".d.ts"));

// Look through each .d.ts file included in a package to
// determine if anything has changed
let upload = false;
for (const file of dtsFiles) {
const generatedDTSPath = join(generatedDir, dirName, file);
const generatedDTSContent = fs.readFileSync(generatedDTSPath, "utf8");
try {
const unpkgURL = `https://unpkg.com/${pkgJSON.name}/${file}`;
const npmDTSReq = await fetch(unpkgURL);
const npmDTSText = await npmDTSReq.text();
upload = upload || npmDTSText !== generatedDTSContent;
} catch (error) {
// Not here, definitely needs to be uploaded
upload = true;
}
}

// Publish via npm
if (upload) {
if (process.env.NODE_AUTH_TOKEN) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a missing NODE_AUTH_TOKEN should mean failure, or printing a dry-run message like "Dry run: would have uploaded ${name} with 'npm publish --access public ...' "

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do log at the end that it was a dry run, so I think this is probably fine, but I can expand on the logs for the dry run.

Copy link
Member

Choose a reason for hiding this comment

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

👍 more logs are good

const publish = spawnSync("npm", ["publish", "--access", "public"], {
cwd: join("packages", dirName),
});

if (publish.status) {
console.log(publish.stdout?.toString());
console.log(publish.stderr?.toString());
process.exit(publish.status);
} else {
console.log(publish.stdout?.toString());
}
}

uploaded.push(dirName);
}
}

// Warn if we did a dry run.
if (!process.env.NODE_AUTH_TOKEN) {
console.log(
"Did a dry run because process.env.NODE_AUTH_TOKEN is not set."
);
}

if (uploaded.length) {
console.log("Uploaded: ", uploaded.join(", "));
} else {
console.log("No uploads");
}
};

go();
21 changes: 21 additions & 0 deletions deploy/template/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Microsoft Corporation.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
12 changes: 12 additions & 0 deletions deploy/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Installation
> `npm install --save {{name}}`

# Summary
{{description}}


### Additional Details
* Last updated: {{date}}
* Dependencies: none
* Global values: none

15 changes: 15 additions & 0 deletions deploy/template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@types/xyz",
"version": "0.0.x",
"description": "TypeScript definitions for xyz",
"license": "MIT",
"contributors": [],
"main": "",
"types": "index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/microsoft/TypeScript-DOM-Lib-Generator.git"
},
"scripts": {},
"dependencies": {}
}