Skip to content

Commit b85127a

Browse files
author
Orta Therox
authored
Merge pull request #1085 from saschanaz/lint-deploy
Lint deploy/ scripts
2 parents 1f0fe3d + eb75b3a commit b85127a

9 files changed

+328
-159
lines changed

.github/workflows/deploy.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ jobs:
2424
- run: npm test
2525

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

2929
# Deploy anything which differs from the npm version of a tsconfig
3030
- name: "Deploy built packages to NPM"
31-
run: node deploy/deployChangedPackages.mjs
31+
run: node deploy/deployChangedPackages.js
3232
env:
3333
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
3434
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

deploy/createTypesPackages.mjs renamed to deploy/createTypesPackages.js

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// @ts-check
22

3-
// node deploy/createTypesPackages.mjs
4-
3+
// node deploy/createTypesPackages.js
4+
5+
/**
6+
* @template T
7+
* @typedef {T extends (infer U)[] ? U : T} ArrayInner
8+
*/
9+
/**
10+
* @typedef {ArrayInner<typeof packages>} Package
11+
*/
512
// prettier-ignore
613
export const packages = [
714
{
@@ -52,11 +59,8 @@ import { fileURLToPath } from "url";
5259
import semver from "semver";
5360
import pkg from "prettier";
5461
const { format } = pkg;
55-
import { execSync } from "child_process";
5662

5763
const go = async () => {
58-
const gitSha = execSync("git rev-parse HEAD").toString().trim().slice(0, 7);
59-
6064
const generatedDir = new URL("generated/", import.meta.url);
6165
const templateDir = new URL("template/", import.meta.url);
6266

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

9094
// Setup the files in the repo
91-
const newPkgJSON = await updatePackageJSON(pkg, packagePath, gitSha);
95+
const newPkgJSON = await updatePackageJSON(pkg, packagePath);
9296
copyREADME(pkg, newPkgJSON, new URL("README.md", packagePath));
9397

9498
// Done
9599
console.log("Built:", pkg.name);
96100
}
97101
};
98102

99-
async function updatePackageJSON(pkg, packagePath, gitSha) {
103+
/**
104+
* @param {Package} pkg
105+
* @param {URL} packagePath
106+
*/
107+
async function updatePackageJSON(pkg, packagePath) {
100108
const pkgJSONPath = new URL("package.json", packagePath);
101109
const packageText = fs.readFileSync(pkgJSONPath, "utf8");
110+
/** @type {import("./template/package.json")} */
102111
const packageJSON = JSON.parse(packageText);
103112
packageJSON.name = pkg.name;
104113
packageJSON.description = pkg.description;
105114

106115
// Bump the last version of the number from npm,
107116
// or use the _version in tsconfig if it's higher,
108117
// or default to 0.0.1
109-
let version = pkg.version || "0.0.1";
118+
let version = "0.0.1";
110119
try {
111120
const npmResponse = await fetch(
112121
`https://registry.npmjs.org/${packageJSON.name}`
@@ -128,7 +137,6 @@ async function updatePackageJSON(pkg, packagePath, gitSha) {
128137
}
129138

130139
packageJSON.version = version;
131-
packageJSON.domLibGeneratorSha = gitSha;
132140

133141
fs.writeFileSync(
134142
pkgJSONPath,
@@ -140,7 +148,12 @@ async function updatePackageJSON(pkg, packagePath, gitSha) {
140148
return packageJSON;
141149
}
142150

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

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

160-
// Appends any files marked as autoImport in the metadata.
173+
/**
174+
* Appends any files marked as autoImport in the metadata.
175+
* @param {Package} pkg
176+
* @param {URL} packagePath
177+
*/
161178
function prependAutoImports(pkg, packagePath) {
162179
const index = new URL("index.d.ts", packagePath);
163180
if (!fs.existsSync(index)) return;

deploy/deployChangedPackages.mjs renamed to deploy/deployChangedPackages.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// @ts-check
22

3-
// node deploy/deployChangedPackages.mjs
3+
// node deploy/deployChangedPackages.js
44

5-
// Builds on the results of createTypesPackages.mjs and deploys the
5+
// Builds on the results of createTypesPackages.js and deploys the
66
// ones which have changed.
77

88
import * as fs from "fs";
99
import { basename } from "path";
10-
import { spawnSync, execSync } from "child_process";
10+
import { spawnSync } from "child_process";
1111
import { Octokit } from "@octokit/rest";
1212
import printDiff from "print-diff";
13-
import { generateChangelogFrom } from "../lib/changelog.js";
14-
import { packages } from "./createTypesPackages.mjs";
13+
import { gitShowFile, generateChangelogFrom } from "../lib/changelog.js";
14+
import { packages } from "./createTypesPackages.js";
1515
import { fileURLToPath } from "node:url";
1616

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

3538
const dtsFiles = fs
3639
.readdirSync(packageDir)
@@ -42,9 +45,11 @@ for (const dirName of fs.readdirSync(generatedDir)) {
4245
// determine if anything has changed
4346
let upload = false;
4447
for (const file of dtsFiles) {
45-
const originalFilename = basename(
46-
thisPackageMeta.files.find((f) => f.to === file).from
47-
);
48+
const filemap = thisPackageMeta.files.find((f) => f.to === file);
49+
if (!filemap) {
50+
throw new Error(`Couldn't find ${file} from ${pkgJSON.name}`);
51+
}
52+
const originalFilename = basename(filemap.from);
4853

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

126+
/**
127+
* @param {string} tag
128+
* @param {string} body
129+
*/
121130
async function createRelease(tag, body) {
122131
const authToken = process.env.GITHUB_TOKEN || process.env.GITHUB_API_TOKEN;
123132
const octokit = new Octokit({ auth: authToken });
@@ -145,7 +154,3 @@ function verify() {
145154
"There isn't an ENV var set up for creating a GitHub release, expected GITHUB_TOKEN."
146155
);
147156
}
148-
149-
function gitShowFile(commitish, path) {
150-
return execSync(`git show "${commitish}":${path}`, { encoding: "utf-8" });
151-
}

deploy/jsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2020",
4+
"module": "esnext", // es2022
5+
"moduleResolution": "node",
6+
"strict": true,
7+
"resolveJsonModule": true
8+
}
9+
}
File renamed without changes.

deploy/versionChangelog.mjs renamed to deploy/versionChangelog.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

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

5-
import { generateChangelogFrom } from "../lib/changelog.js";
6-
import { packages } from "./createTypesPackages.mjs";
7-
import { execSync } from "child_process";
5+
import { gitShowFile, generateChangelogFrom } from "../lib/changelog.js";
6+
import { packages } from "./createTypesPackages.js";
87
import { basename } from "path";
98

109
const [name, before, to] = process.argv.slice(2);
@@ -33,7 +32,4 @@ const go = () => {
3332
}
3433
};
3534

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

0 commit comments

Comments
 (0)