Skip to content

Commit

Permalink
Add templates for Node.js, CRA, Next.js, Expo (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
karlhorky committed Oct 29, 2023
1 parent f01c94c commit 75ed4d8
Show file tree
Hide file tree
Showing 34 changed files with 213 additions and 60 deletions.
2 changes: 1 addition & 1 deletion __tests__/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6",
"eslint-config-upleveled": "^5.1.4",
"eslint-config-upleveled": "^6.0.0-0",
"postcss-styled-syntax": "^0.5.0",
"stylelint": "^15.9.0",
"stylelint-config-upleveled": "^1.0.2",
Expand Down
8 changes: 4 additions & 4 deletions __tests__/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions __tests__/prettier.config.cjs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ const config = {
trailingComma: 'all',
};

module.exports = config;
export default config;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ const config = {
extends: ['stylelint-config-upleveled'],
};

module.exports = config;
export default config;
71 changes: 34 additions & 37 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@ const projectPackageJson = JSON.parse(
const projectDependencies = projectPackageJson.dependencies || {};
const projectDevDependencies = projectPackageJson.devDependencies || {};


const [projectType, projectTypeTitle] =
'postgres' in projectDependencies
? ['next-js-postgresql', 'Next.js with PostgreSQL']
: 'next' in projectDependencies
? ['next-js', 'Next.js']
: '@upleveled/react-scripts' in projectDependencies
? ['create-react-app', 'Create React App']
: 'expo' in projectDependencies
? ['expo', 'Expo (React Native)']
: ['node-js', 'Node.js'];

console.log(`Detected project type: ${projectTypeTitle}`);

// Commented out in case we need to patch Next.js again in the future
// if ('next' in projectDependencies) {
// // Remove previous patches in package.json
// if (projectPackageJson?.pnpm?.patchedDependencies) {
Expand All @@ -31,7 +44,7 @@ const projectDevDependencies = projectPackageJson.devDependencies || {};
// );
// }
// }

//
// writeFileSync(
// projectPackageJsonPath,
// JSON.stringify(projectPackageJson, null, 2) + '\n',
Expand All @@ -51,7 +64,7 @@ const newDevDependenciesToInstall = [

if (
// Install SafeQL dependencies in Postgres.js projects
'postgres' in projectDependencies &&
projectType === 'next-js-postgresql' &&
// SafeQL currently not supported on Windows
// https://github.com/ts-safeql/safeql/issues/80
process.platform !== 'win32'
Expand All @@ -60,8 +73,9 @@ if (
}

if (
'@upleveled/react-scripts' in projectDependencies ||
'next' in projectDependencies
projectType === 'create-react-app' ||
projectType === 'next-js' ||
projectType === 'next-js-postgresql'
) {
newDevDependenciesToInstall.push(
'@types/react',
Expand Down Expand Up @@ -101,6 +115,7 @@ const templatePath = resolve(
dirname(fileURLToPath(import.meta.url)),
'..',
'templates',
projectType,
);

const templateFileNamesAndPaths =
Expand Down Expand Up @@ -140,42 +155,22 @@ for (const {
name: templateFileName,
path: templateFilePath,
} of templateFileNamesAndPaths) {
// Don't copy create-react-app types for non-create-react-app / non-Next.js projects
if (
templateFileName === 'react-app-env.d.ts' &&
!('@upleveled/react-scripts' in projectDependencies)
) {
continue;
}

// Don't copy Stylelint config for projects not using create-react-app or Next.js
if (
['.vscode/settings.json', 'stylelint.config.cjs'].includes(
templateFileName,
) &&
!(
'@upleveled/react-scripts' in projectDependencies ||
'next' in projectDependencies
)
) {
continue;
}

const filePathInProject = join(process.cwd(), templateFileName);

let overwriteExistingFile = false;

if (existsSync(filePathInProject)) {
// Always overwrite prettier.config.mjs in Postgres.js projects
if (
templateFileName === 'prettier.config.mjs' &&
projectType === 'next-js-postgresql'
) {
overwriteExistingFile = true;
}

// Always overwrite tsconfig.json
if (templateFileName === 'tsconfig.json') {
const projectTsconfigJson = JSON.parse(
readFileSync(join(process.cwd(), 'tsconfig.json'), 'utf-8')
// Remove comments from tsconfig.json
.replace(/^\s*\/\/.*/gm, ''),
);

if ('plugins' in (projectTsconfigJson.compilerOptions || {})) {
overwriteExistingFile = true;
}
overwriteExistingFile = true;
}

if (!overwriteExistingFile) {
Expand All @@ -200,8 +195,9 @@ console.log('✅ Done copying config files');

try {
if (
(projectType === 'next-js' || projectType === 'next-js-postgresql') &&
readFileSync(join(process.cwd(), 'jsconfig.json'), 'utf-8').trim() ===
`{
`{
"compilerOptions": {
"paths": {
"@/*": ["./*"]
Expand Down Expand Up @@ -246,7 +242,8 @@ writeFileSync(

console.log('✅ Done updating .gitignore');

// if ('next' in projectDependencies) {
// Commented out in case we need to patch Next.js again in the future
// if (projectType === 'next-js' || projectType === 'next-js-postgresql') {
// const patchesPath = join(process.cwd(), 'patches');

// // Remove previous patch files
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-config-upleveled",
"version": "5.1.4",
"version": "6.0.0-0",
"description": "UpLeveled ESLint defaults for programming in JavaScript, TypeScript, React, Next.js, Node.js, Postgres.js",
"keywords": [
"eslint",
Expand Down
2 changes: 1 addition & 1 deletion prettier.config.cjs → prettier.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ const config = {
trailingComma: 'all',
};

module.exports = config;
export default config;
9 changes: 0 additions & 9 deletions templates/.eslintignore

This file was deleted.

3 changes: 3 additions & 0 deletions templates/create-react-app/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# /build is default build directory for `@upleveled/react-scripts`
# Ref: https://github.com/upleveled/create-react-app/pull/2
build
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions templates/create-react-app/prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('prettier').Options} */
const config = {
singleQuote: true,
trailingComma: 'all',
};

export default config;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ const config = {
extends: ['stylelint-config-upleveled'],
};

module.exports = config;
export default config;
13 changes: 13 additions & 0 deletions templates/create-react-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "eslint-config-upleveled/tsconfig.base.json",
"include": [
"**/.eslintrc.cjs",
"**/*.ts",
"**/*.tsx",
"**/*.js",
"**/*.jsx",
"**/*.cjs",
"**/*.mjs"
]
}
6 changes: 6 additions & 0 deletions templates/expo/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('@typescript-eslint/utils').TSESLint.Linter.Config} */
const config = {
extends: ['upleveled'],
};

module.exports = config;
7 changes: 7 additions & 0 deletions templates/expo/prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('prettier').Options} */
const config = {
singleQuote: true,
trailingComma: 'all',
};

export default config;
20 changes: 20 additions & 0 deletions templates/expo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "eslint-config-upleveled/tsconfig.base.json",
"compilerOptions": {
// Based on expo/tsconfig.base.json
// https://github.com/expo/expo/blob/main/packages/expo/tsconfig.base.json
"jsx": "react-native",
"target": "ESNext"
},
"include": [
"**/.eslintrc.cjs",
"**/*.ts",
"**/*.tsx",
"**/*.js",
"**/*.jsx",
"**/*.cjs",
"**/*.mjs"
],
"exclude": ["node_modules"]
}
2 changes: 2 additions & 0 deletions templates/next-js-postgresql/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Next.js build folder
.next
6 changes: 6 additions & 0 deletions templates/next-js-postgresql/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('@typescript-eslint/utils').TSESLint.Linter.Config} */
const config = {
extends: ['upleveled'],
};

module.exports = config;
12 changes: 12 additions & 0 deletions templates/next-js-postgresql/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
// Ignore unknown CSS at rules for Tailwind CSS
"css.lint.unknownAtRules": "ignore",
// Enable Stylelint in javascript and typescriptreact (for CSS-in-JS) and scss
"stylelint.validate": [
"css",
"scss",
"postcss",
"javascript",
"typescriptreact"
]
}
7 changes: 7 additions & 0 deletions templates/next-js-postgresql/prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('prettier').Options} */
const config = {
singleQuote: true,
trailingComma: 'all',
};

export default config;
6 changes: 6 additions & 0 deletions templates/next-js-postgresql/stylelint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type { import('stylelint').Config } */
const config = {
extends: ['stylelint-config-upleveled'],
};

export default config;
File renamed without changes.
2 changes: 2 additions & 0 deletions templates/next-js/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Next.js build folder
.next
6 changes: 6 additions & 0 deletions templates/next-js/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('@typescript-eslint/utils').TSESLint.Linter.Config} */
const config = {
extends: ['upleveled'],
};

module.exports = config;
12 changes: 12 additions & 0 deletions templates/next-js/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
// Ignore unknown CSS at rules for Tailwind CSS
"css.lint.unknownAtRules": "ignore",
// Enable Stylelint in javascript and typescriptreact (for CSS-in-JS) and scss
"stylelint.validate": [
"css",
"scss",
"postcss",
"javascript",
"typescriptreact"
]
}
7 changes: 7 additions & 0 deletions templates/next-js/prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('prettier').Options} */
const config = {
singleQuote: true,
trailingComma: 'all',
};

export default config;
6 changes: 6 additions & 0 deletions templates/next-js/stylelint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type { import('stylelint').Config } */
const config = {
extends: ['stylelint-config-upleveled'],
};

export default config;
21 changes: 21 additions & 0 deletions templates/next-js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "eslint-config-upleveled/tsconfig.base.json",
"compilerOptions": {
"plugins": [
{
"name": "next"
}
]
},
"include": [
"**/.eslintrc.cjs",
"**/*.ts",
"**/*.tsx",
"**/*.js",
"**/*.jsx",
"**/*.cjs",
"**/*.mjs",
".next/types/**/*.ts"
]
}
6 changes: 6 additions & 0 deletions templates/node-js/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('@typescript-eslint/utils').TSESLint.Linter.Config} */
const config = {
extends: ['upleveled'],
};

module.exports = config;
7 changes: 7 additions & 0 deletions templates/node-js/prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('prettier').Options} */
const config = {
singleQuote: true,
trailingComma: 'all',
};

export default config;

0 comments on commit 75ed4d8

Please sign in to comment.