From 222dc97e17afa242afaafe5a3a77974dccc438e7 Mon Sep 17 00:00:00 2001 From: Aditya Mathur <57684218+MathurAditya724@users.noreply.github.com> Date: Fri, 29 Nov 2024 13:31:37 +0530 Subject: [PATCH] fix: resolved npm_execpath issue (#79) * fix: resolved npm_execpath issue * test: resolved the failing test * chore: minor restructuring * fix: pm could also be passed from cli options --- src/hook.ts | 1 + src/hooks/after-create.test.ts | 2 ++ src/hooks/after-create.ts | 12 ++++++++++++ src/hooks/dependencies.ts | 10 ++++++++-- src/index.ts | 15 +++++++++++---- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/hook.ts b/src/hook.ts index 0799d5c..272b655 100644 --- a/src/hook.ts +++ b/src/hook.ts @@ -37,6 +37,7 @@ export class Hook { type AfterHookOptions = { projectName: string directoryPath: string + packageManager: string } type AfterHookFunction = (options: AfterHookOptions) => void diff --git a/src/hooks/after-create.test.ts b/src/hooks/after-create.test.ts index a7f9ea6..0b9da0a 100644 --- a/src/hooks/after-create.test.ts +++ b/src/hooks/after-create.test.ts @@ -25,6 +25,7 @@ name = "%%PROJECT_NAME%%-staging" const projectName = 'test-projectNAME+123' const directoryPath = './tmp' const wranglerPath = join(directoryPath, 'wrangler.toml') + const packageManager = 'npm' const firstHookContent = ` name = "test-projectname-123" @@ -47,6 +48,7 @@ name = "%%PROJECT_NAME%%-staging" afterCreateHook.applyHook('cloudflare-workers', { projectName, directoryPath, + packageManager, }) expect(readFileSync).toHaveBeenCalledWith(wranglerPath, 'utf-8') expect(writeFileSync).nthCalledWith(1, wranglerPath, firstHookContent) diff --git a/src/hooks/after-create.ts b/src/hooks/after-create.ts index f5c23c2..acaac51 100644 --- a/src/hooks/after-create.ts +++ b/src/hooks/after-create.ts @@ -17,6 +17,18 @@ afterCreateHook.addHook( }, ) +const PACKAGE_MANAGER = new RegExp(/\$npm_execpath/g) + +afterCreateHook.addHook( + ['cloudflare-pages', 'x-basic'], + ({ packageManager, directoryPath }) => { + const packageJsonPath = path.join(directoryPath, 'package.json') + const packageJson = readFileSync(packageJsonPath, 'utf-8') + const rewritten = packageJson.replaceAll(PACKAGE_MANAGER, packageManager) + writeFileSync(packageJsonPath, rewritten) + }, +) + const COMPATIBILITY_DATE = /compatibility_date\s*=\s*"\d{4}-\d{2}-\d{2}"/ afterCreateHook.addHook( ['cloudflare-workers', 'cloudflare-pages'], diff --git a/src/hooks/dependencies.ts b/src/hooks/dependencies.ts index dc706b7..1e50376 100644 --- a/src/hooks/dependencies.ts +++ b/src/hooks/dependencies.ts @@ -24,7 +24,11 @@ const currentPackageManager = getCurrentPackageManager() // Deno and Netlify need no dependency installation step const excludeTemplate = ['deno', 'netlify'] -export type EventMap = { dependencies: unknown[]; completed: unknown[] } +export type EventMap = { + dependencies: unknown[] + packageManager: unknown[] + completed: unknown[] +} const registerInstallationHook = ( template: string, @@ -50,7 +54,7 @@ const registerInstallationHook = ( let isVersion1 = false try { const { stdout } = await execa('deno', ['-v']) - isVersion1 = stdout.split(' ')[1].split('.')[0] == '1' + isVersion1 = stdout.split(' ')[1].split('.')[0] === '1' } catch { isVersion1 = true } @@ -88,6 +92,8 @@ const registerInstallationHook = ( }) } + emitter.emit('packageManager', packageManager) + emitter.on('dependencies', async () => { chdir(directoryPath) diff --git a/src/index.ts b/src/index.ts index 38117b0..4244a26 100644 --- a/src/index.ts +++ b/src/index.ts @@ -151,6 +151,12 @@ async function main( const emitter = new EventEmitter() + // Default package manager + let packageManager = pm ?? 'npm' + emitter.addListener('packageManager', (pm) => { + packageManager = String(pm) + }) + registerInstallationHook(templateName, install, pm, emitter) try { @@ -169,14 +175,15 @@ async function main( offline, force: true, }, - ).then(() => { - spinner.success() - emitter.emit('dependencies') - }) + ) + + spinner.success() + emitter.emit('dependencies') afterCreateHook.applyHook(templateName, { projectName, directoryPath: targetDirectoryPath, + packageManager, }) } catch (e) { throw new Error(