Skip to content

Commit

Permalink
fix: Added node template to repo
Browse files Browse the repository at this point in the history
Fixed #489
  • Loading branch information
svrooij committed Sep 24, 2021
1 parent 295ead3 commit 5f12b44
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .generator/node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# node-sonos template

This folder contains the template files for [node-sonos](://github.com/bencevans/node-sonos), and is not used in the actual library just yet.

## Folder content

| File | Description | Remarks |
|:-----|:------------|:--------|
| [service](./service.hbs) | Single service | Strong type service template |

## Use this template

To use this template you'll need both this generator and node-sonos checked out.

```shell
# from root of repository

# generate the intermediate file (this will take the online documentation file)
npx @svrooij/sonos-docs combine

# Make sure you don't have any changes in node-sonos, that makes it unclear what the generator changed
# generate the service files (set the correct root folder for node-sonos)
npx @svrooij/sonos-docs generate ./.generator/node/ ./
```

## Generator

See the generator documentation [here](https://github.com/svrooij/sonos-api-docs/tree/main/generator/sonos-docs).
43 changes: 43 additions & 0 deletions .generator/node/all-services.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{#each services}}
const {{serviceName}} = require('./{{#if data.filename}}{{data.filename}}{{else}}{{kebab name}}.service{{/if}}')
{{/each}}

/**
* Sonos AllServices
*
* A generated class to access all Sonos UPNP services.
*
* @remarks This file is generated, do not edit manually. https://svrooij.io/sonos-api-docs
* @exports
* @class AllServices
*/
class AllServices {
constructor (host, port) {
this.host = host
this.port = port
}

{{#each services}}
/**
* Get instance of {{name}} service
*
* @returns {{serviceName}}
*/
{{serviceName}} () {
if (!this.{{lower serviceName}}) {
this.{{lower serviceName}} = new {{serviceName}}(this.host, this.port)
}
return this.{{lower serviceName}}
}
{{#unless @last}}

{{/unless}}
{{/each}}
}

module.exports = {
AllServices,
{{#each services}}
{{serviceName}}{{#unless @last}},{{/unless}}
{{/each}}
}
66 changes: 66 additions & 0 deletions .generator/node/docs-service.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
layout: default
title: {{name}}
---
# {{name}} service
{{#if description}}

{{description}}
{{/if}}
{{#if availableAt}}

The {{name}} service is available on these models: {{#each availableAt}}`{{this}}`{{#unless @last}} / {{/unless}}{{/each}}.
{{/if}}

```js
const Sonos = require('sonos').Sonos
const sonos = new Sonos('192.168.x.x')
sonos.generatedServices.{{serviceName}}().OneOfTheMethodsBelow({...})
```

## Actions

{{#each actions}}
### {{name}}
{{#if description}}

{{description}}
{{/if}}
{{#if inputs}}

```js
const result = await sonos.generatedServices.{{../serviceName}}.{{name}}({ {{#each inputs}}{{name}}:...{{#unless @last}}, {{/unless}}{{/each}} });
```

Input object:

| property | type | description |
|:----------|:-----|:------------|
{{#each inputs}}
| **{{name}}** | `{{{relatedStateVariable.dataType}}}` | {{{description}}}{{#if relatedStateVariable.allowedValues}} Allowed values: {{#each relatedStateVariable.allowedValues}}`{{this}}`{{#unless @last}} / {{/unless}}{{/each}}{{/if}} |
{{/each}}
{{else}}

```js
const result = await sonos.generatedServices.{{../serviceName}}.{{name}}();
```
{{/if}}
{{#if outputs}}

Output object:

| property | type | description |
|:----------|:-----|:------------|
{{#each outputs}}
| **{{name}}** | `{{{relatedStateVariable.dataType}}}` | {{{description}}}{{#if relatedStateVariable.allowedValues}} Possible values: {{#each relatedStateVariable.allowedValues}}`{{this}}`{{#unless @last}} / {{/unless}}{{/each}}{{/if}} |
{{/each}}
{{else}}

This actions returns a boolean whether or not the requests succeeded.
{{/if}}
{{#if remarks}}

**Remarks** {{{remarks}}}
{{/if}}

{{/each}}
56 changes: 56 additions & 0 deletions .generator/node/service.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const Service = require('./Service')
/**
* Sonos {{serviceName}}
{{#if description}}
*
* {{description}}
{{/if}}
*
* @author Stephan van Rooij - https://svrooij.io
* @remarks This file is generated, do not edit manually. https://svrooij.io/sonos-api-docs
* @export
* @class {{serviceName}}
* @extends {Service}
*/
class {{serviceName}} extends Service {
constructor (host, port) {
super()
this.name = '{{name}}'
this.host = host
this.port = port || 1400
this.controlURL = '{{controlURL}}'
this.eventSubURL = '{{eventSubURL}}'
this.SCPDURL = '{{discoveryUri}}'
}

// #region actions
{{#each actions}}
/**
* {{name}}{{#if description}} - {{{description}}}{{/if}}
{{#if inputs}}
*
* @param {Object} [options] - An object with the following properties
{{/if}}
{{#each inputs}}
* @param { {{~ relatedStateVariable.dataType ~}} } options.{{name}}{{#if description}} - {{{description}}}{{/if}}{{#if relatedStateVariable.allowedValues}} [ {{#each relatedStateVariable.allowedValues}}'{{this}}'{{#unless @last}} / {{/unless}}{{/each}} ]{{/if}}
{{/each}}
{{#if remarks}}
* @remarks {{{remarks}}}
{{/if}}
{{#if outputs}}
* @returns {Object} response object, with these properties {{#each outputs}}'{{name}}'{{#unless @last}}, {{/unless}}{{/each}}
{{/if}}
*/
{{#if inputs}}
async {{name}} (options{{{sonos_if_only_instance_id inputs ' = { InstanceID: 0 }'}}}) { return this._request('{{name}}', options) }
{{else}}
async {{name}} () { return this._request('{{name}}') }
{{/if}}
{{#unless @last}}

{{/unless}}
{{/each}}
// #endregion
}

module.exports = {{serviceName}}
44 changes: 44 additions & 0 deletions .generator/node/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"$schema": "https://svrooij.io/sonos-api-docs/schema/template.json",
"name": "Node sonos",
"slug": "node",
"repository": "https://github.com/bencevans/node-sonos/",
"author": {
"name": "svrooij",
"link": "https://svrooij.io"
},
"files": [
{
"file": "service.hbs",
"outputFile": "lib/services/{snService}.service.js",
"usage":"service"
},
{
"file": "all-services.hbs",
"outputFile": "lib/services/all-services.js",
"usage":"index"
},
{
"file": "docs-service.hbs",
"outputFile": "docs/services/{snService}-service.md",
"usage":"service"
}
],
"dataTypes": {
"ui4": "number",
"i4": "number",
"ui2": "number",
"i2": "number",
"MetaData": "string",
"ZoneGroupState": "string"
},
"serviceData": {
"AVTransportService": {
"filename": "av-transport.service"
},
"HTControlService": {
"filename": "ht-control.service"
}
},
"license": "MIT"
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ config/*local.json

# Ignore Mac DS_Store files
.DS_Store

.cache
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"lint": "standard",
"test": "npm run lint && mocha test/sonos.test.js --exit --timeout 20000",
"env-run": "npm run $CMD",
"docs": "jsdox -t ./docs/generator/templates --output docs lib/ && jsdox -t ./docs/generator/templates --output docs/services lib/services && jsdox -t ./docs/generator/templates --output docs/events lib/events"
"docs": "jsdox -t ./docs/generator/templates --output docs lib/ && jsdox -t ./docs/generator/templates --output docs/services lib/services && jsdox -t ./docs/generator/templates --output docs/events lib/events",
"generator": "npx @svrooij/sonos-docs combine && npx @svrooij/sonos-docs generate ./.generator/node/ ./ && npx standard --fix"

},
"repository": {
"type": "git",
Expand Down

0 comments on commit 5f12b44

Please sign in to comment.