Skip to content

Commit

Permalink
fix(node-modules-util): fix ts type parsing error for 'alipay-sdk'
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Dec 20, 2021
1 parent 690df32 commit d41dd21
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 9 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ services:
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./packages/system-server:/app
- ./packages/cloud-function:/app/node_modules/cloud-function-engine:ro
- ./packages/database-proxy:/app/node_modules/database-proxy:ro
- ./packages/database-ql:/app/node_modules/database-ql:ro
- ./packages/database-ql:/app/node_modules/database-proxy/node_modules/database-ql:ro
Expand Down
4 changes: 4 additions & 0 deletions packages/node-modules-utils/copy2app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rm -rf ../app-service/node_modules/node-modules-utils/dist
rm -rf ../app-service/node_modules/node-modules-utils/src
cp -r dist ../app-service/node_modules/node-modules-utils/dist
cp -r src ../app-service/node_modules/node-modules-utils/src
4 changes: 4 additions & 0 deletions packages/node-modules-utils/copy2sys.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rm -rf ../system-server/node_modules/cloud-function-engine/dist
rm -rf ../system-server/node_modules/cloud-function-engine/src
cp -r dist ../system-server/node_modules/cloud-function-engine/dist
cp -r src ../system-server/node_modules/cloud-function-engine/src
5 changes: 4 additions & 1 deletion packages/node-modules-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"build": "tsc",
"watch": "tsc -w",
"test": "npx mocha tests/**test.js",
"prepublishOnly": "npm run build"
"prepublishOnly": "npm run build",
"copy2app": "sh copy2app.sh",
"copy2sys": "sh copy2sys.sh",
"copy4dev": "sh copy2app.sh && sh copy2sys.sh"
},
"bugs": {
"url": "https://github.com/Maslow/less-framework/issues"
Expand Down
13 changes: 10 additions & 3 deletions packages/node-modules-utils/src/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,29 @@ export class PackageDeclaration extends PackageInfo {
* 2. 如果未读取到,则指定 package.json#main 所在目录 下的 index.d.ts 为 typings
*/
async resolveTypingsEntryPath() {

let defaultFilename = 'index.d.ts'
const entryStat = await this.exists(this.entryFile)
if (entryStat.isFile()) {
const { name } = path.parse(this.entryFile)
defaultFilename = `${name}.d.ts`
}

const defaultTypings = path.join(this.entryPath, defaultFilename)
const typings = this.info?.typings || this.info?.types

const defaultTypings = path.join(this.entryPath, 'index.d.ts')
if (!typings) {
return defaultTypings
}


const typingFile = path.join(this.rootPath, typings)
const stat = await this.exists(typingFile)
if (!stat) {
return defaultTypings
}

if (stat.isDirectory()) {
return path.join(typingFile, 'index.d.ts')
return path.join(typingFile, defaultFilename)
}

return typingFile
Expand Down
13 changes: 10 additions & 3 deletions packages/node-modules-utils/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export class PackageInfo {
*/
rootPath: string

/**
* entry file
* 为 main / module 指定的入口文件
*/
entryFile: string

/**
* entry path
* 为 main / module 指定的入口文件所在目录
Expand All @@ -38,10 +44,10 @@ export class PackageInfo {
}

get dependencyNames(): string[] {
if(!this.dependencies) return []
if (!this.dependencies) return []
return Object.keys(this.dependencies)
}

/**
* common entry file
*/
Expand Down Expand Up @@ -124,6 +130,7 @@ export class PackageInfo {
entryPath = path.dirname(entryPath)
}

this.entryFile = entryFile
this.entryPath = entryPath
}

Expand Down Expand Up @@ -164,7 +171,7 @@ export class PackageInfo {
* 文件或文件是否存在
* @param p
*/
async exists(p: string) {
async exists(p: string) {
try {
const stat = await fse.stat(p)
return stat
Expand Down
14 changes: 13 additions & 1 deletion packages/node-modules-utils/tests/declare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('npm-util(unit): Package Declaration Load', () => {
})

/**
* load from self package: less-api-database
* load from self package: database-proxy
*/
it('load d.ts of database-proxy (typings)', async () => {
const pkg = new PackageDeclaration('database-proxy', nmp)
Expand All @@ -46,5 +46,17 @@ describe('npm-util(unit): Package Declaration Load', () => {

assert.strictEqual(pkg.name, 'database-proxy')
assert.ok(pkg.declarations.length > 0)
})

/**
* load from self package: alipay-sdk
*/
it('load d.ts of alipay-sdk (typings)', async () => {
const pkg = new PackageDeclaration('alipay-sdk', nmp)
await pkg.load()
// console.log(pkg)

assert.strictEqual(pkg.name, 'alipay-sdk')
assert.ok(pkg.declarations.length > 0)
})
})
8 changes: 8 additions & 0 deletions packages/node-modules-utils/tests/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ describe('npm-util(unit): Package parse', () => {
// console.log(pkg)
assert.strictEqual(pkg.name, '@types/express')
})


it('get pkg dir: alipay-sdk', async () => {
const pkg = new PackageInfo('alipay-sdk', nmp)
await pkg.parsePackageInfo()
// console.log(pkg)
assert.strictEqual(pkg.name, 'alipay-sdk')
})
})

0 comments on commit d41dd21

Please sign in to comment.