Best Practices

When starting a new project

  • Use Lerna
  • Use typescript
  • Use eslint
  • Use prettier
  • Use jest
  • Use husky
    • Used to lint files

Lerna

1
npx lerna init
2
npx lerna bootstrap --npm-client=yarn --use-workspaces
Copied!
Update lerna.json
lerna.json
1
{
2
"npmClient": "yarn",
3
"useWorkspaces": "true"
4
}
Copied!
Install
1
yarn install
Copied!
Update package.json
package.json
1
{
2
"scripts": {
3
"lint": "yarn run lerna run lint"
4
},
5
"workspaces": ["packages/*"]
6
}
Copied!
package.json when creating a new package:
packages/new-package/package.json
1
{
2
"name": "new-package",
3
"private": true,
4
"version": "0.0.0"
5
}
Copied!

Package Management

Each package manages it's own typescript and eslint.
For example, at the root directory.
1
yarn lerna run lint
Copied!

Use JSON5 over JSON

The JSON5 Data Interchange Format (JSON5) is a superset of JSON that aims to alleviate some of the limitations of JSON by expanding its syntax to include some productions from ECMAScript 5.1.
1
yarn add json5
Copied!
JSON5 Short Example:
1
{
2
// comments
3
unquoted: 'and you can quote me on that',
4
singleQuotes: 'I can use "double quotes" here',
5
lineBreaks: "Look, Mom! \
6
No \\n's!",
7
newLine: "1\n\
8
2\n\
9
3",
10
hexadecimal: 0xdecaf,
11
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
12
positiveSign: +1,
13
trailingComma: 'in objects', andIn: ['arrays',],
14
"backwardsCompatible": "with JSON",
15
}
Copied!

Package Recommendations

commander: command-line arguments

1
yarn add commander
Copied!

dotenv

1
yarn add dotenv
Copied!
As early as possible:
JavaScript:
1
require('dotenv').config()
Copied!
TypeScript:
1
ts-node -r dotenv/config scripts/test.sh
Copied!
Or
1
import { config: dotenv } from "dotenv"
2
dotenv();
Copied!

fs-extra

fs-extra adds file system methods that aren't included in the native fs module and adds promise support to the fs methods. It also uses graceful-fs to prevent EMFILEerrors. It should be a drop in replacement for fs.
1
yarn add fs-extra
Copied!
Last modified 2yr ago