Skip to content

Commit

Permalink
fix: normalizes bundle hash on any node version
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Aug 21, 2018
1 parent d03e0e7 commit ce7afaf
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jspm_packages
tests/simple-long-path/long-src-path
# is linked to the temp dir of the os
e2e/__workdir_synlink__
**/.ts-jest-e2e.json
# while refactoring...
old/

Expand Down
15 changes: 15 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ jspm_packages
old

*.tgz

# others
.npmignore
.eslintrc.js
jest.config.js
.vscode
.github
appveyor.yml
.editorconfig
.prettierignore
tsconfig.json
.prettierrc
.travis.yml
tsconfig.build.json
tslint.json
1 change: 1 addition & 0 deletions e2e/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ module.exports = {
'<rootDir>/e2e/__serializers__/test-run-result.ts',
'<rootDir>/e2e/__serializers__/test-file-io.ts',
],
verbose: true,
}
56 changes: 53 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"doctoc": "^1.3.1",
"eslint": "^5.4.0",
"fs-extra": "^7.0.0",
"glob-gitignore": "^1.0.9",
"husky": "^0.14.3",
"jest": "^23.4.1",
"js-yaml": "^3.12.0",
Expand Down
12 changes: 6 additions & 6 deletions scripts/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ const path = require('path')
const Paths = require('./lib/paths')
const { createHash } = require('crypto')
const logger = require('./lib/logger')
const { createBundle, realDigest } = require('./lib/bundle')
const { createBundle, packageDigest } = require('./lib/bundle')
const npm = require('./lib/npm')

const configFile = path.resolve(__dirname, '..', 'e2e', 'jest.config.js')
const configFile = path.join(Paths.e2eRootDir, 'jest.config.js')

let parentArgs = process.argv.slice(2)
if (parentArgs.includes('--coverage')) {
logger.warn(
'Coverages cannot be activated for e2e tests (but can in each e2e test).'
)
parentArgs = parentArgs.filter(a => a !== '--coverage')
}
if (!parentArgs.includes('--verbose')) parentArgs.push('--verbose')

function getDirectories(rootDir) {
return fs.readdirSync(rootDir).filter(function(file) {
Expand All @@ -30,7 +30,7 @@ function getDirectories(rootDir) {
function sha1(...data) {
const hash = createHash('sha1')
data.forEach(item => hash.update(item))
return hash.digest('base64').toString()
return hash.digest('hex').toString()
}

function log(...msg) {
Expand All @@ -48,8 +48,8 @@ function setupE2e() {

// get the hash of the bundle (to know if we should install it again or not)
// we need to compute it ourselfs as the npm pack creates different tgz even tho content has not changed
const bundleHash = realDigest(bundle)
log('bundled ts-jest digest:', bundleHash)
const bundleHash = packageDigest(bundle)
log('ts-jest digest:', bundleHash)

// ensure directory exists before copying over
fs.mkdirpSync(Paths.e2eWorkTemplatesDir)
Expand Down
32 changes: 28 additions & 4 deletions scripts/lib/bundle.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
const npm = require('./npm')
const logger = require('./logger')
const { rootDir } = require('./paths')
const { join } = require('path')
const { join, resolve } = require('path')
const { readFileSync, statSync } = require('fs')
const { createHash } = require('crypto')
const { sync: globIgnore } = require('glob-gitignore')

// This will trigger the build as well (not using yarn since yarn pack is bugy)
// Except that on npm < 4.0.0 the prepare doesn't exists

module.exports = function createBundle() {
function createBundle(log = logger.log.bind(logger)) {
if (!npm.can.prepare()) {
logger.log('building ts-jest')
log('building ts-jest')
npm.spawnSync(['-s', 'run', 'build'], { cwd: rootDir })
}
logger.log('creating ts-jest bundle')
log('creating ts-jest bundle')
const res = npm.spawnSync(['-s', 'pack'], { cwd: rootDir })
return join(rootDir, res.stdout.toString().trim())
}

function packageDigest() {
const files = globIgnore(join(rootDir, '**'), {
ignore: readFileSync(join(rootDir, '.npmignore'))
.toString('utf8')
.split(/\n/g)
.filter(l => l && !/^(\s*#.+|\s*)$/.test(l)),
})
const hash = createHash('sha1')
files.sort().forEach(file => {
if (statSync(file).isDirectory()) return
hash.update(readFileSync(resolve(file)))
hash.update('\x00')
})
return hash.digest('hex')
}

module.exports = {
createBundle,
packageDigest,
}
2 changes: 1 addition & 1 deletion scripts/test-external-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { tmpdir } = require('os')
const { rootDir } = require('./lib/paths')
const { existsSync, realpathSync } = require('fs')
const logger = require('./lib/logger')
const createBundle = require('./lib/bundle')
const { createBundle } = require('./lib/bundle')
const npm = require('./lib/npm')

let projectPath = process.argv[2]
Expand Down
8 changes: 4 additions & 4 deletions src/lib/debug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('debug', () => {
__setup()
debug('foo')
expect(stdoutSpy).toHaveBeenCalledTimes(1)
expect(stdoutSpy).toHaveBeenCalledWith('ts-jest: foo\n')
expect(stdoutSpy.mock.calls[0][0]).toBe('ts-jest: foo\n')
})
it('should NOT log to stdout when TS_JEST_DEBUG is falsy', () => {
process.env.TS_JEST_DEBUG = ''
Expand All @@ -40,14 +40,14 @@ describe('warn', () => {
__setup()
warn('foo')
expect(stderrSpy).toHaveBeenCalledTimes(1)
expect(stderrSpy).toHaveBeenCalledWith('ts-jest: foo\n')
expect(stderrSpy.mock.calls[0][0]).toBe('ts-jest: foo\n')
})
it('should log to stderr even when TS_JEST_DEBUG is falsy', () => {
delete process.env.TS_JEST_DEBUG
__setup()
warn('foo')
expect(stderrSpy).toHaveBeenCalledTimes(1)
expect(stderrSpy).toHaveBeenCalledWith('ts-jest: foo\n')
expect(stderrSpy.mock.calls[0][0]).toBe('ts-jest: foo\n')
})
})

Expand All @@ -60,7 +60,7 @@ describe('wrapWithDebug', () => {
__setup()
expect(wrapAndCall('bar')).toBe('hello bar')
expect(stdoutSpy).toHaveBeenCalledTimes(1)
expect(stdoutSpy).toHaveBeenCalledWith('ts-jest: foo\n')
expect(stdoutSpy.mock.calls[0][0]).toBe('ts-jest: foo\n')
})
it('should NOT log to stdout when TS_JEST_DEBUG is falsy', () => {
process.env.TS_JEST_DEBUG = ''
Expand Down
5 changes: 3 additions & 2 deletions src/lib/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export const defaultLogger: Logger = (
...args: any[]
) => {
// we use stderr/stdout dirrectly so that the log won't be swallowed by jest
// the dummy calback is to ensure output on CI with node 6
if (kind === 'warn') {
process.stderr.write(format(msg, ...args) + '\n')
process.stderr.write(format(msg, ...args) + '\n', () => undefined)
} else if (kind) {
process.stdout.write(format(msg, ...args) + '\n')
process.stdout.write(format(msg, ...args) + '\n', () => undefined)
}
}

Expand Down

0 comments on commit ce7afaf

Please sign in to comment.