cockpit-cli v0.1.2
Cockpit CLI
A CLI for Cockpit Headless CMS that uses Cockpit SDK
Parses data structures from a project outputing meta-data that can be used directly on Cockpit.
Gets data from Cockpit collection schemas and components groups to make it easier to manipulate or generate.
Install
cd my-app
npm install cockpit-cliUsage
cockpit-cli [options]
#For example (--verbose || -v)
cockpit-cli --init, --menu, --components, --helpConfiguration
$ cockpit-cli --init
When setting up a project run --init so that the CLI can set up a /cockpit folder with a config.js file to connect the project to cockpit using cockpit-sdk .
module.exports = {
host: "<HOST>",
accessToken: "<ACCESS_TOKEN>"
};To edit the file at ./cockpit/config.js:
Go into Cockpit and access a
collectionedit menu where the tab with the descriptionOthershould be selected.Enable the
CONTENT PREVIEWoption. This is where thehostis (or should be).Copy the contents of those fields to the
config.jsfile.The
accessTokencan be retrieved on Cockpit settings page atAPI access. Choose betweenFull access API-keyor aCustom keybut please bear in mind that aCustom Keypermission level can be restricted in comparison to aFull access API-key.
It's possible to edit theconfig.jsfile at anytime but if in doubt about which one to choose, please checkout Cockpit API documentation.
Structure example
└── src
├── components
│ ├── footer
│ │ └── definition.js
│ │ └── index.js
| |
│ └── heading
│ └── definition.js
│ └── index.js
|
└── layouts
└── bar
└── definition.js
└── index.js
# Or from the root
├── components
| └── paragraph
| └── definition.js
| └── index.js
|
└── layouts
└── bar
└── definition.js
└── index.js
folders: ['components', 'layouts', 'materials', 'elements', 'meta', 'pages', 'scripts']Using components as example
definition.js files should contain the structure for Cockpit components like field names, types and options. Checkout the Cockpit fieldtypes documentation.
A paragraph component could have a definition.js file as described:
module.exports = {
group: "test",
fields: [
{
name: "color",
type: "collectionlink",
options: {
link: "Colors"
}
},
{
name: "text",
type: "textarea",
default: ""
},
{
name: "container",
type: "select",
options: {
options: ["medium", "small", "spaceless", "left-aligned"]
},
default: ""
},
{
name: "lead",
type: "boolean",
default: false
}
]
};The output result from parsing this component with the CLI is this:
"paragraph": {
"group": "test",
"fields": [
{
"name": "color",
"type": "collectionlink",
"options": {
"link": "Colors"
}
},
{
"name": "text",
"type": "textarea",
"default": ""
},
{
"name": "container",
"type": "select",
"options": {
"options": [
"medium",
"small",
"spaceless",
"left-aligned"
]
},
"default": ""
},
{
"name": "lead",
"type": "boolean",
"default": false
}
]
}The CLI parses the content of the definition.js file to .json as showed above, and it can separate them in groups by detecting if there is a value assigned to the key group in the component/definition.js file.
If no groups are specified then all groups are selected and this means all the definitions.js files are going to be parsed and it is possible to generate a cockpit/components.json file with all the parsed data or copy that data to the clipboard.
Component groups & Collection schemas
$ cockpit-cli --menu
Launches the CLI menu where it's possible to:
💾 Save schemas :

💾 All collectionscreates a folder at/cockpit/schemasthat contains data from all the schemas in all the collections in parsed to.jsonformat .💾 Select a collectioncreates a folder at/cockpit/schemasthat contains data from the schema of the selected collection.The
Saveoption always overwrites previous folders or files created by the CLI.
📄 Get components fields :

✂️ Allparses all thedefinition.jsfiles and copies the parsed result to the clipboard.✂️ Groupslaunches a new menu where it is possible to select one of thegroupsthat are defined in the project data structuredefinition.jsand copies the data from the component or components that correspond to the selectedgroupto clipboard.💾 Save Filecreates a file at/cockpit/components.jsonwith all the parsed data from all thedefinition.jsfiles.The
Saveoption always overwrites previous folders or files created by the CLI.
Cockpit has a core components structure, this means that when adding a new component there will always be a group of components already pre-defined to which it is possible to add your own.
The CLI outputs a data structure ready to be added to cockpit components.js file.
Cockpit components.js
- Go to Cockpit
- Access finder
- Select the
config folderand inside it thecomponents.jsfile
The parsed structure by the CLI can be pasted here, for example, adding the data of a specific component group or overwriting the complete file by pasting the data from all component groups.
The parsed data should be pasted in components.js as an object:
window.CP_LAYOUT_COMPONENTS = {};Only the contents of this object should be edited, like this:
window.CP_LAYOUT_COMPONENTS = {
"paragraph": {
"group": "test",
"fields": [
{
"name": "color",
"type": "collectionlink",
"options": {
"link": "Colors"
}
},
{
"name": "text",
"type": "textarea",
"default": ""
},
{
"name": "container",
"type": "select",
"options": {
"options": ["medium", "small", "spaceless", "left-aligned"]
},
"default": ""
},
{
"name": "lead",
"type": "boolean",
"default": false
}
]
}
}After the file is saved and when trying to add a new component to a cockpit collection these components that were parsed by the CLI should now appear as an option inside their specified groups.
config/components.js does not exist yet
Do not worry, just creat one:
- Go to Cockpit
- Access finder
- Select this icon

- Choose
folderand name itconfig - Go inside
config folderand click the same icon - Choose
fileand name itcomponents.js - Paste this inside the file:
window.CP_LAYOUT_COMPONENTS = {};$ cockpit-cli --components
This simply acts as a shortcut to create the /cockpit/components.json file and to copy the parsed data to the clipboard.