diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 664f19b..8e6af32 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..241ef0b --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +dist +node_modules +__fixtures__ diff --git a/package.json b/package.json index c14e79a..fb50a79 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/find-root/src/index.ts b/packages/find-root/src/index.ts index 916afa9..ef9ea87 100644 --- a/packages/find-root/src/index.ts +++ b/packages/find-root/src/index.ts @@ -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 => { - 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 => { + 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; @@ -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); @@ -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; @@ -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); @@ -190,30 +178,36 @@ export function findRootSync( }; } -async function findUp(matcher: (directory: string) => Promise, cwd: string) { - let directory = path.resolve(cwd); - const { root } = path.parse(directory); +async function findUp( + matcher: (directory: string) => Promise, + 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); + } } diff --git a/packages/get-packages/README.md b/packages/get-packages/README.md index 6d6d1d3..522e009 100644 --- a/packages/get-packages/README.md +++ b/packages/get-packages/README.md @@ -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; - isMonorepoRootSync(directory: string): boolean; - getPackages(directory: string): Promise; - getPackagesSync(directory: string): Packages; + readonly type: string; + isMonorepoRoot(directory: string): Promise; + isMonorepoRootSync(directory: string): boolean; + getPackages(directory: string): Promise; + getPackagesSync(directory: string): Packages; } interface Package { - packageJson: PackageJSON; - dir: string; - relativeDir: string; + packageJson: PackageJSON; + dir: string; + relativeDir: string; } interface Packages { diff --git a/packages/get-packages/src/index.test.ts b/packages/get-packages/src/index.test.ts index f464817..3daccb2 100644 --- a/packages/get-packages/src/index.test.ts +++ b/packages/get-packages/src/index.test.ts @@ -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); @@ -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) { @@ -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) { @@ -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) { @@ -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) { diff --git a/packages/get-packages/src/index.ts b/packages/get-packages/src/index.ts index 135e2bc..6807922 100644 --- a/packages/get-packages/src/index.ts +++ b/packages/get-packages/src/index.ts @@ -1,4 +1,3 @@ - import path from "path"; import { findRoot, findRootSync, FindRootOptions } from "@manypkg/find-root"; import { Packages, MonorepoRoot, Tool } from "@manypkg/tools"; @@ -37,7 +36,9 @@ export async function getPackages( options?: GetPackagesOptions ): Promise { 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); @@ -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); diff --git a/packages/tools/src/BoltTool.ts b/packages/tools/src/BoltTool.ts index a4b60ca..e5cd9a3 100644 --- a/packages/tools/src/BoltTool.ts +++ b/packages/tools/src/BoltTool.ts @@ -18,7 +18,10 @@ export const BoltTool: Tool = { async isMonorepoRoot(directory: string): Promise { 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; } @@ -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; } @@ -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` diff --git a/packages/tools/src/LernaTool.ts b/packages/tools/src/LernaTool.ts index 51cee9a..428cdb3 100644 --- a/packages/tools/src/LernaTool.ts +++ b/packages/tools/src/LernaTool.ts @@ -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, @@ -22,7 +17,7 @@ export const LernaTool: Tool = { async isMonorepoRoot(directory: string): Promise { 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; } @@ -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 { diff --git a/packages/tools/src/PnpmTool.ts b/packages/tools/src/PnpmTool.ts index a542a75..2acc6ea 100644 --- a/packages/tools/src/PnpmTool.ts +++ b/packages/tools/src/PnpmTool.ts @@ -3,12 +3,7 @@ import yaml from "js-yaml"; import fs from "fs"; import fsp from "fs/promises"; -import { - Tool, - PackageJSON, - Packages, - InvalidMonorepoError, -} from "./Tool"; +import { Tool, PackageJSON, Packages, InvalidMonorepoError } from "./Tool"; import { expandPackageGlobs, expandPackageGlobsSync, @@ -16,10 +11,12 @@ import { import { readJson, readJsonSync } from "./utils"; async function readYamlFile(path: string): Promise { - return fsp.readFile(path, 'utf8').then(data => yaml.load(data)) as Promise; + return fsp + .readFile(path, "utf8") + .then((data) => yaml.load(data)) as Promise; } function readYamlFileSync(path: string): T { - return yaml.load(fs.readFileSync(path, 'utf8')) as T; + return yaml.load(fs.readFileSync(path, "utf8")) as T; } export interface PnpmWorkspaceYaml { @@ -72,7 +69,7 @@ export const PnpmTool: Tool = { const manifest = await readYamlFile<{ packages?: string[] }>( path.join(rootDir, "pnpm-workspace.yaml") ); - const pkgJson = await readJson(rootDir, "package.json") as PackageJSON; + const pkgJson = (await readJson(rootDir, "package.json")) as PackageJSON; const packageGlobs: string[] = manifest.packages!; return { diff --git a/packages/tools/src/RootTool.ts b/packages/tools/src/RootTool.ts index 2ab2c0e..b050f99 100644 --- a/packages/tools/src/RootTool.ts +++ b/packages/tools/src/RootTool.ts @@ -26,7 +26,7 @@ export const RootTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = await readJson(rootDir, "package.json") as PackageJSON; + const pkgJson = (await readJson(rootDir, "package.json")) as PackageJSON; const pkg: Package = { dir: rootDir, relativeDir: ".", diff --git a/packages/tools/src/YarnTool.ts b/packages/tools/src/YarnTool.ts index 406be58..f2ebcbd 100644 --- a/packages/tools/src/YarnTool.ts +++ b/packages/tools/src/YarnTool.ts @@ -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, @@ -21,7 +16,10 @@ export const YarnTool: Tool = { async isMonorepoRoot(directory: string): Promise { try { - const pkgJson = await readJson(directory, "package.json") as YarnPackageJSON; + const pkgJson = (await readJson( + directory, + "package.json" + )) as YarnPackageJSON; if (pkgJson.workspaces) { if ( Array.isArray(pkgJson.workspaces) || @@ -41,7 +39,10 @@ export const YarnTool: Tool = { isMonorepoRootSync(directory: string): boolean { try { - const pkgJson = readJsonSync(directory, "package.json") as YarnPackageJSON; + const pkgJson = readJsonSync( + directory, + "package.json" + ) as YarnPackageJSON; if (pkgJson.workspaces) { if ( Array.isArray(pkgJson.workspaces) || @@ -63,7 +64,10 @@ export const YarnTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = await readJson(rootDir, "package.json") as YarnPackageJSON; + const pkgJson = (await readJson( + rootDir, + "package.json" + )) as YarnPackageJSON; const packageGlobs: string[] = Array.isArray(pkgJson.workspaces) ? pkgJson.workspaces : pkgJson.workspaces!.packages; diff --git a/packages/tools/src/utils.ts b/packages/tools/src/utils.ts index 9decd1e..08ed269 100644 --- a/packages/tools/src/utils.ts +++ b/packages/tools/src/utils.ts @@ -2,12 +2,8 @@ import fs from "fs"; import fsp from "fs/promises"; import path from "path"; -export const readJson = async (directory: string, file: string) => JSON.parse((await fsp.readFile( - path.join(directory, file), - "utf-8" - ))); +export const readJson = async (directory: string, file: string) => + JSON.parse(await fsp.readFile(path.join(directory, file), "utf-8")); -export const readJsonSync = (directory: string, file: string) => JSON.parse(fs.readFileSync( - path.join(directory, file), - "utf-8" - )); +export const readJsonSync = (directory: string, file: string) => + JSON.parse(fs.readFileSync(path.join(directory, file), "utf-8")); diff --git a/test-gatsby/docs/README.md b/test-gatsby/docs/README.md index c6f822d..1c00c4c 100644 --- a/test-gatsby/docs/README.md +++ b/test-gatsby/docs/README.md @@ -4,30 +4,21 @@ What we want: To be able to generate the following pages in a gatsby site on a per-workspace basis -/packages // index list of packages -/packages/:package-name // render the docs/README.md || README.md -/packages/:package-name/docs // index list of docs pages in the package (hidden in nav, but here for url hackers) -/packages/:package-name/docs/:some-file // render the docs file of the appropriate name -/packages/:package-name/arbitrary-page // a file from /docs that has frontmatter specifying a path +/packages // index list of packages /packages/:package-name // render the docs/README.md || README.md /packages/:package-name/docs // index list of docs pages in the package (hidden in nav, but here for url hackers) /packages/:package-name/docs/:some-file // render the docs file of the appropriate name /packages/:package-name/arbitrary-page // a file from /docs that has frontmatter specifying a path Some other notes: -We want the first part of the path to be configurable -We want to correct relative links into absolute links (because relative links are the worst) -related: correct links to files to links to their website location (removing pathname etc) +We want the first part of the path to be configurable We want to correct relative links into absolute links (because relative links are the worst) related: correct links to files to links to their website location (removing pathname etc) Navigation data should be trivial to get ## Second plugin -I want to make a second plugin called `gatsby-source-workspace-deep` (something like this, need another better word). -This should wrap `gatsby-source-workspace` and then use [onCreateNode](https://www.gatsbyjs.org/docs/node-apis/#onCreateNode) -to modify the workspace schema to populate information from the package.json. +I want to make a second plugin called `gatsby-source-workspace-deep` (something like this, need another better word). This should wrap `gatsby-source-workspace` and then use [onCreateNode](https://www.gatsbyjs.org/docs/node-apis/#onCreateNode) to modify the workspace schema to populate information from the package.json. Bonus: It would be good to allow users to extend their own `package.json` fields easily, so we have a standard, but also add stuff you want. -I'm probably going to add some config for the page-generator of the website into individual package.jsons so they can control some stuff, -as well as the config in the page-generator plugin itself +I'm probably going to add some config for the page-generator of the website into individual package.jsons so they can control some stuff, as well as the config in the page-generator plugin itself Derived fields I want in the secondPlugin diff --git a/test-gatsby/package.json b/test-gatsby/package.json index 55eb1d1..2c2eeca 100644 --- a/test-gatsby/package.json +++ b/test-gatsby/package.json @@ -1,7 +1,7 @@ { "name": "test-gatsby-thing", - "private": true, "version": "0.0.7", + "private": true, "dependencies": { "@manypkg/gatsby-source-workspace": "0.5.1", "@mdx-js/mdx": "^1.5.1", diff --git a/tsconfig.json b/tsconfig.json index d47de10..b466982 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -46,7 +46,7 @@ // "typeRoots": [], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ - "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */