Skip to content

Commit

Permalink
feat: correct exit code on init/test fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Ni55aN committed Dec 23, 2023
1 parent 0709fb7 commit 7c176d7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/commands/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { join } from 'path'

Check warning on line 1 in src/commands/test/index.ts

View workflow job for this annotation

GitHub Actions / ci / ci

Run autofix to sort these imports!
import fs from 'fs'
import { appsCachePath } from '../../consts'

export async function validateTestRun(app: string, dist: string): Promise<{ error: string | null }> {
const indexHtmlPath = join(process.cwd(), appsCachePath, app, dist, 'index.html')
const indexHtmlExists = await fs.promises.stat(indexHtmlPath).catch(() => false)

if (!indexHtmlExists) {
return { error: 'index.html not found. Check if the app was built correctly' }
}

return { error: null }
}
47 changes: 36 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { App } from 'rete-kit'
import { appsCachePath, projects } from './consts'
import { log } from './ui'
import { fixtures, getFeatures, stackNames, validate } from './commands/init'
import { validateTestRun } from './commands/test'

const program = createCommand()

Expand All @@ -29,6 +30,7 @@ program
const depsAlias = options.depsAlias ? resolve(cwd, options.depsAlias) : undefined
const stacks = options.stack ? options.stack.split(',') : stackNames
const stackVersions = options.stackVersions ? options.stackVersions.split(',') : null
let exitCode = 0

const { error } = validate(stacks, stackVersions)

Expand All @@ -48,16 +50,25 @@ program

log('success')('Start creating', chalk.yellow(stack, `v${version}`), 'application in ', folder)

process.chdir(join(cwd, appsCachePath))
await App.createApp({
name: folder,
stack,
version,
features: features.map(f => f && f.name).filter(Boolean) as string[],
depsAlias,
next
})
await execa('npm', ['run', 'build'], { cwd: join(cwd, appsCachePath, folder) })
try {
process.chdir(join(cwd, appsCachePath))
await App.createApp({
name: folder,
stack,
version,
features: features.map(f => f && f.name).filter(Boolean) as string[],
depsAlias,
next
})
await execa('npm', ['run', 'build'], { cwd: join(cwd, appsCachePath, folder) })
} catch (err) {
console.error(err)
log('fail', 'FAIL')('Initialization of', folder, 'failed.')
exitCode = 1
}
}
if (exitCode) {
process.exit(exitCode)
}
})

Expand All @@ -72,6 +83,7 @@ program
.action(async (options: { updateSnapshots?: boolean, stack?: string, stackVersions?: string, project?: string, grep?: string }) => {

Check warning on line 83 in src/index.ts

View workflow job for this annotation

GitHub Actions / ci / ci

This line has a length of 134. Maximum allowed is 120

Check warning on line 83 in src/index.ts

View workflow job for this annotation

GitHub Actions / ci / ci

Async arrow function has too many statements (24). Maximum allowed is 12
const stacks = options.stack ? options.stack.split(',') : null
const stackVersions = options.stackVersions ? options.stackVersions.split(',') : null
let exitCode = 0

for (const fixture of fixtures) {
const { folder, stack, version } = fixture
Expand All @@ -85,6 +97,14 @@ program
const SERVE = App.builders[stack].getStaticPath(folder, version)
const playwrightFolder = dirname(require.resolve('@playwright/test'))

const { error } = await validateTestRun(APP, SERVE)

if (error) {
log('fail', 'FAIL')(chalk.red(error))
exitCode = 1
continue
}

await execa(`${playwrightFolder}/cli.js`, [
'test',
'--config', join(__dirname, './playwright.config.js'),
Expand All @@ -94,9 +114,14 @@ program
], { env: { APP, SERVE }, stdio: 'inherit' })
log('success', 'DONE')('Testing for', chalk.yellow(folder), 'done')
} catch (err) {
log('fail', 'FAIL')('Tests in', folder, 'failed.', err)
console.error(err)
log('fail', 'FAIL')('Tests in', folder, 'failed.')
exitCode = 1
}
}
if (exitCode) {
process.exit(exitCode)
}
})

program.parse(process.argv)
Expand Down

0 comments on commit 7c176d7

Please sign in to comment.