Skip to content

Commit fe072a2

Browse files
authored
chore: Add a Pinion generator to initialise a new package (#3361)
1 parent 9e0aadc commit fe072a2

14 files changed

Lines changed: 395 additions & 88 deletions

File tree

deno/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

generators/package.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { Callable, PinionContext } from '@feathershq/pinion'
2+
import { generator, install, prompt, runGenerators, toFile } from '@feathershq/pinion'
3+
4+
export interface ModuleContext extends PinionContext {
5+
name: string
6+
uppername: string
7+
description: string
8+
moduleName: string
9+
packagePath: Callable<string, ModuleContext>
10+
}
11+
12+
export const generate = (context: ModuleContext) =>
13+
generator(context)
14+
.then(
15+
prompt<ModuleContext>([
16+
{
17+
type: 'input',
18+
name: 'name',
19+
message: 'What is the name of the module?'
20+
},
21+
{
22+
type: 'input',
23+
name: 'description',
24+
message: 'Write a short description'
25+
}
26+
])
27+
)
28+
.then((ctx) => {
29+
return {
30+
...ctx,
31+
moduleName: `@feathersjs/${ctx.name}`,
32+
uppername: ctx.name.charAt(0).toUpperCase() + ctx.name.slice(1),
33+
packagePath: toFile('packages', ctx.name)
34+
}
35+
})
36+
.then(runGenerators(__dirname, 'package'))
37+
.then(
38+
install<ModuleContext>(
39+
['@types/node', 'shx', 'ts-node', 'typescript', 'mocha'],
40+
true,
41+
(context) => `npm --workspace packages/${context.name}`
42+
)
43+
)

generators/package/index.tpl.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { generator, renderTemplate, toFile } from '@feathershq/pinion'
2+
import { ModuleContext } from '../package'
3+
4+
interface Context extends ModuleContext {}
5+
6+
const template = ({ name }: Context) => `
7+
export function ${name}() {
8+
return 'Hello from ${name}'
9+
}
10+
`
11+
12+
export const generate = (context: Context) =>
13+
generator(context).then(renderTemplate(template, toFile(context.packagePath, 'src', 'index.ts')))

generators/package/license.tpl.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { generator, renderTemplate, toFile } from '@feathershq/pinion'
2+
import { ModuleContext } from '../package'
3+
4+
interface Context extends ModuleContext {}
5+
6+
export const generate = (context: Context) =>
7+
generator(context).then(
8+
renderTemplate(
9+
`The MIT License (MIT)
10+
11+
Copyright (c) ${new Date().getFullYear()} Feathers Contributors
12+
13+
Permission is hereby granted, free of charge, to any person obtaining a copy
14+
of this software and associated documentation files (the "Software"), to deal
15+
in the Software without restriction, including without limitation the rights
16+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
copies of the Software, and to permit persons to whom the Software is
18+
furnished to do so, subject to the following conditions:
19+
20+
The above copyright notice and this permission notice shall be included in all
21+
copies or substantial portions of the Software.
22+
23+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
SOFTWARE.
30+
`,
31+
toFile<Context>(context.packagePath, 'LICENSE')
32+
)
33+
)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { generator, toFile, writeJSON } from '@feathershq/pinion'
2+
import { ModuleContext } from '../package'
3+
4+
interface Context extends ModuleContext {}
5+
6+
export const generate = (context: Context) =>
7+
generator(context).then(
8+
writeJSON<Context>(
9+
({ moduleName, description, name }) => ({
10+
name: moduleName,
11+
description,
12+
version: '0.0.0',
13+
homepage: 'https://feathersjs.com',
14+
keywords: ['feathers'],
15+
license: 'MIT',
16+
repository: {
17+
type: 'git',
18+
url: 'git://github.com/feathersjs/feathers.git',
19+
directory: `packages/${name}`
20+
},
21+
author: {
22+
name: 'Feathers contributor',
23+
email: 'hello@feathersjs.com',
24+
url: 'https://feathersjs.com'
25+
},
26+
contributors: [],
27+
bugs: {
28+
url: 'https://github.com/feathersjs/feathers/issues'
29+
},
30+
engines: {
31+
node: '>= 20'
32+
},
33+
files: ['CHANGELOG.md', 'LICENSE', 'README.md', 'src/**', 'lib/**', 'esm/**'],
34+
// module: './esm/index.js',
35+
main: './lib/index.js',
36+
types: './src/index.ts',
37+
exports: {
38+
'.': {
39+
// import: './esm/index.js',
40+
require: './lib/index.js',
41+
types: './src/index.ts'
42+
}
43+
},
44+
scripts: {
45+
prepublish: 'npm run compile',
46+
pack: 'npm pack --pack-destination ../generators/test/build',
47+
compile: 'shx rm -rf lib/ && tsc && npm run pack',
48+
test: 'mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts'
49+
},
50+
publishConfig: {
51+
access: 'public'
52+
},
53+
dependencies: {},
54+
devDependencies: {}
55+
}),
56+
toFile('packages', context.name, 'package.json')
57+
)
58+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { generator, renderTemplate, toFile } from '@feathershq/pinion'
2+
import { ModuleContext } from '../package'
3+
4+
const template = ({ description, moduleName }: ModuleContext) => `# ${moduleName}
5+
6+
[![CI](https://github.com/feathersjs/feathers/workflows/CI/badge.svg)](https://github.com/feathersjs/feathers/actions?query=workflow%3ACI)
7+
[![Download Status](https://img.shields.io/npm/dm/${moduleName}.svg?style=flat-square)](https://www.npmjs.com/package/${moduleName})
8+
[![Discord](https://badgen.net/badge/icon/discord?icon=discord&label)](https://discord.gg/qa8kez8QBx)
9+
10+
> ${description}
11+
12+
## Installation
13+
14+
\`\`\`
15+
npm install ${moduleName} --save
16+
\`\`\`
17+
18+
## Documentation
19+
20+
Refer to the [Feathers API documentation](https://feathersjs.com/api) for more details.
21+
22+
## License
23+
24+
Copyright (c) ${new Date().getFullYear()} [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
25+
26+
Licensed under the [MIT license](LICENSE).
27+
`
28+
29+
export const generate = (context: ModuleContext) =>
30+
generator(context).then(renderTemplate(template, toFile(context.packagePath, 'README.md')))

generators/package/test.tpl.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { generator, renderTemplate, toFile } from '@feathershq/pinion'
2+
import { ModuleContext } from '../package'
3+
4+
interface Context extends ModuleContext {}
5+
6+
const template = ({ moduleName, name }: Context) => /** ts */ `import { strict as assert } from 'assert'
7+
import { ${name} } from '../src/index'
8+
9+
describe('${moduleName}', () => {
10+
it('initializes', () => {
11+
assert.equal(${name}(), 'Hello from ${name}')
12+
})
13+
})
14+
`
15+
16+
export const generate = (context: Context) =>
17+
generator(context).then(
18+
renderTemplate(template, toFile<Context>(context.packagePath, 'test', 'index.test.ts'))
19+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { generator, toFile, writeJSON } from '@feathershq/pinion'
2+
import { ModuleContext } from '../package'
3+
4+
export const generate = (context: ModuleContext) =>
5+
generator(context).then(
6+
writeJSON(
7+
{
8+
extends: '../../tsconfig',
9+
include: ['src/**/*.ts'],
10+
compilerOptions: {
11+
outDir: 'lib'
12+
}
13+
},
14+
toFile(context.packagePath, 'tsconfig.json')
15+
)
16+
)

0 commit comments

Comments
 (0)