Skip to content

Commit

Permalink
build: add nx plugin for generating new packages
Browse files Browse the repository at this point in the history
It looks like the plugin needs to be in CommonJS as all `nx` plugins
need to be in CommonJS: https://www.github.com/nrwl/nx/issues/15682

Useful docs: https://nx.dev/extending-nx/recipes/local-generators

Related to #83
  • Loading branch information
JoosepAlviste committed Dec 31, 2023
1 parent a4e4d5c commit 9232b36
Show file tree
Hide file tree
Showing 19 changed files with 1,770 additions and 480 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
"build": "nx affected -t build",
"migrate": "pnpm -F api migrate",
"migrate:test": "dotenv -e .env.test -- pnpm -F api migrate:test",
"test:e2e": "dotenv -e .env.e2e -- nx test:e2e @serieslist/e2e"
"test:e2e": "dotenv -e .env.e2e -- nx test:e2e @serieslist/e2e",
"generate:package": "nx generate @serieslist/nx-plugin:package"
},
"devDependencies": {
"@nx/devkit": "17.2.8",
"@nx/plugin": "^17.2.8",
"dotenv-cli": "^7.1.0",
"nx": "17.2.8"
}
}

4 changes: 4 additions & 0 deletions packages/nx-plugin/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@serieslist/eslint-config-base",
"root": true
}
1 change: 1 addition & 0 deletions packages/nx-plugin/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@serieslist/prettier-config"
5 changes: 5 additions & 0 deletions packages/nx-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @serieslist/nx-plugin

A plugin for `nx` defining some useful generators.

Useful docs: https://nx.dev/extending-nx/recipes/local-generators
9 changes: 9 additions & 0 deletions packages/nx-plugin/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"generators": {
"package": {
"factory": "./src/generators/package/generator",
"schema": "./src/generators/package/schema.json",
"description": "package generator"
}
}
}
18 changes: 18 additions & 0 deletions packages/nx-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@serieslist/nx-plugin",
"version": "0.0.1",
"type": "commonjs",
"main": "./src/index.ts",
"typings": "./src/index.ts",
"generators": "./generators.json",
"dependencies": {
"@nx/devkit": "17.2.8",
"tslib": "^2.3.0"
},
"devDependencies": {
"@serieslist/eslint-config-base": "workspace:*",
"@serieslist/prettier-config": "workspace:*",
"@serieslist/typescript-config-base": "workspace:*",
"typescript": "^4.9.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@serieslist/eslint-config-base",
"root": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@serieslist/prettier-config"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@serieslist/<?= name %>",
"version": "1.0.0",
"type": "module",
"exports": {
".": {
"import": "./src/index.ts",
"types": "./src/index.ts"
}
},
"scripts": {
"lint": "eslint src",
"tsc": "tsc --noEmit"
},
"devDependencies": {
"@serieslist/eslint-config-base": "workspace:*",
"@serieslist/prettier-config": "workspace:*",
"@serieslist/typescript-config-base": "workspace:*",
"typescript": "^4.9.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const variable = "<%= name %>";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@serieslist/typescript-config-base"
}
16 changes: 16 additions & 0 deletions packages/nx-plugin/src/generators/package/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { join } from 'path'

import { formatFiles, generateFiles, type Tree } from '@nx/devkit'

import { type PackageGeneratorSchema } from './schema'

export async function packageGenerator(
tree: Tree,
options: PackageGeneratorSchema,
) {
const projectRoot = `packages/${options.name}`
generateFiles(tree, join(__dirname, 'files'), projectRoot, options)
await formatFiles(tree)
}

export default packageGenerator
3 changes: 3 additions & 0 deletions packages/nx-plugin/src/generators/package/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type PackageGeneratorSchema = {
name: string
}
18 changes: 18 additions & 0 deletions packages/nx-plugin/src/generators/package/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "Package",
"title": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use?"
}
},
"required": ["name"]
}
Empty file added packages/nx-plugin/src/index.ts
Empty file.
6 changes: 6 additions & 0 deletions packages/nx-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@serieslist/typescript-config-base",
"compilerOptions": {
"module": "commonjs"
}
}
9 changes: 9 additions & 0 deletions packages/nx-plugin/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"]
}
Loading

0 comments on commit 9232b36

Please sign in to comment.