Skip to content

Commit

Permalink
fix: fixed root stylelint creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillip9587 committed Oct 8, 2023
1 parent 06d70a4 commit bb05744
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
66 changes: 59 additions & 7 deletions e2e/nx-stylelint-e2e/tests/nx-stylelint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config>('.stylelintrc.json');
let rootConfig = readJson<Config>('.stylelintrc.json');
expect(rootConfig).toStrictEqual<Config>({
ignoreFiles: ['**/*'],
overrides: [
Expand All @@ -76,7 +80,7 @@ describe('nx-stylelint-e2e', () => {
rules: {},
});

const projectConfig = readJson<Config>(`${projName}/.stylelintrc.json`);
let projectConfig = readJson<Config>(`${projName}/.stylelintrc.json`);
expect(projectConfig).toStrictEqual<Config>({
extends: ['../.stylelintrc.json'],
ignoreFiles: ['!**/*'],
Expand All @@ -89,23 +93,71 @@ 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<ProjectConfiguration>(`${projName}/project.json`);

let projectJson = readJson<ProjectConfiguration>(`${projName}/project.json`);
expect(projectJson.targets.stylelint).toStrictEqual<TargetConfiguration>({
executor: 'nx-stylelint:lint',
options: {
lintFilePatterns: [`${projName}/**/*.css`],
},
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<Config>('.stylelintrc.json');
expect(rootConfig).toStrictEqual<Config>({
ignoreFiles: ['**/*'],
overrides: [
{
files: ['**/*.css'],
extends: ['stylelint-config-standard'],
rules: {},
},
{
files: ['**/*.scss'],
extends: ['stylelint-config-standard-scss'],
rules: {},
},
],
rules: {},
});

projectConfig = readJson<Config>(`${proj2Name}/.stylelintrc.json`);
expect(projectConfig).toStrictEqual<Config>({
extends: ['../.stylelintrc.json'],
ignoreFiles: ['!**/*'],
overrides: [
{
files: ['**/*.css'],
rules: {},
},
{
files: ['**/*.scss'],
rules: {},
},
],
});

projectJson = readJson<ProjectConfiguration>(`${proj2Name}/project.json`);
expect(projectJson.targets.stylelint).toStrictEqual<TargetConfiguration>({
executor: 'nx-stylelint:lint',
options: {
lintFilePatterns: [`${proj2Name}/**/*.css`, `${proj2Name}/**/*.scss`],
},
outputs: ['{options.outputFile}'],
});
}, 120000);

describe('--formatter', () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/nx-stylelint/src/generators/init/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -144,3 +145,25 @@ function createRecommendedStylelintConfiguration(tree: Tree, scss: boolean) {

writeJson<Config>(tree, '.stylelintrc.json', config);
}

function isCompatibleRootConfig(tree: Tree): boolean {
const config = readJson<Config>(tree, '.stylelintrc.json');

return config.ignoreFiles === '**/*' || (Array.isArray(config.ignoreFiles) && config.ignoreFiles.includes('**/*'));
}

function addScssToStylelintConfiguration(tree: Tree) {
updateJson<Config, Config>(tree, '.stylelintrc.json', (value) => ({
...value,
overrides: Array.from(
new Set([
...(value.overrides ?? []),
{
files: ['**/*.scss'],
extends: ['stylelint-config-standard-scss'],
rules: {},
},
])
),
}));
}

0 comments on commit bb05744

Please sign in to comment.