forked from directus/directus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create command to extension-sdk CLI (directus#6590)
* Add create command to extension-sdk CLI * Extract extension package.json key name to shared * Check package.json before building extensions * Add source field to package.json * Pin extension-sdk verson in scaffolded package * Change options color to magenta
- Loading branch information
Showing
19 changed files
with
277 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
templates |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import path from 'path'; | ||
import chalk from 'chalk'; | ||
import fse from 'fs-extra'; | ||
import execa from 'execa'; | ||
import ora from 'ora'; | ||
import { EXTENSION_TYPES, EXTENSION_PKG_KEY } from '@directus/shared/constants'; | ||
import { ExtensionType } from '@directus/shared/types'; | ||
|
||
const pkg = require('../../../../package.json'); | ||
|
||
const TEMPLATE_PATH = path.resolve(__dirname, '..', '..', '..', '..', 'templates'); | ||
|
||
export default async function create(type: ExtensionType, name: string): Promise<void> { | ||
const targetPath = path.resolve(name); | ||
|
||
if (!EXTENSION_TYPES.includes(type)) { | ||
console.log( | ||
`${chalk.bold.red('[Error]')} Extension type ${chalk.bold( | ||
type | ||
)} does not exist. Available extension types: ${EXTENSION_TYPES.map((t) => chalk.bold.magenta(t)).join(', ')}.` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
if (await fse.pathExists(targetPath)) { | ||
const info = await fse.stat(targetPath); | ||
|
||
if (!info.isDirectory()) { | ||
console.log( | ||
`${chalk.bold.red('[Error]')} Destination ${chalk.bold(name)} already exists and is not a directory.` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const files = await fse.readdir(targetPath); | ||
|
||
if (files.length > 0) { | ||
console.log(`${chalk.bold.red('[Error]')} Destination ${chalk.bold(name)} already exists and is not empty.`); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
const spinner = ora(`Scaffolding Directus extension...`).start(); | ||
|
||
await fse.ensureDir(targetPath); | ||
|
||
await fse.copy(path.join(TEMPLATE_PATH, 'common'), targetPath); | ||
await fse.copy(path.join(TEMPLATE_PATH, type), targetPath); | ||
|
||
const packageManifest = { | ||
name: `directus-extension-${name}`, | ||
version: '1.0.0', | ||
keywords: ['directus', 'directus-extension', `directus-custom-${type}`], | ||
[EXTENSION_PKG_KEY]: { | ||
type: type, | ||
path: 'dist/index.js', | ||
source: 'src/index.js', | ||
host: `^${pkg.version}`, | ||
hidden: false, | ||
}, | ||
scripts: { | ||
build: 'directus-extension build', | ||
}, | ||
devDependencies: { | ||
'@directus/extension-sdk': pkg.version, | ||
}, | ||
}; | ||
|
||
await fse.writeJSON(path.join(targetPath, 'package.json'), packageManifest, { spaces: '\t' }); | ||
|
||
await execa('npm', ['install'], { cwd: targetPath }); | ||
|
||
spinner.succeed('Done'); | ||
|
||
console.log(` | ||
Your ${type} extension has been created at ${chalk.green(targetPath)} | ||
Build your extension by running: | ||
${chalk.blue('cd')} ${name} | ||
${chalk.blue('npm run')} build | ||
`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<template> | ||
<div>Value: {{ value }}</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: { | ||
value: String, | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import DisplayComponent from './display.vue'; | ||
|
||
export default { | ||
id: 'custom', | ||
name: 'Custom', | ||
description: 'This is my custom display!', | ||
icon: 'box', | ||
handler: DisplayComponent, | ||
types: ['string'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function registerEndpoint(router) { | ||
router.get('/', (req, res) => res.send('Hello, World!')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const axios = require('axios'); | ||
|
||
module.exports = function registerHook() { | ||
return { | ||
'items.create': function () { | ||
axios.post('http://example.com/webhook'); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import InterfaceComponent from './interface.vue'; | ||
|
||
export default { | ||
id: 'custom', | ||
name: 'Custom', | ||
description: 'This is my custom interface!', | ||
icon: 'box', | ||
component: InterfaceComponent, | ||
types: ['string'], | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/extension-sdk/templates/interface/src/interface.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<template> | ||
<input :value="value" @input="handleChange($event.target.value)" /> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
emits: ['input'], | ||
props: { | ||
value: String, | ||
}, | ||
methods: { | ||
handleChange(value) { | ||
this.$emit('input', value); | ||
}, | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ref } from 'vue'; | ||
import LayoutComponent from './layout.vue'; | ||
|
||
export default { | ||
id: 'custom', | ||
name: 'Custom', | ||
icon: 'box', | ||
component: LayoutComponent, | ||
slots: { | ||
options: () => null, | ||
sidebar: () => null, | ||
actions: () => null, | ||
}, | ||
setup(props) { | ||
const name = ref('Custom layout state'); | ||
|
||
return { name }; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<template> | ||
<div>{{ name }} - Collection: {{ props.collection }}</div> | ||
</template> | ||
|
||
<script> | ||
import { toRefs } from 'vue'; | ||
import { useLayoutState } from '@directus/extension-sdk'; | ||
export default { | ||
setup() { | ||
const layoutState = useLayoutState(); | ||
const { props, name } = toRefs(layoutState.value); | ||
return { props, name }; | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import ModuleComponent from './module.vue'; | ||
|
||
export default { | ||
id: 'custom', | ||
name: 'Custom', | ||
icon: 'box', | ||
routes: [ | ||
{ | ||
path: '', | ||
component: ModuleComponent, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<template> | ||
<private-view title="My Custom Module">Content goes here...</private-view> | ||
</template> | ||
|
||
<script> | ||
export default {}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters