Skip to content

Commit

Permalink
fix: eslint needs extra configs to resolve paths correctly
Browse files Browse the repository at this point in the history
Merged the release CI fixes from another branch here as that one is failing for unknow reasons
  • Loading branch information
SimeonC committed Oct 2, 2023
1 parent c750302 commit 1b88453
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 65 deletions.
4 changes: 2 additions & 2 deletions .github/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ runs:

- name: Setup branch for NX (main)
shell: bash
if: github.ref == 'refs/head/main'
if: github.ref == 'refs/heads/main'
run: git branch -u origin/main main

- name: Setup branch for NX (!main)
shell: bash
if: github.ref != 'refs/head/main'
if: github.ref != 'refs/heads/main'
run: git branch --track main origin/main

- name: Install without scripts 🔧
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ jobs:

- uses: ./.github/setup

- name: Build
run: npm run build

- name: Run Tests
run: npm test

Expand Down
42 changes: 42 additions & 0 deletions packages/nx/src/generators/quality/eslintConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as path from 'path';
import { Tree } from '@nx/devkit';
import { getNxProjectRoot, outputPrettyFile } from '@tablecheck/frontend-utils';
import * as fs from 'fs-extra';

function getConfigs(projectRoot: string) {
const mapToPath = (filename: string) => path.join(projectRoot, filename);
const nxAppConfigs = ['tsconfig.app.json', 'tsconfig.spec.json'].map(
mapToPath,
);
const defaultConfigs = ['tsconfig.base.json', 'tsconfig.json'].map(mapToPath);
if (nxAppConfigs.every((config) => fs.existsSync(config))) {
return nxAppConfigs;
}
return defaultConfigs.filter((config) => fs.existsSync(config));
}

export function generateEslintConfig(tree: Tree, projectName: string) {
const { projectRoot } = getNxProjectRoot(tree, projectName);
const projectTsConfigs =
getConfigs(projectRoot)
.map((tsConfig) => path.relative(tree.root, tsConfig))
.join(',') ||
'/* could not detect tsconfig.json files, manually set them here */';
const fileContent = `
module.exports = {
extends: ['@tablecheck/eslint-config'],
parserOptions: {
project: [${projectTsConfigs}],
},
settings: {
'import/resolver': {
typescript: {
project: [${projectTsConfigs}],
},
},
},
rules: {},
};
`;
outputPrettyFile(path.join(projectRoot, '.eslintrc.cjs'), fileContent);
}
7 changes: 0 additions & 7 deletions packages/nx/src/generators/quality/files/.eslintrc.cjs

This file was deleted.

58 changes: 5 additions & 53 deletions packages/nx/src/generators/quality/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,16 @@ import {
addDependenciesToPackageJson,
Tree,
updateJson,
addProjectConfiguration,
readProjectConfiguration,
updateProjectConfiguration,
} from '@nx/devkit';
import { getNxProjectRoot } from '@tablecheck/frontend-utils';
import merge from 'lodash/merge';
import { PackageJson } from 'type-fest';

import { getLatestVersions } from '../../utils/dependencies';
import generateIcons from '../ts-carbon-icons/generator';
import generateFileTypes from '../ts-file-types/generator';
import { FileTypesGeneratorSchema } from '../ts-file-types/schema';
import generateConfig from '../ts-node-config/generator';

function updateProjectConfig(tree: Tree, projectName: string) {
const { projectSourceRoot, projectRoot } = getNxProjectRoot(
tree,
projectName,
);
const lintTarget = {
executor: '@tablecheck/nx:quality',
outputs: ['{options.outputFile}'],
options: {
lintFilePatterns: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'].map(
(pattern) =>
path.relative(projectRoot, path.join(projectSourceRoot, pattern)),
),
},
};
const lintFormatTarget = merge({}, lintTarget, {
options: {
fix: true,
},
});
try {
const projectConfig = readProjectConfiguration(tree, projectName);
updateProjectConfiguration(
tree,
projectName,
merge(projectConfig, {
targets: {
quality: lintTarget,
'quality:format': lintFormatTarget,
},
}),
);
} catch (e) {
console.error(
'Failed to detect existing project config, generating new project.json to run executors',
e,
);
addProjectConfiguration(tree, projectName, {
root: '.',
sourceRoot: 'src',
targets: {
quality: lintTarget,
'quality:format': lintFormatTarget,
},
});
}
}
import { generateEslintConfig } from './eslintConfig';
import { updateProjectConfig } from './projectConfig';

export async function qualityGenerator(
tree: Tree,
Expand All @@ -85,6 +34,8 @@ export async function qualityGenerator(
'@tablecheck/commitlint-config',
'@tablecheck/eslint-config',
'@tablecheck/prettier-config',
'@typescript-eslint/eslint-plugin',
'@typescript-eslint/parser',
]),
)();
updateJson(tree, 'package.json', (json: PackageJson) => {
Expand All @@ -105,6 +56,7 @@ export async function qualityGenerator(
path.relative(process.cwd(), tree.root),
{},
);
generateEslintConfig(tree, schema.project);
execSync('npx husky install', {
cwd: process.cwd(),
stdio: 'inherit',
Expand Down
61 changes: 61 additions & 0 deletions packages/nx/src/generators/quality/projectConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as path from 'path';

import {
Tree,
addProjectConfiguration,
readProjectConfiguration,
updateProjectConfiguration,
} from '@nx/devkit';
import { getNxProjectRoot } from '@tablecheck/frontend-utils';
import merge from 'lodash/merge';

export function updateProjectConfig(tree: Tree, projectName: string) {
const { projectSourceRoot, projectRoot } = getNxProjectRoot(
tree,
projectName,
);
const lintTarget = {
executor: '@tablecheck/nx:quality',
outputs: ['{options.outputFile}'],
options: {
lintFilePatterns: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'].map(
(pattern) =>
path.join(
'{projectRoot}',
path.relative(projectRoot, path.join(projectSourceRoot, pattern)),
),
),
},
};
const lintFormatTarget = merge({}, lintTarget, {
options: {
fix: true,
},
});
try {
const projectConfig = readProjectConfiguration(tree, projectName);
updateProjectConfiguration(
tree,
projectName,
merge(projectConfig, {
targets: {
quality: lintTarget,
'quality:format': lintFormatTarget,
},
}),
);
} catch (e) {
console.error(
'Failed to detect existing project config, generating new project.json to run executors',
e,
);
addProjectConfiguration(tree, projectName, {
root: '.',
sourceRoot: 'src',
targets: {
quality: lintTarget,
'quality:format': lintFormatTarget,
},
});
}
}

0 comments on commit 1b88453

Please sign in to comment.