Skip to content

Instantly share code, notes, and snippets.

@vdelacou
Last active July 23, 2023 06:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdelacou/996f290aa5d108b55688229e15ff4b3b to your computer and use it in GitHub Desktop.
Save vdelacou/996f290aa5d108b55688229e15ff4b3b to your computer and use it in GitHub Desktop.
Start node project
{
"env": {
"node": true,
"es2020": true
},
"extends": [
"plugin:prettier/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint", "prettier"],
"rules": {}
}

How to install a typescript project

npm init private

curl -o .gitignore https://gist.githubusercontent.com/vdelacou/1954a25d720f702b4af011ba2773001b/raw/.gitignore
curl -o .prettierrc https://gist.githubusercontent.com/vdelacou/58484f1c11af70aaa457f4e5c289e893/raw/.prettierrc
mkdir .vscode
curl -o .vscode/extensions.json https://gist.githubusercontent.com/vdelacou/996f290aa5d108b55688229e15ff4b3b/raw/extensions.json
curl -o .vscode/settings.json https://gist.githubusercontent.com/vdelacou/996f290aa5d108b55688229e15ff4b3b/raw/settings.json
curl -o .editorconfig https://gist.githubusercontent.com/vdelacou/58484f1c11af70aaa457f4e5c289e893/raw/.editorconfig
npm install --save-dev typescript ts-node eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser prettier eslint-config-prettier eslint-plugin-prettier
npm install husky --save-dev
npx json -I -f package.json -e 'this.husky={"hooks":{"pre-commit": "npm run lint","pre-push": "npm run lint"}}'
npx json -I -f package.json -e 'this.scripts={...this.scripts , "lint": "eslint . --ext .js,.ts" }'
npx json -I -f package.json -e 'this.scripts={...this.scripts , "start": "ts-node ./src/example.ts" }'
npm install winston
npm install --save-dev @types/node
mkdir src
curl -o src/example.ts https://gist.githubusercontent.com/vdelacou/996f290aa5d108b55688229e15ff4b3b/raw/example.ts
curl -o .eslintrc.json https://gist.githubusercontent.com/vdelacou/996f290aa5d108b55688229e15ff4b3b/raw/.eslintrc.json
curl -o tsconfig.json https://gist.githubusercontent.com/vdelacou/996f290aa5d108b55688229e15ff4b3b/raw/tsconfig.json
npm install --save-dev jest @types/jest ts-jest
curl -o jest.config.js https://gist.githubusercontent.com/vdelacou/996f290aa5d108b55688229e15ff4b3b/raw/jest.config.js
mkdir src/lib
curl -o src/lib/log.ts https://gist.githubusercontent.com/vdelacou/996f290aa5d108b55688229e15ff4b3b/raw/log.ts
npx json -I -f package.json -e 'this.scripts={...this.scripts , "test": "jest" }'
curl -o src/example.test.ts https://gist.githubusercontent.com/vdelacou/996f290aa5d108b55688229e15ff4b3b/raw/example.test.ts
import { sum } from './example';
describe('Calc', () => {
test('should return 10 for add(6, 4)', () => {
expect(sum(6, 4)).toBe(10);
});
test('should return 9 for add(10, -1)', () => {
expect(sum(10, -1)).toBe(9);
});
});
import { newLogger } from './lib/log';
const logger = newLogger('password');
export const sum = (a: number, b: number): number => {
return a + b;
};
const delay = (milliseconds: number, a: number, b: number): Promise<number> => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(sum(a, b));
}, milliseconds);
});
};
(async () => {
logger.info('Start');
const count = await delay(1000, 2, 6);
logger.info(`End with count: ${count}`, { count });
})().catch((e) => {
logger.error(e);
});
{
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode", "editorconfig.editorconfig", "eamodio.gitlens", "orta.vscode-jest"]
}
module.exports = {
roots: ['<rootDir>/src'],
testMatch: ['**/__tests__/**/*.+(ts|js)', '**/?(*.)+(spec|test).+(ts|js)'],
transform: {
'^.+\\.(ts)$': 'ts-jest',
},
collectCoverageFrom: ['**/*.{js,ts}', '!**/*.d.ts', '!**/node_modules/**'],
globals: {
'ts-jest': {
tsconfig: 'tsconfig.json',
},
},
};
import winston from 'winston';
const { createLogger, format, transports } = winston;
export const newLogger = (service: string): winston.Logger => {
const logger = createLogger({
level: 'info',
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
format.ms(),
format.errors({ stack: true }),
format.splat(),
format.json()
),
defaultMeta: { service },
transports: [new transports.Console()],
});
return logger;
};
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"typescript.tsdk": "node_modules\\typescript\\lib",
"files.autoSave": "afterDelay",
"search.exclude": {
"**/node_modules": true,
"**/.vscode": true
},
"git.autofetch": true,
"javascript.format.enable": false,
"typescript.implementationsCodeLens.enabled": true,
"editor.trimAutoWhitespace": true,
"files.encoding": "utf8",
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,
"eslint.validate": ["javascript", "typescript"],
"[javascript]": {
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
},
"[typescript]": {
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
}
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"esModuleInterop": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment