Skip to content

Commit

Permalink
feat: added a scss option to the configuration generator and deprecat…
Browse files Browse the repository at this point in the history
…ed the scss generator
  • Loading branch information
Phillip9587 committed Oct 8, 2023
1 parent c3292a9 commit 06d70a4
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 70 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ The name of the project.

Type: `string`

#### `scss`

Add SCSS Language support.

Type: `boolean`

Default: `false`

#### `skipFormat`

Skip formatting files.
Expand All @@ -119,7 +127,9 @@ Type: `boolean`

Default: `false`

## `nx-stylelint:scss` generator
## DEPRECATED `nx-stylelint:scss` generator

**This generator is deprecated and will be removed in v17. Use the configuration generator with the scss option instead**

Add scss support to a nx-stylelint configuration.

Expand Down
2 changes: 1 addition & 1 deletion packages/nx-stylelint/executors.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"executors": {
"lint": {
"implementation": "./src/executors/lint/executor",
Expand Down
8 changes: 5 additions & 3 deletions packages/nx-stylelint/generators.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"name": "nx-stylelint",
"version": "0.0.1",
"generators": {
Expand All @@ -19,7 +19,8 @@
"scss": {
"factory": "./src/generators/scss/generator",
"schema": "./src/generators/scss/schema.json",
"description": "Add scss support to a nx-stylelint configuration"
"description": "Add scss support to a nx-stylelint configuration",
"x-deprecated": "This generator is deprecated and will be removed in v17. Use the configuration generator with the scss option instead."
}
},
"schematics": {
Expand All @@ -39,7 +40,8 @@
"scss": {
"factory": "./src/generators/scss/compat",
"schema": "./src/generators/scss/schema.json",
"description": "Add scss support to a nx-stylelint configuration"
"description": "Add scss support to a nx-stylelint configuration",
"x-deprecated": "This generator is deprecated and will be removed in v17. Use the configuration generator with the scss option instead."
}
}
}
2 changes: 1 addition & 1 deletion packages/nx-stylelint/src/executors/lint/schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"version": 2,
"outputCapture": "direct-nodejs",
"$schema": "http://json-schema.org/schema",
"title": "Stylelint Lint Target",
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,34 @@ describe('nx-stylelint:configuration generator', () => {
await libraryGenerator(tree, { name: 'test', compiler: 'tsc' });
await generator(tree, defaultOptions);

const packagejson = readJson(tree, 'package.json');
expect(packagejson.devDependencies['stylelint']).toBe('^15.0.0');
expect(packagejson.devDependencies['stylelint-config-standard']).toBe('^34.0.0');
expect(packagejson.devDependencies['stylelint-config-standard-scss']).toBeUndefined();

const config = readProjectConfiguration(tree, 'test');
expect(config.targets?.['stylelint']).toStrictEqual({
executor: 'nx-stylelint:lint',
outputs: ['{options.outputFile}'],
options: {
lintFilePatterns: ['libs/test/**/*.css'],
},
});

expect(config).toBeDefined();
expect(config.targets?.['stylelint']).toBeDefined();
expect(config.targets?.['stylelint'].executor).toBe('nx-stylelint:lint');
expect(config.targets?.['stylelint'].options.format).toBeUndefined();
expect(config.targets?.['stylelint'].options.lintFilePatterns).toContain('libs/test/**/*.css');
expect(tree.exists('.stylelintrc.json')).toBeTruthy();
expect(tree.exists(projectStylelint)).toBeTruthy();

const projectStylelintConfig = readJson<Config>(tree, projectStylelint);
expect(projectStylelintConfig.extends).toHaveLength(1);
expect(projectStylelintConfig.extends).toContain('../../.stylelintrc.json');
expect(projectStylelintConfig.ignoreFiles).toContain('!**/*');
expect(projectStylelintConfig.overrides).toStrictEqual([
{
files: ['**/*.css'],
rules: {},
},
]);
expect(projectStylelintConfig).toStrictEqual({
extends: ['../../.stylelintrc.json'],
ignoreFiles: ['!**/*'],
overrides: [
{
files: ['**/*.css'],
rules: {},
},
],
});
});

it('should add stylelint target alongside other targets, run init generator and create project .stylelinrrc.json', async () => {
Expand Down Expand Up @@ -129,11 +137,14 @@ describe('nx-stylelint:configuration generator', () => {
await generator(tree, { ...defaultOptions, formatter: 'json' });

const config = readProjectConfiguration(tree, 'test');

expect(config).toBeDefined();
expect(config.targets?.['stylelint']).toBeDefined();
expect(config.targets?.['stylelint'].executor).toBe('nx-stylelint:lint');
expect(config.targets?.['stylelint'].options.formatter).toBe('json');
expect(config.targets?.['stylelint']).toStrictEqual({
executor: 'nx-stylelint:lint',
outputs: ['{options.outputFile}'],
options: {
formatter: 'json',
lintFilePatterns: ['libs/test/**/*.css'],
},
});
});

it('should print a error if the format is not defined', async () => {
Expand All @@ -152,4 +163,46 @@ describe('nx-stylelint:configuration generator', () => {
);
});
});

describe('--scss', () => {
it('should add stylelint target, run init generator and create project .stylelinrrc.json with SCSS support', async () => {
const projectStylelint = `libs/test/.stylelintrc.json`;

await libraryGenerator(tree, { name: 'test', compiler: 'tsc' });
await generator(tree, { ...defaultOptions, scss: true });

const packagejson = readJson(tree, 'package.json');
expect(packagejson.devDependencies['stylelint']).toBe('^15.0.0');
expect(packagejson.devDependencies['stylelint-config-standard']).toBe('^34.0.0');
expect(packagejson.devDependencies['stylelint-config-standard-scss']).toBe('^11.0.0');

const config = readProjectConfiguration(tree, 'test');
expect(config.targets?.['stylelint']).toStrictEqual({
executor: 'nx-stylelint:lint',
outputs: ['{options.outputFile}'],
options: {
lintFilePatterns: ['libs/test/**/*.css', 'libs/test/**/*.scss'],
},
});

expect(tree.exists('.stylelintrc.json')).toBeTruthy();
expect(tree.exists(projectStylelint)).toBeTruthy();

const projectStylelintConfig = readJson<Config>(tree, projectStylelint);
expect(projectStylelintConfig).toStrictEqual({
extends: ['../../.stylelintrc.json'],
ignoreFiles: ['!**/*'],
overrides: [
{
files: ['**/*.css'],
rules: {},
},
{
files: ['**/*.scss'],
rules: {},
},
],
});
});
});
});
28 changes: 21 additions & 7 deletions packages/nx-stylelint/src/generators/configuration/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function configurationGenerator(
host: Tree,
options: ConfigurationGeneratorSchema
): Promise<void | GeneratorCallback> {
const init = await initGenerator(host, { skipFormat: options.skipFormat });
const init = await initGenerator(host, { skipFormat: options.skipFormat, scss: options.scss });

const normalizedOptions = normalizeSchema(host, options);

Expand Down Expand Up @@ -63,14 +63,19 @@ function normalizeSchema(tree: Tree, options: ConfigurationGeneratorSchema): Nor
};
}

function addStylelintTarget(host: Tree, options: NormalizedSchema) {
const projectConfig = readProjectConfiguration(host, options.project);
function addStylelintTarget(tree: Tree, options: NormalizedSchema) {
const projectConfig = readProjectConfiguration(tree, options.project);

const targetOptions: Partial<LintExecutorSchema> = {
lintFilePatterns: [joinPathFragments(options.projectRoot, '**', '*.css')],
formatter: options.formatter === 'string' ? undefined : options.formatter,
};

if (options.scss) {
targetOptions.lintFilePatterns ??= [];
targetOptions.lintFilePatterns.push(joinPathFragments(options.projectRoot, '**', '*.scss'));
}

projectConfig.targets = {
...projectConfig.targets,
stylelint: {
Expand All @@ -79,11 +84,11 @@ function addStylelintTarget(host: Tree, options: NormalizedSchema) {
options: targetOptions,
},
};
updateProjectConfiguration(host, options.project, projectConfig);
updateProjectConfiguration(tree, options.project, projectConfig);
}

function createStylelintConfig(host: Tree, options: NormalizedSchema) {
writeJson<Config>(host, joinPathFragments(options.projectRoot, '.stylelintrc.json'), {
function createStylelintConfig(tree: Tree, options: NormalizedSchema) {
const config = {
extends: [joinPathFragments(offsetFromRoot(options.projectRoot), '.stylelintrc.json')],
ignoreFiles: ['!**/*'],
overrides: [
Expand All @@ -92,5 +97,14 @@ function createStylelintConfig(host: Tree, options: NormalizedSchema) {
rules: {},
},
],
});
};

if (options.scss) {
config.overrides.push({
files: ['**/*.scss'],
rules: {},
});
}

writeJson<Config>(tree, joinPathFragments(options.projectRoot, '.stylelintrc.json'), config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import type { FormatterType } from 'stylelint';
export interface ConfigurationGeneratorSchema {
formatter?: FormatterType;
project: string;
skipFormat: boolean;
scss?: boolean;
skipFormat?: boolean;
}
14 changes: 11 additions & 3 deletions packages/nx-stylelint/src/generators/configuration/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "NxStylelintProjectConfiguration",
"title": "Add stylelint configuration to a project",
"type": "object",
Expand Down Expand Up @@ -43,14 +43,22 @@
"label": "verbose"
}
]
}
},
"x-priority": "important"
},
"project": {
"type": "string",
"description": "The name of the project.",
"$default": {
"$source": "projectName"
}
},
"x-priority": "important"
},
"scss": {
"description": "Add SCSS Language support.",
"type": "boolean",
"default": false,
"x-priority": "important"
},
"skipFormat": {
"description": "Skip formatting files.",
Expand Down
40 changes: 38 additions & 2 deletions packages/nx-stylelint/src/generators/init/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,44 @@ describe('nx-stylelint:init generator', () => {
expect(packagejson.devDependencies['stylelint-config-standard']).toBe('^34.0.0');

const stylelintrc = readJson<Config>(tree, '.stylelintrc.json');
expect(stylelintrc.ignoreFiles?.length).toBe(1);
expect(stylelintrc.ignoreFiles).toContain('**/*');
expect(stylelintrc).toStrictEqual<Config>({
ignoreFiles: ['**/*'],
overrides: [
{
files: ['**/*.css'],
extends: ['stylelint-config-standard'],
rules: {},
},
],
rules: {},
});
});

it('should add dependencies and create recommended root configuration with scss support', async () => {
await generator(tree, { ...defaultOptions, scss: true });

const packagejson = readJson(tree, 'package.json');
expect(packagejson.devDependencies['stylelint']).toBe('^15.0.0');
expect(packagejson.devDependencies['stylelint-config-standard']).toBe('^34.0.0');
expect(packagejson.devDependencies['stylelint-config-standard-scss']).toBe('^11.0.0');

const stylelintrc = readJson<Config>(tree, '.stylelintrc.json');
expect(stylelintrc).toStrictEqual<Config>({
ignoreFiles: ['**/*'],
overrides: [
{
files: ['**/*.css'],
extends: ['stylelint-config-standard'],
rules: {},
},
{
files: ['**/*.scss'],
extends: ['stylelint-config-standard-scss'],
rules: {},
},
],
rules: {},
});
});

it('should skip creation of .stylelintrc.json and recommended dependencies when already present', async () => {
Expand Down
Loading

0 comments on commit 06d70a4

Please sign in to comment.