Skip to content

Commit

Permalink
refact: remove ttlcache package, make src/bin.js clean
Browse files Browse the repository at this point in the history
  • Loading branch information
i5ting committed Oct 2, 2022
1 parent d0970a2 commit f2e6c97
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 80 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@
},
"homepage": "https://github.com/i5ting/ts-junit#readme",
"dependencies": {
"@isaacs/ttlcache": "^1.2.0",
"debug": "^4.3.1",
"recast": "^0.21.5",
"typescript": "^4.8.3",
"uvu": "^0.5.1",
"yargs": "^17.5.1"
},
"devDependencies": {
"@types/lru-cache": "^7.10.10",
"@types/node": "^18.7.18",
"@types/yargs": "^17.0.13",
"concurrently": "^5.3.0",
Expand Down
7 changes: 0 additions & 7 deletions src/Context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import * as fs from 'node:fs'
import { load } from './loadObject/scan';

import { Debug } from './Utils'

// import { Watch } from './Watch'

import { Parse } from './parse'

const debug = Debug("ts-junit")

import IStrategy from './IStrategy'
Expand Down Expand Up @@ -50,7 +45,6 @@ export default class Context {

assert(fs.existsSync(jsFile))


debug(" --- runTest --- ")
let nodeList = [load(jsFile)]

Expand Down Expand Up @@ -83,5 +77,4 @@ export default class Context {
this.strategy.test.run()
}
}

}
1 change: 0 additions & 1 deletion src/Parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const debug = Debug('parse')
export function getEableRunDataMapping(commonjsFile: string) {
const allTest = getDataMapping(commonjsFile)
const clazz = allTest.find(item => item['Class']?.length > 0)
// console.dir(clazz)

if (clazz['Disabled']?.length > 0) {
console.log(clazz['Disabled'] + 'has @Disabled decorator, no need to run any test!')
Expand Down
1 change: 0 additions & 1 deletion src/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from 'node:fs'
// import * as Debugger from 'debug'
import debugModule from 'debug';
const debug = new debugModule('foo');

Expand Down
13 changes: 5 additions & 8 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ export const processedFiles = new Set()
export const needCompileFiles = new Array()

function getImportsForFile(file: string, options?: any) {
// console.dir(file)
const fileInfo = ts.preProcessFile(readFileSync(file).toString())

if (options && options.verbose)
console.log('getImportsForFile ' + file + ': ' + fileInfo.importedFiles.map((el) => el.fileName).join(', '))
return fileInfo.importedFiles
.map((importedFile: FileReference) => importedFile.fileName)
.flatMap((fileName: string) => {
// console.dir(fileName)
if (!fileName.startsWith('.')) {
libFiles.add(fileName)
}

return fileName
// flat map is not ideal here, because we could hit multiple valid imports, and not the first aka best one
//return applyPathMapping(fileName, path_mapping)
})
.filter((x: string) => x.startsWith('.')) // only relative paths allowed
.flatMap((fileName: string) => {
Expand Down Expand Up @@ -60,8 +57,8 @@ export function getAllImportsForFile(file: string, options?: Object) {
getImportsForFile(file, options)
let count: number = 0
localFiles.forEach((i) => {
count++;
//
count++

if (!processedFiles.has(i) && count > 0) {
// processedFiles.add(i)
getAllImportsForFile(i, options)
Expand All @@ -79,8 +76,8 @@ export function getDependencyImports(files: any) {
const alibFiles = new Set()
const alocalFiles = new Set()

files.map((a: string) => {
getImportsForFile(a)
files.map((file: string) => {
getImportsForFile(file)

libFiles.forEach(alibFiles.add, alibFiles)
localFiles.forEach(alibFiles.add, alocalFiles)
Expand Down
44 changes: 2 additions & 42 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,8 @@
#!/usr/bin/env node

import fs from 'node:fs'
import path from 'node:path'

import yargs from 'yargs/yargs';

import { hideBin } from 'yargs/helpers'

import { executeWithDefaultStrategy, executeFileWithDefaultStrategy } from '.'

const argv = yargs(hideBin(process.argv)).argv

// console.dir(argv)

run(argv['_'])


function run(rest: any) {
rest.map(function (i: string) {
let item = path.resolve(process.cwd(), i)

try {
const stat = fs.lstatSync(item)

let fileOrDirType = stat.isDirectory() ? 'dir' : stat.isFile() ? 'file' : 'other'

switch (fileOrDirType) {
case 'dir':
console.warn('find dir ' + item)
executeWithDefaultStrategy([item])
break;
case 'file':
console.warn('find file 2' + item.replace('.ts', ''))

executeFileWithDefaultStrategy([item.replace('.ts', '')])
break;
default:
console.warn('unknow type')
break;
}
} catch (error) {
throw error
}

})
}
import { execute } from '.'

execute(yargs(hideBin(process.argv)).argv['_'])
6 changes: 0 additions & 6 deletions src/cache.ts

This file was deleted.

37 changes: 32 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import fs from 'node:fs'
import path from 'node:path'
import Context from './Context'
import IStrategy from './IStrategy'
import UvuStrategy from './UvuStrategy'
import { WatchDir, WatchFile } from './Watch'
import { Debug } from './Utils'
Expand All @@ -16,7 +18,6 @@ export * from './loadObject/scan'
export * from './loadObject/require'
export * from './loadObject/flatten'


export function executeWithDefaultStrategy(dirs: string[]) {
debug('executeWithDefaultStrategy')

Expand All @@ -39,8 +40,34 @@ export function executeFileWithDefaultStrategy(testFiles: string[]) {
})
}

export function execute(dir: string[], strategy: IStrategy) {
export function execute(rest: any) {
debug('execute With Strategy')
const context = new Context(strategy)
// context.runTests(dir)
console.time('build ts')
rest.map(function (i: string) {
let item = path.resolve(process.cwd(), i)

try {
const stat = fs.lstatSync(item)

let fileOrDirType = stat.isDirectory() ? 'dir' : stat.isFile() ? 'file' : 'other'

switch (fileOrDirType) {
case 'dir':
console.warn('find dir ' + item)
executeWithDefaultStrategy([item])
break;
case 'file':
console.warn('find file 2' + item.replace('.ts', ''))

executeFileWithDefaultStrategy([item.replace('.ts', '')])
break;
default:
console.warn('unknow type')
break;
}
} catch (error) {
throw error
}
})
console.timeEnd('build ts')
}
4 changes: 1 addition & 3 deletions src/watch.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// import * as os from "os";
import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";

import { EventEmitter } from 'node:events';

import { getDependencyImports, getAllImportsForFile, getNeedCompileFiles, libFiles } from "./ast";
import { getAllImportsForFile, getNeedCompileFiles } from "./ast";

import { Debug } from './Utils'
import Context from './Context'
import { cache } from './cache'
import { getTsFiles } from './index'

const debug = Debug('watch')
Expand Down
1 change: 0 additions & 1 deletion test/parse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { test } from 'uvu';
import * as path from 'node:path'
import * as fs from 'node:fs'
import * as assert from 'uvu/assert';

import { Parse, getDataMapping, getEableRunDataMapping } from '../src'
Expand Down
6 changes: 2 additions & 4 deletions test/uvu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from 'uvu';
import * as path from 'node:path'
import * as assert from 'uvu/assert';

import { getDependencyImports, getAllImportsForFile, getNeedCompileFiles, libFiles } from "../src/ast";
import { getAllImportsForFile, getNeedCompileFiles } from "../src/ast";

test('Math.sqrt()', () => {
assert.is(Math.sqrt(4), 2);
Expand Down Expand Up @@ -31,9 +31,7 @@ test.only('getAllImportsForFile()', () => {

var needCompileFiles = getNeedCompileFiles()

// console.dir(needCompileFiles)

assert.is(needCompileFiles.length, 15);
assert.is(needCompileFiles.length, 14);
});


Expand Down

0 comments on commit f2e6c97

Please sign in to comment.