-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin): add plugin for generators (#26)
closes #4 This PR adds: - `plugin` for the main `ngxtension` plugin that provides generators such as `ng add` (at the moment) - `local-plugin` for various tasks in the repo itself. Right now, it has `convert-entry-point-to-project` generator
- Loading branch information
Showing
47 changed files
with
1,700 additions
and
245 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
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 @@ | ||
{ | ||
"eslint.validate": ["json"] | ||
} |
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,32 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": { | ||
"@nx/dependency-checks": "error" | ||
} | ||
}, | ||
{ | ||
"files": ["./package.json", "./generators.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": { | ||
"@nx/nx-plugin-checks": "error" | ||
} | ||
} | ||
] | ||
} |
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 @@ | ||
# local-plugin | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Building | ||
|
||
Run `nx build local-plugin` to build the library. | ||
|
||
## Running unit tests | ||
|
||
Run `nx test local-plugin` to execute the unit tests via [Jest](https://jestjs.io). |
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 @@ | ||
{ | ||
"generators": { | ||
"convert-entry-point-to-project": { | ||
"factory": "./src/generators/convert-entry-point-to-project/generator", | ||
"schema": "./src/generators/convert-entry-point-to-project/schema.json", | ||
"description": "convert-entry-point-to-project generator" | ||
} | ||
} | ||
} |
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 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'local-plugin', | ||
preset: '../../jest.preset.js', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../coverage/libs/local-plugin', | ||
}; |
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,12 @@ | ||
{ | ||
"name": "local-plugin", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"@nx/devkit": "16.8.1", | ||
"tslib": "^2.3.0" | ||
}, | ||
"type": "commonjs", | ||
"main": "./src/index.js", | ||
"typings": "./src/index.d.ts", | ||
"generators": "./generators.json" | ||
} |
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,66 @@ | ||
{ | ||
"name": "local-plugin", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/local-plugin/src", | ||
"projectType": "library", | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/js:tsc", | ||
"outputs": ["{options.outputPath}"], | ||
"options": { | ||
"outputPath": "dist/libs/local-plugin", | ||
"main": "libs/local-plugin/src/index.ts", | ||
"tsConfig": "libs/local-plugin/tsconfig.lib.json", | ||
"assets": [ | ||
"libs/local-plugin/*.md", | ||
{ | ||
"input": "./libs/local-plugin/src", | ||
"glob": "**/!(*.ts)", | ||
"output": "./src" | ||
}, | ||
{ | ||
"input": "./libs/local-plugin/src", | ||
"glob": "**/*.d.ts", | ||
"output": "./src" | ||
}, | ||
{ | ||
"input": "./libs/local-plugin", | ||
"glob": "generators.json", | ||
"output": "." | ||
}, | ||
{ | ||
"input": "./libs/local-plugin", | ||
"glob": "executors.json", | ||
"output": "." | ||
} | ||
] | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": [ | ||
"libs/local-plugin/**/*.ts", | ||
"libs/local-plugin/package.json", | ||
"libs/local-plugin/generators.json" | ||
] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/local-plugin/jest.config.ts", | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
73 changes: 73 additions & 0 deletions
73
libs/local-plugin/src/generators/convert-entry-point-to-project/generator.spec.ts
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,73 @@ | ||
import { | ||
UnitTestRunner, | ||
libraryGenerator, | ||
librarySecondaryEntryPointGenerator, | ||
} from '@nx/angular/generators'; | ||
import { Tree, readProjectConfiguration } from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import initGenerator from './generator'; | ||
|
||
const project = 'main-entry'; | ||
const entryPoint = 'entry-point'; | ||
|
||
describe('convert-entry-point-to-project generator', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
async function setup(isReady = false) { | ||
tree = createTreeWithEmptyWorkspace(); | ||
|
||
await libraryGenerator(tree, { | ||
name: project, | ||
publishable: true, | ||
importPath: project, | ||
routing: false, | ||
spec: false, | ||
skipTests: true, | ||
skipModule: true, | ||
unitTestRunner: UnitTestRunner.None, | ||
}); | ||
|
||
if (isReady) { | ||
await librarySecondaryEntryPointGenerator(tree, { | ||
name: entryPoint, | ||
library: project, | ||
skipModule: true, | ||
}); | ||
} | ||
} | ||
|
||
// | ||
it('should run successfully', async () => { | ||
expect(true).toBeTruthy(); | ||
}); | ||
|
||
it('should fail fast for name == "src"', async () => { | ||
await setup(); | ||
await initGenerator(tree, { name: 'src', project }); | ||
expect(nothingHappened(tree)).toEqual(true); | ||
}); | ||
|
||
it('should throw for a non project', async () => { | ||
await setup(); | ||
await expect( | ||
initGenerator(tree, { name: entryPoint, project: 'any' }) | ||
).rejects.toThrowError(/cannot find configuration for 'any'/i); | ||
}); | ||
|
||
it('should work properly', async () => { | ||
await setup(true); | ||
await initGenerator(tree, { name: entryPoint, project }); | ||
expect(nothingHappened(tree)).toEqual(false); | ||
}); | ||
}); | ||
|
||
function nothingHappened(tree: Tree) { | ||
const projectConfiguration = readProjectConfiguration(tree, project); | ||
return !tree.exists( | ||
projectConfiguration.root + '/' + entryPoint + '/project.json' | ||
); | ||
} |
100 changes: 100 additions & 0 deletions
100
libs/local-plugin/src/generators/convert-entry-point-to-project/generator.ts
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,100 @@ | ||
import { | ||
Tree, | ||
addProjectConfiguration, | ||
formatFiles, | ||
getWorkspaceLayout, | ||
logger, | ||
readJson, | ||
readProjectConfiguration, | ||
updateJson, | ||
} from '@nx/devkit'; | ||
import type { ConvertEntryPointToProjectGeneratorSchema } from './schema'; | ||
|
||
export async function convertEntryPointToProjectGenerator( | ||
tree: Tree, | ||
options: ConvertEntryPointToProjectGeneratorSchema | ||
) { | ||
const { name, project } = options; | ||
if (name === 'src') { | ||
logger.warn(`[local-plugin] entry point "src" is invalid`); | ||
return; | ||
} | ||
|
||
const projectConfiguration = readProjectConfiguration(tree, project); | ||
|
||
const projectPackageJson = readJson( | ||
tree, | ||
projectConfiguration.root + '/package.json' | ||
); | ||
|
||
if (!projectPackageJson.name) { | ||
logger.error( | ||
`[local-plugin] project ${project} does not have a name in package.json` | ||
); | ||
return; | ||
} | ||
|
||
const { libsDir } = getWorkspaceLayout(tree); | ||
|
||
const entryPointPath = libsDir | ||
? `${libsDir}/${project}/${name}` | ||
: `${project}/${name}`; | ||
|
||
const isExist = tree.exists(entryPointPath); | ||
if (!isExist) { | ||
logger.error(`[local-plugin] ${name} not found as an entry point`); | ||
return; | ||
} | ||
|
||
const isProjectJsonExist = tree.exists(`${entryPointPath}/project.json`); | ||
if (isProjectJsonExist) { | ||
logger.info(`[local-plugin] ${name} entry point is already a Project`); | ||
return; | ||
} | ||
|
||
addProjectConfiguration(tree, `${projectPackageJson.name}/${name}`, { | ||
root: entryPointPath, | ||
projectType: 'library', | ||
sourceRoot: `${entryPointPath}/src`, | ||
targets: { | ||
test: { | ||
executor: '@nx/jest:jest', | ||
outputs: ['{workspaceRoot}/coverage/{projectRoot}'], | ||
options: { | ||
jestConfig: `${projectConfiguration.root}/jest.config.ts`, | ||
passWithNoTests: true, | ||
}, | ||
configurations: { | ||
ci: { | ||
ci: true, | ||
codeCoverage: true, | ||
}, | ||
}, | ||
}, | ||
lint: { | ||
executor: '@nx/linter:eslint', | ||
outputs: ['{options.outputFile}'], | ||
options: { | ||
lintFilePatterns: [ | ||
`${entryPointPath}/**/*.ts`, | ||
`${entryPointPath}/**/*.html`, | ||
], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
updateJson(tree, `${projectConfiguration.root}/project.json`, (json) => { | ||
if (json.targets?.lint?.options?.lintFilePatterns) { | ||
json.targets.lint.options.lintFilePatterns = | ||
json.targets.lint.options.lintFilePatterns.filter( | ||
(pattern: string) => !pattern.includes(entryPointPath) | ||
); | ||
} | ||
return json; | ||
}); | ||
|
||
await formatFiles(tree); | ||
} | ||
|
||
export default convertEntryPointToProjectGenerator; |
4 changes: 4 additions & 0 deletions
4
libs/local-plugin/src/generators/convert-entry-point-to-project/schema.d.ts
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,4 @@ | ||
export interface ConvertEntryPointToProjectGeneratorSchema { | ||
name: string; | ||
project: string; | ||
} |
Oops, something went wrong.