Skip to content

Commit 447ef37

Browse files
committed
add CI; move prod to dist/; update readme
1 parent 3218601 commit 447ef37

15 files changed

+6481
-4765
lines changed

.eslintignore

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
*.config.ts
33
*.test.js
44
*.test.ts
5-
cjs
6-
esm
7-
umd
8-
dist
9-
types
5+
dist/cjs
6+
dist/esm
7+
dist/umd
8+
dist/types

.github/workflows/publish.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Publish
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- name: Setup .npmrc file to publish to npm
13+
uses: actions/setup-node@v1
14+
with:
15+
node-version: '10.x'
16+
registry-url: 'https://registry.npmjs.org'
17+
- name: Install modules
18+
run: npm install
19+
- name: Build
20+
run: npm run build
21+
- name: Publish to npm
22+
run: npm publish --access public
23+
env:
24+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
25+
- name: Setup .npmrc file to publish to GitHub Packages
26+
uses: actions/setup-node@v1
27+
with:
28+
registry-url: 'https://npm.pkg.github.com'
29+
scope: '@tomchen'
30+
- name: Publish to GitHub Packages
31+
run: npm publish
32+
env:
33+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
9+
jobs:
10+
build:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
max-parallel: 4
14+
matrix:
15+
os:
16+
- ubuntu-latest
17+
- macos-latest
18+
- windows-latest
19+
node: [10.x, 12.x, 15.x]
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
- name: Set up Node.js
24+
uses: actions/setup-node@v1
25+
with:
26+
node-version: ${{ matrix.node }}
27+
- name: Install modules
28+
run: yarn
29+
- name: Run tests
30+
run: yarn test

.gitignore

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
cjs
2-
esm
3-
umd
4-
# dist
5-
types
1+
dist/cjs
2+
dist/esm
3+
dist/umd
4+
dist/types
65

76

87
# Logs

README.md

+99-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,112 @@
1-
# Example TypeScript Package ready to be published on npm
1+
# Example TypeScript Package ready to be published on npm for 2021
22

3-
Example TypeScript Package ready to be published on npm.
3+
[![npm package](https://img.shields.io/badge/npm%20i-example--typescript--package-brightgreen)](https://www.npmjs.com/package/example-typescript-package) [![version number](<![npm](https://img.shields.io/npm/v/example-typescript-package?color=green&label=version)>)](https://github.com/tomchen/example-typescript-package/releases) [![Actions Status](https://github.com/tomchen/example-typescript-package/workflows/Test/badge.svg)](https://github.com/tomchen/example-typescript-package/actions) [![License](https://img.shields.io/github/license/tomchen/example-typescript-package)](https://github.com/tomchen/example-typescript-package/blob/master/LICENSE)
44

5-
It generates CommonJS (in **cjs** folder), ES Modules (in **esm** folder), bundled and minified UMD (in **umd** folder), as well as TypeScript declaration files (in **types** folder).
5+
This is an example TypeScript Package ready to be published on npm. It has been set up with automated tests and package publishing workflow using GitHub Actions CI/CD. It is made primarily for GitHub<a href="https://github.com/" title="Github"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/github-icon.svg" alt="Github" width="21px" height="21px"></a> + VS Code<a href="https://code.visualstudio.com/" title="Visual Studio Code"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/visual-studio-code.svg" alt="Visual Studio Code" width="21px" height="21px"></a> (Windows<a href="https://www.microsoft.com/windows" title="Windows"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/microsoft-windows.svg" alt="Windows" width="21px" height="21px"></a> / Mac<a href="https://www.apple.com/macos/" title="Mac OS"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/macOS.svg" alt="Mac OS" width="21px" height="21px"></a> / Linux<a href="https://www.linuxfoundation.org/" title="Linux"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/linux-tux.svg" alt="Linux" width="21px" height="21px"></a>) users who are about to write and publish their first TypeScript npm package. This package could serve as a starter / boilerplate / demo for them.
66

7-
It uses yarn, TypeScript compiler, jest, webpack, eslint, prettier, and primarily designed to be used with VS Code.
7+
It uses npm<a href="https://www.npmjs.com/" title="npm"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/npm.svg" alt="npm" width="21px" height="21px"></a>, TypeScript compiler<a href="https://www.typescriptlang.org/" title="Typescript"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/typescript-icon.svg" alt="Typescript" width="21px" height="21px"></a>, Jest<a href="https://jestjs.io/" title="Jest"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/jest.svg" alt="Jest" width="21px" height="21px"></a>, webpack<a href="https://webpack.js.org/" title="webpack"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/webpack.svg" alt="webpack" width="21px" height="21px"></a>, ESLint<a href="https://eslint.org/" title="ESLint"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/eslint.svg" alt="ESLint" width="21px" height="21px"></a>, Prettier<a href="https://prettier.io/" title="Prettier"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/prettier.svg" alt="Prettier" width="21px" height="21px"></a>.
8+
9+
The production files include CommonJS, ES Modules, UMD version and TypeScript declaration files.
810

911
## Development
1012

13+
You need to have [Node.js](https://nodejs.org/en/download/) installed. Node includes npm as its default package manager.
14+
15+
Open the whole package folder with a good code editor, preferably [Visual Studio Code](https://code.visualstudio.com/download). Consider installing VS Code extensions [ES Lint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) and [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode).
16+
17+
In the VS Code top menu: **Terminal** -> **New Terminal**
18+
19+
### Install dependencies
20+
21+
Install dependencies with npm:
22+
23+
```bash
24+
npm i
25+
```
26+
27+
### Write your code
28+
29+
Make necessary changes in **package.json** (name, version, description, keywords, author, homepage and other URLs).
30+
31+
Write your code in **src** folder, and unit test in **test** folder, replacing the original files there.
32+
33+
The VS Code shortcuts for formatting of a code file are: <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>F</kbd> (Windows); <kbd>Shift</kbd> + <kbd>Option (Alt)</kbd> + <kbd>F</kbd> (MacOS); <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>I</kbd> (Linux).
34+
35+
Change code linting and formatting settings in **.prettierrc.js** if you want.
36+
37+
### Test
38+
39+
Test your code with Jest framework:
40+
41+
```bash
42+
npm run test
43+
```
44+
45+
### Build
46+
47+
Build production (distribution) files in your **dist** folder:
48+
1149
```bash
12-
yarn
13-
yarn build
50+
npm run build
1451
```
1552

53+
It generates CommonJS (in **dist/cjs** folder), ES Modules (in **dist/esm** folder), bundled and minified UMD (in **dist/umd** folder), as well as TypeScript declaration files (in **dist/types** folder).
54+
55+
### Try it before publishing
56+
57+
Run:
58+
59+
```bash
60+
npm link
61+
```
62+
63+
[npm link](https://docs.npmjs.com/cli/v6/commands/npm-link) will create a symlink in the global folder, which may be **{prefix}/lib/node_modules/example-typescript-package** or **C:\Users\<username>\AppData\Roaming\npm\node_modules\example-typescript-package**.
64+
65+
Create an empty folder elsewhere, you don't even need to `npm init` (to generate **package.json**). Open the folder with VS Code, open a terminal and just run:
66+
67+
```bash
68+
npm link example-typescript-package
69+
```
70+
71+
This will create a symbolic link from globally-installed example-typescript-package to **node_modules/** of the current folder.
72+
73+
You can then create a, for example, **testnum.ts** file with the content:
74+
75+
```ts
76+
import { Num } from 'example-typescript-package'
77+
console.log(new Num(5).add(new Num(6)).val() === 11)
78+
```
79+
80+
If you don't see any linting errors in VS Code, if you put your mouse cursor over `Num` and see its type, then it's all good.
81+
82+
Whenever you want to uninstall the globally-installed example-typescript-package and remove the symlink in the global folder, run:
83+
84+
```bash
85+
npm uninstall example-typescript-package -g
86+
```
87+
88+
### Publish
89+
90+
#### Manual Publishing
91+
92+
...
93+
94+
#### CI Publishing
95+
96+
...
97+
1698
## Notes
1799

18-
* It uses yarn but you can easily switch to npm, of course (remember to change `scripts`.`build` in **package.json**)
19-
* Whether you use npm as your package manager ≠ Whether you can publish to the npm registry
100+
- It uses npm but you can easily switch to yarn, of course (remember to change all "npm" in `scripts` in the file **package.json**)
101+
- Whether you use npm as your package manager ≠ Whether you can publish to the npm registry
102+
- Works fine in VS Code. In my configuration **.eslintrc** and **.prettierrc** cooperate perfectly
103+
- See `scripts`.`build` in **package.json** for other predefined script commands
104+
105+
## References
20106

21-
* Works fine in VS Code. Consider installing VS Code extensions [ES Lint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) and [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode). In my configuration **.eslintrc** and **.prettierrc** cooperate perfectly
22-
* You probably already know that: the shortcuts for formatting of a code file are <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>F</kbd> (Windows); <kbd>Shift</kbd> + <kbd>Option (Alt)</kbd> + <kbd>F</kbd> (MacOS); <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>I</kbd> (Linux)
107+
- [Creating and publishing unscoped public packages - npm docs](https://docs.npmjs.com/creating-and-publishing-unscoped-public-packages)
108+
- [npm-publish - npm docs](https://docs.npmjs.com/cli/v6/commands/npm-publish)
109+
- [Publishing - TypeScript docs](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html)
110+
- [Publishing Node.js packages - GitHub Docs](https://docs.github.com/en/free-pro-team@latest/actions/guides/publishing-nodejs-packages)
23111

24-
* See `scripts`.`build` in **package.json** for other predefined script commands
112+
Btw, if you want to publish Python package, go to [Example PyPI (Python Package Index) Package & Tutorial / Instruction / Workflow for 2021](https://github.com/tomchen/example_pypi_package).

cleanup.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const deleteFolderRecursive = (path) => {
2020
const folder = process.argv.slice(2)[0]
2121

2222
if (folder) {
23-
deleteFolderRecursive(folder)
23+
deleteFolderRecursive(Path.join('dist', folder))
2424
} else {
25-
deleteFolderRecursive('cjs')
26-
deleteFolderRecursive('esm')
27-
deleteFolderRecursive('umd')
28-
deleteFolderRecursive('types')
25+
deleteFolderRecursive('dist/cjs')
26+
deleteFolderRecursive('dist/esm')
27+
deleteFolderRecursive('dist/umd')
28+
deleteFolderRecursive('dist/types')
2929
}

config/tsconfig.cjs.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"extends": "./tsconfig.base",
33
"compilerOptions": {
44
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
5-
"outDir": "../cjs" /* Redirect output structure to the directory. */
5+
"outDir": "../dist/cjs" /* Redirect output structure to the directory. */
66
}
77
}

config/tsconfig.esm.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"extends": "./tsconfig.base",
33
"compilerOptions": {
44
"module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
5-
"outDir": "../esm" /* Redirect output structure to the directory. */
5+
"outDir": "../dist/esm" /* Redirect output structure to the directory. */
66
}
77
}

config/tsconfig.types.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"compilerOptions": {
44
"declaration": true /* Generates corresponding '.d.ts' file. */,
55
"emitDeclarationOnly": true,
6-
"outDir": "../types" /* Redirect output structure to the directory. */
6+
"outDir": "../dist/types" /* Redirect output structure to the directory. */
77
}
88
}

config/tsconfig.umd.json

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"extends": "./tsconfig.base",
33
"compilerOptions": {
44
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
5-
// "outDir": "../dist" /* Redirect output structure to the directory. */,
65
"declaration": false /* Generates corresponding '.d.ts' file. */
76
}
87
}

config/webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
mode: 'production',
55
entry: './src/index.ts',
66
output: {
7-
path: path.resolve(__dirname, '../umd'),
7+
path: path.resolve(__dirname, '../dist/umd'),
88
filename: 'index.js',
99
library: 'exampleTypescriptPackage',
1010
libraryTarget: 'umd',

0 commit comments

Comments
 (0)