Skip to content

Commit

Permalink
fix: Happy path npm run build working
Browse files Browse the repository at this point in the history
  • Loading branch information
jlacivita committed Jun 7, 2024
1 parent d3c7b5c commit 8810817
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 173 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"prepare:setup": "npx mkdirp ./dist/docs ./build/docs/markdown ./build/docs/wiki ./build/sdk/javascript/src",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --config=jest.config.json --detectOpenHandles",
"build": "npm run validate && npm run build:docs && npm run build:sdk",
"validate": "node ./src/cli.mjs validate --input ./test/openrpc --schemas test/schemas --transformations && npm run build:openrpc && node ./src/cli.mjs validate --input ./build/sdk-open-rpc.json",
"build:openrpc": "node ./src/cli.mjs openrpc --input ./test --template ./src/openrpc-template.json --server ./build/sdk-open-rpc.json --schemas test/schemas",
"build:sdk": "node ./src/cli.mjs sdk --server ./build/sdk-open-rpc.json --template ./test/sdk --output ./build/sdk/javascript/src --schemas test/schemas",
"build:docs": "node ./src/cli.mjs docs --server ./build/sdk-open-rpc.json --output ./build/docs/markdown --schemas test/schemas --as-path",
"build:wiki": "node ./src/cli.mjs docs --server ./build/sdk-open-rpc.json --output ./build/docs/wiki --schemas test/schemas",
"validate": "node ./src/cli.mjs validate --input ./test/openrpc --schemas test/schemas --transformations --bidirectional && npm run build:openrpc && node ./src/cli.mjs validate --input ./build/sdk-open-rpc.json",
"build:openrpc": "node ./src/cli.mjs openrpc --input ./test --template ./src/openrpc-template.json --server ./build/sdk-open-rpc.json --client ./build/sdk-app-open-rpc.json -schemas test/schemas",
"build:sdk": "node ./src/cli.mjs sdk --server ./build/sdk-open-rpc.json --client ./build/sdk-app-open-rpc.json --template ./test/sdk --output ./build/sdk/javascript/src --schemas test/schemas",
"build:docs": "node ./src/cli.mjs docs --server ./build/sdk-open-rpc.json --client ./build/sdk-app-open-rpc.json --output ./build/docs/markdown --schemas test/schemas --as-path",
"build:wiki": "node ./src/cli.mjs docs --server ./build/sdk-open-rpc.json --client ./build/sdk-app-open-rpc.json --output ./build/docs/wiki --schemas test/schemas",
"dist": "npm run validate && npm run build:sdk && npm run build:docs && npm run test",
"prepare": "husky install"
},
Expand Down
7 changes: 4 additions & 3 deletions src/docs/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import { readJson } from '../shared/filesystem.mjs'
/************************************************************************************************/
// destructure well-known cli args and alias to variables expected by script
const run = async ({
input: input,
server: server,
client: client,
template: template,
output: output,
examples: examples,
Expand All @@ -39,7 +40,7 @@ const run = async ({
// Important file/directory locations
try {
// Important file/directory locations
const packageJsonFile = path.join(path.dirname(input), '..', 'package.json')
const packageJsonFile = path.join(path.dirname(server), '..', 'package.json')
const packageJson = await readJson(packageJsonFile)
libraryName = packageJson.name || libraryName
}
Expand All @@ -50,7 +51,7 @@ const run = async ({

const config = await readJson(path.join(language, 'language.config.json'))

return macrofy(input, template, output, {
return macrofy(server, client, template, output, {
headline: "documentation",
outputDirectory: 'content',
sharedTemplates: path.join(language, 'templates'),
Expand Down
2 changes: 1 addition & 1 deletion src/firebolt-openrpc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$id": "https://meta.comcast.com/firebolt/openrpc",
"$id": "https://meta.rdkcentral.com/firebolt/schemas/openrpc",
"title": "FireboltOpenRPC",
"oneOf": [
{
Expand Down
57 changes: 29 additions & 28 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ const generateAggregateMacros = (server, additional, templates, library) => {

template = getTemplate('/codeblocks/mock-import', templates)
if (template && module.info) {
console.log(`aggregate: ${module.info.title}`)
console.dir(module.info)
acc.mockImports += insertInfoMacros(template + '\n', infoMacros)
}
Expand Down Expand Up @@ -1180,7 +1179,7 @@ function generateExamples(json = {}, mainTemplates = {}, languages = {}) {
langcode: templates['__config'].langcode,
code: getTemplateForExample(method, templates)
.replace(/\$\{rpc\.example\.params\}/g, JSON.stringify(Object.fromEntries(example.params.map(param => [param.name, param.value])))),
result: getTemplateForExampleResult(method, templates)
result: method.result && getTemplateForExampleResult(method, templates)
.replace(/\$\{example\.result\}/g, JSON.stringify(value, null, '\t'))
.replace(/\$\{example\.result\.item\}/g, Array.isArray(value) ? JSON.stringify(value[0], null, '\t') : ''),
template: lang === 'JSON-RPC' ? getTemplate('/examples/jsonrpc', mainTemplates) : getTemplateForExample(method, mainTemplates) // getTemplate('/examples/default', mainTemplates)
Expand Down Expand Up @@ -1447,13 +1446,13 @@ function insertMethodMacros(template, methodObj, server, client, templates, type
if (Object.keys(languages).length && template.indexOf('${method.signature}') >= 0) {
const lang = languages[Object.keys(languages)[0]]
signature = getTemplateForDeclaration(methodObj, templates, 'declarations')
types.setTemplates(lang)
Types.setTemplates(lang)
const currentConfig = JSON.parse(JSON.stringify(config))
config.operators = config.operators || {}
config.operators.paramDelimiter = ', '
signature = insertMethodMacros(signature, methodObj, json, lang, type)
signature = insertMethodMacros(signature, methodObj, server, client, lang, type)
config = currentConfig
types.setTemplates(templates)
Types.setTemplates(templates)
}
else {
signature = ''
Expand Down Expand Up @@ -1945,30 +1944,32 @@ function insertProviderInterfaceMacros(template, _interface, server = {}, client
// insert the standard method templates for each provider
if (match) {
iface.forEach(method => {
// add a tag to pick the correct template
method.tags.unshift({
name: 'provider'
})
const parametersSchema = method.params[0].schema
const parametersShape = Types.getSchemaShape(parametersSchema, document, { templateDir: state.typeTemplateDir, namespace: !config.copySchemasIntoModules })
let methodBlock = insertMethodMacros(getTemplateForMethod(method, templates), method, server, client, templates)
methodBlock = methodBlock.replace(/\${parameters\.shape\}/g, parametersShape)
const hasProviderParameters = parametersSchema && parametersSchema.properties && Object.keys(parametersSchema.properties).length > 0
if (hasProviderParameters) {
const lines = methodBlock.split('\n')
for (let i = lines.length - 1; i >= 0; i--) {
if (lines[i].match(/\$\{provider\.param\.[a-zA-Z]+\}/)) {
let line = lines[i]
lines.splice(i, 1)
line = insertProviderParameterMacros(line, method.params[0].schema, document)
lines.splice(i++, 0, line)

// uni-directional providers have all params composed into an object, these macros output them
if (!client) {
const parametersSchema = method.params[0].schema
const parametersShape = Types.getSchemaShape(parametersSchema, document, { templateDir: state.typeTemplateDir, namespace: !config.copySchemasIntoModules })
methodBlock = methodBlock.replace(/\${parameters\.shape\}/g, parametersShape)

const hasProviderParameters = parametersSchema && parametersSchema.properties && Object.keys(parametersSchema.properties).length > 0
if (hasProviderParameters) {
const lines = methodBlock.split('\n')
for (let i = lines.length - 1; i >= 0; i--) {
if (lines[i].match(/\$\{provider\.param\.[a-zA-Z]+\}/)) {
let line = lines[i]
lines.splice(i, 1)
line = insertProviderParameterMacros(line, method.params[0].schema, document)
lines.splice(i++, 0, line)
}
}
methodBlock = lines.join('\n')
}
else {
methodBlock = methodBlock.replace(/\$\{if\.provider\.params\}.*?\$\{end\.if\.provider\.params\}/gms, '')
}
methodBlock = lines.join('\n')
}
else {
methodBlock = methodBlock.replace(/\$\{if\.provider\.params\}.*?\$\{end\.if\.provider\.params\}/gms, '')
}

methodsBlock += methodBlock
})

Expand All @@ -1990,12 +1991,12 @@ function insertProviderInterfaceMacros(template, _interface, server = {}, client

// first check for indented lines, and do the fancy indented replacement
.replace(/^([ \t]+)(.*?)\$\{provider\.interface\.example\.result\}/gm, '$1$2' + indent(JSON.stringify(method.examples[0].result.value, null, ' '), '$1'))
.replace(/^([ \t]+)(.*?)\$\{provider\.interface\.example\.parameters\}/gm, '$1$2' + indent(JSON.stringify(method.examples[0].params[0].value, null, ' '), '$1'))
.replace(/^([ \t]+)(.*?)\$\{provider\.interface\.example\.parameters\}/gm, '$1$2' + indent(JSON.stringify(method.examples[0].params[0]?.value || '', null, ' '), '$1'))
// okay now just do the basic replacement (a single regex for both was not fun)
.replace(/\$\{provider\.interface\.example\.result\}/g, JSON.stringify(method.examples[0].result.value))
.replace(/\$\{provider\.interface\.example\.parameters\}/g, JSON.stringify(method.examples[0].params[0].value))
.replace(/\$\{provider\.interface\.example\.parameters\}/g, JSON.stringify(method.examples[0].params[0]?.value || ''))

.replace(/\$\{provider\.interface\.example\.correlationId\}/g, JSON.stringify(method.examples[0].params[1].value.correlationId))
.replace(/\$\{provider\.interface\.example\.correlationId\}/g, JSON.stringify(method.examples[0].params[1]?.value.correlationId || ''))

// a set of up to three RPC "id" values for generating intersting examples with matching ids
.replace(/\$\{provider\.interface\.i\}/g, i)
Expand Down
2 changes: 1 addition & 1 deletion src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const macrofy = async (
treeshakePattern = null,
treeshakeEntry = null,
treeshakeTypes = [],
moduleWhitelist
moduleWhitelist = []
} = options

return new Promise( async (resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ function getSchemaType(schema, module, { templateDir = 'types', link = false, co
return insertConstMacros(getTemplate(path.join(templateDir, 'const')), schema, module)
}
else if (schema['x-method']) {
const target = JSON.parse(JSON.stringify(module.methods.find(m => m.name === schema['x-method'].split('.').pop())))
const target = JSON.parse(JSON.stringify(module.methods.find(m => m.name === schema['x-method'])))

// transform the method copy params to be in the order of the x-additional-params array (and leave out any we don't want)
if (schema['x-additional-params']) {
Expand Down
2 changes: 1 addition & 1 deletion src/openrpc-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
{
"name": "rpc.discover",
"summary": "The OpenRPC schema for this JSON-RPC API",
"params": [],
"tags": [
{
"name": "capabilities",
"x-uses": [ "xrn:firebolt:capability:rpc:discover" ]
}
],
"params": [],
"result": {
"name": "OpenRPC Schema",
"schema": {
Expand Down
15 changes: 5 additions & 10 deletions src/shared/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -479,19 +479,14 @@ const createNotifierFromProperty = property => {
'x-event': methodRename(notifier, name => 'on' + name.charAt(0).toUpperCase() + name.substring(1))
})

if (property.tags.find(t => (t.name == 'property' || t.name.startsWith('property:')) && (subscriberType === 'global'))) {
notifier.params = []
notifier.result = {
name: "info",
schema: {
"$ref": "#/components/schemas/" + methodRename(property, name => name.charAt(0).toUpperCase() + name.substring(1) + 'ChangedInfo'),
}
}
}

notifier.params.push(notifier.result)
delete notifier.result

notifier.examples.forEach(example => {
example.params.push(example.result)
delete example.result
})

return notifier
}

Expand Down
52 changes: 36 additions & 16 deletions src/update/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const run = async ({
const moduleList = input ? await readDir(path.join(input, 'openrpc'), { recursive: true, base: path.resolve('.') }) : []
const modules = await readFiles(moduleList, path.resolve('.') + '/.')

console.log(input)
console.log(path.resolve(input))
console.dir(moduleList)

Object.keys(modules).forEach(key => {
let json = JSON.parse(modules[key])

Expand Down Expand Up @@ -62,8 +66,15 @@ function update(json) {

// move params out of custom extension, and unwrap them into individual parameters
method.params.splice(0, method.params.length)
const request = getReferencedSchema(method.result.schema.$ref, json)
const params = getReferencedSchema((request.allOf ? request.allOf[1] : request).properties.parameters.$ref, json)
console.dir(method)
console.log(method.result.schema.$ref)
const request = method.result.schema.$ref ? getReferencedSchema(method.result.schema.$ref, json) : method.result.schema
console.dir(request, { depth: 10 })
console.log((request.allOf ? request.allOf[1] : request).properties.parameters)
let params = (request.allOf ? request.allOf[1] : request).properties.parameters
if (params.$ref) {
params = getReferencedSchema(params.$ref, json)
}

// add required params first, in order listed
params.required && params.required.forEach(p => {
Expand All @@ -76,28 +87,32 @@ function update(json) {
})

// add unrequired params in arbitrary order... (there's currently no provider method method with more than one unrequired param)
Object.keys(params.properties).forEach(p => {
method.params.push({
name: p,
required: false,
schema: params.properties[p]
})
delete params.properties[p]
})

if (params.type === "object" && params.properties) {
Object.keys(params.properties).forEach(p => {
method.params.push({
name: p,
required: false,
schema: params.properties[p]
})
delete params.properties[p]
})
}

// move result out of custom extension
method.result = {
name: 'result',
schema: isEvent(method)['x-response']
schema: isEvent(method)['x-response'] || { type: "null", examples: [ null ] }
}

// fix example pairings
method.examples.forEach((example, i) => {
example.params = Object.entries(example.result.value.parameters).map(entry => ({
name: entry[0],
value: entry[1]
}))
if (example.result.value.parameters) {
example.params = Object.entries(example.result.value.parameters).map(entry => ({
name: entry[0],
value: entry[1]
}))
}

const result = method.result.schema.examples ? method.result.schema.examples[Math.min(i, method.result.schema.examples.length-1)] : getReferencedSchema(method.result.schema.$ref, json).examples[0]
example.result = {
"name": "result",
Expand Down Expand Up @@ -132,6 +147,11 @@ function update(json) {

// remove the result, since this is a notification
delete method.result

method.examples.forEach(example => {
example.params.push(example.result)
delete example.result
})
}
return method
})
Expand Down
28 changes: 14 additions & 14 deletions test/openrpc/advanced.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@
{
"name": "appId",
"value": "hulu"
},
{
"name": "result",
"value": {
"foo": "bar"
}
}
],
"result": {
"name": "result",
"value": {
"foo": "bar"
}
}
]
}
]
},
Expand Down Expand Up @@ -112,14 +112,14 @@
{
"name": "state",
"value": "inactive"
},
{
"name": "result",
"value": {
"foo": "bar"
}
}
],
"result": {
"name": "result",
"value": {
"foo": "bar"
}
}
]
}
]
},
Expand Down
Loading

0 comments on commit 8810817

Please sign in to comment.