diff --git a/.github/workflows/package-manager-ci.yml b/.github/workflows/package-manager-ci.yml new file mode 100644 index 000000000..20bd81cd8 --- /dev/null +++ b/.github/workflows/package-manager-ci.yml @@ -0,0 +1,91 @@ +name: package-manager-ci +on: + push: + branches: + - main + - package-manager-load +jobs: + pnpm: + name: pnpm package manager on ${{ matrix.node-version }} ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [windows-latest, ubuntu-latest] + node-version: [14] + steps: + - uses: actions/checkout@v2.3.4 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2.4.0 + with: + node-version: ${{ matrix.node-version }} + - name: Use pnpm + uses: pnpm/action-setup@v2.0.1 + with: + version: ^6.0.0 + - name: Install dependancies + run: pnpm install + - name: Tests + run: pnpm run test-ci-pnpm + + yarn: + name: yarn package manager on ${{ matrix.node-version }} ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [windows-latest, ubuntu-latest] + node-version: [14] + steps: + - uses: actions/checkout@v2.3.4 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2.4.0 + with: + node-version: ${{ matrix.node-version }} + - name: Use yarn + run: | + yarn set version berry && yarn set version 2 + echo "nodeLinker: node-modules" >> .yarnrc.yml + # see https://github.com/yarnpkg/berry/issues/2935#issuecomment-911299992 + yarn add --dev typescript@~4.3.2 + yarn install + env: + # needed due the yarn.lock file in pino's .gitignore + YARN_ENABLE_IMMUTABLE_INSTALLS: false + - name: Tests + run: yarn run test-ci + + yarn-pnp: + name: yarn-pnp package manager on ${{ matrix.node-version }} ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [windows-latest, ubuntu-latest] + node-version: [14] + steps: + - uses: actions/checkout@v2.3.4 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2.4.0 + with: + node-version: ${{ matrix.node-version }} + - name: Use yarn + run: | + yarn set version berry + echo 'nodeLinker: pnp + packageExtensions: + debug@*: + dependencies: + supports-color: "*" + treport@*: + dependencies: + tap-yaml: "*" + ' >> .yarnrc.yml + yarn add --dev typescript@~4.3.2 + yarn install + yarn add --dev transport@link:./test/fixtures/transport + env: + # needed due the yarn.lock file in pino's .gitignore + YARN_ENABLE_IMMUTABLE_INSTALLS: false + - name: Tests + run: yarn run test-ci-yarn-pnp diff --git a/.gitignore b/.gitignore index b552ce577..bab272c8f 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,12 @@ build/Release # Dependency directory node_modules +# yarn cache +.yarn +.yarnrc.yml +.pnp +.pnp.* + # Optional npm cache directory .npm diff --git a/lib/worker.js b/lib/worker.js index 0b6c4989b..184ade043 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -10,8 +10,19 @@ const build = require('pino-abstract-transport') module.exports = async function ({ targets }) { targets = await Promise.all(targets.map(async (t) => { - const toLoad = 'file://' + t.target - const stream = await (await import(toLoad)).default(t.options) + let fn + try { + const toLoad = 'file://' + t.target + fn = (await import(toLoad)).default + } catch (error) { + // See this PR for details: https://github.com/pinojs/thread-stream/pull/34 + if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND')) { + fn = require(t.target) + } else { + throw error + } + } + const stream = await fn(t.options) return { level: t.level, stream diff --git a/package.json b/package.json index ab1a67617..2e157e20e 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,8 @@ "lint": "eslint .", "test": "npm run lint && tap test/*test.js test/*/*test.js && npm run test-types", "test-ci": "npm run lint && tap --no-check-coverage test/*test.js test/*/*test.js --coverage-report=lcovonly && npm run test-types", + "test-ci-pnpm": "pnpm run lint && tap --no-coverage --no-check-coverage test/*test.js test/*/*test.js && pnpm run test-types", + "test-ci-yarn-pnp": "yarn run lint && tap --no-check-coverage test/*test.js test/*/*test.js --coverage-report=lcovonly", "test-types": "tsc && tsd && ts-node test/types/pino.ts", "cov-ui": "tap --coverage-report=html test/*test.js test/*/*test.js", "bench": "node benchmarks/utils/runbench all", @@ -108,7 +110,7 @@ "pino-std-serializers": "^4.0.0", "quick-format-unescaped": "^4.0.3", "sonic-boom": "^2.2.1", - "thread-stream": "^0.11.0" + "thread-stream": "^0.11.1" }, "tsd": { "directory": "test/types" diff --git a/test/esm/index.test.js b/test/esm/index.test.js index 0d1a96cc8..80ac19212 100644 --- a/test/esm/index.test.js +++ b/test/esm/index.test.js @@ -3,7 +3,9 @@ const t = require('tap') const semver = require('semver') -if (!semver.satisfies(process.versions.node, '^13.3.0 || ^12.10.0 || >= 14.0.0')) { +const { isYarnPnp } = require('../helper') + +if (!semver.satisfies(process.versions.node, '^13.3.0 || ^12.10.0 || >= 14.0.0') || isYarnPnp) { t.skip('Skip esm because not supported by Node') } else { // Node v8 throw a `SyntaxError: Unexpected token import` @@ -17,7 +19,7 @@ if (!semver.satisfies(process.versions.node, '^13.3.0 || ^12.10.0 || >= 14.0.0') }) } -if (!semver.satisfies(process.versions.node, '>= 14.13.0 || ^12.20.0')) { +if (!semver.satisfies(process.versions.node, '>= 14.13.0 || ^12.20.0') || isYarnPnp) { t.skip('Skip named exports because not supported by Node') } else { // Node v8 throw a `SyntaxError: Unexpected token import` diff --git a/test/helper.js b/test/helper.js index ed8073453..f2f468008 100644 --- a/test/helper.js +++ b/test/helper.js @@ -8,6 +8,7 @@ const pid = process.pid const hostname = os.hostname() const isWin = process.platform === 'win32' +const isYarnPnp = process.versions.pnp !== undefined function getPathToNull () { return isWin ? '\\\\.\\NUL' : '/dev/null' @@ -72,4 +73,4 @@ function watchFileCreated (filename) { }) } -module.exports = { getPathToNull, sink, check, once, sleep, watchFileCreated, isWin } +module.exports = { getPathToNull, sink, check, once, sleep, watchFileCreated, isWin, isYarnPnp } diff --git a/test/transport.test.js b/test/transport.test.js index 9cd95ee5d..d77e99843 100644 --- a/test/transport.test.js +++ b/test/transport.test.js @@ -5,7 +5,7 @@ const { join } = require('path') const { once } = require('events') const { readFile, symlink, unlink } = require('fs').promises const { test } = require('tap') -const { isWin, watchFileCreated } = require('./helper') +const { isWin, isYarnPnp, watchFileCreated } = require('./helper') const pino = require('../') const url = require('url') const strip = require('strip-ansi') @@ -15,6 +15,26 @@ const writer = require('flush-write-stream') const { pid } = process const hostname = os.hostname() +async function installTransportModule () { + if (isYarnPnp) { + return + } + try { + await uninstallTransportModule() + } catch {} + await symlink( + join(__dirname, 'fixtures', 'transport'), + join(__dirname, '..', 'node_modules', 'transport') + ) +} + +async function uninstallTransportModule () { + if (isYarnPnp) { + return + } + await unlink(join(__dirname, '..', 'node_modules', 'transport')) +} + test('pino.transport with file', async ({ same, teardown }) => { const destination = join( os.tmpdir(), @@ -53,21 +73,14 @@ test('pino.transport with package', { skip: isWin }, async ({ same, teardown }) '_' + Math.random().toString(36).substr(2, 9) ) - try { - await unlink(join(__dirname, '..', 'node_modules', 'transport')) - } catch {} - - await symlink( - join(__dirname, 'fixtures', 'transport'), - join(__dirname, '..', 'node_modules', 'transport') - ) + await installTransportModule() const transport = pino.transport({ target: 'transport', options: { destination } }) teardown(async () => { - await unlink(join(__dirname, '..', 'node_modules', 'transport')) + await uninstallTransportModule() transport.end() }) const instance = pino(transport) @@ -297,14 +310,7 @@ test('pino.transport with package as a target', { skip: isWin }, async ({ same, '_' + Math.random().toString(36).substr(2, 9) ) - try { - await unlink(join(__dirname, '..', 'node_modules', 'transport')) - } catch {} - - await symlink( - join(__dirname, 'fixtures', 'transport'), - join(__dirname, '..', 'node_modules', 'transport') - ) + await installTransportModule() const transport = pino.transport({ targets: [{ @@ -313,7 +319,7 @@ test('pino.transport with package as a target', { skip: isWin }, async ({ same, }] }) teardown(async () => { - await unlink(join(__dirname, '..', 'node_modules', 'transport')) + await uninstallTransportModule() transport.end() }) const instance = pino(transport)