Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve issue with monorepo resolution and cleanup #2443

Merged
merged 5 commits into from
Dec 6, 2024
Merged
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
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
4 changes: 2 additions & 2 deletions packages/lingui-next/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { isMonorepo } = require('@graphcommerce/next-config')
const { findParentPath } = require('@graphcommerce/next-config')
const { formatter } = require('@lingui/format-po')

/**
Expand All @@ -15,7 +15,7 @@ function linguiNextConfig(config) {
const { locales, ...otherConfig } = config
return {
orderBy: 'messageId',
locales: isMonorepo() ? ['en', 'nl', 'fr', 'de', 'es', 'it'] : config.locales,
locales: findParentPath(process.cwd()) ? ['en', 'nl', 'fr', 'de', 'es', 'it'] : config.locales,
// formatOptions: { lineNumbers: false, origins: false, explicitIdAsDefault: true },
format: formatter({ explicitIdAsDefault: true, lineNumbers: false, origins: false }),
catalogs: [
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateConfig = generateConfig;
// eslint-disable-next-line import/no-extraneous-dependencies
const fs_1 = require("fs");
const cli_1 = require("@graphql-codegen/cli");
const core_1 = require("@swc/core");
const dotenv_1 = __importDefault(require("dotenv"));
const fs_1 = require("fs");
const isMonorepo_1 = require("../../utils/isMonorepo");
const resolveDependenciesSync_1 = require("../../utils/resolveDependenciesSync");
const resolveDependency_1 = require("../../utils/resolveDependency");
Expand Down Expand Up @@ -42,7 +42,7 @@ async function generateConfig() {
},
},
},
...((0, isMonorepo_1.isMonorepo)() && {
...((0, isMonorepo_1.findParentPath)(process.cwd()) && {
'../../docs/framework/config.md': {
plugins: ['@graphcommerce/graphql-codegen-markdown-docs'],
},
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
6 changes: 3 additions & 3 deletions packagesDev/next-config/src/config/commands/generateConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { writeFileSync } from 'fs'
import { generate } from '@graphql-codegen/cli'
import { transformFileSync } from '@swc/core'
import dotenv from 'dotenv'
import { writeFileSync } from 'fs'
import { isMonorepo } from '../../utils/isMonorepo'
import { findParentPath } from '../../utils/isMonorepo'
import { resolveDependenciesSync } from '../../utils/resolveDependenciesSync'
import { resolveDependency } from '../../utils/resolveDependency'

Expand Down Expand Up @@ -42,7 +42,7 @@ export async function generateConfig() {
},
},
},
...(isMonorepo() && {
...(findParentPath(process.cwd()) && {
'../../docs/framework/config.md': {
plugins: ['@graphcommerce/graphql-codegen-markdown-docs'],
},
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
Loading