From abc6b132a8ade2fc141d114c282c04249cb9cd68 Mon Sep 17 00:00:00 2001 From: Steven Noorbergen <91969936+steven-noorbergen@users.noreply.github.com> Date: Sun, 5 May 2024 16:35:22 +0200 Subject: [PATCH] feat(npm): Build and publish NPM package (#43) --- .editorconfig | 2 ++ .eslintignore | 2 ++ .eslintrc.js | 29 +++++++++++++++++++ .github/workflows/release.yml | 29 ++++++++++++++++++- .github/workflows/testing.yml | 32 +++++++++++++++++++++ .npmrc | 1 + package.json | 41 +++++++++++++++++++++++++-- rollup.config.mjs | 53 +++++++++++++++++++++++++++++++++++ src/index.ts | 2 ++ tsconfig.json | 19 +++++++++++++ 10 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 .editorconfig create mode 100644 .eslintignore create mode 100644 .eslintrc.js create mode 100644 .github/workflows/testing.yml create mode 100644 .npmrc create mode 100644 rollup.config.mjs create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e53f54d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,2 @@ +[*] +max_line_length=120 diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..1eae0cf --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +dist/ +node_modules/ diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..4f0ac52 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,29 @@ +module.exports = { + env: { + browser: true, + es6: true, + }, + extends: [ + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended", + "plugin:import/recommended", + ], + plugins: ["@typescript-eslint", "import", "prettier"], + rules: { + "newline-per-chained-call": "off", + }, + parserOptions: { + project: "./tsconfig.json", + }, + ignorePatterns: [], + overrides: [ + { + // The files listed below are part of the build process, so they will be using packages that are listed + // under devDependences and/or peerDependencies, so we need to be lenient with the import/no-extraneous-dependencies + files: [".eslintrc.js", "rollup.config.mjs"], + rules: { + "import/no-extraneous-dependencies": ["error", { peerDependencies: true, devDependencies: true }], + }, + }, + ], +}; diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7890bec..404fc2d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,11 +14,27 @@ jobs: - name: Checkout uses: actions/checkout@v4 + - name: Install NodeJS + uses: actions/setup-node@v4 + with: + registry-url: https://npm.pkg.github.com + scope: "@eveshipfit" + env: + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Set version + run: | + # Remove the "v" from the version. + VERSION=$(echo ${{ github.ref_name }} | cut -b2-) + echo "Version: ${VERSION}" + sed -i 's/"version": "0.0.0-git"/"version": "'${VERSION}'"/' package.json + - name: Install dependencies run: | sudo apt update sudo apt install -y --no-install-recommends rclone protobuf-compiler pip install -r requirements.txt + npm install - name: Setup rclone run: | @@ -63,9 +79,20 @@ jobs: run: | python download_icons.py sde/fsd + - name: Build package + run: | + npm run build + + - name: Publish package + uses: JS-DevTools/npm-publish@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + registry: "https://npm.pkg.github.com" + package: package.json + - name: Publish to R2 bucket run: | - rclone copy dist eveshipfit:eveshipfit/${{ github.event.release.tag_name }}/ --progress + rclone copy dist eveshipfit:eveshipfit/${{ github.event.release.tag_name }}/ --progress --exclude=*.{js,js.map,d.ts} env: RCLONE_CONFIG_EVESHIPFIT_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }} RCLONE_CONFIG_EVESHIPFIT_ENDPOINT: ${{ secrets.R2_ENDPOINT }} diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 0000000..b74f7b0 --- /dev/null +++ b/.github/workflows/testing.yml @@ -0,0 +1,32 @@ +name: Testing + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + testing: + name: Testing + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install NodeJS + uses: actions/setup-node@v4 + with: + registry-url: https://npm.pkg.github.com + scope: "@eveshipfit" + env: + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Install dependencies + run: npm install + + - name: Run linter + run: npm run lint diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..5cf5267 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +@eveshipfit:registry=https://npm.pkg.github.com diff --git a/package.json b/package.json index b3a9cb9..f026387 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,42 @@ { - "dependencies": { - "protobufjs-cli": "^1.1.2" + "name": "@eveshipfit/data", + "publishConfig": { + "registry": "https://npm.pkg.github.com/EVEShipFit" + }, + "version": "0.0.0-git", + "description": "Converted data from the EVE Online SDE", + "scripts": { + "build": "rollup -c", + "lint": "eslint src --ext .js,.ts --cache && prettier -c src", + "prettier": "prettier -w src" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/EVEShipFit/data.git" + }, + "author": "Patric Stout ", + "license": "SEE LICENSE IN LICENSE", + "main": "dist/cjs/index.js", + "module": "dist/esm/index.js", + "files": [ + "dist" + ], + "types": "dist/index.d.ts", + "devDependencies": { + "@rollup/plugin-commonjs": "^25.0.7", + "@rollup/plugin-node-resolve": "^15.2.3", + "@rollup/plugin-replace": "^5.0.5", + "@rollup/plugin-terser": "^0.4.4", + "@typescript-eslint/eslint-plugin": "^7.8.0", + "eslint": "^8.57.0", + "eslint-config-prettier": "^9.1.0", + "eslint-plugin-import": "^2.29.1", + "eslint-plugin-prettier": "^5.1.3", + "protobufjs-cli": "^1.1.2", + "rollup": "^4.4.0", + "rollup-plugin-dts": "^6.1.0", + "rollup-plugin-esbuild": "^6.1.0", + "rollup-plugin-node-externals": "^7.0.1", + "typescript": "^5.2.2" } } diff --git a/rollup.config.mjs b/rollup.config.mjs new file mode 100644 index 0000000..cc89273 --- /dev/null +++ b/rollup.config.mjs @@ -0,0 +1,53 @@ +import commonjs from "@rollup/plugin-commonjs"; +import dts from "rollup-plugin-dts"; +import esbuild from "rollup-plugin-esbuild"; +import nodeExternals from "rollup-plugin-node-externals"; +import nodeResolve from "@rollup/plugin-node-resolve"; +import terser from "@rollup/plugin-terser"; +import replace from "@rollup/plugin-replace"; + +import { readFileSync } from "fs"; + +const packageJson = JSON.parse(readFileSync("package.json", "utf-8")); + +export default [ + { + input: "src/index.ts", + output: [ + { + file: "dist/cjs/index.js", + format: "cjs", + sourcemap: true, + }, + { + file: "dist/esm/index.js", + format: "esm", + sourcemap: true, + }, + ], + plugins: [ + replace({ + preventAssignment: true, + values: { + "process.env.VERSION": JSON.stringify(packageJson.version), + "process.env.BUILD_TIME": JSON.stringify(new Date().toISOString()), + }, + }), + nodeExternals(), + nodeResolve(), + commonjs(), + esbuild({ tsconfig: "./tsconfig.json" }), + terser(), + ], + }, + { + input: "src/index.ts", + output: [ + { + file: "dist/index.d.ts", + format: "esm", + }, + ], + plugins: [dts({ tsconfig: "./tsconfig.json" })], + }, +]; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..3205f67 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,2 @@ +export const ESF_DATA_VERSION = process.env.VERSION as string; +export const ESF_DATA_BUILD_TIME = process.env.BUILD_TIME as string; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..065b596 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "allowSyntheticDefaultImports": true, + "declaration": true, + "declarationDir": "dist/types", + "emitDeclarationOnly": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "module": "ESNext", + "moduleResolution": "node", + "outDir": "dist", + "skipLibCheck": true, + "sourceMap": true, + "strict": true, + "target": "es2016" + }, + "include": ["src/**/*.ts", "src/**/*.d.ts"], + "exclude": ["node_modules"] +}