Skip to content

Commit

Permalink
Merge pull request #1085 from saschanaz/lint-deploy
Browse files Browse the repository at this point in the history
Lint deploy/ scripts
  • Loading branch information
Orta Therox authored Aug 2, 2021
2 parents 1f0fe3d + eb75b3a commit b85127a
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 159 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
- run: npm test

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

# Deploy anything which differs from the npm version of a tsconfig
- name: "Deploy built packages to NPM"
run: node deploy/deployChangedPackages.mjs
run: node deploy/deployChangedPackages.js
env:
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39 changes: 28 additions & 11 deletions deploy/createTypesPackages.mjs → deploy/createTypesPackages.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// @ts-check

// node deploy/createTypesPackages.mjs

// node deploy/createTypesPackages.js

/**
* @template T
* @typedef {T extends (infer U)[] ? U : T} ArrayInner
*/
/**
* @typedef {ArrayInner<typeof packages>} Package
*/
// prettier-ignore
export const packages = [
{
Expand Down Expand Up @@ -52,11 +59,8 @@ import { fileURLToPath } from "url";
import semver from "semver";
import pkg from "prettier";
const { format } = pkg;
import { execSync } from "child_process";

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

const generatedDir = new URL("generated/", import.meta.url);
const templateDir = new URL("template/", import.meta.url);

Expand Down Expand Up @@ -88,25 +92,30 @@ const go = async () => {
prependAutoImports(pkg, packagePath);

// Setup the files in the repo
const newPkgJSON = await updatePackageJSON(pkg, packagePath, gitSha);
const newPkgJSON = await updatePackageJSON(pkg, packagePath);
copyREADME(pkg, newPkgJSON, new URL("README.md", packagePath));

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

async function updatePackageJSON(pkg, packagePath, gitSha) {
/**
* @param {Package} pkg
* @param {URL} packagePath
*/
async function updatePackageJSON(pkg, packagePath) {
const pkgJSONPath = new URL("package.json", packagePath);
const packageText = fs.readFileSync(pkgJSONPath, "utf8");
/** @type {import("./template/package.json")} */
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";
let version = "0.0.1";
try {
const npmResponse = await fetch(
`https://registry.npmjs.org/${packageJSON.name}`
Expand All @@ -128,7 +137,6 @@ async function updatePackageJSON(pkg, packagePath, gitSha) {
}

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

fs.writeFileSync(
pkgJSONPath,
Expand All @@ -140,7 +148,12 @@ async function updatePackageJSON(pkg, packagePath, gitSha) {
return packageJSON;
}

// Copies the README and adds some rudimentary templating to the file.
/**
* Copies the README and adds some rudimentary templating to the file.
* @param {Package} pkg
* @param {import("./template/package.json")} pkgJSON
* @param {URL} writePath
*/
function copyREADME(pkg, pkgJSON, writePath) {
let readme = fs.readFileSync(new URL(pkg.readme, import.meta.url), "utf-8");

Expand All @@ -157,7 +170,11 @@ function copyREADME(pkg, pkgJSON, writePath) {
fs.writeFileSync(writePath, readme);
}

// Appends any files marked as autoImport in the metadata.
/**
* Appends any files marked as autoImport in the metadata.
* @param {Package} pkg
* @param {URL} packagePath
*/
function prependAutoImports(pkg, packagePath) {
const index = new URL("index.d.ts", packagePath);
if (!fs.existsSync(index)) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// @ts-check

// node deploy/deployChangedPackages.mjs
// node deploy/deployChangedPackages.js

// Builds on the results of createTypesPackages.mjs and deploys the
// Builds on the results of createTypesPackages.js and deploys the
// ones which have changed.

import * as fs from "fs";
import { basename } from "path";
import { spawnSync, execSync } from "child_process";
import { spawnSync } from "child_process";
import { Octokit } from "@octokit/rest";
import printDiff from "print-diff";
import { generateChangelogFrom } from "../lib/changelog.js";
import { packages } from "./createTypesPackages.mjs";
import { gitShowFile, generateChangelogFrom } from "../lib/changelog.js";
import { packages } from "./createTypesPackages.js";
import { fileURLToPath } from "node:url";

verify();
Expand All @@ -31,6 +31,9 @@ for (const dirName of fs.readdirSync(generatedDir)) {
// We'll need to map back from the filename in the npm package to the
// generated file in baselines inside the git tag
const thisPackageMeta = packages.find((p) => p.name === pkgJSON.name);
if (!thisPackageMeta) {
throw new Error(`Couldn't find ${pkgJSON.name}`);
}

const dtsFiles = fs
.readdirSync(packageDir)
Expand All @@ -42,9 +45,11 @@ for (const dirName of fs.readdirSync(generatedDir)) {
// determine if anything has changed
let upload = false;
for (const file of dtsFiles) {
const originalFilename = basename(
thisPackageMeta.files.find((f) => f.to === file).from
);
const filemap = thisPackageMeta.files.find((f) => f.to === file);
if (!filemap) {
throw new Error(`Couldn't find ${file} from ${pkgJSON.name}`);
}
const originalFilename = basename(filemap.from);

const generatedDTSPath = new URL(file, packageDir);
const generatedDTSContent = fs.readFileSync(generatedDTSPath, "utf8");
Expand Down Expand Up @@ -118,6 +123,10 @@ if (uploaded.length) {
console.log("No uploads");
}

/**
* @param {string} tag
* @param {string} body
*/
async function createRelease(tag, body) {
const authToken = process.env.GITHUB_TOKEN || process.env.GITHUB_API_TOKEN;
const octokit = new Octokit({ auth: authToken });
Expand Down Expand Up @@ -145,7 +154,3 @@ function verify() {
"There isn't an ENV var set up for creating a GitHub release, expected GITHUB_TOKEN."
);
}

function gitShowFile(commitish, path) {
return execSync(`git show "${commitish}":${path}`, { encoding: "utf-8" });
}
9 changes: 9 additions & 0 deletions deploy/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "es2020",
"module": "esnext", // es2022
"moduleResolution": "node",
"strict": true,
"resolveJsonModule": true
}
}
File renamed without changes.
8 changes: 2 additions & 6 deletions deploy/versionChangelog.mjs → deploy/versionChangelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

// npm run ts-changelog @types/web 0.0.1 0.0.3

import { generateChangelogFrom } from "../lib/changelog.js";
import { packages } from "./createTypesPackages.mjs";
import { execSync } from "child_process";
import { gitShowFile, generateChangelogFrom } from "../lib/changelog.js";
import { packages } from "./createTypesPackages.js";
import { basename } from "path";

const [name, before, to] = process.argv.slice(2);
Expand Down Expand Up @@ -33,7 +32,4 @@ const go = () => {
}
};

const gitShowFile = (commitish, path) =>
execSync(`git show "${commitish}":${path}`, { encoding: "utf-8" });

go();
Loading

0 comments on commit b85127a

Please sign in to comment.