Skip to content

Commit

Permalink
build: update scripts for Prettier v3, switch to ESM (#581)
Browse files Browse the repository at this point in the history
* build: remove uneeded `fix-package-json` script

* build: switch to ESM for scripts

* style: replace tabs for spaces for consistency

* build(typescript.mjs): remove need for intermediate variable

* style: prettier

* build: update lint script
  • Loading branch information
wolfy1339 authored Sep 22, 2023
1 parent b5b99d1 commit f36b704
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 68 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ jobs:
cache: npm
- run: npm ci
- run: npm run build
- name: "Fix pkg.files file pattern"
run: node scripts/fix-package-json.js
- run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.OCTOKITBOT_PAT }}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"scripts": {
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"docs": "typedoc --readme none --out docs src/index.ts && touch docs/.nojekyll",
"lint": "prettier --check \"{src,test,scripts}/**/*.{js,ts,json}\" README.md package.json !src/generated/* !scripts/update-endpoints/generated/*",
"lint:fix": "prettier --write \"{src,test,scripts}/**/*.{js,ts,json}\" README.md package.json !src/generated/* !scripts/update-endpoints/generated/*",
"lint": "prettier --check \"{src,test,scripts}/**/*.{js,mjs,ts,json}\" README.md package.json !src/generated/* !scripts/update-endpoints/generated/*",
"lint:fix": "prettier --write \"{src,test,scripts}/**/*.{js,mjs,ts,json}\" README.md package.json !src/generated/* !scripts/update-endpoints/generated/*",
"pretest": "npm run -s lint",
"test": "npx tsc --project tsconfig.test.json",
"update-endpoints": "npm-run-all update-endpoints:*",
"update-endpoints:fetch-json": "node scripts/update-endpoints/fetch-json",
"update-endpoints:typescript": "node scripts/update-endpoints/typescript",
"update-endpoints:package": "node scripts/update-endpoints/package"
"update-endpoints:fetch-json": "node scripts/update-endpoints/fetch-json.mjs",
"update-endpoints:typescript": "node scripts/update-endpoints/typescript.mjs",
"update-endpoints:package": "node scripts/update-endpoints/package.mjs"
},
"repository": "github:octokit/types.ts",
"keywords": [
Expand Down
53 changes: 26 additions & 27 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import { copyFile, readFile, writeFile, rm, mkdir } from "node:fs/promises";


async function main() {
// Start with a clean slate
await rm("pkg", { recursive: true, force: true });
await mkdir("pkg");
// Start with a clean slate
await rm("pkg", { recursive: true, force: true });
await mkdir("pkg");

// Copy the README, LICENSE to the pkg folder
await copyFile("LICENSE", "pkg/LICENSE");
await copyFile("README.md", "pkg/README.md");
// Copy the README, LICENSE to the pkg folder
await copyFile("LICENSE", "pkg/LICENSE");
await copyFile("README.md", "pkg/README.md");

// Handle the package.json
let pkg = JSON.parse((await readFile("package.json", "utf8")).toString());
// Remove unnecessary fields from the package.json
delete pkg.scripts;
delete pkg.prettier;
delete pkg.release;
delete pkg.jest;
await writeFile(
"pkg/package.json",
JSON.stringify(
{
...pkg,
files: ["dist-types/**"],
types: "dist-types/index.d.ts",
sideEffects: false,
},
null,
2
)
);
// Handle the package.json
let pkg = JSON.parse((await readFile("package.json", "utf8")).toString());
// Remove unnecessary fields from the package.json
delete pkg.scripts;
delete pkg.prettier;
delete pkg.release;
delete pkg.jest;
await writeFile(
"pkg/package.json",
JSON.stringify(
{
...pkg,
files: ["dist-types/**"],
types: "dist-types/index.d.ts",
sideEffects: false,
},
null,
2,
),
);
}
main();
15 changes: 0 additions & 15 deletions scripts/fix-package-json.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const { writeFileSync } = require("fs");
const path = require("path");
import { writeFileSync } from "fs";
import { resolve } from "path";
import { fileURLToPath } from "url";

const graphql = require("github-openapi-graphql-query");
const prettier = require("prettier");
import graphql from "github-openapi-graphql-query";
import { format } from "prettier";

if (!process.env.VERSION) {
throw new Error(`VERSION environment variable must be set`);
}

const __dirname = fileURLToPath(new URL(".", import.meta.url));

const version = process.env.VERSION.replace(/^v/, "");

const QUERY = `
Expand Down Expand Up @@ -39,8 +42,8 @@ async function main() {
});

writeFileSync(
path.resolve(__dirname, "generated", "endpoints.json"),
prettier.format(JSON.stringify(endpoints), {
resolve(__dirname, "generated", "endpoints.json"),
await format(JSON.stringify(endpoints), {
parser: "json",
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const { writeFileSync } = require("fs");
import { writeFileSync, readFileSync } from "fs";

if (!process.env.VERSION) {
throw new Error(`VERSION environment variable must be set`);
}

const pkg = require("../../package.json");
const parentDir = new URL("../..", import.meta.url);
const pkg = JSON.parse(
readFileSync(new URL("package.json", parentDir), "utf8"),
);

if (!pkg.octokit) {
pkg.octokit = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const { readFileSync, writeFileSync } = require("fs");
const { resolve } = require("path");
import { readFileSync, writeFileSync } from "fs";
import { resolve } from "path";

const Handlebars = require("handlebars");
const prettier = require("prettier");
const sortKeys = require("sort-keys");
import handlebars from "handlebars";
import { format } from "prettier";
import sortKeys from "sort-keys";

const ENDPOINTS = require("./generated/endpoints.json");
const ENDPOINTS = JSON.parse(
readFileSync(new URL("generated/endpoints.json", import.meta.url), "utf8"),
);
const ENDPOINTS_PATH = resolve(
process.cwd(),
"src",
Expand All @@ -20,7 +22,7 @@ const ENDPOINTS_TEMPLATE_PATH = resolve(
"endpoints.ts.template",
);

const template = Handlebars.compile(
const template = handlebars.compile(
readFileSync(ENDPOINTS_TEMPLATE_PATH, "utf8"),
);

Expand Down Expand Up @@ -63,10 +65,7 @@ async function run() {
endpointsByRoute: sortKeys(endpointsByRoute, { deep: true }),
});

writeFileSync(
ENDPOINTS_PATH,
prettier.format(result, { parser: "typescript" }),
);
writeFileSync(ENDPOINTS_PATH, await format(result, { parser: "typescript" }));
console.log(`${ENDPOINTS_PATH} updated.`);
}

Expand Down

0 comments on commit f36b704

Please sign in to comment.