From 31630e3230066f9e30cbbb54f034816074da57e9 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 11 Jul 2025 14:54:14 +0200 Subject: [PATCH 1/7] Core: Fix moving log file across disks --- code/core/src/common/utils/cli.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/code/core/src/common/utils/cli.ts b/code/core/src/common/utils/cli.ts index 260ebdad1ce9..9907ebfbbb8b 100644 --- a/code/core/src/common/utils/cli.ts +++ b/code/core/src/common/utils/cli.ts @@ -1,6 +1,6 @@ import type { WriteStream } from 'node:fs'; import { createWriteStream, mkdirSync } from 'node:fs'; -import { readFile, realpath, rename, rm, writeFile } from 'node:fs/promises'; +import { copyFile, readFile, realpath, rm, writeFile } from 'node:fs/promises'; import os from 'node:os'; import { join } from 'node:path'; @@ -147,10 +147,11 @@ export const createLogStream = async ( return new Promise((resolve, reject) => { logStream.once('open', () => { - const moveLogFile = async () => rename(temporaryLogPath, finalLogPath); const clearLogFile = async () => writeFile(temporaryLogPath, ''); const removeLogFile = async () => rm(temporaryLogPath, { recursive: true, force: true }); const readLogFile = async () => readFile(temporaryLogPath, { encoding: 'utf8' }); + // Can't use rename because it doesn't work across disks. + const moveLogFile = async () => copyFile(temporaryLogPath, finalLogPath).then(removeLogFile); resolve({ logStream, moveLogFile, clearLogFile, removeLogFile, readLogFile }); }); logStream.once('error', reject); From d6522800ef0ea56a65642c0487e10ce76506dfec Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Mon, 14 Jul 2025 11:11:15 +0200 Subject: [PATCH 2/7] Add some debug logging around framework detection --- code/core/src/cli/detect.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/code/core/src/cli/detect.ts b/code/core/src/cli/detect.ts index ac9b15923799..043e98360fab 100644 --- a/code/core/src/cli/detect.ts +++ b/code/core/src/cli/detect.ts @@ -99,6 +99,7 @@ export function detectFrameworkPreset( packageJson = {} as PackageJsonWithMaybeDeps ): ProjectType | null { const result = [...supportedTemplates, unsupportedTemplate].find((framework) => { + logger.debug(`- Trying framework preset: ${framework.preset}`); return getFrameworkPreset(packageJson, framework) !== null; }); @@ -227,16 +228,20 @@ export async function detect( options: { force?: boolean; html?: boolean } = {} ) { try { + logger.debug('Checking for NX project'); if (await isNxProject()) { return ProjectType.NX; } + logger.debug('Checking for HTML project'); if (options.html) { return ProjectType.HTML; } const { packageJson } = packageManager.primaryPackageJson; - + logger.debug( + `Checking package.json for framework preset:\n${JSON.stringify(packageJson, null, 2)}` + ); return detectFrameworkPreset(packageJson); } catch (e) { return ProjectType.UNDETECTED; From ecbc2fdc9df41f32d4f258610832613ce8b3a306 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Tue, 15 Jul 2025 13:41:16 +0200 Subject: [PATCH 3/7] Use process.write rather than logger.debug --- code/core/src/cli/detect.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/code/core/src/cli/detect.ts b/code/core/src/cli/detect.ts index 043e98360fab..fc78ea493d49 100644 --- a/code/core/src/cli/detect.ts +++ b/code/core/src/cli/detect.ts @@ -228,19 +228,19 @@ export async function detect( options: { force?: boolean; html?: boolean } = {} ) { try { - logger.debug('Checking for NX project'); + process.stdout.write('\nChecking for NX project\n'); if (await isNxProject()) { return ProjectType.NX; } - logger.debug('Checking for HTML project'); + process.stdout.write('\nChecking for HTML project\n'); if (options.html) { return ProjectType.HTML; } const { packageJson } = packageManager.primaryPackageJson; - logger.debug( - `Checking package.json for framework preset:\n${JSON.stringify(packageJson, null, 2)}` + process.stdout.write( + `\nChecking package.json for framework preset:\n${JSON.stringify(packageJson, null, 2)}\n` ); return detectFrameworkPreset(packageJson); } catch (e) { From 2b126f8cbfa6d97992c82ae0f55e90bd64afd746 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Tue, 15 Jul 2025 14:39:23 +0200 Subject: [PATCH 4/7] Log cwd and project root --- code/core/src/cli/helpers.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/code/core/src/cli/helpers.ts b/code/core/src/cli/helpers.ts index d07f05db54af..6e606c4f2560 100644 --- a/code/core/src/cli/helpers.ts +++ b/code/core/src/cli/helpers.ts @@ -261,7 +261,10 @@ export async function adjustTemplate(templatePath: string, templateData: Record< } export async function isNxProject() { - return findUpSync('nx.json', { stopAt: getProjectRoot() }); + process.stdout.write(`\ncwd: ${process.cwd()}\n`); + const projectRoot = getProjectRoot(); + process.stdout.write(`\nprojectRoot: ${projectRoot}\n`); + return findUpSync('nx.json', { stopAt: projectRoot }); } export function coerceSemver(version: string) { From dbc6ee9c1b1c72ef429b9485cdc67a045a9f0dd6 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Tue, 15 Jul 2025 17:35:35 +0200 Subject: [PATCH 5/7] More logging --- code/core/src/common/utils/paths.ts | 38 ++++++++++++----------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/code/core/src/common/utils/paths.ts b/code/core/src/common/utils/paths.ts index 95657805df84..0035ef4728ac 100644 --- a/code/core/src/common/utils/paths.ts +++ b/code/core/src/common/utils/paths.ts @@ -8,45 +8,36 @@ let projectRoot: string | undefined; export const getProjectRoot = () => { if (projectRoot) { + process.stdout.write(`\nfound cached projectRoot: ${projectRoot}\n`); return projectRoot; } let result; // Allow manual override in cases where auto-detect doesn't work if (process.env.STORYBOOK_PROJECT_ROOT) { + process.stdout.write(`\nusing STORYBOOK_PROJECT_ROOT: ${process.env.STORYBOOK_PROJECT_ROOT}\n`); return process.env.STORYBOOK_PROJECT_ROOT; } try { - const found = findUpSync('.git', { type: 'directory' }); + process.stdout.write(`\nsearching for repository root\n`); + const found = + findUpSync('.git', { type: 'directory' }) || + findUpSync('.svn', { type: 'directory' }) || + findUpSync('.hg', { type: 'directory' }); if (found) { + process.stdout.write(`\nfound repository root: ${found}\n`); result = join(found, '..'); } } catch (e) { - // - } - - try { - const found = findUpSync('.svn', { type: 'directory' }); - if (found) { - result = result || join(found, '..'); - } - } catch (e) { - // - } - - try { - const found = findUpSync('.hg', { type: 'directory' }); - if (found) { - result = result || join(found, '..'); - } - } catch (e) { - // + process.stdout.write(`\nerror searching for repository root: ${e}\n`); } try { const splitDirname = __dirname.split('node_modules'); + process.stdout.write(`\nsplitDirname: ${splitDirname}\n`); const isSplitDirnameReachable = !relative(splitDirname[0], process.cwd()).startsWith('..'); + process.stdout.write(`\nisSplitDirnameReachable: ${isSplitDirnameReachable}\n`); result = result || (isSplitDirnameReachable @@ -55,20 +46,23 @@ export const getProjectRoot = () => { : undefined : undefined); } catch (e) { - // + process.stdout.write(`\nerror searching for splitDirname: ${e}\n`); } try { + process.stdout.write(`\nsearching for lock file\n`); const found = findUpSync(LOCK_FILES, { type: 'file', }); if (found) { + process.stdout.write(`\nfound lock file: ${found}\n`); result = result || join(found, '..'); } } catch (e) { - // + process.stdout.write(`\nerror searching for lock file: ${e}\n`); } + process.stdout.write(`\nresult: ${result}\n`); projectRoot = result || process.cwd(); return projectRoot; From 60488856cfad26f823686f3b6cc66d39e6320c2e Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Mon, 21 Jul 2025 15:58:23 +0200 Subject: [PATCH 6/7] Fix __dirname check to avoid npm-cache, move it to be the last check and clean up logging --- code/core/src/cli/detect.ts | 6 ---- code/core/src/cli/helpers.ts | 2 -- code/core/src/common/utils/paths.ts | 47 +++++++++++------------------ 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/code/core/src/cli/detect.ts b/code/core/src/cli/detect.ts index fc78ea493d49..487e1f67bd92 100644 --- a/code/core/src/cli/detect.ts +++ b/code/core/src/cli/detect.ts @@ -99,7 +99,6 @@ export function detectFrameworkPreset( packageJson = {} as PackageJsonWithMaybeDeps ): ProjectType | null { const result = [...supportedTemplates, unsupportedTemplate].find((framework) => { - logger.debug(`- Trying framework preset: ${framework.preset}`); return getFrameworkPreset(packageJson, framework) !== null; }); @@ -228,20 +227,15 @@ export async function detect( options: { force?: boolean; html?: boolean } = {} ) { try { - process.stdout.write('\nChecking for NX project\n'); if (await isNxProject()) { return ProjectType.NX; } - process.stdout.write('\nChecking for HTML project\n'); if (options.html) { return ProjectType.HTML; } const { packageJson } = packageManager.primaryPackageJson; - process.stdout.write( - `\nChecking package.json for framework preset:\n${JSON.stringify(packageJson, null, 2)}\n` - ); return detectFrameworkPreset(packageJson); } catch (e) { return ProjectType.UNDETECTED; diff --git a/code/core/src/cli/helpers.ts b/code/core/src/cli/helpers.ts index 6e606c4f2560..eda8f1935948 100644 --- a/code/core/src/cli/helpers.ts +++ b/code/core/src/cli/helpers.ts @@ -261,9 +261,7 @@ export async function adjustTemplate(templatePath: string, templateData: Record< } export async function isNxProject() { - process.stdout.write(`\ncwd: ${process.cwd()}\n`); const projectRoot = getProjectRoot(); - process.stdout.write(`\nprojectRoot: ${projectRoot}\n`); return findUpSync('nx.json', { stopAt: projectRoot }); } diff --git a/code/core/src/common/utils/paths.ts b/code/core/src/common/utils/paths.ts index 0035ef4728ac..de29cb6d8c15 100644 --- a/code/core/src/common/utils/paths.ts +++ b/code/core/src/common/utils/paths.ts @@ -8,63 +8,52 @@ let projectRoot: string | undefined; export const getProjectRoot = () => { if (projectRoot) { - process.stdout.write(`\nfound cached projectRoot: ${projectRoot}\n`); return projectRoot; } - let result; // Allow manual override in cases where auto-detect doesn't work if (process.env.STORYBOOK_PROJECT_ROOT) { - process.stdout.write(`\nusing STORYBOOK_PROJECT_ROOT: ${process.env.STORYBOOK_PROJECT_ROOT}\n`); return process.env.STORYBOOK_PROJECT_ROOT; } try { - process.stdout.write(`\nsearching for repository root\n`); const found = findUpSync('.git', { type: 'directory' }) || findUpSync('.svn', { type: 'directory' }) || findUpSync('.hg', { type: 'directory' }); if (found) { - process.stdout.write(`\nfound repository root: ${found}\n`); - result = join(found, '..'); + projectRoot = join(found, '..'); + return projectRoot; } } catch (e) { process.stdout.write(`\nerror searching for repository root: ${e}\n`); } try { - const splitDirname = __dirname.split('node_modules'); - process.stdout.write(`\nsplitDirname: ${splitDirname}\n`); - const isSplitDirnameReachable = !relative(splitDirname[0], process.cwd()).startsWith('..'); - process.stdout.write(`\nisSplitDirnameReachable: ${isSplitDirnameReachable}\n`); - result = - result || - (isSplitDirnameReachable - ? splitDirname.length >= 2 - ? splitDirname[0] - : undefined - : undefined); + const found = findUpSync(LOCK_FILES, { type: 'file' }); + if (found) { + projectRoot = join(found, '..'); + return projectRoot; + } } catch (e) { - process.stdout.write(`\nerror searching for splitDirname: ${e}\n`); + process.stdout.write(`\nerror searching for lock file: ${e}\n`); } try { - process.stdout.write(`\nsearching for lock file\n`); - const found = findUpSync(LOCK_FILES, { - type: 'file', - }); - if (found) { - process.stdout.write(`\nfound lock file: ${found}\n`); - result = result || join(found, '..'); + const [basePath, rest] = __dirname.split(`${sep}node_modules${sep}`, 2); + if ( + rest && + !basePath.includes(`${sep}npm-cache${sep}`) && + !relative(basePath, process.cwd()).startsWith('..') + ) { + projectRoot = basePath; + return projectRoot; } } catch (e) { - process.stdout.write(`\nerror searching for lock file: ${e}\n`); + process.stdout.write(`\nerror searching for splitDirname: ${e}\n`); } - process.stdout.write(`\nresult: ${result}\n`); - projectRoot = result || process.cwd(); - + projectRoot = process.cwd(); return projectRoot; }; From 32ae86fa09ff7cffd875953bbae2c52c77b54ebe Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Mon, 21 Jul 2025 16:01:57 +0200 Subject: [PATCH 7/7] Simplify --- code/core/src/cli/helpers.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/code/core/src/cli/helpers.ts b/code/core/src/cli/helpers.ts index eda8f1935948..d07f05db54af 100644 --- a/code/core/src/cli/helpers.ts +++ b/code/core/src/cli/helpers.ts @@ -261,8 +261,7 @@ export async function adjustTemplate(templatePath: string, templateData: Record< } export async function isNxProject() { - const projectRoot = getProjectRoot(); - return findUpSync('nx.json', { stopAt: projectRoot }); + return findUpSync('nx.json', { stopAt: getProjectRoot() }); } export function coerceSemver(version: string) {