-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a09d913
commit ad3c262
Showing
8 changed files
with
239 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { createNodes, type StylelintPluginOptions } from './src/plugins/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,112 @@ | ||
import { CreateNodesContext } from '@nx/devkit'; | ||
import { vol } from 'memfs'; | ||
import { createNodes } from './plugin'; | ||
|
||
jest.mock('node:fs', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
return require('memfs').fs; | ||
}); | ||
jest.mock('fs', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
return require('memfs').fs; | ||
}); | ||
jest.mock('fs/promises', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
return require('memfs').fs.promises; | ||
}); | ||
|
||
describe('nx-stylelint/plugin', () => { | ||
const createNodesFunction = createNodes[1]; | ||
let context: CreateNodesContext; | ||
|
||
beforeEach(async () => { | ||
context = { | ||
nxJsonConfiguration: { | ||
// These defaults should be overridden by plugin | ||
targetDefaults: { | ||
stylelint: { | ||
cache: false, | ||
inputs: ['foo', '^foo'], | ||
}, | ||
}, | ||
namedInputs: { | ||
default: ['{projectRoot}/**/*'], | ||
production: ['!{projectRoot}/**/*.spec.ts'], | ||
}, | ||
}, | ||
workspaceRoot: '', | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
vol.reset(); | ||
jest.resetModules(); | ||
}); | ||
|
||
it('should create nodes for nested project', async () => { | ||
vol.fromJSON( | ||
{ | ||
'apps/my-app/.stylelintrc.json': JSON.stringify({ | ||
extends: ['../.stylelintrc.yaml', 'stylelint-config-standard'], | ||
}), | ||
'apps/my-app/project.json': `{}`, | ||
'apps/.stylelintrc.yaml': `extends: | ||
- ../.stylelintrc.json`, | ||
'.stylelintrc.json': JSON.stringify({ extends: ['stylelint-config-standard'] }), | ||
'package.json': `{}`, | ||
}, | ||
'', | ||
); | ||
|
||
const nodes = await createNodesFunction('apps/my-app/.stylelintrc.json', {}, context); | ||
|
||
expect(nodes).toMatchInlineSnapshot(` | ||
{ | ||
"projects": { | ||
"apps/my-app": { | ||
"targets": { | ||
"stylelint": { | ||
"cache": true, | ||
"command": "stylelint", | ||
"inputs": [ | ||
"default", | ||
"{projectRoot}/.stylelintrc.json", | ||
"{workspaceRoot}/apps/.stylelintrc.yaml", | ||
"{workspaceRoot}/.stylelintrc.json", | ||
{ | ||
"externalDependencies": [ | ||
"stylelint", | ||
], | ||
}, | ||
], | ||
"options": { | ||
"args": [ | ||
""**/*.css"", | ||
], | ||
"cwd": "apps/my-app", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
it('should not create targets when there is no project.json or package.json', async () => { | ||
vol.fromJSON( | ||
{ | ||
'apps/my-app/.stylelintrc.json': JSON.stringify({ | ||
extends: ['../../.stylelintrc.json'], | ||
}), | ||
'.stylelintrc.json': JSON.stringify({ extends: ['stylelint-config-standard'] }), | ||
'package.json': `{}`, | ||
}, | ||
'', | ||
); | ||
|
||
const nodes = await createNodesFunction('apps/my-app/.stylelintrc.json', {}, context); | ||
|
||
expect(nodes).toStrictEqual({}); | ||
}); | ||
}); |
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,70 @@ | ||
import { CreateNodes, joinPathFragments, workspaceRoot } from '@nx/devkit'; | ||
import { existsSync } from 'node:fs'; | ||
import * as nodePath from 'node:path'; | ||
import { readAffectingStylelintConfigFiles } from '../utils/config-file'; | ||
|
||
export interface StylelintPluginOptions { | ||
targetName?: string; | ||
lintFilePatterns?: string | string[]; | ||
} | ||
|
||
export const createNodes: CreateNodes<StylelintPluginOptions> = [ | ||
'**/.stylelintrc.{json,yml,yaml,js,cjs,mjs}', | ||
async (configFilePath, options, context) => { | ||
const { targetName, lintFilePatterns } = normalizeOptions(options); | ||
|
||
const projectRoot = nodePath.dirname(configFilePath); | ||
|
||
// Do not create a project if package.json and project.json isn't there. | ||
if ( | ||
!existsSync(nodePath.join(context.workspaceRoot, projectRoot, 'package.json')) && | ||
!existsSync(nodePath.join(context.workspaceRoot, projectRoot, 'project.json')) | ||
) | ||
return {}; | ||
|
||
const inputConfigFiles = await getInputConfigFiles(configFilePath, projectRoot); | ||
|
||
return { | ||
projects: { | ||
[projectRoot]: { | ||
targets: { | ||
[targetName]: { | ||
command: `stylelint`, | ||
cache: true, | ||
options: { | ||
cwd: projectRoot, | ||
args: [...lintFilePatterns.map((pattern) => `"${pattern}"`)], | ||
}, | ||
inputs: ['default', ...inputConfigFiles, { externalDependencies: ['stylelint'] }], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
}, | ||
]; | ||
|
||
async function getInputConfigFiles(configFilePath: string, projectRoot: string): Promise<string[]> { | ||
return [...(await readAffectingStylelintConfigFiles(configFilePath))] | ||
.filter((p) => p.startsWith(workspaceRoot)) | ||
.map((configFilePath) => { | ||
if (configFilePath.startsWith(workspaceRoot)) { | ||
configFilePath = nodePath.relative(workspaceRoot, configFilePath); | ||
|
||
if (configFilePath.startsWith(projectRoot)) { | ||
configFilePath = joinPathFragments('{projectRoot}', configFilePath.substring(projectRoot.length)); | ||
} else { | ||
configFilePath = joinPathFragments('{workspaceRoot}', configFilePath); | ||
} | ||
} | ||
|
||
return configFilePath; | ||
}); | ||
} | ||
|
||
function normalizeOptions(options: StylelintPluginOptions | undefined) { | ||
return { | ||
targetName: options?.targetName ?? 'stylelint', | ||
lintFilePatterns: options?.lintFilePatterns ? [options?.lintFilePatterns].flat() : ['**/*.css'], | ||
}; | ||
} |
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 @@ | ||
export function isRelativePath(path: string): boolean { | ||
return path === '.' || path === '..' || path.startsWith('./') || path.startsWith('../'); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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