Skip to content

Commit

Permalink
chore: Add a Pinion generator to initialise a new package
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Dec 1, 2023
1 parent 9e0aadc commit 9e69a48
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 88 deletions.
3 changes: 0 additions & 3 deletions deno/README.md

This file was deleted.

43 changes: 43 additions & 0 deletions generators/package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { Callable, PinionContext } from '@feathershq/pinion'
import { generator, install, prompt, runGenerators, toFile } from '@feathershq/pinion'

export interface ModuleContext extends PinionContext {
name: string
uppername: string
description: string
moduleName: string
packagePath: Callable<string, ModuleContext>
}

export const generate = (context: ModuleContext) =>
generator(context)
.then(
prompt<ModuleContext>([
{
type: 'input',
name: 'name',
message: 'What is the name of the module?'
},
{
type: 'input',
name: 'description',
message: 'Write a short description'
}
])
)
.then((ctx) => {
return {
...ctx,
moduleName: `@feathersjs/${ctx.name}`,
uppername: ctx.name.charAt(0).toUpperCase() + ctx.name.slice(1),
packagePath: toFile('packages', ctx.name)
}
})
.then(runGenerators(__dirname, 'package'))
.then(
install<ModuleContext>(
['@types/node', 'shx', 'ts-node', 'typescript', 'mocha'],
true,
(context) => `npm --workspace packages/${context.name}`
)
)
13 changes: 13 additions & 0 deletions generators/package/index.tpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { generator, renderTemplate, toFile } from '@feathershq/pinion'
import { ModuleContext } from '../package'

interface Context extends ModuleContext {}

const template = ({ name }: Context) => `
export function ${name}() {
return 'Hello from ${name}'
}
`

export const generate = (context: Context) =>
generator(context).then(renderTemplate(template, toFile(context.packagePath, 'src', 'index.ts')))
33 changes: 33 additions & 0 deletions generators/package/license.tpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { generator, renderTemplate, toFile } from '@feathershq/pinion'
import { ModuleContext } from '../package'

interface Context extends ModuleContext {}

export const generate = (context: Context) =>
generator(context).then(
renderTemplate(
`The MIT License (MIT)
Copyright (c) ${new Date().getFullYear()} Feathers Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`,
toFile<Context>(context.packagePath, 'LICENSE')
)
)
58 changes: 58 additions & 0 deletions generators/package/package.json.tpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { generator, toFile, writeJSON } from '@feathershq/pinion'
import { ModuleContext } from '../package'

interface Context extends ModuleContext {}

export const generate = (context: Context) =>
generator(context).then(
writeJSON<Context>(
({ moduleName, description, name }) => ({
name: moduleName,
description,
version: '0.0.0',
homepage: 'https://feathersjs.com',
keywords: ['feathers'],
license: 'MIT',
repository: {
type: 'git',
url: 'git://github.com/feathersjs/feathers.git',
directory: `packages/${name}`
},
author: {
name: 'Feathers contributor',
email: '[email protected]',
url: 'https://feathersjs.com'
},
contributors: [],
bugs: {
url: 'https://github.com/feathersjs/feathers/issues'
},
engines: {
node: '>= 20'
},
files: ['CHANGELOG.md', 'LICENSE', 'README.md', 'src/**', 'lib/**', 'esm/**'],
// module: './esm/index.js',
main: './lib/index.js',
types: './src/index.ts',
exports: {
'.': {
// import: './esm/index.js',
require: './lib/index.js',
types: './src/index.ts'
}
},
scripts: {
prepublish: 'npm run compile',
pack: 'npm pack --pack-destination ../generators/test/build',
compile: 'shx rm -rf lib/ && tsc && npm run pack',
test: 'mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts'
},
publishConfig: {
access: 'public'
},
dependencies: {},
devDependencies: {}
}),
toFile('packages', context.name, 'package.json')
)
)
30 changes: 30 additions & 0 deletions generators/package/readme.md.tpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { generator, renderTemplate, toFile } from '@feathershq/pinion'
import { ModuleContext } from '../package'

const template = ({ description, moduleName }: ModuleContext) => `# ${moduleName}
[![CI](https://github.com/feathersjs/feathers/workflows/CI/badge.svg)](https://github.com/feathersjs/feathers/actions?query=workflow%3ACI)
[![Download Status](https://img.shields.io/npm/dm/${moduleName}.svg?style=flat-square)](https://www.npmjs.com/package/${moduleName})
[![Discord](https://badgen.net/badge/icon/discord?icon=discord&label)](https://discord.gg/qa8kez8QBx)
> ${description}
## Installation
\`\`\`
npm install ${moduleName} --save
\`\`\`
## Documentation
Refer to the [Feathers API documentation](https://feathersjs.com/api) for more details.
## License
Copyright (c) ${new Date().getFullYear()} [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
Licensed under the [MIT license](LICENSE).
`

export const generate = (context: ModuleContext) =>
generator(context).then(renderTemplate(template, toFile(context.packagePath, 'README.md')))
19 changes: 19 additions & 0 deletions generators/package/test.tpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { generator, renderTemplate, toFile } from '@feathershq/pinion'
import { ModuleContext } from '../package'

interface Context extends ModuleContext {}

const template = ({ moduleName, name }: Context) => /** ts */ `import { strict as assert } from 'assert'
import { ${name} } from '../src/index'
describe('${moduleName}', () => {
it('initializes', () => {
assert.equal(${name}(), 'Hello from ${name}')
})
})
`

export const generate = (context: Context) =>
generator(context).then(
renderTemplate(template, toFile<Context>(context.packagePath, 'test', 'index.test.ts'))
)
16 changes: 16 additions & 0 deletions generators/package/tsconfig.json.tpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { generator, toFile, writeJSON } from '@feathershq/pinion'
import { ModuleContext } from '../package'

export const generate = (context: ModuleContext) =>
generator(context).then(
writeJSON(
{
extends: '../../tsconfig',
include: ['src/**/*.ts'],
compilerOptions: {
outDir: 'lib'
}
},
toFile(context.packagePath, 'tsconfig.json')
)
)
Loading

0 comments on commit 9e69a48

Please sign in to comment.