Skip to content

Support ESM #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

const rules = {
}

module.exports = {
extends: '@chatie',
rules,
}
92 changes: 92 additions & 0 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: NPM

on: [push, pull_request]

jobs:
build:
name: Build
strategy:
matrix:
os:
- ubuntu-latest
# - macos-latest
node:
- 16

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Install Dependencies
run: npm install

- name: Test
run: npm test

pack:
name: Pack
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16

- name: Install Dependencies
run: npm install

- name: Generate Version
run: ./scripts/generate-version.sh

- name: Pack Testing
run: ./scripts/npm-pack-testing.sh

publish:
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/v'))
name: Publish
needs: [build, pack]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16
registry-url: https://registry.npmjs.org/

- name: Install Dependencies
run: npm install

- name: Generate Version
run: ./scripts/generate-version.sh

- name: Set Publish Config
run: ./scripts/package-publish-config-tag.sh

- name: Build Dist
run: npm run dist

- name: Check Branch
id: check-branch
run: |
if [[ ${{ github.ref }} =~ ^refs/heads/(main|v[0-9]+\.[0-9]+.*)$ ]]; then
echo ::set-output name=match::true
fi # See: https://stackoverflow.com/a/58869470/1123955
- name: Is A Publish Branch
if: steps.check-branch.outputs.match == 'true'
run: |
NAME=$(npx pkg-jq -r .name)
VERSION=$(npx pkg-jq -r .version)
if npx version-exists "$NAME" "$VERSION"
then echo "$NAME@$VERSION exists on NPM, skipped."
else npm publish
fi
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Is Not A Publish Branch
if: steps.check-branch.outputs.match != 'true'
run: echo 'Not A Publish Branch'
53 changes: 0 additions & 53 deletions .travis.yml

This file was deleted.

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ It seems useless at first, but if you want to use manage many Child Class for a

## CHANGELOG

### master v0.9 (Aug 26, 2021)

1. ESM Support
2. GitHub Action enabled

### v0.6 (May 2018)

1. add new function: `instanceToClass()` for getting back the `Class` from an existing `instance`.
Expand Down
20 changes: 11 additions & 9 deletions examples/example.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
// tslint:disable:variable-name
import * as assert from 'assert'
// eslint disable lowerCamelCase
import assert from 'assert'

import {
cloneClass,
instanceToClass,
} from '../src/'
} from '../src/mod'

class Employee {

public static company: string

constructor(
constructor (
public name: string,
) {
}

public info() {
console.log(`Employee ${this.name}, Company ${(this.constructor as any).company}`)
public info () {
console.info(`Employee ${this.name}, Company ${(this.constructor as any).company}`)
}

}

console.log(`
console.info(`
# Example 1: cloneClass()
`)

Expand All @@ -37,7 +39,7 @@ employeeGg.info()
employeeMs.info()
// Output: Employee Jerry, Company Microsoft

console.log(`
console.info(`
# Example 2: instanceToClass()
`)

Expand All @@ -49,4 +51,4 @@ const anotherEmployee = new RestoreGoogleEmployee('Mary')
anotherEmployee.info()
// Output: Employee Mary, Company Google

console.log()
console.info()
55 changes: 33 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
{
"name": "clone-class",
"version": "0.7.3",
"version": "0.9.2",
"description": "Clone an ES6 Class as Another Class Name for Isolating Class Static Properties.",
"main": "src/index.js",
"typings": "src/index.d.ts",
"type": "module",
"exports": {
".": {
"import": "./dist/esm/src/mod.js",
"require": "./dist/cjs/src/mod.js"
}
},
"typings": "./dist/esm/src/mod.d.ts",
"engines": {
"node": ">=14"
},
"scripts": {
"clean": "shx rm -fr dist/*",
"dist": "npm run clean && tsc && shx cp {README.md,package.json} dist/",
"pack": "npm pack dist/",
"dist": "npm run clean && tsc && tsc -p tsconfig.cjs.json && npm run fixup && shx cp {README.md,package.json} dist/",
"fixup": "echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json",
"example": "ts-node examples/example.ts",
"lint": "npm run lint:ts",
"lint:ts": "tslint --project tsconfig.json && tsc --noEmit",
"test": "npm run test:unit",
"test:unit": "blue-tape -r ts-node/register \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\"",
"lint": "npm run lint:es && npm run lint:ts",
"lint:ts": "tsc --noEmit",
"lint:es": "eslint --ignore-pattern fixtures/ \"src/**/*.ts\" \"tests/**/*.ts\"",
"test": "npm run lint && npm run test:unit",
"test:unit": "tap --node-arg=--loader=ts-node/esm \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\"",
"test:pack": "bash -x scripts/npm-pack-testing.sh"
},
"repository": {
Expand All @@ -32,24 +42,25 @@
},
"homepage": "https://github.com/huan/clone-class#readme",
"devDependencies": {
"@types/blue-tape": "^0.1.31",
"@types/semver": "^7.3.1",
"@types/node": "^15.0.0",
"blue-tape": "^1.0.0",
"git-scripts": "^0.4.3",
"@chatie/eslint-config": "^0.13.5",
"@chatie/git-scripts": "^0.6.2",
"@chatie/tsconfig": "^0.17.2",
"semver": "^7.3.2",
"shx": "^0.3.0",
"ts-node": "^9.0.0",
"tslint": "^6.1.2",
"typescript": "^4.0.3"
},
"git": {
"scripts": {
"pre-push": "./scripts/pre-push.sh"
}
"tstest": "^0.5.16",
"typescript": "^4.3"
},
"publishConfig": {
"access": "public",
"tag": "latest"
},
"files": [
"dist",
"src"
],
"git": {
"scripts": {
"pre-push": "npx git-scripts-pre-push"
}
}
}
13 changes: 0 additions & 13 deletions scripts/development-release.ts

This file was deleted.

20 changes: 20 additions & 0 deletions scripts/generate-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
set -e

SRC_VERSION_TS_FILE='src/version.ts'

[ -f ${SRC_VERSION_TS_FILE} ] || {
echo ${SRC_VERSION_TS_FILE}" not found"
exit 1
}

VERSION=$(npx pkg-jq -r .version)

cat <<_SRC_ > ${SRC_VERSION_TS_FILE}
/**
* This file was auto generated from scripts/generate-version.sh
*/
export const VERSION: string = '${VERSION}'
_SRC_

echo "$VERSION generated."
39 changes: 35 additions & 4 deletions scripts/npm-pack-testing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,56 @@
set -e

npm run dist
# must use `dist/` instead of `dist`
# or npm will re-pack a npm module named `dist` from npmjs.com
npm run pack
npm pack

TMPDIR="/tmp/npm-pack-testing.$$"
mkdir "$TMPDIR"
mv *-*.*.*.tgz "$TMPDIR"
cp tests/fixtures/smoke-testing.ts "$TMPDIR"

cd $TMPDIR

npm init -y
npm install *-*.*.*.tgz \
@types/node \
typescript \
typescript@latest

#
# CommonJS
#
./node_modules/.bin/tsc \
--esModuleInterop \
--lib esnext \
--noEmitOnError \
--noImplicitAny \
--skipLibCheck \
--target es5 \
--module CommonJS \
--moduleResolution node \
smoke-testing.ts

echo
echo "CommonJS: pack testing..."
node smoke-testing.js

#
# ES Modules
#

# https://stackoverflow.com/a/59203952/1123955
echo "`jq '.type="module"' package.json`" > package.json

./node_modules/.bin/tsc \
--esModuleInterop \
--lib esnext,dom \
--noEmitOnError \
--noImplicitAny \
--skipLibCheck \
--target es2020 \
--module es2020 \
--moduleResolution node \
smoke-testing.ts

echo
echo "ES Module: pack testing..."
node smoke-testing.js
Loading