Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 29 additions & 27 deletions src/execute/create-temporary-directory.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof createTemporaryDirectory>>
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<typeof createTemporaryDirectory>
>;
6 changes: 3 additions & 3 deletions src/execute/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,7 +42,7 @@ export async function execute({
}),
);

await originalPrettier.dispatch()
await originalPrettier.dispatch();

// Setup alternativeVersionPrettier
await logger.log("Setting up alternativeVersionPrettier...");
Expand All @@ -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),
Expand Down
134 changes: 67 additions & 67 deletions src/execute/install-prettier.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof installPrettier>>
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<ReturnType<typeof installPrettier>>;

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())}`,
};
}
2 changes: 1 addition & 1 deletion src/execute/run-prettier.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from "path";

Check failure on line 1 in src/execute/run-prettier.ts

View workflow job for this annotation

GitHub Actions / Lint

'path' is defined but never used
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,
Expand Down
24 changes: 10 additions & 14 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 => {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions src/tools/npm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import spawn, {type Subprocess, type Options} from "nano-spawn";
export async function pack(options: Options): Promise<Subprocess> {
return await spawn("npm", ['pack'], options);
}
import spawn, { type Subprocess, type Options } from "nano-spawn";

export async function pack(options: Options): Promise<Subprocess> {
return await spawn("npm", ["pack"], options);
}
10 changes: 5 additions & 5 deletions src/tools/yarn.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await spawn("yarn", [], { cwd });
}

export async function init(args: string [] ,options: Options): Promise<void> {
await spawn("yarn", ['init',...args], options);
export async function init(args: string[], options: Options): Promise<void> {
await spawn("yarn", ["init", ...args], options);
}

export async function add(args: string [] ,options: Options): Promise<void> {
await spawn("yarn", ['add',...args], options);
export async function add(args: string[], options: Options): Promise<void> {
await spawn("yarn", ["add", ...args], options);
}
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down
Loading