Skip to content

Commit

Permalink
feat: standardise eslint and prettier configs
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyu2001 authored Sep 17, 2024
1 parent d99e479 commit b934a1b
Show file tree
Hide file tree
Showing 33 changed files with 839 additions and 776 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ jobs:

- name: Lint 🛀
run: pnpm lint

- name: Check for formatting 🧹
run: pnpm format:check
28 changes: 13 additions & 15 deletions apps/docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
import { defineConfig } from "vitepress";
import { scanDir } from "./utils";
import { defineConfig } from 'vitepress'
import { scanDir } from './utils'

export default defineConfig({
lang: "en-US",
title: "Starter Kitty",
description: "Common app components that are safe-by-default.",
lang: 'en-US',
title: 'Starter Kitty',
description: 'Common app components that are safe-by-default.',
markdown: { attrs: { disable: true } },
themeConfig: {
search: {
provider: "local",
provider: 'local',
},

// https://vitepress.dev/reference/default-theme-config
nav: [
{ text: "Home", link: "/" },
{ text: "Examples", link: "/examples" },
{ text: "API", link: "/api" },
{ text: 'Home', link: '/' },
{ text: 'Examples', link: '/examples' },
{ text: 'API', link: '/api' },
],

socialLinks: [
{ icon: "github", link: "https://github.com/opengovsg/starter-kitty" },
],
socialLinks: [{ icon: 'github', link: 'https://github.com/opengovsg/starter-kitty' }],

docFooter: {
prev: false,
next: false,
},

sidebar: {
"/examples/": scanDir("examples"),
"/api/": scanDir("api"),
'/examples/': scanDir('examples'),
'/api/': scanDir('api'),
},
},
ignoreDeadLinks: [
// ignore all localhost links
/^https?:\/\/localhost/,
],
});
})
42 changes: 20 additions & 22 deletions apps/docs/.vitepress/utils.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
import fs from "node:fs";
import path from "node:path";
import fs from 'node:fs'
import path from 'node:path'

export const scanDir = (dir: string) => {
let res = fs
.readdirSync(path.resolve(__dirname, `../${dir}`))
.filter((item) => !item.startsWith(".")) as string[];
let res = fs.readdirSync(path.resolve(__dirname, `../${dir}`)).filter(item => !item.startsWith('.')) as string[]
if (res) {
const arr = [];
const arr = []
for (let item of res) {
const parts = item.split(".");
const parts = item.split('.')

let currItem = arr;
let currentPath = "";
let currItem = arr
let currentPath = ''
while (parts.length > 2) {
const part = parts.shift();
currentPath += (currentPath === "" ? "" : ".") + part;
const found = currItem.find((item) => item.text === part);
const part = parts.shift()
currentPath += (currentPath === '' ? '' : '.') + part
const found = currItem.find(item => item.text === part)
if (found) {
currItem = found.items;
currItem = found.items
} else {
const newItem = {
text: part,
items: [],
link: path.join(dir, currentPath),
};
currItem.push(newItem);
currItem = newItem.items;
}
currItem.push(newItem)
currItem = newItem.items
}
}
const found = currItem.find((item) => item.text === parts[0]);
const found = currItem.find(item => item.text === parts[0])
if (!found)
currItem.push({
text: parts[0],
items: [],
link: path.join(dir, item),
});
})
}
return arr;
return arr
} else {
console.warn("No files found in the directory");
return [];
console.warn('No files found in the directory')
return []
}
};
}
8 changes: 5 additions & 3 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
"scripts": {
"dev": "vitepress dev",
"build:docs": "vitepress build",
"format": "prettier --write .",
"format:check": "prettier --check .",
"preview": "vitepress preview"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"vitepress": "^1.3.1",
"vue": "~3.4.31",
"@opengovsg/starter-kitty-validators": "workspace:*"
}
"vue": "~3.4.31"
},
"prettier": "@opengovsg/prettier-config-starter-kitty"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"lint": "turbo lint",
"test": "turbo test",
"ci:report": "turbo ci:report",
"format": "prettier --write \"**/*.{ts,tsx,md}\""
"format": "turbo format",
"format:check": "turbo format:check"
},
"devDependencies": {
"prettier": "^3.2.5",
Expand Down
29 changes: 29 additions & 0 deletions packages/eslint-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { resolve } = require("node:path");

const project = resolve(process.cwd(), "tsconfig.json");

/** @type {import("eslint").Linter.Config} */
module.exports = {
extends: ["opengovsg"],
ignorePatterns: ["dist/**/*", "vitest.config.ts", "vitest.setup.ts"],
plugins: ["import", "eslint-plugin-tsdoc"],
rules: {
"import/no-unresolved": "error",
"tsdoc/syntax": "error",
},
parser: "@typescript-eslint/parser",
parserOptions: {
project: "**/tsconfig.json",
},
settings: {
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".tsx"],
},
"import/resolver": {
typescript: {
alwaysTryTypes: true,
project: project,
},
},
},
};
20 changes: 20 additions & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@opengovsg/eslint-config-starter-kitty",
"private": true,
"files": [
"index.js"
],
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.56.0",
"eslint-config-opengovsg": "^3.0.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-tsdoc": "^0.3.0",
"prettier": "^2.8.4"
}
}
7 changes: 7 additions & 0 deletions packages/prettier-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
printWidth: 120,
semi: false,
singleQuote: true,
trailingComma: "all",
arrowParens: "avoid",
};
12 changes: 12 additions & 0 deletions packages/prettier-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@opengovsg/prettier-config-starter-kitty",
"private": true,
"files": [
"index.js"
],
"devDependencies": {
"eslint": "^8",
"prettier": "^2.8.4",
"typescript": "^5.3.3"
}
}
22 changes: 3 additions & 19 deletions packages/safe-fs/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
{
"extends": ["opengovsg"],
"ignorePatterns": ["dist/**/*", "vitest.config.ts", "vitest.setup.ts"],
"plugins": ["import", "eslint-plugin-tsdoc"],
"rules": {
"import/no-unresolved": "error",
"tsdoc/syntax": "error"
},
"root": true,
"extends": ["@opengovsg/eslint-config-starter-kitty/index.js"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "**/tsconfig.json"
"project": true,
},
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".tsx"]
},
"import/resolver": {
"typescript": {
"alwaysTryTypes": true,
"project": "**/tsconfig.json"
}
}
}
}
6 changes: 0 additions & 6 deletions packages/safe-fs/.prettierrc

This file was deleted.

17 changes: 6 additions & 11 deletions packages/safe-fs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,21 @@
"build:report": "api-extractor run --local --verbose",
"build:docs": "api-documenter markdown --input-folder ../../temp/ --output-folder ../../apps/docs/api/",
"lint": "eslint \"**/*.{js,jsx,ts,tsx}\" --cache",
"format": "prettier --write .",
"format:check": "prettier --check .",
"test": "vitest",
"ci:report": "api-extractor run --verbose"
},
"devDependencies": {
"@opengovsg/eslint-config-starter-kitty": "workspace:*",
"@opengovsg/prettier-config-starter-kitty": "workspace:*",
"@swc/core": "^1.6.13",
"@types/node": "^18.19.47",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.56.0",
"eslint-config-opengovsg": "^3.0.0",
"eslint-config-prettier": "^8.6.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-tsdoc": "^0.3.0",
"memfs": "^4.11.1",
"prettier": "^2.8.4",
"tsc-alias": "^1.8.10",
"tsup": "^8.1.0",
"typescript": "^5.4.5",
"vitest": "^2.0.2"
}
},
"prettier": "@opengovsg/prettier-config-starter-kitty"
}
10 changes: 4 additions & 6 deletions packages/safe-fs/src/__tests__/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('getter', () => {
const content = 'Async Hello, World!'

await new Promise<void>((resolve, reject) => {
sfs.writeFile(filePath, content, (err) => {
sfs.writeFile(filePath, content, err => {
if (err) reject(err)
else resolve()
})
Expand All @@ -105,14 +105,14 @@ describe('getter', () => {
const appendedContent = 'Appended async content'

await new Promise<void>((resolve, reject) => {
sfs.writeFile(filePath, initialContent, (err) => {
sfs.writeFile(filePath, initialContent, err => {
if (err) reject(err)
else resolve()
})
})

await new Promise<void>((resolve, reject) => {
sfs.appendFile(filePath, appendedContent, (err) => {
sfs.appendFile(filePath, appendedContent, err => {
if (err) reject(err)
else resolve()
})
Expand Down Expand Up @@ -179,9 +179,7 @@ describe('getter', () => {
const validPath = 'valid/nested/path.txt'
const newPath = 'valid/new.txt'

expect(() =>
sfs.mkdirSync('valid/nested', { recursive: true }),
).not.toThrow()
expect(() => sfs.mkdirSync('valid/nested', { recursive: true })).not.toThrow()
expect(() => sfs.writeFileSync(validPath, content)).not.toThrow()
expect(() => sfs.readFileSync(validPath)).not.toThrow()
expect(() => sfs.renameSync(validPath, newPath)).not.toThrow()
Expand Down
14 changes: 3 additions & 11 deletions packages/safe-fs/src/getter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ import fs from 'node:fs'
import PARAMS_TO_SANITIZE from '@/params'
import { sanitizePath } from '@/sanitizers'

type ReturnType<F extends CallableFunction> = F extends (
...args: infer A
) => infer R
? R
: never
type ReturnType<F extends CallableFunction> = F extends (...args: infer A) => infer R ? R : never

type FsFunction = Extract<(typeof fs)[keyof typeof fs], CallableFunction>

export const createGetter: (
basePath: string,
) => ProxyHandler<typeof fs>['get'] =
export const createGetter: (basePath: string) => ProxyHandler<typeof fs>['get'] =
(basePath: string) => (target: typeof fs, p: keyof typeof fs, receiver) => {
if (typeof target[p] === 'function') {
const func = Reflect.get(target, p, receiver) as FsFunction
Expand All @@ -28,9 +22,7 @@ export const createGetter: (
}
return arg
})
return (func as CallableFunction)(...sanitizedArgs) as ReturnType<
typeof func
>
return (func as CallableFunction)(...sanitizedArgs) as ReturnType<typeof func>
}
}
return func
Expand Down
12 changes: 2 additions & 10 deletions packages/safe-fs/src/sanitizers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ import path from 'node:path'

const LEADING_DOT_SLASH_REGEX = /^(\.\.(\/|\\|$))+/

export const sanitizePath = (
dangerousPath: PathLike,
rootPath: string,
): string => {
return path.join(
rootPath,
path
.normalize(dangerousPath.toString())
.replace(LEADING_DOT_SLASH_REGEX, ''),
)
export const sanitizePath = (dangerousPath: PathLike, rootPath: string): string => {
return path.join(rootPath, path.normalize(dangerousPath.toString()).replace(LEADING_DOT_SLASH_REGEX, ''))
}
Loading

0 comments on commit b934a1b

Please sign in to comment.