Skip to content

Commit

Permalink
Add plugin generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanblock committed Sep 18, 2023
1 parent 0abf668 commit 4fa319c
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 2 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"bugs": "https://github.com/architect/aws-lite/issues",
"scripts": {
"generate-plugins": "node scripts/generate-plugins",
"lint": "eslint --fix .",
"test": "npm run lint && npm run coverage",
"test:integration": "cross-env tape 'test/integration/**/*-test.js' | tap-spec",
Expand Down Expand Up @@ -39,6 +40,7 @@
"files": [
"src"
],
"workspaces": [],
"eslintConfig": {
"extends": "@architect/eslint-config"
},
Expand All @@ -47,6 +49,10 @@
"branches": 100,
"lines": 100,
"functions": 100,
"statements": 100
"statements": 100,
"exclude": [
"plugins/",
"test/"
]
}
}
}
20 changes: 20 additions & 0 deletions scripts/generate-plugins/_package-tmpl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "<filled in by the plugin generator>",
"version": "0.0.0",
"description": "<filled in by the plugin generator>",
"homepage": "https://github.com/architect/aws-lite",
"repository": {
"type": "git",
"url": "https://github.com/architect/aws-lite"
},
"bugs": "https://github.com/architect/aws-lite/issues",
"main": "src/index.js",
"engines": {
"node": ">=16"
},
"author": "@YOUR_GITHUB",
"license": "Apache-2.0",
"files": [
"src"
]
}
43 changes: 43 additions & 0 deletions scripts/generate-plugins/_plugin-tmpl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const service = '$NAME'
const required = true

/**
* Plugin maintained by: @YOUR_GITHUB
*/
module.exports = {
service,
methods: {
// TODO: include a reference link with each method, example:
// https://docs.aws.amazon.com/lambda/latest/dg/API_GetFunctionConfiguration.html
$ReplaceMe: {
validate: {
name: { type: 'string', required },
},
request: async ({ name }) => {
return {
endpoint: `/$API_VER/${name}/etc`
}
},
error: async (error) => {
if (error.statusCode === 400 &&
error?.error?.message?.match(/validation error/)) {
error.metadata.type = 'Validation error'
}
return error
}
},

// TODO: add API link
$ReplaceMeToo: {
validate: {
name: { type: 'string', required },
},
request: async ({ name }) => {
return {
endpoint: `/$API_VER/${name}/etc`
}
},
// error: async (error) => error
},
}
}
15 changes: 15 additions & 0 deletions scripts/generate-plugins/_readme-tmpl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# `$NAME`

> $DESC

## Install

```sh
npm i $NAME
```


## Learn more

Please see the [main `aws-lite` readme](https://github.com/architect/aws-lite) for more information about `aws-lite` plugins.
49 changes: 49 additions & 0 deletions scripts/generate-plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#! /usr/bin/env node
let { join } = require('path')
let { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
const cwd = process.cwd()

// Break this into a separate file if it becomes too big / unwieldy!
const plugins = [
{ name: 'dynamodb', service: 'DynamoDB' },
]
const pluginTmpl = readFileSync(join(__dirname, '_plugin-tmpl.js')).toString()
const readmeTmpl = readFileSync(join(__dirname, '_readme-tmpl.md')).toString()
const packageTmpl = readFileSync(join(__dirname, '_package-tmpl.json'))

plugins.forEach(plugin => {
let pluginDir = join(cwd, 'plugins', plugin.name)
if (!existsSync(pluginDir)) {
let pluginSrc = join(pluginDir, 'src')
mkdirSync(pluginSrc, { recursive: true })

let name = `@aws-lite/${plugin.name}`
let desc = `Official \`aws-lite\` plugin for ${plugin.service}`

// src/index.js
let src = pluginTmpl
.replace(/\$NAME/g, plugin.name)
writeFileSync(join(pluginSrc, 'index.js'), src)

// package.json
let pkg = JSON.parse(packageTmpl)
pkg.name = name
pkg.description = desc
writeFileSync(join(pluginDir, 'package.json'), JSON.stringify(pkg, null, 2))

// readme.md
let readme = readmeTmpl
.replace(/\$NAME/g, name)
.replace(/\$DESC/g, desc)
writeFileSync(join(pluginDir, 'readme.md'), readme)

let projectPkgFile = join(cwd, 'package.json')
let projectPkg = JSON.parse(readFileSync(projectPkgFile))
let workspace = `plugins/${plugin.name}`
if (!projectPkg.workspaces.includes(workspace)) {
projectPkg.workspaces.push(workspace)
projectPkg.workspaces = projectPkg.workspaces.sort()
writeFileSync(projectPkgFile, JSON.stringify(projectPkg, null, 2))
}
}
})

0 comments on commit 4fa319c

Please sign in to comment.