• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const fixtures = require('../common/fixtures');
4const path = require('path');
5const assert = require('assert');
6const exec = require('child_process').execFile;
7const fs = require('fs');
8
9const tmpdir = require('../common/tmpdir');
10tmpdir.refresh();
11const tmpDir = tmpdir.path;
12
13// Check that running the symlink executes the target as the correct type
14const symlinks = [
15  {
16    source: 'extensionless-symlink-to-mjs-file',
17    target: fixtures.path('es-modules/mjs-file.mjs'),
18    prints: '.mjs file',
19    errorsWithPreserveSymlinksMain: false
20  }, {
21    source: 'extensionless-symlink-to-cjs-file',
22    target: fixtures.path('es-modules/cjs-file.cjs'),
23    prints: '.cjs file',
24    errorsWithPreserveSymlinksMain: false
25  }, {
26    source: 'extensionless-symlink-to-file-in-module-scope',
27    target: fixtures.path('es-modules/package-type-module/index.js'),
28    prints: 'package-type-module',
29    // The package scope of the symlinks' sources is commonjs, and this
30    // symlink's target is a .js file in a module scope, so when the scope
31    // is evaluated based on the source (commonjs) this esm file should error
32    errorsWithPreserveSymlinksMain: true
33  }, {
34    source: 'extensionless-symlink-to-file-in-explicit-commonjs-scope',
35    target: fixtures.path('es-modules/package-type-commonjs/index.js'),
36    prints: 'package-type-commonjs',
37    errorsWithPreserveSymlinksMain: false
38  }, {
39    source: 'extensionless-symlink-to-file-in-implicit-commonjs-scope',
40    target: fixtures.path('es-modules/package-without-type/index.js'),
41    prints: 'package-without-type',
42    errorsWithPreserveSymlinksMain: false
43  },
44];
45
46symlinks.forEach((symlink) => {
47  const mainPath = path.join(tmpDir, symlink.source);
48  fs.symlinkSync(symlink.target, mainPath);
49
50  const flags = [
51    '',
52    '--preserve-symlinks-main',
53  ];
54  flags.forEach((nodeOptions) => {
55    const opts = {
56      env: Object.assign({}, process.env, { NODE_OPTIONS: nodeOptions })
57    };
58    exec(process.execPath, [mainPath], opts, common.mustCall(
59      (err, stdout) => {
60        if (nodeOptions.includes('--preserve-symlinks-main')) {
61          if (symlink.errorsWithPreserveSymlinksMain &&
62              err.toString().includes('Error')) return;
63          else if (!symlink.errorsWithPreserveSymlinksMain &&
64                    stdout.includes(symlink.prints)) return;
65          assert.fail(`For ${JSON.stringify(symlink)}, ${
66            (symlink.errorsWithPreserveSymlinksMain) ?
67              'failed to error' : 'errored unexpectedly'
68          } with --preserve-symlinks-main`);
69        } else {
70          if (stdout.includes(symlink.prints)) return;
71          assert.fail(`For ${JSON.stringify(symlink)}, failed to find ` +
72            `${symlink.prints} in: <\n${stdout}\n>`);
73        }
74      }
75    ));
76  });
77});
78