diff --git a/package.json b/package.json index 4a6e012..69a4e12 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "globals": "^16.5.0", "npm-run-all": "^4.1.5", "outdent": "^0.8.0", - "prettier": "3.6.2", + "prettier": "3.7.4", "temp-dir": "^3.0.0", "typescript": "^5.9.3", "typescript-eslint": "^8.46.2", diff --git a/src/execute/create-temporary-directory.ts b/src/execute/create-temporary-directory.ts index 9097938..5c449b9 100644 --- a/src/execute/create-temporary-directory.ts +++ b/src/execute/create-temporary-directory.ts @@ -1,27 +1,29 @@ -import fs from 'node:fs/promises' -import os from 'node:os' -import path from 'node:path'; -import crypto from "node:crypto"; - -export async function createTemporaryDirectory() { - const directory = path.join( - // The following quoted from https://github.com/es-tooling/module-replacements/blob/27d1acd38f19741e31d2eae561a5c8a914373fc5/docs/modules/tempy.md?plain=1#L20-L21, not sure if it's true - // MacOS and possibly some other platforms return a symlink from `os.tmpdir`. - // For some applications, this can cause problems; thus, we use `realpath`. - await fs.realpath(os.tmpdir()), - crypto.randomBytes(16).toString("hex"), - ); - - fs.mkdir(directory); - - return { - path: directory, - dispatch: () => destroyTemporaryDirectory(directory) - }; -} - -async function destroyTemporaryDirectory(directory: string) { - await fs.rm(directory, {recursive: true, force: true}) -} - -export type TemporaryDirectory = Awaited> \ No newline at end of file +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import crypto from "node:crypto"; + +export async function createTemporaryDirectory() { + const directory = path.join( + // The following quoted from https://github.com/es-tooling/module-replacements/blob/27d1acd38f19741e31d2eae561a5c8a914373fc5/docs/modules/tempy.md?plain=1#L20-L21, not sure if it's true + // MacOS and possibly some other platforms return a symlink from `os.tmpdir`. + // For some applications, this can cause problems; thus, we use `realpath`. + await fs.realpath(os.tmpdir()), + crypto.randomBytes(16).toString("hex"), + ); + + fs.mkdir(directory); + + return { + path: directory, + dispatch: () => destroyTemporaryDirectory(directory), + }; +} + +async function destroyTemporaryDirectory(directory: string) { + await fs.rm(directory, { recursive: true, force: true }); +} + +export type TemporaryDirectory = Awaited< + ReturnType +>; diff --git a/src/execute/index.ts b/src/execute/index.ts index 0937f38..505241b 100644 --- a/src/execute/index.ts +++ b/src/execute/index.ts @@ -6,7 +6,7 @@ import * as configuration from "../configuration.ts"; import * as git from "../tools/git.ts"; import * as logger from "../logger.ts"; import { getProjects, getTargetRepositoryPath } from "../projects.ts"; -import {installPrettier} from './install-prettier.ts' +import { installPrettier } from "./install-prettier.ts"; export interface ExecuteResultEntry { commitHash: string; @@ -42,7 +42,7 @@ export async function execute({ }), ); - await originalPrettier.dispatch() + await originalPrettier.dispatch(); // Setup alternativeVersionPrettier await logger.log("Setting up alternativeVersionPrettier..."); @@ -54,7 +54,7 @@ export async function execute({ await runPrettier(alternativePrettier, project); }), ); - await alternativePrettier.dispatch() + await alternativePrettier.dispatch(); const diffs = await Promise.all( projects.map(getTargetRepositoryPath).map(git.diffRepository), diff --git a/src/execute/install-prettier.ts b/src/execute/install-prettier.ts index b892449..67fc289 100644 --- a/src/execute/install-prettier.ts +++ b/src/execute/install-prettier.ts @@ -1,67 +1,67 @@ -import path from "node:path"; -import { - PrettierVersion, - PrettierPullRequest, - sourceTypes -} from "../parse.ts"; -import * as configuration from "../configuration.ts"; -import * as brew from "../tools/brew.ts"; -import * as gh from "../tools/gh.ts"; -import * as yarn from "../tools/yarn.ts"; -import * as npm from "../tools/npm.ts"; -import * as unix from "../tools/unix.ts"; -import {createTemporaryDirectory, type TemporaryDirectory} from './create-temporary-directory.ts' - -export type InstalledPrettier = Awaited> - -export async function installPrettier( - prettierVersion: PrettierVersion, -) { - let version: string - let pullRequestDirectory: TemporaryDirectory | undefined - if (prettierVersion.type === sourceTypes.pullRequest) { - ({version, directory: pullRequestDirectory} = await getPullRequest(prettierVersion.number) ) - } else { - ({version} = prettierVersion) - } - - - const directory = await createTemporaryDirectory() - const {path: cwd} = directory - await yarn.init(['-y'], {cwd}) - await yarn.add([`prettier@${version}`],{cwd}) - - await pullRequestDirectory?.dispatch() - - return { - dispatch: () => {directory.dispatch()}, - bin: path.join(cwd, 'node_modules/prettier/bin/prettier.cjs'), - name: prettierVersion.raw - } -} - -async function existsGh() { - return !(await unix.which("gh")).includes("gh not found"); -} - -async function getPullRequest( - pullRequestNumber: PrettierPullRequest['number'], -) { - if (!(await existsGh())) { - await brew.install("gh"); - } - if (configuration.authToken !== "nothing") { - // running locally, `gh` can be already authenticated - await gh.authLoginWithToken(configuration.authToken); - } - - const directory = await createTemporaryDirectory() - - await gh.prCheckout(pullRequestNumber, directory.path); - const {stdout} = await npm.pack({cwd: directory.path}); - - return { - directory, - version: `file:${path.join(directory.path, stdout.trim())}` - }; -} +import path from "node:path"; +import { PrettierVersion, PrettierPullRequest, sourceTypes } from "../parse.ts"; +import * as configuration from "../configuration.ts"; +import * as brew from "../tools/brew.ts"; +import * as gh from "../tools/gh.ts"; +import * as yarn from "../tools/yarn.ts"; +import * as npm from "../tools/npm.ts"; +import * as unix from "../tools/unix.ts"; +import { + createTemporaryDirectory, + type TemporaryDirectory, +} from "./create-temporary-directory.ts"; + +export type InstalledPrettier = Awaited>; + +export async function installPrettier(prettierVersion: PrettierVersion) { + let version: string; + let pullRequestDirectory: TemporaryDirectory | undefined; + if (prettierVersion.type === sourceTypes.pullRequest) { + ({ version, directory: pullRequestDirectory } = await getPullRequest( + prettierVersion.number, + )); + } else { + ({ version } = prettierVersion); + } + + const directory = await createTemporaryDirectory(); + const { path: cwd } = directory; + await yarn.init(["-y"], { cwd }); + await yarn.add([`prettier@${version}`], { cwd }); + + await pullRequestDirectory?.dispatch(); + + return { + dispatch: () => { + directory.dispatch(); + }, + bin: path.join(cwd, "node_modules/prettier/bin/prettier.cjs"), + name: prettierVersion.raw, + }; +} + +async function existsGh() { + return !(await unix.which("gh")).includes("gh not found"); +} + +async function getPullRequest( + pullRequestNumber: PrettierPullRequest["number"], +) { + if (!(await existsGh())) { + await brew.install("gh"); + } + if (configuration.authToken !== "nothing") { + // running locally, `gh` can be already authenticated + await gh.authLoginWithToken(configuration.authToken); + } + + const directory = await createTemporaryDirectory(); + + await gh.prCheckout(pullRequestNumber, directory.path); + const { stdout } = await npm.pack({ cwd: directory.path }); + + return { + directory, + version: `file:${path.join(directory.path, stdout.trim())}`, + }; +} diff --git a/src/execute/run-prettier.ts b/src/execute/run-prettier.ts index 8c52287..b145387 100644 --- a/src/execute/run-prettier.ts +++ b/src/execute/run-prettier.ts @@ -2,7 +2,7 @@ import path from "path"; import spawn from "nano-spawn"; import { type Project, getTargetRepositoryPath } from "../projects.ts"; import * as yarn from "../tools/yarn.ts"; -import {type InstalledPrettier} from './install-prettier.ts' +import { type InstalledPrettier } from "./install-prettier.ts"; export async function runPrettier( prettier: InstalledPrettier, diff --git a/src/parse.ts b/src/parse.ts index f90d0a2..a9014d5 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,24 +1,21 @@ export const sourceTypes = { - pullRequest: 'pull-request', - package: 'package', + pullRequest: "pull-request", + package: "package", } as const; export interface PrettierPackage { type: typeof sourceTypes.package; - version: string, + version: string; raw?: string; } export interface PrettierPullRequest { type: typeof sourceTypes.pullRequest; - number: string, + number: string; raw?: string; } - -export type PrettierVersion = - | PrettierPackage - | PrettierPullRequest; +export type PrettierVersion = PrettierPackage | PrettierPullRequest; export type Project = { repositoryUrl: string; @@ -38,8 +35,7 @@ export function parse(source: string): Command { const tokens = tokenize(source); let alternativePrettier: PrettierVersion | undefined = undefined; - let originalPrettier: PrettierVersion = - defaultPrettierRepositorySource; + let originalPrettier: PrettierVersion = defaultPrettierRepositorySource; for (const [index, token] of tokenize(source).entries()) { const lookahead = (): Token => { @@ -99,7 +95,7 @@ export function parseRepositorySource(token: Token): PrettierVersion { throw new Error(`Unexpected token '${token.kind}', expect 'source'.`); } - const raw = token.value + const raw = token.value; // like "#3465" if (/^#\d+$/.test(raw)) { @@ -112,10 +108,10 @@ export function parseRepositorySource(token: Token): PrettierVersion { // Any source yarn accepts https://yarnpkg.com/cli/add // `sosukesuzuki/prettier#ref`, `3.0.0`, ... and so on - const packagePrefix = 'prettier@' - let version = raw + const packagePrefix = "prettier@"; + let version = raw; if (version.startsWith(packagePrefix)) { - version = version.slice(packagePrefix.length) + version = version.slice(packagePrefix.length); } return { diff --git a/src/tools/npm.ts b/src/tools/npm.ts index 013a805..333d743 100644 --- a/src/tools/npm.ts +++ b/src/tools/npm.ts @@ -1,5 +1,5 @@ -import spawn, {type Subprocess, type Options} from "nano-spawn"; - -export async function pack(options: Options): Promise { - return await spawn("npm", ['pack'], options); -} +import spawn, { type Subprocess, type Options } from "nano-spawn"; + +export async function pack(options: Options): Promise { + return await spawn("npm", ["pack"], options); +} diff --git a/src/tools/yarn.ts b/src/tools/yarn.ts index a8442d5..07e51e1 100644 --- a/src/tools/yarn.ts +++ b/src/tools/yarn.ts @@ -1,13 +1,13 @@ -import spawn, {type Options} from "nano-spawn"; +import spawn, { type Options } from "nano-spawn"; export async function install(cwd: string): Promise { await spawn("yarn", [], { cwd }); } -export async function init(args: string [] ,options: Options): Promise { - await spawn("yarn", ['init',...args], options); +export async function init(args: string[], options: Options): Promise { + await spawn("yarn", ["init", ...args], options); } -export async function add(args: string [] ,options: Options): Promise { - await spawn("yarn", ['add',...args], options); +export async function add(args: string[], options: Options): Promise { + await spawn("yarn", ["add", ...args], options); } diff --git a/yarn.lock b/yarn.lock index e4b365d..2308e4a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3738,7 +3738,7 @@ __metadata: nano-spawn: "npm:^2.0.0" npm-run-all: "npm:^4.1.5" outdent: "npm:^0.8.0" - prettier: "npm:3.6.2" + prettier: "npm:3.7.4" semver: "npm:^7.7.3" temp-dir: "npm:^3.0.0" typescript: "npm:^5.9.3" @@ -3747,12 +3747,12 @@ __metadata: languageName: unknown linkType: soft -"prettier@npm:3.6.2": - version: 3.6.2 - resolution: "prettier@npm:3.6.2" +"prettier@npm:3.7.4": + version: 3.7.4 + resolution: "prettier@npm:3.7.4" bin: prettier: bin/prettier.cjs - checksum: 10c0/488cb2f2b99ec13da1e50074912870217c11edaddedeadc649b1244c749d15ba94e846423d062e2c4c9ae683e2d65f754de28889ba06e697ac4f988d44f45812 + checksum: 10c0/9675d2cd08eacb1faf1d1a2dbfe24bfab6a912b059fc9defdb380a408893d88213e794a40a2700bd29b140eb3172e0b07c852853f6e22f16f3374659a1a13389 languageName: node linkType: hard