From 95cb9bc5a66d15a227045553c833c02a5b8b11b9 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sun, 10 May 2020 15:56:13 -0400 Subject: [PATCH 1/5] Tweak tests to run against packed and installed ts-node instead of local dev tree --- package-lock.json | 5 +++ package.json | 1 + src/externs.d.ts | 4 +++ src/index.spec.ts | 34 +++++++++++++++---- tests/.gitignore | 4 ++- .../from-node-modules.ts | 0 .../node_modules/test.ts | 0 tests/package.json | 5 +++ 8 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 src/externs.d.ts rename tests/{ => from-node-modules}/from-node-modules.ts (100%) rename tests/{ => from-node-modules}/node_modules/test.ts (100%) create mode 100644 tests/package.json diff --git a/package-lock.json b/package-lock.json index dc05ff3c6..ab41169c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1170,6 +1170,11 @@ "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=", "dev": true }, + "pify": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-5.0.0.tgz", + "integrity": "sha512-eW/gHNMlxdSP6dmG6uJip6FXN0EQBwm2clYYd8Wul42Cwu/DK8HEftzsapcNdYe2MfLiIwZqsDk2RDEsTE79hA==" + }, "prelude-ls": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz", diff --git a/package.json b/package.json index 4c4eb65f0..3047d6bb5 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "istanbul": "^0.4.0", "mocha": "^6.2.2", "ntypescript": "^1.201507091536.1", + "pify": "^5.0.0", "proxyquire": "^2.0.0", "react": "^16.0.0", "rimraf": "^3.0.0", diff --git a/src/externs.d.ts b/src/externs.d.ts new file mode 100644 index 000000000..f44c08f51 --- /dev/null +++ b/src/externs.d.ts @@ -0,0 +1,4 @@ +declare module 'pify' { + const _export: typeof import('util').promisify + export = _export +} diff --git a/src/index.spec.ts b/src/index.spec.ts index 355cfcdf3..041be7a6b 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -5,16 +5,36 @@ import semver = require('semver') import ts = require('typescript') import proxyquire = require('proxyquire') import { register, create, VERSION } from './index' +import { mkdtempSync, readdirSync, copyFileSync, rmdirSync, unlinkSync, existsSync } from 'fs' +import * as promisify from 'pify' +const execP = promisify(exec) + +const ROOT_DIR = join(__dirname, '..') const TEST_DIR = join(__dirname, '../tests') +const TARBALL_PATH = join(TEST_DIR, 'ts-node-packed.tgz') const PROJECT = join(TEST_DIR, 'tsconfig.json') -const BIN_PATH = join(__dirname, '../dist/bin') -const BIN_SCRIPT_PATH = join(__dirname, '../dist/bin-script') +const BIN_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node') +const BIN_SCRIPT_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-script') const SOURCE_MAP_REGEXP = /\/\/# sourceMappingURL=data:application\/json;charset=utf\-8;base64,[\w\+]+=*$/ +// Pack and install ts-node locally, necessary to test package "exports" +before(async function () { + this.timeout(30000) + const tempDir = mkdtempSync(join(TEST_DIR, 'tmp')) + await execP(`npm pack "${ROOT_DIR}"`, { cwd: tempDir }) + const tarballPath = join(tempDir, readdirSync(tempDir)[0]) + copyFileSync(tarballPath, TARBALL_PATH) + unlinkSync(tarballPath) + rmdirSync(tempDir) + await execP(`npm install`, { cwd: TEST_DIR }) + const packageLockPath = join(TEST_DIR, 'package-lock.json') + existsSync(packageLockPath) && unlinkSync(packageLockPath) +}) + describe('ts-node', function () { - const cmd = `node "${BIN_PATH}" --project "${PROJECT}"` + const cmd = `"${BIN_PATH}" --project "${PROJECT}"` this.timeout(10000) @@ -35,7 +55,7 @@ describe('ts-node', function () { }) it('should register via cli', function (done) { - exec(`node -r ../register hello-world.ts`, { + exec(`node -r ts-node/register hello-world.ts`, { cwd: TEST_DIR }, function (err, stdout) { expect(err).to.equal(null) @@ -73,7 +93,7 @@ describe('ts-node', function () { }) it('should provide registered information on register', function (done) { - exec(`node -r ../register env.ts`, { + exec(`node -r ts-node/register env.ts`, { cwd: TEST_DIR }, function (err, stdout) { expect(err).to.equal(null) @@ -408,7 +428,7 @@ describe('ts-node', function () { } describe('should read ts-node options from tsconfig.json', function () { - const BIN_EXEC = `node "${join(__dirname, '../dist/bin')}" --project tests/tsconfig-options/tsconfig.json` + const BIN_EXEC = `"${BIN_PATH}" --project tests/tsconfig-options/tsconfig.json` it('should override compiler options from env', function (done) { exec(`${BIN_EXEC} tests/tsconfig-options/log-options.js`, { @@ -481,7 +501,7 @@ describe('ts-node', function () { }) it('should give ts error for invalid node_modules', function (done) { - exec(`${cmd} --compiler-host --skip-ignore tests/from-node-modules`, function (err, stdout) { + exec(`${cmd} --compiler-host --skip-ignore tests/from-node-modules/from-node-modules`, function (err, stdout) { if (err === null) return done('Expected an error') expect(err.message).to.contain('Unable to compile file from external library') diff --git a/tests/.gitignore b/tests/.gitignore index ddf342489..1f16711ae 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1 +1,3 @@ -!node_modules/ +!from-node-modules/node_modules/ +package-lock.json +ts-node-packed.tgz diff --git a/tests/from-node-modules.ts b/tests/from-node-modules/from-node-modules.ts similarity index 100% rename from tests/from-node-modules.ts rename to tests/from-node-modules/from-node-modules.ts diff --git a/tests/node_modules/test.ts b/tests/from-node-modules/node_modules/test.ts similarity index 100% rename from tests/node_modules/test.ts rename to tests/from-node-modules/node_modules/test.ts diff --git a/tests/package.json b/tests/package.json new file mode 100644 index 000000000..26351d2b8 --- /dev/null +++ b/tests/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "ts-node": "file:ts-node-packed.tgz" + } +} From f82c7f51a6aee2116e48008d663c4bb89a4ea445 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sun, 10 May 2020 16:06:23 -0400 Subject: [PATCH 2/5] avoid re-building when packing --- src/index.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.spec.ts b/src/index.spec.ts index 041be7a6b..223c5a29d 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -23,7 +23,7 @@ const SOURCE_MAP_REGEXP = /\/\/# sourceMappingURL=data:application\/json;charset before(async function () { this.timeout(30000) const tempDir = mkdtempSync(join(TEST_DIR, 'tmp')) - await execP(`npm pack "${ROOT_DIR}"`, { cwd: tempDir }) + await execP(`npm pack --ignore-scripts "${ROOT_DIR}"`, { cwd: tempDir }) const tarballPath = join(tempDir, readdirSync(tempDir)[0]) copyFileSync(tarballPath, TARBALL_PATH) unlinkSync(tarballPath) From 286969995495538337a99e09f8535b83b037e310 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sun, 10 May 2020 16:15:14 -0400 Subject: [PATCH 3/5] fix for node 6 --- package-lock.json | 17 ++++++++++++----- package.json | 4 ++-- src/externs.d.ts | 2 +- src/index.spec.ts | 6 +++--- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index ab41169c7..fbcac6edb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1170,11 +1170,6 @@ "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=", "dev": true }, - "pify": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/pify/-/pify-5.0.0.tgz", - "integrity": "sha512-eW/gHNMlxdSP6dmG6uJip6FXN0EQBwm2clYYd8Wul42Cwu/DK8HEftzsapcNdYe2MfLiIwZqsDk2RDEsTE79hA==" - }, "prelude-ls": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz", @@ -1585,6 +1580,18 @@ "source-map": "~0.6.1" } }, + "util.promisify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/util.promisify/-/util.promisify-1.0.1.tgz", + "integrity": "sha512-g9JpC/3He3bm38zsLupWryXHoEcS22YHthuPQSJdMy6KNrzIRzWqcsHzD/WUnqe45whVou4VIsPew37DoXWNrA==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "es-abstract": "^1.17.2", + "has-symbols": "^1.0.1", + "object.getownpropertydescriptors": "^2.1.0" + } + }, "whatwg-fetch": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz", diff --git a/package.json b/package.json index 3047d6bb5..e7ba719e3 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,6 @@ "istanbul": "^0.4.0", "mocha": "^6.2.2", "ntypescript": "^1.201507091536.1", - "pify": "^5.0.0", "proxyquire": "^2.0.0", "react": "^16.0.0", "rimraf": "^3.0.0", @@ -78,7 +77,8 @@ "tslint": "^6.1.0", "tslint-config-standard": "^9.0.0", "typescript": "^3.7.2", - "typescript-json-schema": "^0.42.0" + "typescript-json-schema": "^0.42.0", + "util.promisify": "^1.0.1" }, "peerDependencies": { "typescript": ">=2.7" diff --git a/src/externs.d.ts b/src/externs.d.ts index f44c08f51..d9257bd38 100644 --- a/src/externs.d.ts +++ b/src/externs.d.ts @@ -1,4 +1,4 @@ -declare module 'pify' { +declare module 'util.promisify' { const _export: typeof import('util').promisify export = _export } diff --git a/src/index.spec.ts b/src/index.spec.ts index 223c5a29d..5a55ac1cb 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -5,8 +5,8 @@ import semver = require('semver') import ts = require('typescript') import proxyquire = require('proxyquire') import { register, create, VERSION } from './index' -import { mkdtempSync, readdirSync, copyFileSync, rmdirSync, unlinkSync, existsSync } from 'fs' -import * as promisify from 'pify' +import { mkdtempSync, readdirSync, copyFileSync, rmdirSync, unlinkSync, existsSync, readFileSync, writeFileSync } from 'fs' +import * as promisify from 'util.promisify' const execP = promisify(exec) @@ -25,7 +25,7 @@ before(async function () { const tempDir = mkdtempSync(join(TEST_DIR, 'tmp')) await execP(`npm pack --ignore-scripts "${ROOT_DIR}"`, { cwd: tempDir }) const tarballPath = join(tempDir, readdirSync(tempDir)[0]) - copyFileSync(tarballPath, TARBALL_PATH) + writeFileSync(TARBALL_PATH, readFileSync(tarballPath)) unlinkSync(tarballPath) rmdirSync(tempDir) await execP(`npm install`, { cwd: TEST_DIR }) From 1bbc7cd32f9bd4a24224a28d3e465411dfcf8af7 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sun, 10 May 2020 19:09:13 -0400 Subject: [PATCH 4/5] move npm pack into build step --- package.json | 8 +++++--- scripts/build-pack.js | 20 ++++++++++++++++++++ src/index.spec.ts | 10 +--------- 3 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 scripts/build-pack.js diff --git a/package.json b/package.json index e7ba719e3..10c769361 100644 --- a/package.json +++ b/package.json @@ -22,14 +22,16 @@ "scripts": { "lint": "tslint \"src/**/*.ts\" --project tsconfig.json", "lint-fix": "tslint \"src/**/*.ts\" --project tsconfig.json --fix", - "clean": "rimraf dist && rimraf tsconfig.schema.json && rimraf tsconfig.schemastore-schema.json", - "build": "npm run clean && npm run build-tsc && npm run build-configSchema", + "clean": "rimraf dist && rimraf tsconfig.schema.json && rimraf tsconfig.schemastore-schema.json && rimraf tests/ts-node-packed.tgz", + "build": "npm run build-nopack && npm run build-pack", + "build-nopack": "npm run clean && npm run build-tsc && npm run build-configSchema", "build-tsc": "tsc", "build-configSchema": "typescript-json-schema --topRef --refs --validationKeywords allOf --out tsconfig.schema.json tsconfig.json TsConfigSchema && node --require ./register ./scripts/create-merged-schema", + "build-pack": "node ./scripts/build-pack.js", "test-spec": "mocha dist/**/*.spec.js -R spec --bail", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- \"dist/**/*.spec.js\" -R spec --bail", "test": "npm run build && npm run lint && npm run test-cov", - "prepare": "npm run build" + "prepare": "npm run build-nopack" }, "engines": { "node": ">=6.0.0" diff --git a/scripts/build-pack.js b/scripts/build-pack.js new file mode 100644 index 000000000..38811e99c --- /dev/null +++ b/scripts/build-pack.js @@ -0,0 +1,20 @@ +// Written in JS to support Windows +// Would otherwise be written as inline bash in package.json script + +const { exec } = require('child_process') +const { mkdtempSync, writeFileSync, readFileSync, unlinkSync, rmdirSync, readdirSync } = require('fs') +const { join } = require('path') + +const testDir = join(__dirname, '../tests') +const tarballPath = join(testDir, 'ts-node-packed.tgz') +const tempDir = mkdtempSync(join(testDir, 'tmp')) +exec(`npm pack "${join(__dirname, '..')}"`, { cwd: tempDir }, (err, stdout) => { + if (err) { + console.error(err) + process.exit(1) + } + const tempTarballPath = join(tempDir, readdirSync(tempDir)[0]) + writeFileSync(tarballPath, readFileSync(tempTarballPath)) + unlinkSync(tempTarballPath) + rmdirSync(tempDir) +}) diff --git a/src/index.spec.ts b/src/index.spec.ts index 5a55ac1cb..0d41f65d2 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -5,14 +5,12 @@ import semver = require('semver') import ts = require('typescript') import proxyquire = require('proxyquire') import { register, create, VERSION } from './index' -import { mkdtempSync, readdirSync, copyFileSync, rmdirSync, unlinkSync, existsSync, readFileSync, writeFileSync } from 'fs' +import { unlinkSync, existsSync } from 'fs' import * as promisify from 'util.promisify' const execP = promisify(exec) -const ROOT_DIR = join(__dirname, '..') const TEST_DIR = join(__dirname, '../tests') -const TARBALL_PATH = join(TEST_DIR, 'ts-node-packed.tgz') const PROJECT = join(TEST_DIR, 'tsconfig.json') const BIN_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node') const BIN_SCRIPT_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-script') @@ -22,12 +20,6 @@ const SOURCE_MAP_REGEXP = /\/\/# sourceMappingURL=data:application\/json;charset // Pack and install ts-node locally, necessary to test package "exports" before(async function () { this.timeout(30000) - const tempDir = mkdtempSync(join(TEST_DIR, 'tmp')) - await execP(`npm pack --ignore-scripts "${ROOT_DIR}"`, { cwd: tempDir }) - const tarballPath = join(tempDir, readdirSync(tempDir)[0]) - writeFileSync(TARBALL_PATH, readFileSync(tarballPath)) - unlinkSync(tarballPath) - rmdirSync(tempDir) await execP(`npm install`, { cwd: TEST_DIR }) const packageLockPath = join(TEST_DIR, 'package-lock.json') existsSync(packageLockPath) && unlinkSync(packageLockPath) From b56dfdefbb7e4ec111e300eb2bc7d99e9ab94a31 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sun, 10 May 2020 19:22:08 -0400 Subject: [PATCH 5/5] update package-lock --- package-lock.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/package-lock.json b/package-lock.json index b983b8851..23e6967c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1580,6 +1580,18 @@ "source-map": "~0.6.1" } }, + "util.promisify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/util.promisify/-/util.promisify-1.0.1.tgz", + "integrity": "sha512-g9JpC/3He3bm38zsLupWryXHoEcS22YHthuPQSJdMy6KNrzIRzWqcsHzD/WUnqe45whVou4VIsPew37DoXWNrA==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "es-abstract": "^1.17.2", + "has-symbols": "^1.0.1", + "object.getownpropertydescriptors": "^2.1.0" + } + }, "whatwg-fetch": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz",