Skip to content

Commit

Permalink
Format unformatted code and add Prettier check to CI (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Aug 9, 2024
1 parent 66cdbc0 commit 237f38f
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 145 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,20 @@ jobs:

- name: Check Types
run: yarn tsc

formatting:
name: Formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: 16.x

- name: Install Dependencies
run: yarn

- name: Check Formatting
run: yarn format:check
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
__fixtures__
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"test-gatsby"
],
"scripts": {
"format": "prettier --write .",
"format:check": "prettier --check .",
"postinstall": "preconstruct dev && manypkg check",
"release": "preconstruct build && changeset publish",
"test": "jest",
Expand Down
142 changes: 68 additions & 74 deletions packages/find-root/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,25 @@ export async function findRoot(
let monorepoRoot: MonorepoRoot | undefined;
const tools = options.tools || DEFAULT_TOOLS;

await findUp(
async (directory) => {
return Promise.all(
tools.map(async (tool): Promise<MonorepoRoot | undefined> => {
if (await tool.isMonorepoRoot(directory)) {
return {
tool: tool,
rootDir: directory,
};
}
})
)
.then((x) => x.find((value) => value))
.then((result) => {
if (result) {
monorepoRoot = result;
return directory;
}
});
},
cwd
);
await findUp(async (directory) => {
return Promise.all(
tools.map(async (tool): Promise<MonorepoRoot | undefined> => {
if (await tool.isMonorepoRoot(directory)) {
return {
tool: tool,
rootDir: directory,
};
}
})
)
.then((x) => x.find((value) => value))
.then((result) => {
if (result) {
monorepoRoot = result;
return directory;
}
});
}, cwd);

if (monorepoRoot) {
return monorepoRoot;
Expand All @@ -112,19 +109,16 @@ export async function findRoot(
// If there is no monorepo root, but we can find a single package json file, we will
// return a "RootTool" repo, which is the special case where we just have a root package
// with no monorepo implementation (i.e.: a normal package folder).
let rootDir = await findUp(
async (directory) => {
try {
await fsp.access(path.join(directory, "package.json"));
return directory;
} catch (err) {
if (!isNoEntryError(err)) {
throw err;
}
let rootDir = await findUp(async (directory) => {
try {
await fsp.access(path.join(directory, "package.json"));
return directory;
} catch (err) {
if (!isNoEntryError(err)) {
throw err;
}
},
cwd
);
}
}, cwd);

if (!rootDir) {
throw new NoPkgJsonFound(cwd);
Expand All @@ -146,20 +140,17 @@ export function findRootSync(
let monorepoRoot: MonorepoRoot | undefined;
const tools = options.tools || DEFAULT_TOOLS;

findUpSync(
(directory) => {
for (const tool of tools) {
if (tool.isMonorepoRootSync(directory)) {
monorepoRoot = {
tool: tool,
rootDir: directory,
};
return directory;
}
findUpSync((directory) => {
for (const tool of tools) {
if (tool.isMonorepoRootSync(directory)) {
monorepoRoot = {
tool: tool,
rootDir: directory,
};
return directory;
}
},
cwd
);
}
}, cwd);

if (monorepoRoot) {
return monorepoRoot;
Expand All @@ -172,13 +163,10 @@ export function findRootSync(
// If there is no monorepo root, but we can find a single package json file, we will
// return a "RootTool" repo, which is the special case where we just have a root package
// with no monorepo implementation (i.e.: a normal package folder).
const rootDir = findUpSync(
(directory) => {
const exists = fs.existsSync(path.join(directory, "package.json"));
return exists ? directory : undefined;
},
cwd
);
const rootDir = findUpSync((directory) => {
const exists = fs.existsSync(path.join(directory, "package.json"));
return exists ? directory : undefined;
}, cwd);

if (!rootDir) {
throw new NoPkgJsonFound(cwd);
Expand All @@ -190,30 +178,36 @@ export function findRootSync(
};
}

async function findUp(matcher: (directory: string) => Promise<string | undefined>, cwd: string) {
let directory = path.resolve(cwd);
const { root } = path.parse(directory);
async function findUp(
matcher: (directory: string) => Promise<string | undefined>,
cwd: string
) {
let directory = path.resolve(cwd);
const { root } = path.parse(directory);

while (directory && directory !== root) {
const filePath = await matcher(directory);
if (filePath) {
return path.resolve(directory, filePath);
}
while (directory && directory !== root) {
const filePath = await matcher(directory);
if (filePath) {
return path.resolve(directory, filePath);
}

directory = path.dirname(directory);
}
directory = path.dirname(directory);
}
}

function findUpSync(matcher: (directory: string) => string | undefined, cwd: string) {
let directory = path.resolve(cwd);
const { root } = path.parse(directory);
function findUpSync(
matcher: (directory: string) => string | undefined,
cwd: string
) {
let directory = path.resolve(cwd);
const { root } = path.parse(directory);

while (directory && directory !== root) {
const filePath = matcher(directory);
if (filePath) {
return path.resolve(directory, filePath);
}
while (directory && directory !== root) {
const filePath = matcher(directory);
if (filePath) {
return path.resolve(directory, filePath);
}

directory = path.dirname(directory);
}
directory = path.dirname(directory);
}
}
20 changes: 11 additions & 9 deletions packages/get-packages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ This library exports `getPackages` and `getPackagesSync`. It is intended mostly
```typescript
import { getPackages, getPackagesSync } from "@manypkg/get-packages";

const { tool, packages, rootPackage, rootDir } = await getPackages(process.cwd());
const { tool, packages, rootPackage, rootDir } = await getPackages(
process.cwd()
);
const { tool, packages, rootPackage, rootDir } = getPackagesSync(process.cwd());

// From @manypkg/tools

interface Tool {
readonly type: string;
isMonorepoRoot(directory: string): Promise<boolean>;
isMonorepoRootSync(directory: string): boolean;
getPackages(directory: string): Promise<Packages>;
getPackagesSync(directory: string): Packages;
readonly type: string;
isMonorepoRoot(directory: string): Promise<boolean>;
isMonorepoRootSync(directory: string): boolean;
getPackages(directory: string): Promise<Packages>;
getPackagesSync(directory: string): Packages;
}

interface Package {
packageJson: PackageJSON;
dir: string;
relativeDir: string;
packageJson: PackageJSON;
dir: string;
relativeDir: string;
}

interface Packages {
Expand Down
10 changes: 5 additions & 5 deletions packages/get-packages/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fixturez from "fixturez";
import path from 'node:path';
import path from "node:path";
import { getPackages, getPackagesSync } from "./";

const f = fixturez(__dirname);
Expand All @@ -11,7 +11,7 @@ let runTests = (getPackages: GetPackages) => {
const dir = f.copy("yarn-workspace-base");

// Test for both root and subdirectories
for (const location of ['.', 'packages', 'packages/pkg-a']) {
for (const location of [".", "packages", "packages/pkg-a"]) {
const allPackages = await getPackages(path.join(dir, location));

if (allPackages.packages === null) {
Expand Down Expand Up @@ -47,7 +47,7 @@ let runTests = (getPackages: GetPackages) => {
const dir = f.copy("bolt-workspace");

// Test for both root and subdirectories
for (const location of ['.', 'packages', 'packages/pkg-b']) {
for (const location of [".", "packages", "packages/pkg-b"]) {
const allPackages = await getPackages(path.join(dir, location));

if (allPackages.packages === null) {
Expand All @@ -68,7 +68,7 @@ let runTests = (getPackages: GetPackages) => {
const dir = f.copy("pnpm-workspace-base");

// Test for both root and subdirectories
for (const location of ['.', 'packages', 'packages/pkg-a']) {
for (const location of [".", "packages", "packages/pkg-a"]) {
const allPackages = await getPackages(path.join(dir, location));

if (allPackages.packages === null) {
Expand Down Expand Up @@ -104,7 +104,7 @@ let runTests = (getPackages: GetPackages) => {
const dir = f.copy("lerna-workspace-base");

// Test for both root and subdirectories
for (const location of ['.', 'packages', 'packages/pkg-b']) {
for (const location of [".", "packages", "packages/pkg-b"]) {
const allPackages = await getPackages(path.join(dir, location));

if (allPackages.packages === null) {
Expand Down
9 changes: 6 additions & 3 deletions packages/get-packages/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import path from "path";
import { findRoot, findRootSync, FindRootOptions } from "@manypkg/find-root";
import { Packages, MonorepoRoot, Tool } from "@manypkg/tools";
Expand Down Expand Up @@ -37,7 +36,9 @@ export async function getPackages(
options?: GetPackagesOptions
): Promise<Packages> {
const monorepoRoot: MonorepoRoot = await findRoot(dir, options);
const packages: Packages = await monorepoRoot.tool.getPackages(monorepoRoot.rootDir);
const packages: Packages = await monorepoRoot.tool.getPackages(
monorepoRoot.rootDir
);

validatePackages(packages);

Expand All @@ -52,7 +53,9 @@ export function getPackagesSync(
options?: GetPackagesOptions
): Packages {
const monorepoRoot: MonorepoRoot = findRootSync(dir, options);
const packages: Packages = monorepoRoot.tool.getPackagesSync(monorepoRoot.rootDir);
const packages: Packages = monorepoRoot.tool.getPackagesSync(
monorepoRoot.rootDir
);

validatePackages(packages);

Expand Down
15 changes: 12 additions & 3 deletions packages/tools/src/BoltTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export const BoltTool: Tool = {

async isMonorepoRoot(directory: string): Promise<boolean> {
try {
const pkgJson = await readJson(directory, "package.json") as BoltPackageJSON;
const pkgJson = (await readJson(
directory,
"package.json"
)) as BoltPackageJSON;
if (pkgJson.bolt && pkgJson.bolt.workspaces) {
return true;
}
Expand All @@ -33,7 +36,10 @@ export const BoltTool: Tool = {

isMonorepoRootSync(directory: string): boolean {
try {
const pkgJson = readJsonSync(directory, "package.json") as BoltPackageJSON;
const pkgJson = readJsonSync(
directory,
"package.json"
) as BoltPackageJSON;
if (pkgJson.bolt && pkgJson.bolt.workspaces) {
return true;
}
Expand All @@ -50,7 +56,10 @@ export const BoltTool: Tool = {
const rootDir = path.resolve(directory);

try {
const pkgJson = await readJson(rootDir, "package.json") as BoltPackageJSON;
const pkgJson = (await readJson(
rootDir,
"package.json"
)) as BoltPackageJSON;
if (!pkgJson.bolt || !pkgJson.bolt.workspaces) {
throw new InvalidMonorepoError(
`Directory ${rootDir} is not a valid ${BoltTool.type} monorepo root: missing bolt.workspaces entry`
Expand Down
13 changes: 4 additions & 9 deletions packages/tools/src/LernaTool.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import path from "path";

import {
Tool,
PackageJSON,
Packages,
InvalidMonorepoError,
} from "./Tool";
import { Tool, PackageJSON, Packages, InvalidMonorepoError } from "./Tool";
import {
expandPackageGlobs,
expandPackageGlobsSync,
Expand All @@ -22,7 +17,7 @@ export const LernaTool: Tool = {

async isMonorepoRoot(directory: string): Promise<boolean> {
try {
const lernaJson = await readJson(directory, "lerna.json") as LernaJson;
const lernaJson = (await readJson(directory, "lerna.json")) as LernaJson;
if (lernaJson.useWorkspaces !== true) {
return true;
}
Expand Down Expand Up @@ -54,8 +49,8 @@ export const LernaTool: Tool = {
const rootDir = path.resolve(directory);

try {
const lernaJson = await readJson(rootDir, "lerna.json") as LernaJson;
const pkgJson = await readJson(rootDir, "package.json") as PackageJSON;
const lernaJson = (await readJson(rootDir, "lerna.json")) as LernaJson;
const pkgJson = (await readJson(rootDir, "package.json")) as PackageJSON;
const packageGlobs: string[] = lernaJson.packages || ["packages/*"];

return {
Expand Down
Loading

0 comments on commit 237f38f

Please sign in to comment.