Skip to content

Commit

Permalink
bug fixes and improvements
Browse files Browse the repository at this point in the history
project validation
add migrate file script
remove logs
  • Loading branch information
Armaldio committed Oct 27, 2024
1 parent 6816c5e commit 49bdfd7
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 66 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"make": "electron-forge make",
"publish": "electron-forge publish",
"make-dev-prerelease": "pnpm version prerelease --preid=dev",
"supa:types": "supabase gen types typescript --project-id $VITE_SUPABASE_PROJECT_ID > src/shared/database.types.ts"
"supa:types": "supabase gen types typescript --project-id $VITE_SUPABASE_PROJECT_ID > src/shared/database.types.ts",
"migrate-file": "pnpm tsx scripts/migrate-file.mts"
},
"dependencies": {
"@changesets/cli": "2.27.9",
Expand Down
40 changes: 40 additions & 0 deletions scripts/migrate-file.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { savedFileMigrator } from '@@/model'
import * as fs from 'fs/promises'
import { join } from 'path'
import { tmpdir } from 'os'
import { nanoid } from 'nanoid'
import { execa } from 'execa'

const args = process.argv.slice(2)
if (args.length !== 1) {
console.error('Usage: node script.ts <filepath>')
process.exit(1)
}

const filePath = args[0]

async function migrateFile() {
try {
// const tmpFile = join(tmpdir(), nanoid() + '.json')

const data = await fs.readFile(filePath, 'utf8')
const json = JSON.parse(data)
const migratedData = await savedFileMigrator.migrate(json)
const str = JSON.stringify(migratedData, null, 2)
console.log('data', data)
console.log('str', str)
await fs.writeFile(filePath, str, 'utf8')
console.log('File successfully migrated to the latest version of the model.')

// await execa('delta', [filePath, tmpFile], {
// env: {
// DELTA_FEATURES: '+side-by-side'
// }
// })
} catch (error) {
console.error(`Error migrating the file: ${error}`)
process.exit(1)
}
}

await migrateFile()
2 changes: 0 additions & 2 deletions src/renderer/components/nodes/EditorNodeAction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ watchDebounced(
// const variables = await variableToFormattedVariable(vm, data.variables)
// console.log('variables', variables)
console.log('variablesDisplay', variablesDisplay)
resolvedParams.value = await makeResolvedParams(
{
params: value.value.params,
Expand Down
86 changes: 51 additions & 35 deletions src/renderer/components/nodes/ParamEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
</Chip>
</label>

<div class="debug">
<!-- <div class="debug">
<pre>editorTextValue: {{ editorTextValue }}</pre>
<pre>simpleInputValue: {{ simpleInputValue }}</pre>
<pre>param.value.value: {{ param.value }}</pre>
</div>
</div> -->

<div class="infos">
<!-- Is valid button -->
Expand All @@ -51,6 +51,10 @@
</div>
</div>

<div v-if="param === undefined">
Oops
</div>

<!-- Code editor -->
<div class="code-editor-wrapper">
<div class="code-editor-wrapper-inner">
Expand Down Expand Up @@ -188,7 +192,7 @@
</template>

<script setup lang="ts">
import { PropType, computed, ref, toRefs, watch } from 'vue'
import { computed, ref, toRefs, watch } from 'vue'
import type { ValueOf } from 'type-fest'
import { Action, Condition, Event } from '@pipelab/plugin-core'
import { createCodeEditor } from '@renderer/utils/code-editor'
Expand Down Expand Up @@ -216,35 +220,47 @@ const vm = await createQuickJs()
type Params = (Action | Condition | Event)['params']
type Param = ValueOf<BlockAction['params']>
const props = defineProps({
param: {
type: Object as PropType<Param>,
required: false
},
paramDefinition: {
type: Object as PropType<ValueOf<Params>>,
required: true
},
paramKey: {
type: [String, Number],
required: true
},
const props = defineProps<{
param: Param | undefined;
paramDefinition: ValueOf<Params>,
paramKey: string | number
value: {
type: Object as PropType<BlockAction | BlockEvent | BlockCondition | BlockLoop>,
required: true
},
steps: {
type: Object as PropType<Steps>,
required: true
},
variables: {
type: Object as PropType<Variable[]>,
required: true
}
})
value: BlockAction | BlockEvent | BlockCondition | BlockLoop
steps: Steps
variables: Variable[]
}>()
// const props = defineProps({
// param: {
// type: Object as PropType<Param>,
// required: false,
// default: undefined,
// },
// paramDefinition: {
// type: Object as PropType<ValueOf<Params>>,
// required: true
// },
// paramKey: {
// type: [String, Number],
// required: true
// },
// value: {
// type: Object as PropType<BlockAction | BlockEvent | BlockCondition | BlockLoop>,
// required: true
// },
// steps: {
// type: Object as PropType<Steps>,
// required: true
// },
// variables: {
// type: Object as PropType<Variable[]>,
// required: true
// }
// })
const { param, paramKey, paramDefinition, steps, variables } = toRefs(props)
const { paramKey, paramDefinition, steps, variables } = toRefs(props)
const { param } = toRefs(props)
const confirm = useConfirm()
Expand Down Expand Up @@ -278,9 +294,9 @@ const confirmSwitchMode = (event: MouseEvent) => {
}
const toggleMode = async (event: MouseEvent) => {
const target = param.value.editor === 'simple' ? 'editor' : 'simple'
const target = param.value?.editor === 'simple' ? 'editor' : 'simple'
let answer = true
let targetValue = klona(param.value.value)
let targetValue = klona(param.value?.value)
if (target === 'simple') {
event.preventDefault()
Expand Down Expand Up @@ -381,7 +397,7 @@ const doCodeEditorUpdate = throttle(async (newValue) => {
// update on code editor text change
emit('update:modelValue', {
editor: param.value.editor,
editor: param.value?.editor,
value: newValue
})
} catch (e) {
Expand Down Expand Up @@ -473,7 +489,7 @@ const onParamEditorUpdate = (value: unknown) => {
const isModalDisplayed = ref(false)
const onClickInside = () => {
if (param.value.editor === 'editor') {
if (param.value?.editor === 'editor') {
isModalDisplayed.value = true
}
}
Expand All @@ -482,7 +498,7 @@ const onClickOutside = () => {
}
watch(
() => param.value.editor,
() => param.value?.editor,
(newValue) => {
if (newValue === 'editor') {
isModalDisplayed.value = true
Expand Down
34 changes: 28 additions & 6 deletions src/renderer/store/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
BlockLoop,
SavedFile,
savedFileMigrator,
SavedFileValidator,
Steps
} from '@@/model'
import {
Expand All @@ -32,6 +33,7 @@ import { processGraph } from '@@/graph'
import { useLogger } from '@@/logger'
import { klona } from 'klona'
import { create } from 'mutative'
import { parse } from 'valibot'

export type Context = Record<string, unknown>

Expand Down Expand Up @@ -226,6 +228,7 @@ export const useEditor = defineStore('editor', () => {
})
}
}

// } else if (block.type === 'condition') {
// const definition = getNodeDefinition(block.origin.nodeId, block.origin.pluginId)
// const requiredParams = Object.keys(definition?.params ?? {})
Expand Down Expand Up @@ -265,21 +268,40 @@ export const useEditor = defineStore('editor', () => {
debug: true
})

console.log('data', data)
// ensure all params are there
const finalData = create(data, (draft) => {
for (const block of draft.canvas.blocks) {
const definition = getNodeDefinition(block.origin.nodeId, block.origin.pluginId)
if (definition) {
const params = definition.node.params
for (const param of Object.keys(params)) {
if (!(param in block.params)) {
console.warn("adding mising param", param)
block.params[param] = {
editor: 'editor',
value: params[param].value
}
}
}
}
}
})

await parse(SavedFileValidator, finalData)

name.value = data.name
description.value = data.description
name.value = finalData.name
description.value = finalData.description

for (const variable of data.variables) {
for (const variable of finalData.variables) {
addVariable(variable)
}

for (const block of data.canvas.blocks) {
for (const block of finalData.canvas.blocks) {
blocks.value.push(block)
validate(block)
}

for (const trigger of data.canvas.triggers) {
for (const trigger of finalData.canvas.triggers) {
triggers.value.push(trigger)
validate(trigger)
}
Expand Down
4 changes: 0 additions & 4 deletions src/renderer/utils/evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { BlockAction, Steps } from '@@/model'
import { createQuickJs, CreateQuickJSFn } from './quickjs'
import { Variable } from '@pipelab/core-app'
import { useLogger } from '@@/logger'
import { variableToFormattedVariable } from '@renderer/composables/variables'

export const makeResolvedParams = async (
data: {
Expand All @@ -23,7 +21,6 @@ export const makeResolvedParams = async (
try {
const parameterCodeValue = (param.value ?? '').toString()

console.log('parameterCodeValue', parameterCodeValue)
const output = await vm.run(parameterCodeValue, {
steps: data.steps,
params: {},
Expand All @@ -37,6 +34,5 @@ export const makeResolvedParams = async (
logger().error('error', e)
}
}
console.log('result', result)
return result
}
1 change: 1 addition & 0 deletions src/renderer/utils/quickjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const createQuickJs = async () => {
return result
} catch (e) {
logger().error('error', e)
logger().error("Final code was", finalCode)
throw new EvaluationError(e.name, e.message)
} finally {
arena.dispose()
Expand Down
1 change: 1 addition & 0 deletions src/shared/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export type SavedFileV1 = InferOutput<typeof SavedFileValidatorV1>
export type SavedFileV2 = InferOutput<typeof SavedFileValidatorV2>
export type SavedFileV3 = InferOutput<typeof SavedFileValidatorV3>
export type SavedFile = SavedFileV3
export const SavedFileValidator = SavedFileValidatorV3

export const savedFileMigrator = createMigrator<SavedFile>({
migrations: [
Expand Down
39 changes: 28 additions & 11 deletions tests/e2e/fixtures/c3-export.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"version": "1.0.0",
"name": "From Construct to Steam",
"description": "Export from Construct, package with Electron, then upload to Steam",
"name": "From Construct to Steam",
"variables": [],
"canvas": {
"blocks": [
"triggers": [
{
"type": "event",
"origin": {
Expand All @@ -13,7 +12,9 @@
},
"uid": "manual-start",
"params": {}
},
}
],
"blocks": [
{
"uid": "export-construct-project",
"type": "action",
Expand All @@ -22,13 +23,29 @@
"pluginId": "construct"
},
"params": {
"file": "\"./tests/e2e/fixtures/c3-export/test.c3p\"",
"username": "",
"password": "",
"version": "",
"headless": true
"file": {
"editor": "editor",
"value": "\"./tests/e2e/fixtures/c3-export/test.c3p\""
},
"username": {
"editor": "editor",
"value": ""
},
"password": {
"editor": "editor",
"value": ""
},
"version": {
"editor": "editor",
"value": ""
},
"headless": {
"editor": "editor",
"value": true
}
}
}
]
}
}
},
"version": "3.0.0"
}
Loading

0 comments on commit 49bdfd7

Please sign in to comment.