Skip to content

Commit

Permalink
Merge pull request #10 from lucastheisen/master
Browse files Browse the repository at this point in the history
Build does not work cross platform #9
  • Loading branch information
ktsn authored May 26, 2017
2 parents 4043d12 + f7347b6 commit 4f48222
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 42 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
},
"scripts": {
"prepublish": "npm run lint && npm run test && npm run clean && npm run build",
"clean": "rm -rf dist",
"clean": "rimraf dist",
"dev": "tsc -p src -w",
"build": "tsc -p src",
"lint": "tslint \"src/**/*.ts\" && tslint \"test/specs/**/*.ts\"",
"test": "rm -rf test/fixtures/*.d.ts && mocha --compilers ts:espower-typescript/guess test/specs/**/*.ts",
"test": "rimraf test/fixtures/*.d.ts && mocha --compilers ts:espower-typescript/guess test/specs/**/*.ts",
"test:debug": "npm t -- --inspect --debug-brk --recursive"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion test/expects/ts-object.vue.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare var _default: {
declare const _default: {
data(): {
foo: string;
};
Expand Down
40 changes: 23 additions & 17 deletions test/specs/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,45 @@ const host: ts.ParseConfigHost = {

const exists = (fileName: string) => fs.existsSync(fileName)

const root = path.resolve('/')
const pathTo = (...parts: string[]) => path.join(root, 'path', 'to', ...parts)

describe('tsconfig detection', () => {
beforeEach(clear)

it('should find tsconfig.json on a directory', () => {
mock('/path/to/tsconfig.json', {})
const tsconfig = pathTo('tsconfig.json')
mock(tsconfig, {})

const pathname = findConfig('/path/to', exists)
assert(pathname === '/path/to/tsconfig.json')
const pathname = findConfig(pathTo(), exists)
assert(pathname === tsconfig)
})

it('should find tsconfig.json on the closest ancestor', () => {
mock('/path/to/tsconfig.json', {})
mock('/path/to/b/tsconfig.json', {})
mock('/path/to/c/tsconfig.json', {})
mock('/path/to/b/src/test.ts', {})
mock(pathTo('tsconfig.json'), {})
mock(pathTo('b', 'tsconfig.json'), {})
mock(pathTo('c', 'tsconfig.json'), {})
mock(pathTo('b', 'src', 'test.ts'), {})

const pathname = findConfig('/path/to/b/src', exists)
assert(pathname === '/path/to/b/tsconfig.json')
const pathname = findConfig(pathTo('b', 'src'), exists)
assert(pathname === pathTo('b', 'tsconfig.json'))
})

it('returns undefined if config is not found', () => {
const pathname = findConfig('/path/to/src', exists)
const pathname = findConfig(pathTo('src'), exists)
assert(pathname === undefined)
})

it('read tsconfig.json', () => {
mock('/path/to/tsconfig.json', {
mock(pathTo('tsconfig.json'), {
compilerOptions: {
target: 'es5',
module: 'es2015',
moduleResolution: 'node',
experimentalDecorators: true
}
})
const data = readConfig('/path/to/tsconfig.json', host)
const data = readConfig(pathTo('tsconfig.json'), host)
assert.ok(data)

const options = data!.options
Expand All @@ -72,18 +76,20 @@ describe('tsconfig detection', () => {
})

it('returns undefined if the config file is not found', () => {
const data = readConfig('/path/to/tsconfig.json', host)
const data = readConfig(pathTo('tsconfig.json'), host)
assert.ifError(data)
})
})

function mock (fileName: string, data: any): void {
fs.mkdirpSync(fileName.split('/').slice(0, -1).join('/'))
fs.mkdirpSync(path.resolve(fileName, '..'))
fs.writeFileSync(fileName, JSON.stringify(data))
}

function clear (): void {
fs.readdirSync('/').forEach(dir => {
fs.rmdirSync('/' + dir)
})
if (root === '/' || fs.existsSync(root)) {
fs.readdirSync(root).forEach(dir => {
fs.rmdirSync(path.join(root, dir))
})
}
}
9 changes: 5 additions & 4 deletions test/specs/file-util.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import assert = require('power-assert')
import path = require('path')
import { deepestSharedRoot } from '../../src/lib/file-util'

describe('file-util', () => {
it('detects deepest shared root', () => {
const actual = deepestSharedRoot([
'foo/bar/baz',
'foo/bar/foo',
'foo/bar/baz'
path.join('foo', 'bar', 'baz'),
path.join('foo', 'bar', 'foo'),
path.join('foo', 'bar', 'baz')
])

assert(actual === 'foo/bar')
assert(actual === path.join('foo', 'bar'))
})
})
38 changes: 20 additions & 18 deletions test/specs/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,62 @@ import assert = require('power-assert')
import ts = require('typescript')
import { generate } from '../../src/lib/generate'

const resolve = (_path: string) => path.resolve(__dirname, '../', _path)
const testDir = path.resolve(__dirname, '..')
const fixtures = (...parts: string[]) => path.join(testDir, 'fixtures', ...parts)
const expects = (...parts: string[]) => path.join(testDir, 'expects', ...parts)

const compilerOptions: ts.CompilerOptions = {
experimentalDecorators: true
}

function gen(fileName: string, options: ts.CompilerOptions): Promise<never> {
return generate([resolve(fileName)], options)
return generate([fileName], options)
}

function test(a: string, b: string) {
const aStr = normalize(fs.readFileSync(resolve(a), 'utf8'))
const bStr = normalize(fs.readFileSync(resolve(b), 'utf8'))
const aStr = normalize(fs.readFileSync(a, 'utf8'))
const bStr = normalize(fs.readFileSync(b, 'utf8'))
assert.equal(aStr, bStr)
}

function notExists(file: string) {
assert.ok(!fs.existsSync(resolve(file)))
function notExists(fileName: string) {
assert.ok(!fs.existsSync(fileName))
}

describe('generate', () => {
it('should emit d.ts for class component in sfc', () => {
return gen('fixtures/ts-class.vue', compilerOptions).then(() => {
test('fixtures/ts-class.vue.d.ts', 'expects/ts-class.vue.d.ts')
return gen(fixtures('ts-class.vue'), compilerOptions).then(() => {
test(fixtures('ts-class.vue.d.ts'), expects('ts-class.vue.d.ts'))
})
})

it('should emit d.ts for component options in sfc', () => {
return gen('fixtures/ts-object.vue', {}).then(() => {
test('fixtures/ts-object.vue.d.ts', 'expects/ts-object.vue.d.ts')
return gen(fixtures('ts-object.vue'), {}).then(() => {
test(fixtures('ts-object.vue.d.ts'), expects('ts-object.vue.d.ts'))
})
})

it('should not emit d.ts for js', () => {
return gen('fixtures/js.vue', {}).then(() => {
notExists('fixtures/js.vue.d.ts')
return gen(fixtures('js.vue'), {}).then(() => {
notExists(fixtures('js.vue.d.ts'))
})
})

it('should not emit d.ts for normal ts', () => {
return gen('fixtures/not-vue.ts', {}).then(() => {
notExists('fixtures/not-vue.d.ts')
return gen(fixtures('not-vue.ts'), {}).then(() => {
notExists(fixtures('not-vue.d.ts'))
})
})

it('should not emit d.ts if there are errors', () => {
return gen('fixtures/ts-error.vue', compilerOptions).then(() => {
notExists('fixtures/ts-error.vue.d.ts')
return gen(fixtures('ts-error.vue'), compilerOptions).then(() => {
notExists(fixtures('ts-error.vue.d.ts'))
})
})

it('should emit d.ts with imported ts types', () => {
return gen('fixtures/import.vue', compilerOptions).then(() => {
test('fixtures/import.vue.d.ts', 'expects/import.vue.d.ts')
return gen(fixtures('import.vue'), compilerOptions).then(() => {
test(fixtures('import.vue.d.ts'), expects('import.vue.d.ts'))
})
})
})
Expand Down

0 comments on commit 4f48222

Please sign in to comment.