Repository navigation
fix .npmignore, and add test for working npm image #473
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
Open
grahamegrieve
wants to merge
2
commits into
main
Choose a base branch
from
2026-12-gg-npm-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| #!/usr/bin/env node | ||
| // Checks FHIRsmith as installed from its published package (npm install -g fhirsmith), not as a | ||
| // clone: the published-install-check workflow packs the package, installs the tarball globally and | ||
| // runs this on the installed copy. An install from the registry differs from a clone in ways the | ||
| // other checks don't see: | ||
| // - .npmignore decides which files are in the package. A pattern that matches more than meant drops | ||
| // files the server loads (an unanchored vendor/ dropped ips/vendor/, and the server didn't start) | ||
| // - npm doesn't apply the package's overrides or (it appears) its allowScripts when the package is | ||
| // itself installed, so a native module can be left without its binary (roaring, with no error from | ||
| // npm install) | ||
| // | ||
| // Usage: node check-installed-package.js <installed package folder> | ||
| // | ||
| // 1. every require() of a literal string in a .js file in the package has to resolve, from that file, | ||
| // in the installed tree. A require of a devDependency is a warning, not a failure: devDependencies | ||
| // aren't installed, but a file that only tests or tools load can name one | ||
| // 2. the native modules have to load, not just resolve | ||
| // | ||
| // Not checked: comments (examples and remarks name modules), minified bundles and browser assets | ||
| // (*.min.js, static/: their require()s are guarded, for when they run under a module loader), and the | ||
| // known exceptions below | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const { createRequire, builtinModules } = require('module'); | ||
|
|
||
| const pkgDir = path.resolve(process.argv[2] || ''); | ||
| const manifestPath = path.join(pkgDir, 'package.json'); | ||
| if (!process.argv[2] || !fs.existsSync(manifestPath)) { | ||
| console.error('usage: node check-installed-package.js <installed package folder>'); | ||
| process.exit(2); | ||
| } | ||
| const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); | ||
| const devDependencies = new Set(Object.keys(manifest.devDependencies || {})); | ||
| const optionalDependencies = new Set(Object.keys(manifest.optionalDependencies || {})); | ||
| const builtins = new Set(builtinModules); | ||
|
|
||
| // the package a bare specifier names: 'a/b' -> 'a', '@s/a/b' -> '@s/a' | ||
| function packageName(spec) { | ||
| const parts = spec.split('/'); | ||
| return spec.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0]; | ||
| } | ||
|
|
||
| function jsFiles(dir, result = []) { | ||
| for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { | ||
| if (entry.name === 'node_modules') { | ||
| continue; | ||
| } | ||
| const full = path.join(dir, entry.name); | ||
| if (entry.isDirectory()) { | ||
| jsFiles(full, result); | ||
| } else if (entry.isFile() && (entry.name.endsWith('.js') || entry.name.endsWith('.cjs'))) { | ||
| result.push(full); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // file (relative to the package, with /) -> the modules it may require that aren't installed, and why | ||
| const KNOWN = { | ||
| 'utilities/windows-install.js': { modules: ['node-windows'], why: 'run by hand on Windows, after npm install -g node-windows' } | ||
| }; | ||
|
|
||
| function skipFile(rel) { | ||
| return rel.endsWith('.min.js') || rel.startsWith('static/'); | ||
| } | ||
|
|
||
| // comments out, so that examples and remarks in them aren't taken for code. Not a parser: a block | ||
| // comment, and a line that starts with // or *, which is how comments are written here | ||
| function stripComments(source) { | ||
| return source.replace(/\/\*[\s\S]*?\*\//g, '').split('\n').filter((line) => { | ||
| const t = line.trim(); | ||
| return !t.startsWith('//') && !t.startsWith('*'); | ||
| }).join('\n'); | ||
| } | ||
|
|
||
| const REQUIRE = /\brequire\(\s*(['"])([^'"\n]+)\1\s*\)/g; | ||
| const failures = []; | ||
| const warnings = []; | ||
| let checked = 0; | ||
|
|
||
| // 1. the require graph | ||
| const files = jsFiles(pkgDir); | ||
| let scanned = 0; | ||
| for (const file of files) { | ||
| const rel = path.relative(pkgDir, file).split(path.sep).join('/'); | ||
| if (skipFile(rel)) { | ||
| continue; | ||
| } | ||
| scanned++; | ||
| const source = stripComments(fs.readFileSync(file, 'utf8')); | ||
| const req = createRequire(file); | ||
| for (const match of source.matchAll(REQUIRE)) { | ||
| const spec = match[2]; | ||
| if (KNOWN[rel] && KNOWN[rel].modules.includes(spec)) { | ||
| continue; | ||
| } | ||
| if (spec.startsWith('node:') || builtins.has(spec) || builtins.has(spec.split('/')[0])) { | ||
| continue; | ||
| } | ||
| checked++; | ||
| try { | ||
| req.resolve(spec); | ||
| } catch (error) { | ||
| const bare = !spec.startsWith('.') && !spec.startsWith('/'); | ||
| const name = bare ? packageName(spec) : null; | ||
| if (bare && devDependencies.has(name)) { | ||
| warnings.push(`${rel}: require('${spec}') - a devDependency, not installed`); | ||
| } else if (bare && optionalDependencies.has(name)) { | ||
| // checked below, where the native modules are loaded | ||
| warnings.push(`${rel}: require('${spec}') - an optionalDependency that isn't installed`); | ||
| } else { | ||
| failures.push(`${rel}: require('${spec}') doesn't resolve`); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| console.log(`${scanned} files scanned, ${checked} requires checked`); | ||
|
|
||
| // 2. the native modules load. roaring is an optionalDependency, but the tx module can't run without it | ||
| const pkgRequire = createRequire(manifestPath); | ||
| const native = [ | ||
| ['roaring', (m) => new m.RoaringBitmap32([1, 2, 3]).size], | ||
| ['better-sqlite3', (m) => m(':memory:').prepare('select sqlite_version() v').get().v], | ||
| ['sqlite3', (m) => { new m.Database(':memory:').close(); return 'opened'; }], | ||
| ['bcrypt', (m) => (m.hashSync('x', 4) ? 'hashed' : '')] | ||
| ]; | ||
| for (const [name, use] of native) { | ||
| try { | ||
| console.log(`${name}: ${use(pkgRequire(name))}`); | ||
| } catch (error) { | ||
| failures.push(`${name} doesn't load: ${error.message.split('\n')[0]}`); | ||
| } | ||
| } | ||
| // koffi is optional and the server runs without it | ||
| try { | ||
| pkgRequire('koffi'); | ||
| console.log('koffi: loaded'); | ||
| } catch (error) { | ||
| warnings.push(`koffi (optional) doesn't load: ${error.message.split('\n')[0]}`); | ||
| } | ||
|
|
||
| for (const w of warnings) { | ||
| console.log(`warning: ${w}`); | ||
| } | ||
| for (const f of failures) { | ||
| console.log(`::error::${f}`); | ||
| } | ||
| if (failures.length > 0) { | ||
| console.error(`${failures.length} problem(s) with the installed package`); | ||
| process.exit(1); | ||
| } | ||
| console.log('the installed package is complete, and its native modules load'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| name: Published install check | ||
|
|
||
| # Installs FHIRsmith the way a server does - npm install -g of the published package - and checks the | ||
| # installed copy (.github/scripts/check-installed-package.js): every file the code requires is in the | ||
| # package, and the native modules load. A clone (npm ci, the other checks) can't see either problem: | ||
| # .npmignore decides what is in the package, and npm applies neither the package's overrides nor | ||
| # (it appears) its allowScripts when the package is itself installed. | ||
| # | ||
| # Runs with the npm that comes with Node 24 and with the latest npm, as which install scripts npm runs | ||
| # depends on its version. The release calls it before publishing | ||
| on: | ||
| push: | ||
| branches: [main] | ||
| paths-ignore: | ||
| - '**.md' | ||
| - docs/** | ||
| pull_request: | ||
| paths-ignore: | ||
| - '**.md' | ||
| - docs/** | ||
| workflow_dispatch: | ||
| workflow_call: | ||
|
|
||
| # it only reads the repository | ||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| published-install: | ||
| name: Published install (npm ${{ matrix.npm }}) | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 30 | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| npm: [bundled, latest] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Use Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: '24' | ||
|
|
||
| - name: Use the latest npm | ||
| if: matrix.npm == 'latest' | ||
| run: npm install -g npm@latest | ||
|
|
||
| - name: Versions | ||
| run: | | ||
| node --version | ||
| npm --version | ||
|
|
||
| # the tarball npm publish would upload | ||
| - name: Pack | ||
| run: | | ||
| tgz=$(npm pack --pack-destination "$RUNNER_TEMP" --silent | tail -n 1) | ||
| echo "TGZ=$RUNNER_TEMP/$tgz" >> "$GITHUB_ENV" | ||
| echo "packed $tgz" | ||
|
|
||
| # a global install, into its own prefix so it needs no sudo: the same layout as | ||
| # /usr/lib/node_modules/fhirsmith. The dependencies come from the registry, as for npm install -g fhirsmith | ||
| - name: Install the package globally | ||
| run: | | ||
| prefix="$RUNNER_TEMP/global" | ||
| npm install -g --prefix "$prefix" "$TGZ" | ||
| echo "PKG_DIR=$(npm root -g --prefix "$prefix")/fhirsmith" >> "$GITHUB_ENV" | ||
|
|
||
| - name: Check the installed package | ||
| run: node .github/scripts/check-installed-package.js "$PKG_DIR" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.