Skip to content

Commit

Permalink
Merge pull request #2 from retejs/validate-init-parameters
Browse files Browse the repository at this point in the history
Validate init parameters
  • Loading branch information
Ni55aN authored Dec 22, 2023
2 parents d18509e + c50dd36 commit 0878284
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 35 deletions.
45 changes: 10 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,12 @@ import { join, dirname, resolve } from 'path'
import { App } from 'rete-kit'
import { appsCachePath, projects } from './consts'
import { log } from './ui'
import { fixtures, getFeatures, stackNames, validate } from './init'

const program = createCommand()

program.version(require('../package.json').version)

const targets: { stack: App.AppStack, versions: number[] }[] = [
{ stack: 'react', versions: [16, 17, 18] },
{ stack: 'vue', versions: [2, 3] },
{ stack: 'angular', versions: [12, 13, 14, 15, 16, 17] },
{ stack: 'svelte', versions: [3, 4] }
]
const stackNames = targets.map(t => t.stack)

const fixtures = targets
.map(({ stack, versions }) => versions.map(version => ({ stack, version, folder: `${stack}${version}` as const })))
.flat()
.map(({ stack, version, folder }) => ({
stack,
version,
folder
}))

function getFeatures({ stack, version }: (typeof fixtures)[0], next: boolean) {
return [
stack === 'angular' && new App.Features.Angular(version as 12 | 13 | 14 | 15, next),
stack === 'react' && new App.Features.React(version, stack, next),
stack === 'vue' && new App.Features.Vue(version as 2 | 3, next),
stack === 'svelte' && new App.Features.Svelte(version as 3 | 4, next),
new App.Features.ZoomAt(),
new App.Features.OrderNodes(),
new App.Features.Dataflow(next),
new App.Features.Selectable(),
new App.Features.Minimap(next),
new App.Features.Reroute(next)
]
}


program
.command('init')
.description(`Initialize testing tool`)
Expand All @@ -59,16 +27,23 @@ program
const next = options.next || false
const cwd = process.cwd()
const depsAlias = options.depsAlias ? resolve(cwd, options.depsAlias) : undefined
const stacks = options.stack ? options.stack.split(',') : null
const stacks = options.stack ? options.stack.split(',') : stackNames
const stackVersions = options.stackVersions ? options.stackVersions.split(',') : null

const { error } = validate(stacks, stackVersions)

if (error) {
log('fail', 'FAIL')(chalk.red(error))
process.exit(1)
}

await fs.promises.mkdir(join(cwd, appsCachePath), { recursive: true })

for (const fixture of fixtures) {
const features = getFeatures(fixture, next)
const { folder, stack, version } = fixture

if (stacks && !stacks.includes(stack)) continue
if (!stacks.includes(stack)) continue
if (stackVersions && !stackVersions.includes(String(version))) continue

log('success')('Start creating', chalk.yellow(stack, `v${version}`), 'application in ', folder)
Expand Down
55 changes: 55 additions & 0 deletions src/init/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { App } from 'rete-kit'

export const targets: { stack: App.AppStack, versions: number[] }[] = [
{ stack: 'react', versions: [16, 17, 18] },
{ stack: 'vue', versions: [2, 3] },
{ stack: 'angular', versions: [12, 13, 14, 15, 16, 17] },
{ stack: 'svelte', versions: [3, 4] }
]
export const stackNames = targets.map(t => t.stack)

export const fixtures = targets
.map(({ stack, versions }) => versions.map(version => ({ stack, version, folder: `${stack}${version}` as const })))
.flat()
.map(({ stack, version, folder }) => ({
stack,
version,
folder
}))

export function getFeatures({ stack, version }: (typeof fixtures)[0], next: boolean) {
return [
stack === 'angular' && new App.Features.Angular(version as 12 | 13 | 14 | 15, next),
stack === 'react' && new App.Features.React(version, stack, next),
stack === 'vue' && new App.Features.Vue(version as 2 | 3, next),
stack === 'svelte' && new App.Features.Svelte(version as 3 | 4, next),
new App.Features.ZoomAt(),
new App.Features.OrderNodes(),
new App.Features.Dataflow(next),
new App.Features.Selectable(),
new App.Features.Minimap(next),
new App.Features.Reroute(next)
]
}

export function validate(stacks: string[], stackVersions: string[] | null): { error: string | null } {
const unknownStacks = stacks.filter(name => !stackNames.includes(name as App.AppStack))

if (unknownStacks.length > 0) {
return { error: `Unknown stack names: ${unknownStacks.join(', ')}` }
}

if (stacks.length > 1 && stackVersions && stackVersions.length > 0) {
return { error: `You can't specify versions for multiple stacks` }
}

if (stacks.length === 1 && stackVersions && stackVersions.length > 0) {
const unknownVersions = stackVersions.filter(v => !targets.find(t => t.stack === stacks[0])?.versions.includes(Number(v)))

Check warning on line 47 in src/init/index.ts

View workflow job for this annotation

GitHub Actions / release / publish

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

if (unknownVersions.length > 0) {
return { error: `Unknown stack versions: ${unknownVersions.join(', ')}` }
}
}

return { error: null }
}

0 comments on commit 0878284

Please sign in to comment.