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 }}
strategy:
matrix:
node: [ '13', '12', '11', '10', '8' ]
node: [ '14', '13', '12', '11', '10', '8' ]
name: Test with Node v${{ matrix.node }}
steps:
@ -23,6 +23,8 @@ jobs:
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm run lint:check
- run: npm run format:check
- run: npm run build
- 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.
- 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 ).
- 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).
@ -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)
- [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)
- [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)
- [Completely disable](#completely-disable)
- [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
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
### Completely disable

View file

@ -5,34 +5,32 @@
<i>#{DESC}#</i>
<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/npm/dw/#{REPO_NAME}#">
<img src="https://img.shields.io/npm/l/#{REPO_NAME}#">
</p>
<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}#">Documentation</a>
-
<a href="https://gitter.im/#{REPO_NAME}#/">Chat</a>
</p>
---
# Install / Import
```bash
> npm install --save #{REPO_NAME}#
$ npm install --save #{REPO_NAME}#
```
```typescript
import { myFunction, myObject } from '#{REPO_NAME}#';
import { myFunction, myObject } from "#{REPO_NAME}#";
```
Specific imports:
```typescript
import { myFunction } from '#{REPO_NAME}#/myFunction';
import { myObject } from '#{REPO_NAME}#/myObject';
import { myFunction } from "#{REPO_NAME}#/myFunction";
import { myObject } from "#{REPO_NAME}#/myObject";
```
## Import from HTML, with CDN
@ -50,11 +48,14 @@ Or import it as an ES module:
```html
<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>
```
*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

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",
"scripts": {
"test": "node ./dist/test",
"tsc": "npx tsc",
"cdn:bundle:.js": "simplifyify dist/index.js -s #{REPO_NAME_NO_DASHES}# -o dist/bundle.js --debug --bundle",
"cdn:bundle:.js": "simplifyify dist/index.js -s #{REPO_NAME}# -o dist/bundle.js --debug --bundle",
"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:esm": "tsc -p tsconfig.esm.json",
"cdn": "npm run cdn:bundle && npm run cdn:esm",
"build": "npm run tsc && npm run cdn",
"enable_short_import_path": "npm run build && npx denoify_enable_short_npm_import_path",
"clean": "rm -rf node_modules dist"
"build": "tsc && npm run cdn",
"enable_short_import_path": "npm run build && denoify_enable_short_npm_import_path",
"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}#",
"license": "MIT",
"homepage": "https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#",
"files": [
"/dist/*.{d.ts,js,js.map}",
"/dist/tools/",
@ -31,12 +47,20 @@
"/src/tools/"
],
"keywords": [],
"homepage": "https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#",
"devDependencies": {
"typescript": "^3.9.0",
"@types/node": "^10.0.0",
"denoify": "0.2.15",
"evt": "1.7.11",
"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 { myObject } from "./myObject";

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -2,7 +2,7 @@
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": [ "es2015" ],
"lib": ["es2015"],
"declaration": true,
"outDir": "./dist",
"sourceMap": true,
@ -13,11 +13,6 @@
"strict": true,
"downlevelIteration": true
},
"filesGlob": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist/**/*"
]
"filesGlob": ["src/**/*"],
"exclude": ["node_modules", "dist/**/*", "deno_dist/**/*", "mod.ts"]
}