Skip to content

Commit

Permalink
fix: resolved npm_execpath issue (#79)
Browse files Browse the repository at this point in the history
* fix: resolved npm_execpath issue

* test: resolved the failing test

* chore: minor restructuring

* fix: pm could also be passed from cli options
  • Loading branch information
MathurAditya724 authored Nov 29, 2024
1 parent 8165789 commit 222dc97
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Hook<HookFunction extends HookSignature> {
type AfterHookOptions = {
projectName: string
directoryPath: string
packageManager: string
}

type AfterHookFunction = (options: AfterHookOptions) => void
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/after-create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions src/hooks/after-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
10 changes: 8 additions & 2 deletions src/hooks/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand Down Expand Up @@ -88,6 +92,8 @@ const registerInstallationHook = (
})
}

emitter.emit('packageManager', packageManager)

emitter.on('dependencies', async () => {
chdir(directoryPath)

Expand Down
15 changes: 11 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ async function main(

const emitter = new EventEmitter<EventMap>()

// Default package manager
let packageManager = pm ?? 'npm'
emitter.addListener('packageManager', (pm) => {
packageManager = String(pm)
})

registerInstallationHook(templateName, install, pm, emitter)

try {
Expand All @@ -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(
Expand Down

0 comments on commit 222dc97

Please sign in to comment.