diff --git a/src/commands/test/index.ts b/src/commands/test/index.ts new file mode 100644 index 0000000..ef9e474 --- /dev/null +++ b/src/commands/test/index.ts @@ -0,0 +1,14 @@ +import { join } from 'path' +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 } +} diff --git a/src/index.ts b/src/index.ts index 34405de..df0a152 100755 --- a/src/index.ts +++ b/src/index.ts @@ -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() @@ -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) @@ -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) } }) @@ -72,6 +83,7 @@ program .action(async (options: { updateSnapshots?: boolean, stack?: string, stackVersions?: string, project?: string, grep?: string }) => { 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 @@ -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'), @@ -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)