Skip to content

Commit

Permalink
fix: fixes various Windows related issues
Browse files Browse the repository at this point in the history
Co-authored-by: HiDeoo <494699+HiDeoo@users.noreply.github.com>
  • Loading branch information
gpoussel and HiDeoo committed Mar 9, 2024
1 parent 5e1d5f2 commit d890fcd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/libs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export function initGitRepository(repoPath: string) {
}

export function stageFiles(repoPath: string, files: string[]) {
return exec('git', ['add', ...files], { cwd: repoPath, silent: true })
return exec('git', ['add', ...files.sort()], { cwd: repoPath, silent: true })
}
11 changes: 10 additions & 1 deletion src/libs/pm.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os from 'node:os'

import { PACKAGE_MANAGER, PACKAGE_MANAGER_EXECUTE } from '../config'

import { exec, type ExecOptions } from './exec'
Expand Down Expand Up @@ -26,8 +28,15 @@ export function executePackageManagerCommand(appPath: string, args: string[], si
return runPackageManager(appPath, args, { execute: true, silent: !!silent })
}

export function getPackageManagerBinary(execute?: RunOptions['execute']) {
const executable = execute ? PACKAGE_MANAGER_EXECUTE : PACKAGE_MANAGER
const extension = os.platform() === 'win32' ? '.cmd' : ''

return `${executable}${extension}`
}

function runPackageManager(appPath: string, args: string[], options: RunOptions = {}) {
return exec(options.execute ? PACKAGE_MANAGER_EXECUTE : PACKAGE_MANAGER, args, { ...options, cwd: appPath })
return exec(getPackageManagerBinary(options.execute), args, { ...options, cwd: appPath })
}

interface RunOptions extends ExecOptions {
Expand Down
6 changes: 3 additions & 3 deletions src/libs/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ let templateVariables: TemplateVariables | undefined
export async function getTemplatePaths(ignoreSpecialTemplates = true) {
const templatePath = getTemplatesPath()

const allTemplatePaths = await glob(path.join(templatePath, '**/*'), { absolute: true, dot: true, filesOnly: true })
const allTemplatePaths = await glob('**/*', { cwd: templatePath, absolute: true, dot: true, filesOnly: true })

const templates: Template[] = []

for (const absolutePath of allTemplatePaths) {
const template = {
destination: absolutePath.replace(`${templatePath}/`, ''),
destination: absolutePath.replace(`${templatePath}${path.sep}`, ''),
source: absolutePath,
}

Expand Down Expand Up @@ -112,7 +112,7 @@ export function compileTemplate(content: string) {
function getTemplatesPath() {
const dirName = path.dirname(fileURLToPath(import.meta.url))

return path.join(dirName, dirName.endsWith('src/libs') ? '../..' : '..', 'templates')
return path.join(dirName, dirName.endsWith(path.join('src', 'libs')) ? '../..' : '..', 'templates')
}

function isValidTemplateVariable(variable: string): variable is keyof TemplateVariables {
Expand Down
53 changes: 32 additions & 21 deletions tests/app.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawn } from 'node:child_process'
import path from 'node:path'

import type { PackageJson } from 'type-fest'
import * as undici from 'undici'
Expand All @@ -11,7 +12,6 @@ import {
NPM_REGISTRY_URL,
NPM_RELEASE_STEP,
PACKAGE_MANAGER,
PACKAGE_MANAGER_EXECUTE,
PKG_INVALID_DEPENDENCIES,
PKG_KEYS_ORDER,
USER_MAIL,
Expand All @@ -21,6 +21,7 @@ import {
import { UNSUPPORTED_ESLINT_CONFIG_FILENAMES } from '../src/libs/eslint'
import { getPkgTsConfig } from '../src/libs/jsdelivr'
import { parsePkg } from '../src/libs/pkg'
import { getPackageManagerBinary } from '../src/libs/pm'
import type { TemplateVariables } from '../src/libs/template'
import { parseTsConfig, PRESERVED_TS_COMPILER_OPTIONS } from '../src/libs/typescript'

Expand Down Expand Up @@ -302,21 +303,28 @@ describe.each(testScenarios)('$description', ({ appName, options, setup }) => {
})

test('should install dependencies', () => {
expectSpawnToHaveBeenNthCalledWith(PACKAGE_MANAGER, ['install'])
expectSpawnToHaveBeenNthCalledWith(getPackageManagerBinary(), ['install'])
})

test('should configure Git hooks', () => {
expectSpawnToHaveBeenNthCalledWith(PACKAGE_MANAGER_EXECUTE, ['husky', 'init'])
expectSpawnToHaveBeenNthCalledWith(getPackageManagerBinary(true), ['husky', 'init'])
})

test('should run ESLint when updating an existing app', () => {
if (!options.isNew) {
expectSpawnToHaveBeenNthCalledWith(PACKAGE_MANAGER, ['exec', 'eslint', '.', '--fix'])
expectSpawnToHaveBeenNthCalledWith(getPackageManagerBinary(), ['exec', 'eslint', '.', '--fix'])
}
})

test('should prettify the app', () => {
expectSpawnToHaveBeenNthCalledWith(PACKAGE_MANAGER, ['exec', 'prettier', '-w', '--log-level', 'silent', '.'])
expectSpawnToHaveBeenNthCalledWith(getPackageManagerBinary(), [
'exec',
'prettier',
'-w',
'--log-level',
'silent',
'.',
])
})

test('should check if the repository exists on GitHub', () => {
Expand Down Expand Up @@ -357,22 +365,25 @@ describe.each(testScenarios)('$description', ({ appName, options, setup }) => {
})

test('should stage new or updated files', () => {
expectSpawnToHaveBeenNthCalledWith('git', [
'add',
'.github/workflows/integration.yml',
'.github/workflows/release.yml',
'.gitignore',
'.husky/pre-commit',
'.prettierignore',
'.vscode/extensions.json',
'.vscode/settings.json',
'LICENSE',
'README.md',
'eslint.config.mjs',
'package.json',
'tsconfig.json',
'pnpm-lock.yaml',
])
expectSpawnToHaveBeenNthCalledWith(
'git',
[
'add',
'.github/workflows/integration.yml',
'.github/workflows/release.yml',
'.gitignore',
'.husky/pre-commit',
'.prettierignore',
'.vscode/extensions.json',
'.vscode/settings.json',
'LICENSE',
'README.md',
'eslint.config.mjs',
'package.json',
'pnpm-lock.yaml',
'tsconfig.json',
].map((filePath) => path.normalize(filePath)),
)
})

test('should cache jsdelivr results', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function setupTest(testName: string) {
}

export async function getTestDirPaths(testDir: string) {
const testDirPaths = await glob(path.join(testDir, '**/*'), { absolute: true, dot: true, filesOnly: true })
const testDirPaths = await glob('**/*', { cwd: testDir, absolute: true, dot: true, filesOnly: true })

return testDirPaths.map((testDirPath) => testDirPath.replace(testDir, ''))
}
Expand Down

0 comments on commit d890fcd

Please sign in to comment.