Skip to content

Commit 237f38f

Browse files
authored
Format unformatted code and add Prettier check to CI (#218)
1 parent 66cdbc0 commit 237f38f

File tree

16 files changed

+158
-145
lines changed

16 files changed

+158
-145
lines changed

.github/workflows/main.yml

+17
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,20 @@ jobs:
4040

4141
- name: Check Types
4242
run: yarn tsc
43+
44+
formatting:
45+
name: Formatting
46+
runs-on: ubuntu-latest
47+
steps:
48+
- uses: actions/checkout@v3
49+
50+
- name: Set Node.js 16.x
51+
uses: actions/setup-node@v3
52+
with:
53+
node-version: 16.x
54+
55+
- name: Install Dependencies
56+
run: yarn
57+
58+
- name: Check Formatting
59+
run: yarn format:check

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist
2+
node_modules
3+
__fixtures__

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"test-gatsby"
99
],
1010
"scripts": {
11+
"format": "prettier --write .",
12+
"format:check": "prettier --check .",
1113
"postinstall": "preconstruct dev && manypkg check",
1214
"release": "preconstruct build && changeset publish",
1315
"test": "jest",

packages/find-root/src/index.ts

+68-74
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,25 @@ export async function findRoot(
7878
let monorepoRoot: MonorepoRoot | undefined;
7979
const tools = options.tools || DEFAULT_TOOLS;
8080

81-
await findUp(
82-
async (directory) => {
83-
return Promise.all(
84-
tools.map(async (tool): Promise<MonorepoRoot | undefined> => {
85-
if (await tool.isMonorepoRoot(directory)) {
86-
return {
87-
tool: tool,
88-
rootDir: directory,
89-
};
90-
}
91-
})
92-
)
93-
.then((x) => x.find((value) => value))
94-
.then((result) => {
95-
if (result) {
96-
monorepoRoot = result;
97-
return directory;
98-
}
99-
});
100-
},
101-
cwd
102-
);
81+
await findUp(async (directory) => {
82+
return Promise.all(
83+
tools.map(async (tool): Promise<MonorepoRoot | undefined> => {
84+
if (await tool.isMonorepoRoot(directory)) {
85+
return {
86+
tool: tool,
87+
rootDir: directory,
88+
};
89+
}
90+
})
91+
)
92+
.then((x) => x.find((value) => value))
93+
.then((result) => {
94+
if (result) {
95+
monorepoRoot = result;
96+
return directory;
97+
}
98+
});
99+
}, cwd);
103100

104101
if (monorepoRoot) {
105102
return monorepoRoot;
@@ -112,19 +109,16 @@ export async function findRoot(
112109
// If there is no monorepo root, but we can find a single package json file, we will
113110
// return a "RootTool" repo, which is the special case where we just have a root package
114111
// with no monorepo implementation (i.e.: a normal package folder).
115-
let rootDir = await findUp(
116-
async (directory) => {
117-
try {
118-
await fsp.access(path.join(directory, "package.json"));
119-
return directory;
120-
} catch (err) {
121-
if (!isNoEntryError(err)) {
122-
throw err;
123-
}
112+
let rootDir = await findUp(async (directory) => {
113+
try {
114+
await fsp.access(path.join(directory, "package.json"));
115+
return directory;
116+
} catch (err) {
117+
if (!isNoEntryError(err)) {
118+
throw err;
124119
}
125-
},
126-
cwd
127-
);
120+
}
121+
}, cwd);
128122

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

149-
findUpSync(
150-
(directory) => {
151-
for (const tool of tools) {
152-
if (tool.isMonorepoRootSync(directory)) {
153-
monorepoRoot = {
154-
tool: tool,
155-
rootDir: directory,
156-
};
157-
return directory;
158-
}
143+
findUpSync((directory) => {
144+
for (const tool of tools) {
145+
if (tool.isMonorepoRootSync(directory)) {
146+
monorepoRoot = {
147+
tool: tool,
148+
rootDir: directory,
149+
};
150+
return directory;
159151
}
160-
},
161-
cwd
162-
);
152+
}
153+
}, cwd);
163154

164155
if (monorepoRoot) {
165156
return monorepoRoot;
@@ -172,13 +163,10 @@ export function findRootSync(
172163
// If there is no monorepo root, but we can find a single package json file, we will
173164
// return a "RootTool" repo, which is the special case where we just have a root package
174165
// with no monorepo implementation (i.e.: a normal package folder).
175-
const rootDir = findUpSync(
176-
(directory) => {
177-
const exists = fs.existsSync(path.join(directory, "package.json"));
178-
return exists ? directory : undefined;
179-
},
180-
cwd
181-
);
166+
const rootDir = findUpSync((directory) => {
167+
const exists = fs.existsSync(path.join(directory, "package.json"));
168+
return exists ? directory : undefined;
169+
}, cwd);
182170

183171
if (!rootDir) {
184172
throw new NoPkgJsonFound(cwd);
@@ -190,30 +178,36 @@ export function findRootSync(
190178
};
191179
}
192180

193-
async function findUp(matcher: (directory: string) => Promise<string | undefined>, cwd: string) {
194-
let directory = path.resolve(cwd);
195-
const { root } = path.parse(directory);
181+
async function findUp(
182+
matcher: (directory: string) => Promise<string | undefined>,
183+
cwd: string
184+
) {
185+
let directory = path.resolve(cwd);
186+
const { root } = path.parse(directory);
196187

197-
while (directory && directory !== root) {
198-
const filePath = await matcher(directory);
199-
if (filePath) {
200-
return path.resolve(directory, filePath);
201-
}
188+
while (directory && directory !== root) {
189+
const filePath = await matcher(directory);
190+
if (filePath) {
191+
return path.resolve(directory, filePath);
192+
}
202193

203-
directory = path.dirname(directory);
204-
}
194+
directory = path.dirname(directory);
195+
}
205196
}
206197

207-
function findUpSync(matcher: (directory: string) => string | undefined, cwd: string) {
208-
let directory = path.resolve(cwd);
209-
const { root } = path.parse(directory);
198+
function findUpSync(
199+
matcher: (directory: string) => string | undefined,
200+
cwd: string
201+
) {
202+
let directory = path.resolve(cwd);
203+
const { root } = path.parse(directory);
210204

211-
while (directory && directory !== root) {
212-
const filePath = matcher(directory);
213-
if (filePath) {
214-
return path.resolve(directory, filePath);
215-
}
205+
while (directory && directory !== root) {
206+
const filePath = matcher(directory);
207+
if (filePath) {
208+
return path.resolve(directory, filePath);
209+
}
216210

217-
directory = path.dirname(directory);
218-
}
211+
directory = path.dirname(directory);
212+
}
219213
}

packages/get-packages/README.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ This library exports `getPackages` and `getPackagesSync`. It is intended mostly
77
```typescript
88
import { getPackages, getPackagesSync } from "@manypkg/get-packages";
99

10-
const { tool, packages, rootPackage, rootDir } = await getPackages(process.cwd());
10+
const { tool, packages, rootPackage, rootDir } = await getPackages(
11+
process.cwd()
12+
);
1113
const { tool, packages, rootPackage, rootDir } = getPackagesSync(process.cwd());
1214

1315
// From @manypkg/tools
1416

1517
interface Tool {
16-
readonly type: string;
17-
isMonorepoRoot(directory: string): Promise<boolean>;
18-
isMonorepoRootSync(directory: string): boolean;
19-
getPackages(directory: string): Promise<Packages>;
20-
getPackagesSync(directory: string): Packages;
18+
readonly type: string;
19+
isMonorepoRoot(directory: string): Promise<boolean>;
20+
isMonorepoRootSync(directory: string): boolean;
21+
getPackages(directory: string): Promise<Packages>;
22+
getPackagesSync(directory: string): Packages;
2123
}
2224

2325
interface Package {
24-
packageJson: PackageJSON;
25-
dir: string;
26-
relativeDir: string;
26+
packageJson: PackageJSON;
27+
dir: string;
28+
relativeDir: string;
2729
}
2830

2931
interface Packages {

packages/get-packages/src/index.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fixturez from "fixturez";
2-
import path from 'node:path';
2+
import path from "node:path";
33
import { getPackages, getPackagesSync } from "./";
44

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

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

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

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

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

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

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

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

110110
if (allPackages.packages === null) {

packages/get-packages/src/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import path from "path";
32
import { findRoot, findRootSync, FindRootOptions } from "@manypkg/find-root";
43
import { Packages, MonorepoRoot, Tool } from "@manypkg/tools";
@@ -37,7 +36,9 @@ export async function getPackages(
3736
options?: GetPackagesOptions
3837
): Promise<Packages> {
3938
const monorepoRoot: MonorepoRoot = await findRoot(dir, options);
40-
const packages: Packages = await monorepoRoot.tool.getPackages(monorepoRoot.rootDir);
39+
const packages: Packages = await monorepoRoot.tool.getPackages(
40+
monorepoRoot.rootDir
41+
);
4142

4243
validatePackages(packages);
4344

@@ -52,7 +53,9 @@ export function getPackagesSync(
5253
options?: GetPackagesOptions
5354
): Packages {
5455
const monorepoRoot: MonorepoRoot = findRootSync(dir, options);
55-
const packages: Packages = monorepoRoot.tool.getPackagesSync(monorepoRoot.rootDir);
56+
const packages: Packages = monorepoRoot.tool.getPackagesSync(
57+
monorepoRoot.rootDir
58+
);
5659

5760
validatePackages(packages);
5861

packages/tools/src/BoltTool.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export const BoltTool: Tool = {
1818

1919
async isMonorepoRoot(directory: string): Promise<boolean> {
2020
try {
21-
const pkgJson = await readJson(directory, "package.json") as BoltPackageJSON;
21+
const pkgJson = (await readJson(
22+
directory,
23+
"package.json"
24+
)) as BoltPackageJSON;
2225
if (pkgJson.bolt && pkgJson.bolt.workspaces) {
2326
return true;
2427
}
@@ -33,7 +36,10 @@ export const BoltTool: Tool = {
3336

3437
isMonorepoRootSync(directory: string): boolean {
3538
try {
36-
const pkgJson = readJsonSync(directory, "package.json") as BoltPackageJSON;
39+
const pkgJson = readJsonSync(
40+
directory,
41+
"package.json"
42+
) as BoltPackageJSON;
3743
if (pkgJson.bolt && pkgJson.bolt.workspaces) {
3844
return true;
3945
}
@@ -50,7 +56,10 @@ export const BoltTool: Tool = {
5056
const rootDir = path.resolve(directory);
5157

5258
try {
53-
const pkgJson = await readJson(rootDir, "package.json") as BoltPackageJSON;
59+
const pkgJson = (await readJson(
60+
rootDir,
61+
"package.json"
62+
)) as BoltPackageJSON;
5463
if (!pkgJson.bolt || !pkgJson.bolt.workspaces) {
5564
throw new InvalidMonorepoError(
5665
`Directory ${rootDir} is not a valid ${BoltTool.type} monorepo root: missing bolt.workspaces entry`

packages/tools/src/LernaTool.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import path from "path";
22

3-
import {
4-
Tool,
5-
PackageJSON,
6-
Packages,
7-
InvalidMonorepoError,
8-
} from "./Tool";
3+
import { Tool, PackageJSON, Packages, InvalidMonorepoError } from "./Tool";
94
import {
105
expandPackageGlobs,
116
expandPackageGlobsSync,
@@ -22,7 +17,7 @@ export const LernaTool: Tool = {
2217

2318
async isMonorepoRoot(directory: string): Promise<boolean> {
2419
try {
25-
const lernaJson = await readJson(directory, "lerna.json") as LernaJson;
20+
const lernaJson = (await readJson(directory, "lerna.json")) as LernaJson;
2621
if (lernaJson.useWorkspaces !== true) {
2722
return true;
2823
}
@@ -54,8 +49,8 @@ export const LernaTool: Tool = {
5449
const rootDir = path.resolve(directory);
5550

5651
try {
57-
const lernaJson = await readJson(rootDir, "lerna.json") as LernaJson;
58-
const pkgJson = await readJson(rootDir, "package.json") as PackageJSON;
52+
const lernaJson = (await readJson(rootDir, "lerna.json")) as LernaJson;
53+
const pkgJson = (await readJson(rootDir, "package.json")) as PackageJSON;
5954
const packageGlobs: string[] = lernaJson.packages || ["packages/*"];
6055

6156
return {

0 commit comments

Comments
 (0)