diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..d2e47be --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,141 @@ +# This is a Github Workflow that runs tests on any push or pull request. +# If the tests pass and this is a push to the master branch it also runs Semantic Release. +name: CI +on: [push, pull_request] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - name: Use Node.js 24 + uses: actions/setup-node@v5 + with: + node-version: 24 + cache: 'npm' + - run: npm ci + - run: npm run build + - uses: actions/upload-artifact@v4 + with: + name: build + path: | + target + package.json + jsr.json + retention-days: 1 + + push: + name: Push ${{ github.ref }} + needs: build + if: github.event_name == 'push' + runs-on: ubuntu-latest + permissions: + checks: read + statuses: write + contents: write + packages: write + id-token: write + steps: + - name: Checkout + uses: actions/checkout@v5 + with: + fetch-depth: ${{ github.ref == 'refs/heads/main' && '0' || '1' }} + - name: Setup Node.js + uses: actions/setup-node@v5 + with: + node-version: 24 + cache: 'npm' + - uses: actions/download-artifact@v4 + with: + name: build + - name: Fetch deps + run: npm ci + - name: Run tests + run: npm run test + +# - name: Semantic Release (npm) +# if: github.ref == 'refs/heads/main' +# env: +# NPM_TOKEN: ${{ secrets.NPM_TOKEN }} +# NPM_PROVENANCE: true +# GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} +# GIT_BRANCH: 'main' +# GIT_AUTHOR_NAME: ${{ vars.GIT_AUTHOR_NAME }} +# GIT_AUTHOR_EMAIL: ${{ vars.GIT_AUTHOR_EMAIL }} +# GIT_COMMITTER_NAME: ${{ vars.GIT_COMMITTER_NAME }} +# GIT_COMMITTER_EMAIL: ${{ vars.GIT_COMMITTER_EMAIL }} +# run: npx zx-semrel + + pr: + if: github.event_name == 'pull_request' + needs: build + name: PR (Node v${{ matrix.node-version }}, OS ${{ matrix.os }}) + strategy: + matrix: + os: [ ubuntu-latest, windows-latest ] + node-version: [ 18, 24 ] + + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v5 + - name: Setup Node.js + uses: actions/setup-node@v5 + with: + node-version: ${{ matrix.node-version }} + cache: 'npm' + - uses: actions/download-artifact@v4 + with: + name: build + + - name: Run all tests + if: matrix.os == 'ubuntu-latest' && matrix.node-version == 24 + run: | + npm ci + npm run test +# - name: Run spawn tests (win32) +# if: matrix.os == 'windows-latest' && matrix.node-version == 24 +# run: | +# npm ci +# node --loader ts-node/esm --experimental-specifier-resolution=node ./src/test/ts/spawn.test.ts +# timeout-minutes: 5 +# +# - name: Run smoke:esm tests +# if: matrix.node-version >= 12 +# run: npm run test:smoke:esm +# +# - name: Run smoke:cjs tests +# run: npm run test:smoke:cjs + +# smoke-bun: +# runs-on: ubuntu-latest +# name: Smoke Bun +# needs: build +# steps: +# - uses: actions/checkout@v5 +# - name: Setup Bun +# uses: antongolub/action-setup-bun@v1 +# - uses: actions/download-artifact@v4 +# with: +# name: build +# - run: | +# bun ./src/test/smoke/invoke.test.cjs +# bun ./src/test/smoke/invoke.test.mjs +# +# smoke-deno: +# runs-on: ubuntu-latest +# name: Smoke Deno ${{ matrix.deno-version }} +# needs: build +# strategy: +# matrix: +# deno-version: [ v1.x, v2.x ] +# steps: +# - uses: actions/checkout@v5 +# - name: Setup Deno +# uses: denoland/setup-deno@v2 +# with: +# deno-version: ${{ matrix.deno-version }} +# - run: deno install npm:types/node +# - uses: actions/download-artifact@v4 +# with: +# name: build +# - run: deno test --allow-read --allow-sys --allow-env --allow-run ./src/test/smoke/invoke.test.mjs diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..cdd41e1 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,94 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL Advanced" + +on: + push: + branches: ["turn-on-later"] +# push: +# branches: [ "main" ] +# pull_request: +# branches: [ "main" ] +# schedule: +# - cron: '17 20 * * 0' + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + # Runner size impacts CodeQL analysis time. To learn more, please see: + # - https://gh.io/recommended-hardware-resources-for-running-codeql + # - https://gh.io/supported-runners-and-hardware-resources + # - https://gh.io/using-larger-runners (GitHub.com only) + # Consider using larger runners or machines with greater resources for possible analysis time improvements. + runs-on: 'ubuntu-latest' + permissions: + # required for all workflows + security-events: write + + # required to fetch internal or private CodeQL packs + packages: read + + # only required for workflows in private repositories + actions: read + contents: read + + strategy: + fail-fast: false + matrix: + include: + - language: javascript-typescript + build-mode: none + # CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' + # Use `c-cpp` to analyze code written in C, C++ or both + # Use 'java-kotlin' to analyze code written in Java, Kotlin or both + # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both + # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, + # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. + # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how + # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + # If the analyze step fails for one of the languages you are analyzing with + # "We were unable to automatically build your code", modify the matrix above + # to set the build mode to "manual" for that language. Then modify this step + # to build your code. + # â„šī¸ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + - if: matrix.build-mode == 'manual' + shell: bash + run: | + echo 'If you are using a "manual" build mode for one or more of the' \ + 'languages you are analyzing, replace this with the commands to build' \ + 'your code, for example:' + echo ' make bootstrap' + echo ' make release' + exit 1 + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v3 + with: + category: "/language:${{matrix.language}}" diff --git a/.gitignore b/.gitignore index 9a5aced..e7da42c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,139 +1,18 @@ -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* -lerna-debug.log* - -# Diagnostic reports (https://nodejs.org/api/report.html) -report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage -*.lcov - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories +# Deps node_modules/ -jspm_packages/ - -# Snowpack dependency directory (https://snowpack.dev/) -web_modules/ - -# TypeScript cache -*.tsbuildinfo - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Optional stylelint cache -.stylelintcache - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variable files -.env -.env.* -!.env.example - -# parcel-bundler cache (https://parceljs.org/) -.cache -.parcel-cache - -# Next.js build output -.next -out - -# Nuxt.js build / generate output -.nuxt -dist - -# Gatsby files -.cache/ -# Comment in the public line in if your project uses Gatsby and not Next.js -# https://nextjs.org/blog/next-9-1#public-directory-support -# public - -# vuepress build output -.vuepress/dist - -# vuepress v2.x temp and cache directory -.temp -.cache - -# Sveltekit cache directory -.svelte-kit/ - -# vitepress build output -**/.vitepress/dist - -# vitepress cache directory -**/.vitepress/cache - -# Docusaurus cache and generated files -.docusaurus - -# Serverless directories -.serverless/ - -# FuseBox cache -.fusebox/ - -# DynamoDB Local files -.dynamodb/ - -# Firebase cache directory -.firebase/ - -# TernJS port file -.tern-port - -# Stores VSCode versions used for testing VSCode extensions -.vscode-test - -# yarn v3 -.pnp.* -.yarn/* -!.yarn/patches -!.yarn/plugins -!.yarn/releases -!.yarn/sdks -!.yarn/versions - -# Vite logs files -vite.config.js.timestamp-* -vite.config.ts.timestamp-* +yarn.lock +pnpm-lock.yaml + +# Bundles +target/* +!target/cjs/ +!target/esm/ +!target/dts/ +jsr.json +buildstamp.json + +# Temp +temp + +# +.npmrc diff --git a/package.json b/package.json index 39aa66c..81c3ad5 100644 --- a/package.json +++ b/package.json @@ -21,18 +21,18 @@ ], "scripts": { "build": "concurrently 'npm:build:*'", - "_build:js": "node ./src/scripts/build.mjs --entry='./src/main/ts/ip.ts'", - "_build:dts": "tsc --emitDeclarationOnly --outDir target/dts", + "build:js": "node ./src/scripts/build.mjs --entry='./src/main/ts/ip.ts'", + "build:dts": "tsc --emitDeclarationOnly --outDir target/dts", "_build:docs": "typedoc --options src/main/typedoc", - "_build:stamp": "npx buildstamp", - "_build:jsr": "node ./src/scripts/build-jsr.mjs", + "build:stamp": "npx buildstamp", + "build:jsr": "node ./src/scripts/build-jsr.mjs", "test": "concurrently 'npm:test:*'", "test:target": "git diff --exit-code --quiet || (echo 'Uncommitted changes' && exit 1)", "_test:lint": "eslint -c src/test/lint/.eslintrc.json src", "test:unit": "c8 -r lcov -r text -o target/coverage -x src/scripts -x src/test -x target node --experimental-specifier-resolution=node src/test/ts/ip.test.ts", "_test:smoke:esm": "node ./src/test/smoke/invoke.test.mjs", "_test:smoke:cjs": "node src/test/smoke/invoke.test.cjs", - "_test:jsr": "jsr publish --dry-run", + "test:jsr": "jsr publish --dry-run", "test:audit": "npm audit", "_publish:draft": "npm run build && npm publish --no-git-tag-version", "_postrelease": "node src/scripts/build-jsr.mjs && jsr publish --allow-dirty" diff --git a/src/scripts/build-jsr.mjs b/src/scripts/build-jsr.mjs new file mode 100644 index 0000000..d85edb7 --- /dev/null +++ b/src/scripts/build-jsr.mjs @@ -0,0 +1,18 @@ +import fs from 'fs' +import path from 'path' +const cwd = process.cwd() +const pkgJson = JSON.parse(fs.readFileSync(path.resolve(cwd, 'package.json'), 'utf-8')) + +fs.writeFileSync(path.resolve(cwd, 'jsr.json'), JSON.stringify({ + name: '@webpod/ip', + version: pkgJson.version, + exports: { + '.': './src/main/ts/ip.ts' + }, + publish: { + include: [ + 'src/main/ts', + 'README.md' + ] + } +}, null, 2)) diff --git a/src/scripts/build.mjs b/src/scripts/build.mjs new file mode 100644 index 0000000..5e4b7e2 --- /dev/null +++ b/src/scripts/build.mjs @@ -0,0 +1,152 @@ +#!/usr/bin/env node + +import process from 'node:process' +import esbuild from 'esbuild' +import { nodeExternalsPlugin } from 'esbuild-node-externals' +import { entryChunksPlugin } from 'esbuild-plugin-entry-chunks' +import { transformHookPlugin } from 'esbuild-plugin-transform-hook' +import { extractHelpersPlugin } from 'esbuild-plugin-extract-helpers' +import { injectFile } from 'esbuild-plugin-utils' +import minimist from 'minimist' +import glob from 'fast-glob' +import path from "node:path"; + +const unwrapQuotes = str => str.replace(/^['"]|['"]$/g, '') +const argv = minimist(process.argv.slice(2), { + default: { + entry: './src/main/ts/index.ts', + external: 'node:*', + bundle: 'src', // 'all' | 'none' + license: 'eof', + minify: false, + sourcemap: false, + format: 'cjs,esm', + cwd: process.cwd() + }, + boolean: ['minify', 'sourcemap', 'banner'], + string: ['entry', 'external', 'bundle', 'license', 'format', 'map', 'cwd'] +}) +const { entry, external, bundle, minify, sourcemap, license, format, cwd: _cwd } = argv +const plugins = [] +const cwd = Array.isArray(_cwd) ? _cwd[_cwd.length - 1] : _cwd +const entryPoints = entry.includes('*') + ? await glob(unwrapQuotes(entry).split(':'), { absolute: false, onlyFiles: true, cwd }) + : unwrapQuotes(entry).split(':') + +const _bundle = bundle !== 'none' && !process.argv.includes('--no-bundle') +const _external = _bundle + ? external.split(',') + : undefined // https://github.com/evanw/esbuild/issues/1466 + +if (_bundle && entryPoints.length > 1) { + plugins.push(entryChunksPlugin()) +} + +if (bundle === 'src') { + // https://github.com/evanw/esbuild/issues/619 + // https://github.com/pradel/esbuild-node-externals/pull/52 + plugins.push(nodeExternalsPlugin()) +} + +const cjsPlugins = [ + extractHelpersPlugin({ + helper: 'cjslib.cjs', + cwd: 'target/cjs', + include: /\.cjs/, + }), + transformHookPlugin({ + hooks: [ + { + on: 'end', + pattern: entryPointsToRegexp(entryPoints), + transform(contents, p) { + return contents + .toString() + .replaceAll('"node:', '"') + .replace( + /0 && \(module\.exports =(.|\n)+/, + ($0) => { + if (!$0.includes('...')) return $0 + + const lines = $0.split('\n').slice(1, -1) + const vars = [] + const reexports = [] + lines.forEach((l) => { + const e = /\s*\.{3}(require\(.+\))/.exec(l)?.[1] + if (e) { + reexports.push(e) + } else { + vars.push(l) + } + }) + + return `0 && (module.exports = Object.assign({ +${vars.join('\n')} +}, ${reexports.join(',\n')}))` + } + ) + }, + },] + }) +] + +const formats = format.split(',') + +const esmConfig = { + absWorkingDir: cwd, + entryPoints, + outdir: './target/esm', + bundle: _bundle, + external: _external, + minify, + sourcemap, + sourcesContent: false, + platform: 'node', + target: 'es2019', + format: 'esm', + outExtension: { + '.js': '.mjs' + }, + plugins, + legalComments: license, + tsconfig: './tsconfig.json', + +} + +const cjsConfig = { + ...esmConfig, + outdir: './target/cjs', + target: 'es2015', + format: 'cjs', + banner: {}, + outExtension: { + '.js': '.cjs' + }, + plugins: [ + ...plugins, + ...cjsPlugins + ], +} + +for (const format of formats) { + const config = format === 'cjs' ? cjsConfig : esmConfig + + await esbuild + .build(config) + .catch((e) => { + console.error(e) + process.exit(1) + }) +} + +function entryPointsToRegexp(entryPoints) { + return new RegExp( + '(' + entryPoints.map((e) => escapeRegExp(path.parse(e).name)).join('|') + ')\\.cjs$' + ) +} + +function escapeRegExp(str) { + return str.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&') +} + +process.exit(0) diff --git a/src/scripts/test.mjs b/src/scripts/test.mjs new file mode 100644 index 0000000..163a95c --- /dev/null +++ b/src/scripts/test.mjs @@ -0,0 +1,10 @@ +#!/usr/bin/env node + +import glob from 'fast-glob' +import { pathToFileURL } from 'node:url' +import process from 'node:process' + +const focused = process.argv.slice(2) +const suites = focused.length ? focused : await glob('src/test/**/*.test.{ts,cjs,mjs,js}', {cwd: process.cwd(), absolute: true, onlyFiles: true}) + +await Promise.all(suites.map(suite => import(pathToFileURL(suite)))) diff --git a/target/cjs/cjslib.cjs b/target/cjs/cjslib.cjs new file mode 100644 index 0000000..c625f7d --- /dev/null +++ b/target/cjs/cjslib.cjs @@ -0,0 +1,34 @@ + +var __defProp = Object.defineProperty; + +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; + +var __getOwnPropNames = Object.getOwnPropertyNames; + +var __hasOwnProp = Object.prototype.hasOwnProperty; + +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; + +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; + +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +module.exports = { + __defProp, + __getOwnPropDesc, + __getOwnPropNames, + __hasOwnProp, + __export, + __copyProps, + __toCommonJS +}; diff --git a/target/cjs/ip.cjs b/target/cjs/ip.cjs new file mode 100644 index 0000000..f892412 --- /dev/null +++ b/target/cjs/ip.cjs @@ -0,0 +1,18 @@ +"use strict"; +const { + __export, + __toCommonJS +} = require('./cjslib.cjs'); + + +// src/main/ts/ip.ts +var ip_exports = {}; +__export(ip_exports, { + foo: () => foo +}); +module.exports = __toCommonJS(ip_exports); +var foo = "bar"; +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + foo +}); \ No newline at end of file diff --git a/target/dts/ip.d.ts b/target/dts/ip.d.ts new file mode 100644 index 0000000..c48b893 --- /dev/null +++ b/target/dts/ip.d.ts @@ -0,0 +1 @@ +export declare const foo = "bar"; diff --git a/target/esm/ip.mjs b/target/esm/ip.mjs new file mode 100644 index 0000000..50a257e --- /dev/null +++ b/target/esm/ip.mjs @@ -0,0 +1,5 @@ +// src/main/ts/ip.ts +var foo = "bar"; +export { + foo +};