@0zd0/nx-oxfmt 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -0
- package/eslint.config.mjs +34 -0
- package/executors.json +9 -0
- package/generators.json +9 -0
- package/jest.config.cts +10 -0
- package/package.json +25 -0
- package/project.json +63 -0
- package/src/constants.ts +9 -0
- package/src/executors/fmt/fmt.spec.ts +150 -0
- package/src/executors/fmt/fmt.ts +52 -0
- package/src/executors/fmt/schema.d.ts +11 -0
- package/src/executors/fmt/schema.json +63 -0
- package/src/generators/init/init.spec.ts +69 -0
- package/src/generators/init/init.ts +24 -0
- package/src/generators/init/schema.d.ts +2 -0
- package/src/generators/init/schema.json +8 -0
- package/src/index.ts +59 -0
- package/tsconfig.json +16 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# nx-oxfmt
|
|
2
|
+
|
|
3
|
+
An Nx plugin for [oxfmt](https://oxc.rs/docs/guide/usage/formatter/cli.html) - the high-performance JavaScript/TypeScript formatter.
|
|
4
|
+
|
|
5
|
+
## Manual Setup
|
|
6
|
+
|
|
7
|
+
### Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npx nx add @0zd0/nx-oxfmt
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Add the executor to `project.json`:
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"targets": {
|
|
18
|
+
"fmt": {
|
|
19
|
+
"executor": "@nx-oxfmt/plugin:fmt",
|
|
20
|
+
"options": {
|
|
21
|
+
"write": true
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Run Formatting:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
nx run your-project:fmt
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
or
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
nx run-many --target fmt
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Executor Options
|
|
41
|
+
|
|
42
|
+
All options are optional. The executor defaults to writing changes in place.
|
|
43
|
+
|
|
44
|
+
- `check` _(optional)_ - Check if files are formatted, show statistics (default: `false`)
|
|
45
|
+
- `write` _(optional)_ - Format and write files in place (default: `true`)
|
|
46
|
+
- `listDifferent` _(optional)_ - List files that would be changed (default: `false`)
|
|
47
|
+
- `config` _(optional)_ - Path to the configuration file (default: auto-detected by oxfmt)
|
|
48
|
+
- `ignorePath` _(optional)_ - Path to ignore file(s) (default: `.gitignore` and `.prettierignore`)
|
|
49
|
+
- `threads` _(optional)_ - Number of threads to use (default: auto-detected)
|
|
50
|
+
- `withNodeModules` _(optional)_ - Format code in node_modules directory (default: `false`)
|
|
51
|
+
- `noErrorOnUnmatchedPattern` _(optional)_ - Do not exit with error when pattern is unmatched (default: `false`)
|
|
52
|
+
- `additionalArguments` _(optional)_ - Additional arguments to pass to oxfmt as a string
|
|
53
|
+
|
|
54
|
+
## Configuration
|
|
55
|
+
|
|
56
|
+
If no config file is passed, **oxfmt** will automatically look for configuration files (e.g. `.oxfmtrc.json`).
|
|
57
|
+
|
|
58
|
+
See [oxfmt documentation](https://oxc.rs/docs/guide/usage/formatter/cli.html) for configuration options.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import baseConfig from '../eslint.config.mjs'
|
|
2
|
+
|
|
3
|
+
export default [
|
|
4
|
+
...baseConfig,
|
|
5
|
+
{
|
|
6
|
+
files: ['**/*.json'],
|
|
7
|
+
rules: {
|
|
8
|
+
'@nx/dependency-checks': [
|
|
9
|
+
'error',
|
|
10
|
+
{
|
|
11
|
+
ignoredFiles: ['{projectRoot}/eslint.config.{js,cjs,mjs,ts,cts,mts}'],
|
|
12
|
+
},
|
|
13
|
+
],
|
|
14
|
+
},
|
|
15
|
+
languageOptions: {
|
|
16
|
+
parser: await import('jsonc-eslint-parser'),
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
files: ['**/package.json', '**/generators.json'],
|
|
21
|
+
rules: {
|
|
22
|
+
'@nx/nx-plugin-checks': 'error',
|
|
23
|
+
'@nx/dependency-checks': [
|
|
24
|
+
'error',
|
|
25
|
+
{
|
|
26
|
+
ignoredDependencies: ['oxfmt'],
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
languageOptions: {
|
|
31
|
+
parser: await import('jsonc-eslint-parser'),
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
]
|
package/executors.json
ADDED
package/generators.json
ADDED
package/jest.config.cts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
displayName: 'nx-oxfmt',
|
|
3
|
+
preset: '../jest.preset.js',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
|
+
transform: {
|
|
6
|
+
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
|
|
7
|
+
},
|
|
8
|
+
moduleFileExtensions: ['ts', 'js', 'html'],
|
|
9
|
+
coverageDirectory: '../coverage/nx-oxfmt',
|
|
10
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@0zd0/nx-oxfmt",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "./src/index.js",
|
|
5
|
+
"types": "./src/index.d.ts",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@nx/devkit": "22.4.4",
|
|
8
|
+
"oxfmt": "^0.28.0",
|
|
9
|
+
"tslib": "^2.3.0"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"nx",
|
|
13
|
+
"oxfmt",
|
|
14
|
+
"oxc"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/0zd0/nx-oxfmt",
|
|
19
|
+
"directory": "nx-oxfmt"
|
|
20
|
+
},
|
|
21
|
+
"author": "0zd0",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"generators": "./generators.json",
|
|
24
|
+
"executors": "./executors.json"
|
|
25
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nx-oxfmt",
|
|
3
|
+
"$schema": "../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "nx-oxfmt/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"release": {
|
|
7
|
+
"version": {
|
|
8
|
+
"manifestRootsToUpdate": ["dist/{projectRoot}"],
|
|
9
|
+
"currentVersionResolver": "git-tag",
|
|
10
|
+
"fallbackCurrentVersionResolver": "disk"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"tags": [],
|
|
14
|
+
"targets": {
|
|
15
|
+
"build": {
|
|
16
|
+
"executor": "@nx/js:tsc",
|
|
17
|
+
"outputs": ["{options.outputPath}"],
|
|
18
|
+
"options": {
|
|
19
|
+
"outputPath": "dist/nx-oxfmt",
|
|
20
|
+
"main": "nx-oxfmt/src/index.ts",
|
|
21
|
+
"tsConfig": "nx-oxfmt/tsconfig.lib.json",
|
|
22
|
+
"assets": [
|
|
23
|
+
"nx-oxfmt/*.md",
|
|
24
|
+
{
|
|
25
|
+
"input": "./nx-oxfmt/src",
|
|
26
|
+
"glob": "**/!(*.ts)",
|
|
27
|
+
"output": "./src"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"input": "./nx-oxfmt/src",
|
|
31
|
+
"glob": "**/*.d.ts",
|
|
32
|
+
"output": "./src"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"input": "./nx-oxfmt",
|
|
36
|
+
"glob": "generators.json",
|
|
37
|
+
"output": "."
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"input": "./nx-oxfmt",
|
|
41
|
+
"glob": "executors.json",
|
|
42
|
+
"output": "."
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"nx-release-publish": {
|
|
48
|
+
"options": {
|
|
49
|
+
"packageRoot": "dist/{projectRoot}"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"lint": {
|
|
53
|
+
"executor": "@nx/eslint:lint"
|
|
54
|
+
},
|
|
55
|
+
"test": {
|
|
56
|
+
"executor": "@nx/jest:jest",
|
|
57
|
+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
|
58
|
+
"options": {
|
|
59
|
+
"jestConfig": "nx-oxfmt/jest.config.cts"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const NX_PLUGIN_NAME = '@0zd0/nx-oxfmt'
|
|
2
|
+
export const FORMAT_EXECUTOR_NAME = 'fmt'
|
|
3
|
+
export const DEFAULT_FORMAT_TARGET_NAME = 'fmt'
|
|
4
|
+
export const OXFMT_CONFIG_GLOB = '.oxfmtrc.{json,jsonc}'
|
|
5
|
+
export const OXFMT_BIN_NAME = 'oxfmt'
|
|
6
|
+
|
|
7
|
+
export const EXEC = 'pnpm exec'
|
|
8
|
+
|
|
9
|
+
export const NX_PROJECT_CONFIG_GLOB = '**/{package,project}.json'
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { ExecutorContext, logger } from '@nx/devkit'
|
|
2
|
+
|
|
3
|
+
import { FmtExecutorSchema } from './schema'
|
|
4
|
+
import executor from './fmt'
|
|
5
|
+
import { spawn, ChildProcess } from 'child_process'
|
|
6
|
+
import { EXEC, OXFMT_BIN_NAME } from '../../constants'
|
|
7
|
+
|
|
8
|
+
jest.mock('child_process')
|
|
9
|
+
const mockedSpawn = jest.mocked(spawn)
|
|
10
|
+
jest.mock('@nx/devkit', () => ({
|
|
11
|
+
...jest.requireActual('@nx/devkit'),
|
|
12
|
+
logger: {
|
|
13
|
+
info: jest.fn(),
|
|
14
|
+
error: jest.fn(),
|
|
15
|
+
},
|
|
16
|
+
getPackageManagerCommand: jest.fn().mockReturnValue({ exec: 'pnpm exec' }),
|
|
17
|
+
}))
|
|
18
|
+
|
|
19
|
+
const context: ExecutorContext = {
|
|
20
|
+
root: '/root',
|
|
21
|
+
cwd: '/root',
|
|
22
|
+
isVerbose: false,
|
|
23
|
+
projectName: 'my-project',
|
|
24
|
+
projectGraph: {
|
|
25
|
+
nodes: {
|
|
26
|
+
'my-project': {
|
|
27
|
+
type: 'app',
|
|
28
|
+
name: 'my-project',
|
|
29
|
+
data: {
|
|
30
|
+
root: 'apps/my-project',
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
dependencies: {},
|
|
35
|
+
},
|
|
36
|
+
projectsConfigurations: {
|
|
37
|
+
projects: {
|
|
38
|
+
'my-project': {
|
|
39
|
+
root: 'apps/my-project',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
version: 2,
|
|
43
|
+
},
|
|
44
|
+
nxJsonConfiguration: {},
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe('Fmt Executor', () => {
|
|
48
|
+
let mockChildProcess: { on: jest.Mock<unknown, [string, (...args: unknown[]) => void]> }
|
|
49
|
+
let mockOn: jest.Mock<unknown, [string, (...args: unknown[]) => void]>
|
|
50
|
+
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
jest.clearAllMocks()
|
|
53
|
+
|
|
54
|
+
mockOn = jest.fn().mockImplementation(function (this: unknown) {
|
|
55
|
+
return this
|
|
56
|
+
})
|
|
57
|
+
mockChildProcess = {
|
|
58
|
+
on: mockOn,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
mockedSpawn.mockReturnValue(mockChildProcess as unknown as ChildProcess)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
const runAndEmit = async (options: FmtExecutorSchema, exitCode = 0) => {
|
|
65
|
+
const promise = executor(options, context)
|
|
66
|
+
|
|
67
|
+
const closeCall = mockOn.mock.calls.find((call) => call[0] === 'close')
|
|
68
|
+
if (closeCall) {
|
|
69
|
+
closeCall[1](exitCode)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return promise
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
it('should run oxfmt with base paths', async () => {
|
|
76
|
+
const result = await runAndEmit({ write: true })
|
|
77
|
+
|
|
78
|
+
expect(result.success).toBe(true)
|
|
79
|
+
expect(spawn).toHaveBeenCalledWith(
|
|
80
|
+
`${EXEC} ${OXFMT_BIN_NAME}`,
|
|
81
|
+
['--write', 'apps/my-project'],
|
|
82
|
+
expect.objectContaining({ cwd: '/root', shell: true }),
|
|
83
|
+
)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('should correctly pass all boolean flags', async () => {
|
|
87
|
+
await runAndEmit({
|
|
88
|
+
check: true,
|
|
89
|
+
listDifferent: true,
|
|
90
|
+
withNodeModules: true,
|
|
91
|
+
noErrorOnUnmatchedPattern: true,
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
const args = mockedSpawn.mock.calls[0][1]
|
|
95
|
+
expect(args).toContain('--check')
|
|
96
|
+
expect(args).toContain('--list-different')
|
|
97
|
+
expect(args).toContain('--with-node-modules')
|
|
98
|
+
expect(args).toContain('--no-error-on-unmatched-pattern')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should handle threads and config values', async () => {
|
|
102
|
+
await runAndEmit({ threads: 4, config: 'custom.json' })
|
|
103
|
+
|
|
104
|
+
const args = mockedSpawn.mock.calls[0][1]
|
|
105
|
+
expect(args).toContain('--threads=4')
|
|
106
|
+
expect(args).toContain('--config=custom.json')
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('should handle ignorePath array', async () => {
|
|
110
|
+
await runAndEmit({ ignorePath: ['dist', '.next'] })
|
|
111
|
+
|
|
112
|
+
const args = mockedSpawn.mock.calls[0][1]
|
|
113
|
+
expect(args).toContain('--ignore-path=dist')
|
|
114
|
+
expect(args).toContain('--ignore-path=.next')
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('should pass unknown arguments via __unparsed__', async () => {
|
|
118
|
+
await runAndEmit({
|
|
119
|
+
write: true,
|
|
120
|
+
__unparsed__: ['--max-width=120', '--no-semi'],
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
const args = mockedSpawn.mock.calls[0][1]
|
|
124
|
+
expect(args).toContain('--max-width=120')
|
|
125
|
+
expect(args).toContain('--no-semi')
|
|
126
|
+
expect(args[args.length - 1]).toBe('apps/my-project')
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('should return success: false on non-zero exit code', async () => {
|
|
130
|
+
const result = await runAndEmit({}, 1)
|
|
131
|
+
|
|
132
|
+
expect(result.success).toBe(false)
|
|
133
|
+
expect(logger.error).toHaveBeenCalledWith(
|
|
134
|
+
expect.stringContaining('oxfmt exited with code 1'),
|
|
135
|
+
)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('should handle system spawn error (error event)', async () => {
|
|
139
|
+
const promise = executor({}, context)
|
|
140
|
+
|
|
141
|
+
const errorCall = mockOn.mock.calls.find((call) => call[0] === 'error')
|
|
142
|
+
if (errorCall) {
|
|
143
|
+
errorCall[1](new Error('Spawn failed'))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const result = await promise
|
|
147
|
+
expect(result.success).toBe(false)
|
|
148
|
+
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Error starting oxfmt'))
|
|
149
|
+
})
|
|
150
|
+
})
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { PromiseExecutor, logger, getPackageManagerCommand } from '@nx/devkit'
|
|
2
|
+
import { spawn } from 'child_process'
|
|
3
|
+
import { FmtExecutorSchema } from './schema'
|
|
4
|
+
import { OXFMT_BIN_NAME } from '../../constants'
|
|
5
|
+
|
|
6
|
+
const runExecutor: PromiseExecutor<FmtExecutorSchema> = async (options, context) => {
|
|
7
|
+
const projectRoot = context.projectsConfigurations.projects[context.projectName]?.root
|
|
8
|
+
const args: string[] = []
|
|
9
|
+
|
|
10
|
+
if (options.check) args.push('--check')
|
|
11
|
+
if (options.write) args.push('--write')
|
|
12
|
+
if (options.config) args.push(`--config=${options.config}`)
|
|
13
|
+
if (options.listDifferent) args.push('--list-different')
|
|
14
|
+
if (options.withNodeModules) args.push('--with-node-modules')
|
|
15
|
+
if (options.noErrorOnUnmatchedPattern) args.push('--no-error-on-unmatched-pattern')
|
|
16
|
+
if (options.threads) args.push(`--threads=${options.threads}`)
|
|
17
|
+
if (options.ignorePath) {
|
|
18
|
+
const paths = Array.isArray(options.ignorePath) ? options.ignorePath : [options.ignorePath]
|
|
19
|
+
paths.forEach((path) => args.push(`--ignore-path=${path}`))
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(options.__unparsed__)) {
|
|
22
|
+
args.push(...options.__unparsed__)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
args.push(projectRoot)
|
|
26
|
+
|
|
27
|
+
logger.info(`Running oxfmt for ${context.projectName}`)
|
|
28
|
+
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
const child = spawn(`${getPackageManagerCommand().exec} ${OXFMT_BIN_NAME}`, args, {
|
|
31
|
+
cwd: context.root,
|
|
32
|
+
stdio: 'inherit',
|
|
33
|
+
shell: true,
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
child.on('close', (code) => {
|
|
37
|
+
if (code === 0) {
|
|
38
|
+
resolve({ success: true })
|
|
39
|
+
} else {
|
|
40
|
+
logger.error(`oxfmt exited with code ${code}`)
|
|
41
|
+
resolve({ success: false })
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
child.on('error', (e) => {
|
|
46
|
+
logger.error(`Error starting oxfmt: ${e.message}`)
|
|
47
|
+
resolve({ success: false })
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default runExecutor
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface FmtExecutorSchema {
|
|
2
|
+
check?: boolean
|
|
3
|
+
write?: boolean
|
|
4
|
+
listDifferent?: boolean
|
|
5
|
+
config?: string
|
|
6
|
+
ignorePath?: string | string[]
|
|
7
|
+
withNodeModules?: boolean
|
|
8
|
+
noErrorOnUnmatchedPattern?: boolean
|
|
9
|
+
threads?: number
|
|
10
|
+
__unparsed__?: string[]
|
|
11
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/schema",
|
|
3
|
+
"version": 2,
|
|
4
|
+
"title": "Oxfmt executot",
|
|
5
|
+
"description": "Run oxfmt on a project",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"check": {
|
|
9
|
+
"type": "boolean",
|
|
10
|
+
"description": "Check if files are formatted, also show statistics",
|
|
11
|
+
"default": false
|
|
12
|
+
},
|
|
13
|
+
"write": {
|
|
14
|
+
"type": "boolean",
|
|
15
|
+
"description": "Format and write files in place (default)",
|
|
16
|
+
"default": false
|
|
17
|
+
},
|
|
18
|
+
"listDifferent": {
|
|
19
|
+
"type": "boolean",
|
|
20
|
+
"description": "List files that would be changed",
|
|
21
|
+
"default": false
|
|
22
|
+
},
|
|
23
|
+
"config": {
|
|
24
|
+
"type": "string",
|
|
25
|
+
"description": "Path to the configuration file",
|
|
26
|
+
"alias": "c"
|
|
27
|
+
},
|
|
28
|
+
"ignorePath": {
|
|
29
|
+
"description": "Path to ignore file(s). Can be specified multiple times. If not specified, .gitignore and .prettierignore in the current directory are used.",
|
|
30
|
+
"oneOf": [
|
|
31
|
+
{
|
|
32
|
+
"type": "string"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"type": "array",
|
|
36
|
+
"items": {
|
|
37
|
+
"type": "string"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
]
|
|
41
|
+
},
|
|
42
|
+
"withNodeModules": {
|
|
43
|
+
"type": "boolean",
|
|
44
|
+
"description": "Format code in node_modules directory (skipped by default)",
|
|
45
|
+
"default": false
|
|
46
|
+
},
|
|
47
|
+
"noErrorOnUnmatchedPattern": {
|
|
48
|
+
"type": "boolean",
|
|
49
|
+
"description": "Do not exit with error when pattern is unmatched",
|
|
50
|
+
"default": false
|
|
51
|
+
},
|
|
52
|
+
"threads": {
|
|
53
|
+
"type": "number",
|
|
54
|
+
"description": "Number of threads to use. Set to 1 for using only 1 CPU core."
|
|
55
|
+
},
|
|
56
|
+
"additionalArguments": {
|
|
57
|
+
"type": "string",
|
|
58
|
+
"description": "Additional arguments to pass to oxfmt"
|
|
59
|
+
},
|
|
60
|
+
"additionalProperties": true
|
|
61
|
+
},
|
|
62
|
+
"required": []
|
|
63
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'
|
|
2
|
+
import { Tree, readNxJson, updateNxJson } from '@nx/devkit'
|
|
3
|
+
|
|
4
|
+
import initGenerator from './init'
|
|
5
|
+
import { DEFAULT_FORMAT_TARGET_NAME, NX_PLUGIN_NAME } from '../../constants'
|
|
6
|
+
|
|
7
|
+
describe('init generator', () => {
|
|
8
|
+
let tree: Tree
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
tree = createTreeWithEmptyWorkspace()
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should add the plugin to nx.json if not present', async () => {
|
|
15
|
+
await initGenerator(tree)
|
|
16
|
+
|
|
17
|
+
const nxJson = readNxJson(tree)
|
|
18
|
+
|
|
19
|
+
expect(nxJson?.plugins).toContainEqual({
|
|
20
|
+
plugin: NX_PLUGIN_NAME,
|
|
21
|
+
options: {
|
|
22
|
+
formatTargetName: DEFAULT_FORMAT_TARGET_NAME,
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('should not add the plugin if already present as object', async () => {
|
|
28
|
+
const nxJson = readNxJson(tree) || {}
|
|
29
|
+
nxJson.plugins = [
|
|
30
|
+
...(nxJson.plugins || []),
|
|
31
|
+
{
|
|
32
|
+
plugin: NX_PLUGIN_NAME,
|
|
33
|
+
options: {
|
|
34
|
+
formatTargetName: 'custom-lint-name',
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
]
|
|
38
|
+
updateNxJson(tree, nxJson)
|
|
39
|
+
|
|
40
|
+
await initGenerator(tree)
|
|
41
|
+
|
|
42
|
+
const updatedNxJson = readNxJson(tree)
|
|
43
|
+
|
|
44
|
+
const myPlugins = updatedNxJson?.plugins?.filter((plugin) => {
|
|
45
|
+
return typeof plugin === 'string' ?
|
|
46
|
+
plugin === NX_PLUGIN_NAME
|
|
47
|
+
: plugin.plugin === NX_PLUGIN_NAME
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
expect(myPlugins).toHaveLength(1)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('should not add the plugin if already present as string', async () => {
|
|
54
|
+
const nxJson = readNxJson(tree) || {}
|
|
55
|
+
nxJson.plugins = [...(nxJson.plugins || []), NX_PLUGIN_NAME]
|
|
56
|
+
updateNxJson(tree, nxJson)
|
|
57
|
+
|
|
58
|
+
await initGenerator(tree)
|
|
59
|
+
|
|
60
|
+
const updatedNxJson = readNxJson(tree)
|
|
61
|
+
const myPlugins = updatedNxJson?.plugins?.filter((plugin) => {
|
|
62
|
+
return typeof plugin === 'string' ?
|
|
63
|
+
plugin === NX_PLUGIN_NAME
|
|
64
|
+
: plugin.plugin === NX_PLUGIN_NAME
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
expect(myPlugins).toHaveLength(1)
|
|
68
|
+
})
|
|
69
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { readNxJson, Tree, updateNxJson } from '@nx/devkit'
|
|
2
|
+
import { DEFAULT_FORMAT_TARGET_NAME, NX_PLUGIN_NAME } from '../../constants'
|
|
3
|
+
|
|
4
|
+
export default async function initGenerator(tree: Tree) {
|
|
5
|
+
const nxJson = readNxJson(tree) || {}
|
|
6
|
+
const hasPlugin = nxJson.plugins?.some((plugin) => {
|
|
7
|
+
return typeof plugin === 'string' ?
|
|
8
|
+
plugin === NX_PLUGIN_NAME
|
|
9
|
+
: plugin.plugin === NX_PLUGIN_NAME
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
if (!hasPlugin) {
|
|
13
|
+
nxJson.plugins = [
|
|
14
|
+
...(nxJson.plugins || []),
|
|
15
|
+
{
|
|
16
|
+
plugin: NX_PLUGIN_NAME,
|
|
17
|
+
options: {
|
|
18
|
+
formatTargetName: DEFAULT_FORMAT_TARGET_NAME,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
]
|
|
22
|
+
updateNxJson(tree, nxJson)
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createNodesFromFiles,
|
|
3
|
+
CreateNodesV2,
|
|
4
|
+
joinPathFragments,
|
|
5
|
+
TargetConfiguration,
|
|
6
|
+
} from '@nx/devkit'
|
|
7
|
+
import { dirname } from 'path'
|
|
8
|
+
import {
|
|
9
|
+
DEFAULT_FORMAT_TARGET_NAME,
|
|
10
|
+
FORMAT_EXECUTOR_NAME,
|
|
11
|
+
NX_PLUGIN_NAME,
|
|
12
|
+
NX_PROJECT_CONFIG_GLOB,
|
|
13
|
+
OXFMT_CONFIG_GLOB,
|
|
14
|
+
} from './constants'
|
|
15
|
+
|
|
16
|
+
interface OxfmtPluginOptions {
|
|
17
|
+
formatTargetName?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const createNodesV2: CreateNodesV2<OxfmtPluginOptions> = [
|
|
21
|
+
NX_PROJECT_CONFIG_GLOB,
|
|
22
|
+
async (configFiles, options, context) => {
|
|
23
|
+
return await createNodesFromFiles(
|
|
24
|
+
(configFile, options) => {
|
|
25
|
+
const projectRoot = dirname(configFile)
|
|
26
|
+
|
|
27
|
+
const target: TargetConfiguration = {
|
|
28
|
+
executor: `${NX_PLUGIN_NAME}:${FORMAT_EXECUTOR_NAME}`,
|
|
29
|
+
options: {
|
|
30
|
+
...options,
|
|
31
|
+
cwd: projectRoot,
|
|
32
|
+
},
|
|
33
|
+
cache: true,
|
|
34
|
+
inputs: [
|
|
35
|
+
`{workspaceRoot}/${OXFMT_CONFIG_GLOB}`,
|
|
36
|
+
`{projectRoot}/${OXFMT_CONFIG_GLOB}`,
|
|
37
|
+
joinPathFragments('{projectRoot}', '**', '*'),
|
|
38
|
+
{
|
|
39
|
+
externalDependencies: ['oxfmt'],
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
projects: {
|
|
46
|
+
[projectRoot]: {
|
|
47
|
+
targets: {
|
|
48
|
+
[options.formatTargetName ?? DEFAULT_FORMAT_TARGET_NAME]: target,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
configFiles,
|
|
55
|
+
options,
|
|
56
|
+
context,
|
|
57
|
+
)
|
|
58
|
+
},
|
|
59
|
+
]
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../dist/out-tsc",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"types": ["node"]
|
|
7
|
+
},
|
|
8
|
+
"include": ["src/**/*.ts"],
|
|
9
|
+
"exclude": ["jest.config.ts", "jest.config.cts", "src/**/*.spec.ts", "src/**/*.test.ts"]
|
|
10
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../dist/out-tsc",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"moduleResolution": "node10",
|
|
7
|
+
"types": ["jest", "node"]
|
|
8
|
+
},
|
|
9
|
+
"include": [
|
|
10
|
+
"jest.config.ts",
|
|
11
|
+
"jest.config.cts",
|
|
12
|
+
"src/**/*.test.ts",
|
|
13
|
+
"src/**/*.spec.ts",
|
|
14
|
+
"src/**/*.d.ts"
|
|
15
|
+
]
|
|
16
|
+
}
|