Skip to content

Commit

Permalink
Merge pull request #22 from powerhouse-inc/feat-add-commit-lint-and-l…
Browse files Browse the repository at this point in the history
…int-staged

feat: add commit lint and lint staged
  • Loading branch information
ryanwolhuter authored Jan 19, 2024
2 parents f61d369 + b223826 commit bcf3185
Show file tree
Hide file tree
Showing 15 changed files with 1,548 additions and 64 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
node_modules/
54 changes: 54 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"env": {
"es2017": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/strict-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"tsconfigRootDir": ".",
"project": true
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/consistent-type-imports": "off",
"@typescript-eslint/array-type": "off",
"@typescript-eslint/prefer-function-type": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/prefer-nullish-coalescing": "off",
"@typescript-eslint/no-confusing-void-expression": "off",
"@typescript-eslint/consistent-type-definitions": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-misused-promises": "off",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/require-await": "warn",
"@typescript-eslint/no-unnecessary-condition": "warn",
"@typescript-eslint/no-base-to-string": "warn",
"@typescript-eslint/no-floating-promises": "warn",
"@typescript-eslint/restrict-template-expressions": "warn",
"@typescript-eslint/unbound-method": "warn",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-extraneous-class": "warn",
"@typescript-eslint/await-thenable": "warn",
"@typescript-eslint/no-dynamic-delete": "warn",
"@typescript-eslint/consistent-indexed-object-style": "warn",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/no-duplicate-type-constituents": "warn",
"@typescript-eslint/prefer-reduce-type-parameter": "warn",
"@typescript-eslint/no-unnecessary-type-assertion": "warn",
"@typescript-eslint/prefer-promise-reject-errors": "warn",
"@typescript-eslint/no-unused-vars": [
1,
{ "ignoreRestSiblings": true }
],
"@typescript-eslint/ban-ts-comment": "warn"
}
}
57 changes: 57 additions & 0 deletions .github/workflows/check-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Check PR

on:
push:
branches: ['main']
pull_request:
branches: ['main']
types: [opened, synchronize]

jobs:
lint:
name: Lint
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: 20
cache: 'yarn'

- name: Install dependencies
run: yarn --frozen-lockfile

- name: Lint
run: yarn lint

- name: Check types
run: yarn check-types

build:
name: Build
timeout-minutes: 15
runs-on: ubuntu-latest
needs: [lint]
steps:
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: 20
cache: 'yarn'

- name: Install dependencies
run: yarn --frozen-lockfile

- name: Build
run: yarn build
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"
5 changes: 5 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx tsc
npx lint-staged
3 changes: 3 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"./**/*.{js,jsx,ts,tsx,json}": ["eslint --fix"]
}
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"build": "yarn clean && tsc && yarn copy-files",
"build:create-lib": "tsc --project src/create-lib/tsconfig.json",
"publish:create-lib": "yarn publish --cwd ./src/create-lib/",
"start": "ts-node src/cli.ts"
"start": "ts-node src/cli.ts",
"check-types": "tsc --noEmit",
"lint": "eslint . && yarn check-types",
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@acaldas/graphql-codegen-typescript-validation-schema": "^0.12.3",
Expand All @@ -36,9 +39,19 @@
"typescript": "^5.3.3"
},
"devDependencies": {
"@commitlint/cli": "^18.4.4",
"@commitlint/config-conventional": "^18.4.4",
"@total-typescript/ts-reset": "^0.5.1",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"copyfiles": "^2.4.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"graphql": "^16.8.1",
"prettier": "^3.2.2",
"husky": "^8.0.3",
"lint-staged": "^15.2.0",
"prettier": "^3.2.4",
"prettier-plugin-organize-imports": "^3.2.4",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2"
Expand Down
1 change: 1 addition & 0 deletions reset.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@total-typescript/ts-reset';
4 changes: 4 additions & 0 deletions src/codegen/hygen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ async function run(args: string[], { watch = false, format = false } = {}) {
cwd: process.cwd(),
logger,
createPrompter: () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return require('enquirer');
},
exec: (action, body) => {
const opts = body && body.length > 0 ? { input: body } : {};
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
return require('execa').shell(action, opts);
},
debug: !!process.env.DEBUG,
});
if (format) {
const execa = await import('execa');
result.actions
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
.filter(action => ['added', 'inject'].includes(action.status))
.forEach(action => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
execa.$`prettier --ignore-path --write ${action.subject.replace(
'.',
process.cwd(),
Expand Down
6 changes: 4 additions & 2 deletions src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ function getDocumentTypesMap(dir: string) {
if (spec.id) {
documentTypesMap[spec.id] = pascalCase(name);
}
} catch {}
} catch {
console.error(`Failed to parse ${specPath}`);
}
});
return documentTypesMap;
}
Expand Down Expand Up @@ -105,7 +107,7 @@ export async function generateEditor(
const { documentModelsDir, format } = config;
const docummentTypesMap = getDocumentTypesMap(documentModelsDir);

let invalidType = documentTypes.find(
const invalidType = documentTypes.find(
type => !Object.keys(docummentTypesMap).includes(type),
);
if (invalidType) {
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export async function loadDocumentModel(
documentModel = file.state.global;
} else if (path.endsWith('.json')) {
const data = fs.readFileSync(path, 'utf-8');
const document = JSON.parse(data);
const document = JSON.parse(data) as DocumentModelState;
// z.DocumentModelStateSchema().parse(document);
documentModel = document;
} else {
throw new Error('File type not supported. Must be zip or json.');
}
return documentModel;
} catch (error) {
// @ts-ignore
// @ts-expect-error

Check warning on line 24 in src/codegen/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
throw error.code === 'MODULE_NOT_FOUND'
? new Error(`Document model not found.`)
: error;
Expand Down
16 changes: 9 additions & 7 deletions src/create-lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#! /usr/bin/env node

import { exec as _exec } from 'child_process';
import { prompt } from 'enquirer';
import fs from 'fs';
import path from 'path';
import util from 'util';
import fs from 'fs';
import { exec as _exec } from 'child_process';
import {
promptDirectories,
DEFAULT_CONFIG,
configSpec,
parseArgs,
promptDirectories,
} from '../utils';
import { prompt } from 'enquirer';

const exec = util.promisify(_exec);

const BOILERPLATE_REPO =
'https://github.com/powerhouse-inc/document-model-boilerplate.git';

function isUsingYarn() {
return (process.env.npm_config_user_agent || '').indexOf('yarn') === 0;
return (process.env.npm_config_user_agent || '').startsWith('yarn');
}

function buildPackageJson(appPath: string, projectName: string) {
const packageJson = JSON.parse(
fs.readFileSync(path.join(appPath, 'package.json'), 'utf-8'),
);
) as Record<string, any>;

Check warning on line 27 in src/create-lib/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const newPackage = {
...packageJson,
name: projectName,
Expand All @@ -45,7 +45,9 @@ function buildPowerhouseConfig(
editorsDir: string,
) {
const filePath = path.join(appPath, 'powerhouse.config.json');
const packageJson = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
const packageJson = JSON.parse(
fs.readFileSync(filePath, 'utf-8'),
) as Record<string, any>;
const newPackage = {
...packageJson,
documentModelsDir,
Expand Down
12 changes: 7 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generateMock as zodGenerateMock } from '@anatine/zod-mock';
import arg from 'arg';
import { prompt } from 'enquirer';
import arg, { type Spec } from 'arg';
import { readFileSync, writeFileSync } from 'node:fs';
import { generateMock as zodGenerateMock } from '@anatine/zod-mock';

export type PowerhouseConfig = {
documentModelsDir: string;
Expand Down Expand Up @@ -34,9 +34,11 @@ export function getConfig() {
let config: PowerhouseConfig = { ...DEFAULT_CONFIG };
try {
const configStr = readFileSync('./powerhouse.config.json', 'utf-8');
let userConfig: PowerhouseConfig = JSON.parse(configStr);
const userConfig = JSON.parse(configStr) as PowerhouseConfig;
config = { ...config, ...userConfig };
} catch {}
} catch {
console.warn('No powerhouse.config.json found, using defaults');
}
return config;
}

Expand Down Expand Up @@ -100,5 +102,5 @@ export async function promptDirectories(
export type generateMockTypeFn = typeof zodGenerateMock;

export const generateMock: generateMockTypeFn = (zodRef, options) => {
return zodGenerateMock(zodRef, options);
return zodGenerateMock(zodRef, options) as generateMockTypeFn;
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"experimentalDecorators": true,
"declaration": true
},
"include": ["src/**/*.ts", "src/**/.hygen/**/*.ts"]
"include": ["src/**/*.ts", "src/**/.hygen/**/*.ts", "reset.d.ts"]
}
Loading

0 comments on commit bcf3185

Please sign in to comment.