Skip to content

Commit

Permalink
Solve issue with monorepo resolution and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
paales committed Dec 3, 2024
1 parent e528743 commit 06057bd
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 90 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@
"react-dom": "^18.3.1"
},
"devDependencies": {
"@graphcommerce/cli": "9.0.0-canary.107",
"@graphcommerce/cli": "workspace:^",
"@graphcommerce/eslint-config-pwa": "workspace:^",
"@graphcommerce/hygraph-cli": "workspace:^",
"@graphcommerce/next-config": "workspace:^",
"@graphcommerce/prettier-config-pwa": "workspace:^",
"@graphcommerce/typescript-config-pwa": "workspace:^",
"@graphql-codegen/testing": "3.0.4",
"@playwright/test": "1.49.0",
"@testing-library/dom": "^10.4.0",
Expand Down
1 change: 0 additions & 1 deletion packages/cli/dist/bin/is-monorepo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env node
import { spawn } from 'node:child_process';
import 'node:fs';
import path from 'node:path';
import { findParentPath } from '@graphcommerce/next-config';
import { detect } from 'detect-package-manager';
Expand Down
26 changes: 1 addition & 25 deletions packages/cli/src/bin/is-monorepo.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
#!/usr/bin/env node
import type { ChildProcess } from 'node:child_process'
import { spawn } from 'node:child_process'
import fs from 'node:fs'
import path from 'node:path'
import { findParentPath, isMonorepo } from '@graphcommerce/next-config'
import { findParentPath } from '@graphcommerce/next-config'
import { detect } from 'detect-package-manager'

const debug = process.env.DEBUG === '1'
// eslint-disable-next-line no-console
const log = (message: string) => debug && console.log(`is-monorepo: ${message}`)
const logError = (message: string) => console.error(`is-monorepo: ${message}`)

/** Find the nearest parent directory containing a @graphcommerce/* package */
function findRootDir(startDir: string): string | null {
// Start from the parent directory to find a parent @graphcommerce package
let currentDir = path.dirname(startDir)
log(`Looking for parent packages starting from: ${currentDir}`)

while (currentDir !== path.parse(currentDir).root) {
try {
const packageJson = JSON.parse(fs.readFileSync(path.join(currentDir, 'package.json'), 'utf8'))
if (packageJson.name.startsWith('@graphcommerce/')) {
log(`Found root directory at: ${currentDir}`)
return currentDir
}
} catch {
// Continue if package.json doesn't exist or can't be parsed
}
currentDir = path.dirname(currentDir)
}

return null
}

/**
* Executes a command depending if we're running in a monorepo or not. Usage: is-monorepo '[pkgrun]
* run my-script' '[pkgrun] run my-other-script'
Expand All @@ -43,7 +20,6 @@ function findRootDir(startDir: string): string | null {
* - Npm -> 'npm run'
*/
async function main() {
// const isMono = isMonorepo()
const parentPath = findParentPath(process.cwd())

const command = parentPath ? process.argv.slice(2)[0] : process.argv.slice(2)[1]
Expand Down
2 changes: 1 addition & 1 deletion packagesDev/next-config/dist/commands/copyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Found in packages:
Source: ${sourcePath}`);
process.exit(1);
}
console.log(`Creating new file: ${file}`);
console.log(`Creating new file: ${file}\nSource: ${sourcePath}`);
debug('File does not exist yet');
}
// Skip if content is identical (including magic comment)
Expand Down
28 changes: 0 additions & 28 deletions packagesDev/next-config/dist/utils/isMonorepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isMonorepo = isMonorepo;
exports.findParentPath = findParentPath;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
Expand All @@ -20,33 +19,6 @@ function findPackageJson(directory) {
return null;
}
}
/**
* Determines if we're running in a monorepo context and how to handle postinstall scripts.
*
* If there is a parent `@graphcommerce/*` package, we're in a monorepo.
*/
function isMonorepo() {
let currentDir = process.cwd();
log(`Starting directory: ${currentDir}`);
// Start from the parent directory to find a parent @graphcommerce package
currentDir = node_path_1.default.dirname(currentDir);
log(`Looking for parent packages starting from: ${currentDir}`);
// Keep going up until we find a root package or hit the filesystem root
while (currentDir !== node_path_1.default.parse(currentDir).root) {
const packageJson = findPackageJson(currentDir);
if (packageJson) {
log(`Found package.json in: ${currentDir}`);
log(`Package name: ${packageJson.name}`);
if (packageJson.name.startsWith('@graphcommerce/')) {
log('isMonorepo result: true (found parent @graphcommerce package)');
return true;
}
}
currentDir = node_path_1.default.dirname(currentDir);
}
log('isMonorepo result: false (no parent @graphcommerce package found)');
return false;
}
/**
* Finds the path of the parent @graphcommerce package if it exists Returns null if no parent
* package is found
Expand Down
34 changes: 0 additions & 34 deletions packagesDev/next-config/src/utils/isMonorepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,6 @@ function findPackageJson(directory: string): { name: string } | null {
}
}

/**
* Determines if we're running in a monorepo context and how to handle postinstall scripts.
*
* If there is a parent `@graphcommerce/*` package, we're in a monorepo.
*/
export function isMonorepo() {
let currentDir = process.cwd()
log(`Starting directory: ${currentDir}`)

// Start from the parent directory to find a parent @graphcommerce package
currentDir = path.dirname(currentDir)
log(`Looking for parent packages starting from: ${currentDir}`)

// Keep going up until we find a root package or hit the filesystem root
while (currentDir !== path.parse(currentDir).root) {
const packageJson = findPackageJson(currentDir)

if (packageJson) {
log(`Found package.json in: ${currentDir}`)
log(`Package name: ${packageJson.name}`)

if (packageJson.name.startsWith('@graphcommerce/')) {
log('isMonorepo result: true (found parent @graphcommerce package)')
return true
}
}

currentDir = path.dirname(currentDir)
}

log('isMonorepo result: false (no parent @graphcommerce package found)')
return false
}

/**
* Finds the path of the parent @graphcommerce package if it exists Returns null if no parent
* package is found
Expand Down

0 comments on commit 06057bd

Please sign in to comment.