Skip to content

Instantly share code, notes, and snippets.

@adhamankar
Created March 21, 2020 07:38
Show Gist options
  • Save adhamankar/9f189e37478493ea7507dc8d40c8c7ac to your computer and use it in GitHub Desktop.
Save adhamankar/9f189e37478493ea7507dc8d40c8c7ac to your computer and use it in GitHub Desktop.
  • mkdir my-typescript-package && cd my-typescript-package
  • Create a git repository
    git init  
    echo "# My typescript package" >> README.md 
    git add . && git commit -m "Initial commit" 
    git remote add origin < Git Repository Url >
    git push -u origin master
  • Init your Package (to generate package.json)
    npm init -y
  • add .gitignore file to the root.
    echo "node_modules" >> .gitignore
  • Add Typescript as a DevDependency
    npm install --save-dev typescript
  • to compile Typescript we also need a tsconfig.json
    {
        "compilerOptions": {
        "target": "es2015",
        "module": "commonjs",
        "declaration": true,
        "outDir": "./lib",
        "strict": true
        },
        "include": ["src"],
        "exclude": ["node_modules", "**/__tests__/*"]
    }

Start code

  • create index.ts file
    export const PackagePrint = (name: string) => `Printing ${name}`; 
  • update package.json
    "build" : "tsc"
  • Ignore compiled code in git by adding /lib ot .gitignore file
  • Formatting and Linting
    npm install --save-dev prettier tslint tslint-config-prettier
  • In the root, add a tslint.json
    {
        "extends": ["tslint:recommended", "tslint-config-prettier"]
    }
  • And a .prettierrc
    {
        "printWidth": 120,
        "trailingComma": "all",
        "singleQuote": true
    }
  • update package.json scripts section
    "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
    "lint": "tslint -p tsconfig.json"
  • whitelist the files /folders you want to publish by adding files property in package.json
    "files": ["lib/**/*"]
  • Setup testing with jest
    npm install --save-dev jest ts-jest @types/jest
  • Create a new file in the root and name it jestconfig.json
    {
        "transform": {
            "^.+\\.(t|j)sx?$": "ts-jest"
        },
        "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
        "moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
    }
  • Remove the old test script in package.json and change it to:
    "test": "jest --config jestconfig.json"

Write a basic test

In the src folder, add a new folder called tests and inside, add a new file with a name you like, but it has to end with test.ts, for example Greeter.test.ts

    import { PackagePrint } from '../index';
    test('My package printer', () => {
    expect(PackagePrint('my typescript package')).toBe('Printing my typescript package');
    });

Run tests

    npm test

Use the magic scripts in NPM

  • Add scripts in package.json : prepare, prepublishOnly, preversion, version and postversion
    "prepare" : "npm run build",
    "prepublishOnly" : "npm test && npm run lint",
    "preversion" : "npm run lint",
    "version" : "npm run format && git add -A src",
    "postversion" : "git push && git push --tags"

Finishing up package.json

    {
    "name": "my-typescript-package",
    "version": "1.0.0",
    "description": "A bare minimal package",
    "main": "lib/index.js",
    "types": "lib/index.d.ts",
    "scripts": {
        "test": "jest --config jestconfig.json",
        "build": "tsc",
        "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
        "lint": "tslint -p tsconfig.json",
        "prepare": "npm run build",
        "prepublishOnly": "npm test && npm run lint",
        "preversion": "npm run lint",
        "version": "npm run format && git add -A src",
        "postversion": "git push && git push --tags"
    },
    "keywords": ["Hello", "PackagePrinter"],
    "author": "adhamankar",
    ....

publish package

    npm login
    npm publish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment