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

fix: resolved npm_execpath issue #79

Merged
merged 4 commits into from
Nov 29, 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
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