diff --git a/e2e/nx-stylelint-e2e/tests/nx-stylelint.spec.ts b/e2e/nx-stylelint-e2e/tests/nx-stylelint.spec.ts index e92b617b..db51eec8 100644 --- a/e2e/nx-stylelint-e2e/tests/nx-stylelint.spec.ts +++ b/e2e/nx-stylelint-e2e/tests/nx-stylelint.spec.ts @@ -50,20 +50,24 @@ describe('nx-stylelint-e2e', () => { describe('nx-stylelint:configuration', () => { it('should add a stylelint configuration to a project', async () => { const projName = uniq('nx-stylelint'); + const proj2Name = uniq('nx-stylelint'); + await runNxCommandAsync(`generate @nx/js:library --name ${projName}`); + await runNxCommandAsync(`generate @nx/js:library --name ${proj2Name}`); + await runNxCommandAsync(`generate nx-stylelint:configuration --project ${projName}`); expect(() => checkFilesExist('.stylelintrc.json', 'package.json', 'nx.json', `${projName}/.stylelintrc.json`) ).not.toThrow(); - const packageJson = readJson('package.json'); + let packageJson = readJson('package.json'); expect(packageJson.devDependencies['nx-stylelint']).toBeTruthy(); expect(packageJson.devDependencies.stylelint).toBeTruthy(); expect(packageJson.devDependencies['stylelint-config-standard-scss']).toBeUndefined(); - const rootConfig = readJson('.stylelintrc.json'); + let rootConfig = readJson('.stylelintrc.json'); expect(rootConfig).toStrictEqual({ ignoreFiles: ['**/*'], overrides: [ @@ -76,7 +80,7 @@ describe('nx-stylelint-e2e', () => { rules: {}, }); - const projectConfig = readJson(`${projName}/.stylelintrc.json`); + let projectConfig = readJson(`${projName}/.stylelintrc.json`); expect(projectConfig).toStrictEqual({ extends: ['../.stylelintrc.json'], ignoreFiles: ['!**/*'], @@ -89,16 +93,13 @@ describe('nx-stylelint-e2e', () => { }); const nxJson: NxJsonConfiguration = readJson('nx.json'); - expect(nxJson.tasksRunnerOptions.default).toBeTruthy(); expect(nxJson.tasksRunnerOptions.default.options.cacheableOperations).toContain('stylelint'); - expect(nxJson.targetDefaults.stylelint).toStrictEqual({ inputs: ['default', '{workspaceRoot}/.stylelintrc(.(json|yml|yaml|js))?'], }); - const projectJson = readJson(`${projName}/project.json`); - + let projectJson = readJson(`${projName}/project.json`); expect(projectJson.targets.stylelint).toStrictEqual({ executor: 'nx-stylelint:lint', options: { @@ -106,6 +107,57 @@ describe('nx-stylelint-e2e', () => { }, outputs: ['{options.outputFile}'], }); + + await runNxCommandAsync(`generate nx-stylelint:configuration --project ${proj2Name} --scss true`); + + packageJson = readJson('package.json'); + + expect(packageJson.devDependencies['nx-stylelint']).toBeTruthy(); + expect(packageJson.devDependencies.stylelint).toBeTruthy(); + expect(packageJson.devDependencies['stylelint-config-standard-scss']).toBeTruthy(); + + rootConfig = readJson('.stylelintrc.json'); + expect(rootConfig).toStrictEqual({ + ignoreFiles: ['**/*'], + overrides: [ + { + files: ['**/*.css'], + extends: ['stylelint-config-standard'], + rules: {}, + }, + { + files: ['**/*.scss'], + extends: ['stylelint-config-standard-scss'], + rules: {}, + }, + ], + rules: {}, + }); + + projectConfig = readJson(`${proj2Name}/.stylelintrc.json`); + expect(projectConfig).toStrictEqual({ + extends: ['../.stylelintrc.json'], + ignoreFiles: ['!**/*'], + overrides: [ + { + files: ['**/*.css'], + rules: {}, + }, + { + files: ['**/*.scss'], + rules: {}, + }, + ], + }); + + projectJson = readJson(`${proj2Name}/project.json`); + expect(projectJson.targets.stylelint).toStrictEqual({ + executor: 'nx-stylelint:lint', + options: { + lintFilePatterns: [`${proj2Name}/**/*.css`, `${proj2Name}/**/*.scss`], + }, + outputs: ['{options.outputFile}'], + }); }, 120000); describe('--formatter', () => { diff --git a/packages/nx-stylelint/src/generators/init/generator.ts b/packages/nx-stylelint/src/generators/init/generator.ts index 1a65bc8c..16f41ca9 100644 --- a/packages/nx-stylelint/src/generators/init/generator.ts +++ b/packages/nx-stylelint/src/generators/init/generator.ts @@ -26,6 +26,7 @@ export async function initGenerator(host: Tree, options: InitGeneratorSchema): P const installTask = updateDependencies(host, !!options.scss); if (!host.exists('.stylelintrc.json')) createRecommendedStylelintConfiguration(host, !!options.scss); + else if (isCompatibleRootConfig(host)) addScssToStylelintConfiguration(host); else { logger.info( `Stylelint root configuration found! Skipping creation of root .stylelintrc.json! @@ -144,3 +145,25 @@ function createRecommendedStylelintConfiguration(tree: Tree, scss: boolean) { writeJson(tree, '.stylelintrc.json', config); } + +function isCompatibleRootConfig(tree: Tree): boolean { + const config = readJson(tree, '.stylelintrc.json'); + + return config.ignoreFiles === '**/*' || (Array.isArray(config.ignoreFiles) && config.ignoreFiles.includes('**/*')); +} + +function addScssToStylelintConfiguration(tree: Tree) { + updateJson(tree, '.stylelintrc.json', (value) => ({ + ...value, + overrides: Array.from( + new Set([ + ...(value.overrides ?? []), + { + files: ['**/*.scss'], + extends: ['stylelint-config-standard-scss'], + rules: {}, + }, + ]) + ), + })); +}