Add eslint and prettier support

This commit is contained in:
Joseph Garrone 2020-05-27 22:02:44 +02:00
parent 95d413a2ce
commit 89f37d738b
20 changed files with 1971 additions and 170 deletions

5
.eslintignore Normal file
View file

@ -0,0 +1,5 @@
/node_modules/
/dist/
/deno_dist/
/mod.ts
/.eslintrc.js

16
.eslintrc.js Normal file
View file

@ -0,0 +1,16 @@
module.exports = {
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
],
"rules": {
"no-extra-boolean-cast": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
},
};

View file

@ -14,7 +14,7 @@ jobs:
if: ${{ !github.event.created }} if: ${{ !github.event.created }}
strategy: strategy:
matrix: matrix:
node: [ '13', '12', '11', '10', '8' ] node: [ '14', '13', '12', '11', '10', '8' ]
name: Test with Node v${{ matrix.node }} name: Test with Node v${{ matrix.node }}
steps: steps:
@ -23,6 +23,8 @@ jobs:
with: with:
node-version: ${{ matrix.node }} node-version: ${{ matrix.node }}
- run: npm ci - run: npm ci
- run: npm run lint:check
- run: npm run format:check
- run: npm run build - run: npm run build
- run: npm run test - run: npm run test

6
.prettierignore Normal file
View file

@ -0,0 +1,6 @@
/node_modules/
/dist/
/deno_dist/
/mod.ts
/.eslintrc.js
/docs/

11
.prettierrc.json Normal file
View file

@ -0,0 +1,11 @@
{
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"semi": true,
"singleQuote": false,
"quoteProps": "preserve",
"trailingComma": "all",
"bracketSpacing": true,
"arrowParens": "avoid"
}

View file

@ -28,6 +28,8 @@ Besides, good stuff that comes with using this template:
- A branch ``latest`` always in sync with the latest release. - A branch ``latest`` always in sync with the latest release.
- When your users hit *"Go to Definition"* they get redirected to the actual ``.ts`` source file instead of the ``.d.ts``. - When your users hit *"Go to Definition"* they get redirected to the actual ``.ts`` source file instead of the ``.d.ts``.
( Feature disabled by default, refer to [instructions](#enabling-go-to-definition-to-redirect-to-the-source-ts-file) on how to enable it ). ( Feature disabled by default, refer to [instructions](#enabling-go-to-definition-to-redirect-to-the-source-ts-file) on how to enable it ).
- Eslint and Prettifier are automatically run against files staged for commit. ( You can [disable](#disable-linting-and-formatting) this feature. )
If you want your module to support Deno as well checkout [denoify_ci](https://github.com/garronej/denoify_ci). If you want your module to support Deno as well checkout [denoify_ci](https://github.com/garronej/denoify_ci).
@ -41,6 +43,9 @@ If you want your module to support Deno as well checkout [denoify_ci](https://gi
- [Changing the directories structure](#changing-the-directories-structure) - [Changing the directories structure](#changing-the-directories-structure)
- [Enabling "Go to Definition" to redirect to the source ``.ts`` file](#enabling-go-to-definition-to-redirect-to-the-source-ts-file) - [Enabling "Go to Definition" to redirect to the source ``.ts`` file](#enabling-go-to-definition-to-redirect-to-the-source-ts-file)
- [Swipe the image in the ``README.md``](#swipe-the-image-in-the-readmemd) - [Swipe the image in the ``README.md``](#swipe-the-image-in-the-readmemd)
- [Disable linting and formatting](#disable-linting-and-formatting)
- [Disable Prettier](#disable-prettier)
- [Disable Eslint and Prettier altogether](#disable-eslint-and-prettier-altogether)
- [Disable CDN build](#disable-cdn-build) - [Disable CDN build](#disable-cdn-build)
- [Completely disable](#completely-disable) - [Completely disable](#completely-disable)
- [Only disable ES Module build ( ``dist/zz_esm/*`` )](#only-disable-es-module-build--distzz_esm-) - [Only disable ES Module build ( ``dist/zz_esm/*`` )](#only-disable-es-module-build--distzz_esm-)
@ -138,6 +143,61 @@ A good way to host your repo image is to open an issue named ASSET in your proje
While you are at it submit this image as *social preview* in your repos github page's settings so that when you share on While you are at it submit this image as *social preview* in your repos github page's settings so that when you share on
Twitter or Reddit you don't get your GitHub profile picture to show up. Twitter or Reddit you don't get your GitHub profile picture to show up.
## Disable linting and formatting
### Disable Prettier
[Prettier](https://prettier.io) is opinionated, it is OK to want to break free from it.
Remove these ``package.json``'s ``scripts``:
- ``_format``
- ``format``
- ``format:check``
Remove these ``package.json``'s ``devDependencies``:
- ``prettier``
- ``eslint-config-prettier``
In the ``package.json``'s ``lint-staged`` field remove ``"*.{`s,json,md}": [ "prettier --write" ]``
From ``.eslintrc.js``, remove the line: ``"prettier/@typescript-eslint",``.
Delete these files:
- ``.prettierignore``
- ``.prettierrc.json``
In ``.github/workflows/ci.yaml`` remove the line ``- run: npm run format:check``.
### Disable Eslint and Prettier altogether
Remove these ``package.json``'s ``scripts``:
- ``_format``
- ``format``
- ``format:check``
- ``lint:check``
- ``lint``
Remove these ``package.j`on``'s ``devDependencies``:
- ``prettier``
- ``eslint-config-prettier``
- ``eslint``
- ``@typescript-eslint/parser``
- ``@typescript-eslint/eslint-plugin``
- ``husky``
Remove the ``lint-staged`` and ``husky`` fields from the ``package.json``.
Delete these files:
- ``.prettierignore``
- ``.prettierrc.json``
- ``.eslintignore``
- ``.eslintrc.js``
Remove these lines from ``.github/workflows/ci.yaml``:
- ``- run: npm run lint:check``
- ``- run: npm run format:check``
## Disable CDN build ## Disable CDN build
### Completely disable ### Completely disable

View file

@ -5,34 +5,32 @@
<i>#{DESC}#</i> <i>#{DESC}#</i>
<br> <br>
<br> <br>
<img src="https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#/workflows/ci/badge.svg"> <img src="https://github.com/garronej/#{REPO_NAME}#/workflows/ci/badge.svg?branch=develop">
<img src="https://img.shields.io/bundlephobia/minzip/#{REPO_NAME}#"> <img src="https://img.shields.io/bundlephobia/minzip/#{REPO_NAME}#">
<img src="https://img.shields.io/npm/dw/#{REPO_NAME}#"> <img src="https://img.shields.io/npm/dw/#{REPO_NAME}#">
<img src="https://img.shields.io/npm/l/#{REPO_NAME}#">
</p> </p>
<p align="center"> <p align="center">
<a href="https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#">Home</a> <a href="https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#">Home</a>
- -
<a href="https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#">Documentation</a> <a href="https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#">Documentation</a>
-
<a href="https://gitter.im/#{REPO_NAME}#/">Chat</a>
</p> </p>
---
# Install / Import # Install / Import
```bash ```bash
> npm install --save #{REPO_NAME}# $ npm install --save #{REPO_NAME}#
``` ```
```typescript ```typescript
import { myFunction, myObject } from '#{REPO_NAME}#'; import { myFunction, myObject } from "#{REPO_NAME}#";
``` ```
Specific imports: Specific imports:
```typescript ```typescript
import { myFunction } from '#{REPO_NAME}#/myFunction'; import { myFunction } from "#{REPO_NAME}#/myFunction";
import { myObject } from '#{REPO_NAME}#/myObject'; import { myObject } from "#{REPO_NAME}#/myObject";
``` ```
## Import from HTML, with CDN ## Import from HTML, with CDN
@ -42,7 +40,7 @@ Import it via a bundle that creates a global ( wider browser support ):
```html ```html
<script src="//unpkg.com/#{REPO_NAME}#/bundle.min.js"></script> <script src="//unpkg.com/#{REPO_NAME}#/bundle.min.js"></script>
<script> <script>
const { myFunction, myObject } = #{REPO_NAME_NO_DASHES}#; const { myFunction, myObject } = #{REPO_NAME_NO_DASHES}#;
</script> </script>
``` ```
@ -50,11 +48,14 @@ Or import it as an ES module:
```html ```html
<script type="module"> <script type="module">
import { myFunction, myObject } from '//unpkg.com/#{REPO_NAME}#/zz_esm/index.js'; import {
myFunction,
myObject,
} from "//unpkg.com/#{REPO_NAME}#/zz_esm/index.js";
</script> </script>
``` ```
*You can specify the version you wish to import:* [unpkg.com](https://unpkg.com) _You can specify the version you wish to import:_ [unpkg.com](https://unpkg.com)
## Contribute ## Contribute

1841
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -10,19 +10,35 @@
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
"scripts": { "scripts": {
"test": "node ./dist/test", "test": "node ./dist/test",
"tsc": "npx tsc", "cdn:bundle:.js": "simplifyify dist/index.js -s #{REPO_NAME}# -o dist/bundle.js --debug --bundle",
"cdn:bundle:.js": "simplifyify dist/index.js -s #{REPO_NAME_NO_DASHES}# -o dist/bundle.js --debug --bundle",
"cdn:bundle:.min.js": "terser dist/bundle.js -cmo dist/bundle.min.js", "cdn:bundle:.min.js": "terser dist/bundle.js -cmo dist/bundle.min.js",
"cdn:bundle": "npm run cdn:bundle:.js && npm run cdn:bundle:.min.js", "cdn:bundle": "npm run cdn:bundle:.js && npm run cdn:bundle:.min.js",
"cdn:esm": "tsc -p tsconfig.esm.json", "cdn:esm": "tsc -p tsconfig.esm.json",
"cdn": "npm run cdn:bundle && npm run cdn:esm", "cdn": "npm run cdn:bundle && npm run cdn:esm",
"build": "npm run tsc && npm run cdn", "build": "tsc && npm run cdn",
"enable_short_import_path": "npm run build && npx denoify_enable_short_npm_import_path", "enable_short_import_path": "npm run build && denoify_enable_short_npm_import_path",
"clean": "rm -rf node_modules dist" "lint:check": "eslint . --ext .ts,.tsx",
"lint": "npm run lint:check -- --fix",
"_format": "prettier '**/*.{ts,tsx,json,md}'",
"format": "npm run _format -- --write",
"format:check": "npm run _format -- --list-different",
"clean": "rm -rf dist deno_dist node_modules mod.ts"
},
"lint-staged": {
"*.{ts,tsx}": [
"eslint --fix"
],
"*.{ts,tsx,json,md}": [
"prettier --write"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged -v"
}
}, },
"author": "u/#{USER_OR_ORG}#", "author": "u/#{USER_OR_ORG}#",
"license": "MIT", "license": "MIT",
"homepage": "https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#",
"files": [ "files": [
"/dist/*.{d.ts,js,js.map}", "/dist/*.{d.ts,js,js.map}",
"/dist/tools/", "/dist/tools/",
@ -31,12 +47,20 @@
"/src/tools/" "/src/tools/"
], ],
"keywords": [], "keywords": [],
"homepage": "https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#",
"devDependencies": { "devDependencies": {
"typescript": "^3.9.0", "typescript": "^3.9.0",
"@types/node": "^10.0.0", "@types/node": "^10.0.0",
"denoify": "0.2.15", "denoify": "0.2.15",
"evt": "1.7.11", "evt": "1.7.11",
"simplifyify": "8.0.1", "simplifyify": "8.0.1",
"terser": "4.6.13" "terser": "4.6.13",
"husky": "^4.2.1",
"prettier": "^1.19.1",
"eslint": "^7.1.0",
"@typescript-eslint/parser": "^3.0.1",
"@typescript-eslint/eslint-plugin": "^3.0.1",
"eslint-config-prettier": "^6.11.0",
"lint-staged": "^10.2.6"
} }
} }

View file

@ -1,3 +1,2 @@
export { myFunction } from "./myFunction"; export { myFunction } from "./myFunction";
export { myObject } from "./myObject"; export { myObject } from "./myObject";

View file

@ -1,2 +1,3 @@
export function myFunction() {
export const myFunction = ()=> Promise.resolve(["a", "b", "c"]); return Promise.resolve(["a", "b", "c"]);
}

View file

@ -1,4 +1,3 @@
import { toUpperCase } from "./tools/toUpperCase"; import { toUpperCase } from "./tools/toUpperCase";
export const myObject= { "p": toUpperCase("foo") }; export const myObject = { "p": toUpperCase("foo") };

View file

@ -1,5 +1,5 @@
import { getProjectRoot } from "../tools/getProjectRoot";
console.log(
import { getProjectRoot} from "../tools/getProjectRoot"; `Project root path: ${getProjectRoot()} does it seems right ? If yes then PASS`,
);
console.log(`Project root path: ${getProjectRoot()} does it seems right ? If yes then PASS`);

View file

@ -1,40 +1,33 @@
//This will not run on deno, we need a separate test runner for Deno (./mod.ts).
//This will not run on deno, we need a separate index to run our tests.
import * as child_process from "child_process"; import * as child_process from "child_process";
import * as path from "path"; import * as path from "path";
import { Deferred } from "evt/tools/Deferred"; import { Deferred } from "evt/tools/Deferred";
const names = ["myFunction", "myObject", "getProjectRoot"];
(async () => { (async () => {
if (!!process.env.FORK) { if (!!process.env.FORK) {
process.once("unhandledRejection", error => {
process.once("unhandledRejection", error => { throw error; }); throw error;
});
require(process.env.FORK); require(process.env.FORK);
return; return;
} }
for (const name of [ for (const name of names) {
"myFunction",
"myObject",
"getProjectRoot"
]) {
console.log(`Running: ${name}`); console.log(`Running: ${name}`);
const dExitCode = new Deferred<number>(); const dExitCode = new Deferred<number>();
child_process.fork( child_process
__filename, .fork(__filename, undefined, {
undefined, "env": { "FORK": path.join(__dirname, name) },
{ "env": { "FORK": path.join(__dirname, name) } } })
)
.on("message", console.log) .on("message", console.log)
.once("exit", code => dExitCode.resolve(code ?? 1)) .once("exit", code => dExitCode.resolve(code ?? 1));
;
const exitCode = await dExitCode.pr; const exitCode = await dExitCode.pr;
@ -44,7 +37,5 @@ import { Deferred } from "evt/tools/Deferred";
} }
console.log("\n"); console.log("\n");
} }
})(); })();

View file

@ -1,20 +1,16 @@
import { myFunction } from ".."; import { myFunction } from "..";
import { getPromiseAssertionApi } from "evt/tools/testing"; import { getPromiseAssertionApi } from "evt/tools/testing";
const { mustResolve } = getPromiseAssertionApi({ "takeIntoAccountArraysOrdering": true}); const { mustResolve } = getPromiseAssertionApi({
"takeIntoAccountArraysOrdering": true,
});
(async () => { (async () => {
await mustResolve({ await mustResolve({
"promise": myFunction(), "promise": myFunction(),
"expectedData": ["a", "b", "c"], "expectedData": ["a", "b", "c"],
"delay": 0 "delay": 0,
}); });
console.log("PASS"); console.log("PASS");
})(); })();

View file

@ -1,17 +1,7 @@
import { assert } from "evt/tools/typeSafety"; import { assert } from "evt/tools/typeSafety";
import * as inDepth from "evt/tools/inDepth"; import * as inDepth from "evt/tools/inDepth";
import { myObject } from ".."; import { myObject } from "..";
assert( assert(inDepth.same(myObject, { "p": "FOO" }));
inDepth.same(
myObject,
{ "p": "FOO" }
)
);
console.log("PASS"); console.log("PASS");

View file

@ -1,4 +1,3 @@
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
@ -6,17 +5,15 @@ function getProjectRootRec(dirPath: string): string {
if (fs.existsSync(path.join(dirPath, "package.json"))) { if (fs.existsSync(path.join(dirPath, "package.json"))) {
return dirPath; return dirPath;
} }
return getProjectRootRec(path.join(dirPath, "..")) return getProjectRootRec(path.join(dirPath, ".."));
} }
let result: string | undefined = undefined; let result: string | undefined = undefined;
export function getProjectRoot(): string { export function getProjectRoot(): string {
if (result !== undefined) { if (result !== undefined) {
return result; return result;
} }
return result = getProjectRootRec(__dirname); return (result = getProjectRootRec(__dirname));
} }

View file

@ -1,4 +1,3 @@
export function toUpperCase(str: string): string {
export function toUpperCase(str: string): string{
return str.toUpperCase(); return str.toUpperCase();
} }

View file

@ -3,6 +3,6 @@
"include": ["src/index.ts"], "include": ["src/index.ts"],
"compilerOptions": { "compilerOptions": {
"module": "es2015", "module": "es2015",
"outDir": "dist/zz_esm", "outDir": "dist/zz_esm"
} }
} }

View file

@ -1,23 +1,18 @@
{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"target": "es5", "target": "es5",
"lib": [ "es2015" ], "lib": ["es2015"],
"declaration": true, "declaration": true,
"outDir": "./dist", "outDir": "./dist",
"sourceMap": true, "sourceMap": true,
"newLine": "LF", "newLine": "LF",
"noUnusedLocals": true, "noUnusedLocals": true,
"noUnusedParameters": true, "noUnusedParameters": true,
"incremental": true, "incremental": true,
"strict": true, "strict": true,
"downlevelIteration": true "downlevelIteration": true
}, },
"filesGlob": [ "filesGlob": ["src/**/*"],
"src/**/*" "exclude": ["node_modules", "dist/**/*", "deno_dist/**/*", "mod.ts"]
],
"exclude": [
"node_modules",
"dist/**/*"
]
} }