mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 12:11:56 +00:00
Refactor/actions (#2733)
* store, properties, manifest * interfaces * init and backups * fix init and backups * file models * more versions * dependencies * config except dynamic types * clean up config * remove disabled from non-dynamic vaues * actions * standardize example code block formats * wip: actions refactor Co-authored-by: Jade <Blu-J@users.noreply.github.com> * commit types * fix types * update types * update action request type * update apis * add description to actionrequest * clean up imports * revert package json * chore: Remove the recursive to the index * chore: Remove the other thing I was testing * flatten action requests * update container runtime with new config paradigm * new actions strategy * seems to be working * misc backend fixes * fix fe bugs * only show breakages if breakages * only show success modal if result * don't panic on failed removal * hide config from actions page * polyfill autoconfig * use metadata strategy for actions instead of prev * misc fixes * chore: split the sdk into 2 libs (#2736) * follow sideload progress (#2718) * follow sideload progress * small bugfix * shareReplay with no refcount false * don't wrap sideload progress in RPCResult * dont present toast --------- Co-authored-by: Aiden McClelland <me@drbonez.dev> * chore: Add the initial of the creation of the two sdk * chore: Add in the baseDist * chore: Add in the baseDist * chore: Get the web and the runtime-container running * chore: Remove the empty file * chore: Fix it so the container-runtime works --------- Co-authored-by: Matt Hill <MattDHill@users.noreply.github.com> Co-authored-by: Aiden McClelland <me@drbonez.dev> * misc fixes * update todos * minor clean up * fix link script * update node version in CI test * fix node version syntax in ci build * wip: fixing callbacks * fix sdk makefile dependencies * add support for const outside of main * update apis * don't panic! * Chore: Capture weird case on rpc, and log that * fix procedure id issue * pass input value for dep auto config * handle disabled and warning for actions * chore: Fix for link not having node_modules * sdk fixes * fix build * fix build * fix build --------- Co-authored-by: Matt Hill <mattnine@protonmail.com> Co-authored-by: Jade <Blu-J@users.noreply.github.com> Co-authored-by: J H <dragondef@gmail.com> Co-authored-by: Jade <2364004+Blu-J@users.noreply.github.com> Co-authored-by: Matt Hill <MattDHill@users.noreply.github.com>
This commit is contained in:
411
sdk/package/scripts/oldSpecToBuilder.ts
Normal file
411
sdk/package/scripts/oldSpecToBuilder.ts
Normal file
@@ -0,0 +1,411 @@
|
||||
import * as fs from "fs"
|
||||
|
||||
// https://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case
|
||||
export function camelCase(value: string) {
|
||||
return value
|
||||
.replace(/([\(\)\[\]])/g, "")
|
||||
.replace(/^([A-Z])|[\s-_](\w)/g, function (match, p1, p2, offset) {
|
||||
if (p2) return p2.toUpperCase()
|
||||
return p1.toLowerCase()
|
||||
})
|
||||
}
|
||||
|
||||
export async function oldSpecToBuilder(
|
||||
file: string,
|
||||
inputData: Promise<any> | any,
|
||||
options?: Parameters<typeof makeFileContentFromOld>[1],
|
||||
) {
|
||||
await fs.writeFile(
|
||||
file,
|
||||
await makeFileContentFromOld(inputData, options),
|
||||
(err) => console.error(err),
|
||||
)
|
||||
}
|
||||
|
||||
function isString(x: unknown): x is string {
|
||||
return typeof x === "string"
|
||||
}
|
||||
|
||||
export default async function makeFileContentFromOld(
|
||||
inputData: Promise<any> | any,
|
||||
{ StartSdk = "start-sdk", nested = true } = {},
|
||||
) {
|
||||
const outputLines: string[] = []
|
||||
outputLines.push(`
|
||||
import { sdk } from "${StartSdk}"
|
||||
const {InputSpec, List, Value, Variants} = sdk
|
||||
`)
|
||||
const data = await inputData
|
||||
|
||||
const namedConsts = new Set(["InputSpec", "Value", "List"])
|
||||
const inputSpecName = newConst("inputSpecSpec", convertInputSpec(data))
|
||||
const inputSpecMatcherName = newConst(
|
||||
"matchInputSpecSpec",
|
||||
`${inputSpecName}.validator`,
|
||||
)
|
||||
outputLines.push(
|
||||
`export type InputSpecSpec = typeof ${inputSpecMatcherName}._TYPE;`,
|
||||
)
|
||||
|
||||
return outputLines.join("\n")
|
||||
|
||||
function newConst(key: string, data: string, type?: string) {
|
||||
const variableName = getNextConstName(camelCase(key))
|
||||
outputLines.push(
|
||||
`export const ${variableName}${!type ? "" : `: ${type}`} = ${data};`,
|
||||
)
|
||||
return variableName
|
||||
}
|
||||
function maybeNewConst(key: string, data: string) {
|
||||
if (nested) return data
|
||||
return newConst(key, data)
|
||||
}
|
||||
function convertInputSpecInner(data: any) {
|
||||
let answer = "{"
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
const variableName = maybeNewConst(key, convertValueSpec(value))
|
||||
|
||||
answer += `${JSON.stringify(key)}: ${variableName},`
|
||||
}
|
||||
return `${answer}}`
|
||||
}
|
||||
|
||||
function convertInputSpec(data: any) {
|
||||
return `InputSpec.of(${convertInputSpecInner(data)})`
|
||||
}
|
||||
function convertValueSpec(value: any): string {
|
||||
switch (value.type) {
|
||||
case "string": {
|
||||
if (value.textarea) {
|
||||
return `${rangeToTodoComment(
|
||||
value?.range,
|
||||
)}Value.textarea(${JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
description: value.description || null,
|
||||
warning: value.warning || null,
|
||||
required: !(value.nullable || false),
|
||||
placeholder: value.placeholder || null,
|
||||
maxLength: null,
|
||||
minLength: null,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
)})`
|
||||
}
|
||||
return `${rangeToTodoComment(value?.range)}Value.text(${JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
// prettier-ignore
|
||||
required: (
|
||||
value.default != null ? {default: value.default} :
|
||||
value.nullable === false ? {default: null} :
|
||||
!value.nullable
|
||||
),
|
||||
description: value.description || null,
|
||||
warning: value.warning || null,
|
||||
masked: value.masked || false,
|
||||
placeholder: value.placeholder || null,
|
||||
inputmode: "text",
|
||||
patterns: value.pattern
|
||||
? [
|
||||
{
|
||||
regex: value.pattern,
|
||||
description: value["pattern-description"],
|
||||
},
|
||||
]
|
||||
: [],
|
||||
minLength: null,
|
||||
maxLength: null,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
)})`
|
||||
}
|
||||
case "number": {
|
||||
return `${rangeToTodoComment(
|
||||
value?.range,
|
||||
)}Value.number(${JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
description: value.description || null,
|
||||
warning: value.warning || null,
|
||||
// prettier-ignore
|
||||
required: (
|
||||
value.default != null ? {default: value.default} :
|
||||
value.nullable === false ? {default: null} :
|
||||
!value.nullable
|
||||
),
|
||||
min: null,
|
||||
max: null,
|
||||
step: null,
|
||||
integer: value.integral || false,
|
||||
units: value.units || null,
|
||||
placeholder: value.placeholder || null,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
)})`
|
||||
}
|
||||
case "boolean": {
|
||||
return `Value.toggle(${JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
default: value.default || false,
|
||||
description: value.description || null,
|
||||
warning: value.warning || null,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
)})`
|
||||
}
|
||||
case "enum": {
|
||||
const allValueNames = new Set([
|
||||
...(value?.["values"] || []),
|
||||
...Object.keys(value?.["value-names"] || {}),
|
||||
])
|
||||
const values = Object.fromEntries(
|
||||
Array.from(allValueNames)
|
||||
.filter(isString)
|
||||
.map((key) => [key, value?.spec?.["value-names"]?.[key] || key]),
|
||||
)
|
||||
return `Value.select(${JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
description: value.description || null,
|
||||
warning: value.warning || null,
|
||||
|
||||
// prettier-ignore
|
||||
required:(
|
||||
value.default != null ? {default: value.default} :
|
||||
value.nullable === false ? {default: null} :
|
||||
!value.nullable
|
||||
),
|
||||
values,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
)} as const)`
|
||||
}
|
||||
case "object": {
|
||||
const specName = maybeNewConst(
|
||||
value.name + "_spec",
|
||||
convertInputSpec(value.spec),
|
||||
)
|
||||
return `Value.object({
|
||||
name: ${JSON.stringify(value.name || null)},
|
||||
description: ${JSON.stringify(value.description || null)},
|
||||
}, ${specName})`
|
||||
}
|
||||
case "union": {
|
||||
const variants = maybeNewConst(
|
||||
value.name + "_variants",
|
||||
convertVariants(value.variants, value.tag["variant-names"] || {}),
|
||||
)
|
||||
|
||||
return `Value.union({
|
||||
name: ${JSON.stringify(value.name || null)},
|
||||
description: ${JSON.stringify(value.tag.description || null)},
|
||||
warning: ${JSON.stringify(value.tag.warning || null)},
|
||||
|
||||
// prettier-ignore
|
||||
required: ${JSON.stringify(
|
||||
// prettier-ignore
|
||||
value.default != null ? {default: value.default} :
|
||||
value.nullable === false ? {default: null} :
|
||||
!value.nullable,
|
||||
)},
|
||||
}, ${variants})`
|
||||
}
|
||||
case "list": {
|
||||
if (value.subtype === "enum") {
|
||||
const allValueNames = new Set([
|
||||
...(value?.spec?.["values"] || []),
|
||||
...Object.keys(value?.spec?.["value-names"] || {}),
|
||||
])
|
||||
const values = Object.fromEntries(
|
||||
Array.from(allValueNames)
|
||||
.filter(isString)
|
||||
.map((key: string) => [
|
||||
key,
|
||||
value?.spec?.["value-names"]?.[key] ?? key,
|
||||
]),
|
||||
)
|
||||
return `Value.multiselect(${JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
minLength: null,
|
||||
maxLength: null,
|
||||
default: value.default ?? null,
|
||||
description: value.description || null,
|
||||
warning: value.warning || null,
|
||||
values,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
)})`
|
||||
}
|
||||
const list = maybeNewConst(value.name + "_list", convertList(value))
|
||||
return `Value.list(${list})`
|
||||
}
|
||||
case "pointer": {
|
||||
return `/* TODO deal with point removed point "${value.name}" */null as any`
|
||||
}
|
||||
}
|
||||
throw Error(`Unknown type "${value.type}"`)
|
||||
}
|
||||
|
||||
function convertList(value: any) {
|
||||
switch (value.subtype) {
|
||||
case "string": {
|
||||
return `${rangeToTodoComment(value?.range)}List.text(${JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
minLength: null,
|
||||
maxLength: null,
|
||||
default: value.default || null,
|
||||
description: value.description || null,
|
||||
warning: value.warning || null,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
)}, ${JSON.stringify({
|
||||
masked: value?.spec?.masked || false,
|
||||
placeholder: value?.spec?.placeholder || null,
|
||||
patterns: value?.spec?.pattern
|
||||
? [
|
||||
{
|
||||
regex: value.spec.pattern,
|
||||
description: value?.spec?.["pattern-description"],
|
||||
},
|
||||
]
|
||||
: [],
|
||||
minLength: null,
|
||||
maxLength: null,
|
||||
})})`
|
||||
}
|
||||
// case "number": {
|
||||
// return `${rangeToTodoComment(value?.range)}List.number(${JSON.stringify(
|
||||
// {
|
||||
// name: value.name || null,
|
||||
// minLength: null,
|
||||
// maxLength: null,
|
||||
// default: value.default || null,
|
||||
// description: value.description || null,
|
||||
// warning: value.warning || null,
|
||||
// },
|
||||
// null,
|
||||
// 2,
|
||||
// )}, ${JSON.stringify({
|
||||
// integer: value?.spec?.integral || false,
|
||||
// min: null,
|
||||
// max: null,
|
||||
// units: value?.spec?.units || null,
|
||||
// placeholder: value?.spec?.placeholder || null,
|
||||
// })})`
|
||||
// }
|
||||
case "enum": {
|
||||
return "/* error!! list.enum */"
|
||||
}
|
||||
case "object": {
|
||||
const specName = maybeNewConst(
|
||||
value.name + "_spec",
|
||||
convertInputSpec(value.spec.spec),
|
||||
)
|
||||
return `${rangeToTodoComment(value?.range)}List.obj({
|
||||
name: ${JSON.stringify(value.name || null)},
|
||||
minLength: ${JSON.stringify(null)},
|
||||
maxLength: ${JSON.stringify(null)},
|
||||
default: ${JSON.stringify(value.default || null)},
|
||||
description: ${JSON.stringify(value.description || null)},
|
||||
}, {
|
||||
spec: ${specName},
|
||||
displayAs: ${JSON.stringify(value?.spec?.["display-as"] || null)},
|
||||
uniqueBy: ${JSON.stringify(value?.spec?.["unique-by"] || null)},
|
||||
})`
|
||||
}
|
||||
case "union": {
|
||||
const variants = maybeNewConst(
|
||||
value.name + "_variants",
|
||||
convertVariants(
|
||||
value.spec.variants,
|
||||
value.spec["variant-names"] || {},
|
||||
),
|
||||
)
|
||||
const unionValueName = maybeNewConst(
|
||||
value.name + "_union",
|
||||
`${rangeToTodoComment(value?.range)}
|
||||
Value.union({
|
||||
name: ${JSON.stringify(value?.spec?.tag?.name || null)},
|
||||
description: ${JSON.stringify(
|
||||
value?.spec?.tag?.description || null,
|
||||
)},
|
||||
warning: ${JSON.stringify(value?.spec?.tag?.warning || null)},
|
||||
required: ${JSON.stringify(
|
||||
// prettier-ignore
|
||||
'default' in value?.spec ? {default: value?.spec?.default} :
|
||||
!!value?.spec?.tag?.nullable || false ? {default: null} :
|
||||
false,
|
||||
)},
|
||||
}, ${variants})
|
||||
`,
|
||||
)
|
||||
const listInputSpec = maybeNewConst(
|
||||
value.name + "_list_inputSpec",
|
||||
`
|
||||
InputSpec.of({
|
||||
"union": ${unionValueName}
|
||||
})
|
||||
`,
|
||||
)
|
||||
return `${rangeToTodoComment(value?.range)}List.obj({
|
||||
name:${JSON.stringify(value.name || null)},
|
||||
minLength:${JSON.stringify(null)},
|
||||
maxLength:${JSON.stringify(null)},
|
||||
default: [],
|
||||
description: ${JSON.stringify(value.description || null)},
|
||||
warning: ${JSON.stringify(value.warning || null)},
|
||||
}, {
|
||||
spec: ${listInputSpec},
|
||||
displayAs: ${JSON.stringify(value?.spec?.["display-as"] || null)},
|
||||
uniqueBy: ${JSON.stringify(value?.spec?.["unique-by"] || null)},
|
||||
})`
|
||||
}
|
||||
}
|
||||
throw new Error(`Unknown subtype "${value.subtype}"`)
|
||||
}
|
||||
|
||||
function convertVariants(
|
||||
variants: Record<string, unknown>,
|
||||
variantNames: Record<string, string>,
|
||||
): string {
|
||||
let answer = "Variants.of({"
|
||||
for (const [key, value] of Object.entries(variants)) {
|
||||
const variantSpec = maybeNewConst(key, convertInputSpec(value))
|
||||
answer += `"${key}": {name: "${
|
||||
variantNames[key] || key
|
||||
}", spec: ${variantSpec}},`
|
||||
}
|
||||
return `${answer}})`
|
||||
}
|
||||
|
||||
function getNextConstName(name: string, i = 0): string {
|
||||
const newName = !i ? name : name + i
|
||||
if (namedConsts.has(newName)) {
|
||||
return getNextConstName(name, i + 1)
|
||||
}
|
||||
namedConsts.add(newName)
|
||||
return newName
|
||||
}
|
||||
}
|
||||
|
||||
function rangeToTodoComment(range: string | undefined) {
|
||||
if (!range) return ""
|
||||
return `/* TODO: Convert range for this value (${range})*/`
|
||||
}
|
||||
|
||||
// oldSpecToBuilder(
|
||||
// "./inputSpec.ts",
|
||||
// // Put inputSpec here
|
||||
// {},
|
||||
// )
|
||||
Reference in New Issue
Block a user