Skip to content

Commit

Permalink
fix: jest+typescript setup, package.json order, other minor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
joelcox22 committed Mar 30, 2024
1 parent f6788bf commit e335c0d
Show file tree
Hide file tree
Showing 21 changed files with 374 additions and 151 deletions.
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Config } from 'jest';

const config: Config = {
"verbose": true
const config: Config = {
verbose: true,
};

export default config;
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@
"version": "0.0.0",
"private": true,
"description": "A personal opinionated linting tool.",
"keywords": [
"lint"
],
"author": "Joel Cox <[email protected]>",
"license": "0BSD",
"type": "module",
"scripts": {
"lint": "node packages/lint/index.js"
"lint": "node packages/lint/index.js",
"test": "jest"
},
"repository": {
"type": "git",
"url": "https://github.com/joelcox22/lint.git"
},
"keywords": [
"lint"
],
"author": "Joel Cox <[email protected]>",
"license": "0BSD",
"devDependencies": {
"@types/jest": "^.0.0",
"@types/jest": "^29.0.0",
"eslint": "^8.0.0",
"jest": "^29.7.0",
"semantic-release": "^23.0.2",
"semantic-release-monorepo": "^8.0.0",
"typescript": "^5.0.0",
"ts-jest": "^.0.0"
"ts-jest": "^29.0.0",
"@joelbot/eslint-config": "^1.0.0",
"@joelbot/lint": "^1.0.0"
},
"engines": {
"node": ">=18"
},
"workspaces": [
"packages/*"
],
"dependencies": {}
}
]
}
22 changes: 12 additions & 10 deletions packages/eslint-config/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import globals from 'globals';
import js from '@eslint/js';
import ts from 'typescript-eslint';
import json from 'eslint-plugin-json';
import markdown from 'eslint-plugin-markdown';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
Expand All @@ -14,7 +15,7 @@ const ignores = fs
.readFileSync('.gitignore', 'utf-8')
.split('\n')
.map((line) => line.trim())
.filter((line) => !line.startsWith('#') && line !== '');
.filter((line) => !line.startsWith('#') && line.trim() !== '');

debug('calculated eslint ignore list from .gitignore:', ignores);

Expand All @@ -23,6 +24,16 @@ const config = [
ignores,
},
js.configs.recommended,
...ts.configs.recommended,
{
files: ['**/*.{j,t}s'],
languageOptions: {
globals: {
...globals.browser,
...globals.node,
},
},
},
{
files: ['**/*.{jsx,tsx}'],
plugins: {
Expand Down Expand Up @@ -59,15 +70,6 @@ const config = [
processor: json.processors['.json'],
rules: json.configs.recommended.rules,
},
{
files: ['**/*.js', '**/*.ts'],
languageOptions: {
globals: {
...globals.browser,
...globals.node,
},
},
},
{
files: ['**/*.md'],
processor: markdown.processors.markdown,
Expand Down
30 changes: 9 additions & 21 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"name": "@joelbot/eslint-config",
"version": "0.0.0",
"description": "A personal opinionated eslint configuration.",
"private": true,
"main": "index.js",
"type": "module",
"description": "A personal opinionated eslint configuration.",
"author": "Joel Cox <[email protected]>",
"license": "0BSD",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/joelcox22/lint.git"
},
"dependencies": {
"@eslint/eslintrc": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^7.001",
"@typescript-eslint/parser": "^7.0.0",
"app-root-path": "^3.1.0",
"eslint": "^8.0.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -23,25 +26,10 @@
"eslint-plugin-react-hooks": "^4.6.0",
"flatted": "^3.3.1",
"globals": "^14.0.0",
"prettier": "^3.2.5"
"prettier": "^3.2.5",
"typescript-eslint": "^7.4.0"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/joelcox22/lint.git"
},
"private": false,
"devDependencies": {
"semantic-release": "^23.0.2"
},
"release": {
"branches": [
"main"
]
},
"publishConfig": {
"access": "public"
}
}
2 changes: 2 additions & 0 deletions packages/lint/config/editorconfig.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as util from '../util.js';

export const name = 'editorconfig';

export default function configure() {
const config = {
...util.readIni('.editorconfig'),
Expand Down
2 changes: 2 additions & 0 deletions packages/lint/config/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as fs from 'fs';

const debug = util.debug('configure-eslint');

export const name = 'eslint';

export default function configure() {
const packageJson = util.readJson('package.json');
if (packageJson.name === '@joelbot/lint') {
Expand Down
21 changes: 12 additions & 9 deletions packages/lint/config/force-devDependencies.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import * as root from 'app-root-path';
import * as path from 'path';
import * as util from '../util.js';

const debug = util.debug('configure-forced-devDependencies');
const debug = util.debug('force-devDependencies');

export const name = 'force-devDependencies';

export default function configureForcedDevDependencies() {
const rootPackageJson = util.readJson(path.join(root.path, 'package.json'));
const packageJson = util.readJson('package.json');
if (packageJson.name === '@joelbot/lint') {
debug("looks like you're working on @joelbot/lint repo, not forcing devDependencies to avoid screwing things up.");
return;
const force = {};
if (rootPackageJson.name !== '@joelbot/lint') {
force['@joelbot/eslint-config'] = '^1.0.0';
force['@joelbot/lint'] = '^1.0.0';
} else {
debug("skipping @joelbot/* dependencies because it looks like you're working on @joelbot/lint repo");
}
debug('updating eslint configuration files');
const force = {
'@joelbot/eslint-config': '^1.0.0',
'@joelbot/lint': '^1.0.0',
};
const optional = ['semantic-release', 'typescript', 'jest', '@types/jest'];
Object.entries(force).forEach(([name, version]) => {
if (packageJson.dependencies[name]) {
Expand Down
2 changes: 2 additions & 0 deletions packages/lint/config/gitignore.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as fs from 'fs';
import * as util from '../util.js';

export const name = 'gitignore';

export default function configureGitignore() {
const ignore = util.readLines('.gitignore');
const ignoreIfExists = ['node_modules', 'cdk.out', 'dist', 'lib', 'coverage', 'yarn-error.log', '.nyc_output', '.eslintcache', '.DS_Store', 'npm-debug.log', '.parcel-cache'];
Expand Down
80 changes: 55 additions & 25 deletions packages/lint/config/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
import * as path from 'path';
import * as glob from 'glob';
import root from 'app-root-path';
import * as packageJson from './packageJson.js';
import gitignore from './gitignore.js';
import eslint from './eslint.js';
import prettier from './prettier.js';
import vscode from './vscode.js';
import editorconfig from './editorconfig.js';
import markdown from './markdown.js';
import semanticRelease from './semantic-release.js';
import forceDevDependencies from './force-devDependencies.js';
import jest from './jest.js';
import * as gitignore from './gitignore.js';
import * as eslint from './eslint.js';
import * as prettier from './prettier.js';
import * as vscode from './vscode.js';
import * as editorconfig from './editorconfig.js';
import * as markdown from './markdown.js';
import * as semanticRelease from './semantic-release.js';
import * as forceDevDependencies from './force-devDependencies.js';
import * as jest from './jest.js';
import * as util from '../util.js';

const debug = util.debug('configure');

export default function configure() {
Object.entries({
prePackageJson: packageJson.pre,
forceDevDependencies,
jest,
gitignore,
eslint,
prettier,
vscode,
editorconfig,
markdown,
semanticRelease,
postPackageJson: packageJson.post,
}).forEach(([name, applyConfig]) => {
debug(`applying config changes for ${name}`);
applyConfig();
});
const initialDir = process.cwd();
try {
process.chdir(root.path);
debug('starting in directory', root.path);
const json = util.readJson('package.json');
const workspaces = json.workspaces;
const dirs = [root.path];
if (workspaces) {
const workspacePackages = workspaces.flatMap((pattern) => glob.sync(pattern + '/package.json'));
workspacePackages.forEach((file) => {
dirs.push(path.join(process.cwd(), file, '..'));
});
}
const plugins = [packageJson, forceDevDependencies, jest, gitignore, eslint, prettier, vscode, editorconfig, markdown, semanticRelease];
// eslint-disable-next-line no-inner-declarations
function exec(plugin, step) {
if (plugin[step]) {
if (plugin.workspacesSupported) {
for (const dir of dirs) {
debug('executing', step, 'step for', plugin.name, 'in', dir);
process.chdir(dir);
plugin[step](dir === root.path);
}
} else {
debug('executing', step, 'step for', plugin.name, 'in', root.path);
process.chdir(root.path);
plugin[step](true);
}
}
}
for (const plugin of plugins) {
exec(plugin, 'pre');
}
for (const plugin of plugins) {
exec(plugin, 'default');
}
const reversed = plugins.reverse();
for (const plugin of reversed) {
exec(plugin, 'post');
}
} finally {
process.chdir(initialDir);
}
}
4 changes: 3 additions & 1 deletion packages/lint/config/jest.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as util from '../util.js';
import * as fs from 'fs';

export const name = 'jest';

export default function configureJest() {
const packageJson = util.readJson('package.json');
let remove = ['jest.config.js', 'jest.config.ts', 'jest.config.cjs', 'jest.config.cts', 'jest.config.mjs', 'jest.config.mts', 'jest.config.json'];
if ('jest' in packageJson.devDependencies) {
const jestVersion = packageJson.devDependencies.jest;
const jestMajor = jestVersion.replace(/[^\d.].*/, '').split('.')[0];
const jestMajor = jestVersion.replace(/^[^\d]+/, '').split('.')[0];
const latestJestMajor = `^${jestMajor}.0.0`;
packageJson.devDependencies['@types/jest'] = latestJestMajor;
packageJson.devDependencies['ts-jest'] = latestJestMajor;
Expand Down
2 changes: 2 additions & 0 deletions packages/lint/config/markdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as util from '../util.js';

export const name = 'markdown';

export default function configure() {
const settings = {
...util.readJson('.markdownlint.json'),
Expand Down
25 changes: 24 additions & 1 deletion packages/lint/config/packageJson.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as util from '../util.js';

export const name = 'packageJson';

export const workspacesSupported = true;

export function pre() {
const packageJson = util.readJson('package.json');
packageJson.dependencies = packageJson.dependencies || {};
Expand All @@ -15,5 +19,24 @@ export function post() {
if (Object.keys(packageJson.devDependencies).length === 0) {
delete packageJson.devDependencies;
}
util.writeJson('package.json', packageJson);
util.writeJson(
'package.json',
util.sortKeys(packageJson, [
'name',
'version',
'private',
'main',
'description',
'keywords',
'author',
'license',
'type',
'scripts',
'repository',
'dependencies',
'devDependencies',
'optionalDependencies',
'peerDependencies',
]),
);
}
2 changes: 2 additions & 0 deletions packages/lint/config/prettier.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as util from '../util.js';

export const name = 'prettier';

export default function configure() {
const config = {
...util.readJson('.prettierrc'),
Expand Down
Loading

0 comments on commit e335c0d

Please sign in to comment.