Skip to content

Commit

Permalink
feat(shared): 新增文件相关工具函数
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Feb 23, 2021
1 parent f676548 commit 0a62abe
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/cli-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@
"url": "https://github.com/xus-code/cli/issues"
},
"dependencies": {
"@babel/register": "^7.13.0",
"@types/debug": "^4.1.5",
"@types/yargs-parser": "^20.2.0",
"@xus/babel-preset": "^0.1.6",
"assert": "^2.0.0",
"chalk": "^4.1.0",
"debug": "^4.3.1",
"deepmerge": "^4.2.2",
"joi": "^17.4.0",
"lodash": "^4.17.21",
"log-symbols": "^4.0.0",
"ora": "^5.3.0",
"resolve": "^1.20.0",
"yargs-parser": "^20.2.5"
},
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/lodash": "^4.14.168",
"@types/yargs-parser": "^20.2.0"
}
}
40 changes: 40 additions & 0 deletions packages/cli-shared/src/BabelRegister/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createDebug, lodash, winPath } from '../'

const debug = createDebug('xus:shared:BabelRegister')

export class BabelRegister {
only: Record<string, string[]> = {}

setOnlyMap({ key, value }: { key: string; value: string[] }) {
debug(`set ${key} of only map:`)
debug(value)
this.only[key] = value
this.register()
}

register() {
const only = lodash.uniq(
Object.keys(this.only)
.reduce<string[]>((memo, key) => {
return memo.concat(this.only[key])
}, [])
.map(winPath)
)
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('@babel/register')({
presets: [
require.resolve('@xus/babel-preset'),
{
targets: { node: 'current' },
useTransformRuntime: false,
useTypescript: true
}
],
ignore: [/node_modules/],
only,
extensions: ['.jsx', '.js', '.ts', '.tsx'],
babelrc: false,
cache: false
})
}
}
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/cli-shared/src/file/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './getFileMeta'
export * from './lookUpFile'
19 changes: 19 additions & 0 deletions packages/cli-shared/src/file/lookUpFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { existsSync, statSync, readFileSync } from 'fs'
import { join, dirname } from 'path'

export function lookUpFile(
dir: string,
formats: string[],
pathOnly = false
): string | undefined {
for (const format of formats) {
const fullPath = join(dir, format)
if (existsSync(fullPath) && statSync(fullPath).isFile()) {
return pathOnly ? fullPath : readFileSync(fullPath, 'utf-8')
}
}
const parentDir = dirname(dir)
if (parentDir !== dir) {
return lookUpFile(parentDir, formats, pathOnly)
}
}
6 changes: 5 additions & 1 deletion packages/cli-shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ export { default as yParser } from 'yargs-parser'
export { default as assert } from 'assert'
export { default as deepmerge } from 'deepmerge'
export { default as resolve } from 'resolve'
export { default as lodash } from 'lodash'

// custom
export { Logger } from './logger'
export { Spinner } from './spinner'
export { createSchema, validateSchema } from './schema'
export { compatESModuleRequire } from './compatESModuleRequire'
export { winPath } from './winPath'
export { createEnvNameWithXusPrefix } from './env'
export { getFileMeta } from './getFileMeta'
export * from './file'
export { loadModule } from './loadModule'
export * from './pkg'
export { BabelRegister } from './BabelRegister'

// types export
export type {
Expand Down
9 changes: 9 additions & 0 deletions packages/cli-shared/src/pkg/getPkgName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { lookUpFile } from '../file'

export const getPkgName = (root: string) => {
const { name } = JSON.parse(lookUpFile(root, ['package.json']) || `{}`)

if (!name) throw new Error('no name found in package.json')

return name.startsWith('@') ? name.split('/')[1] : name
}
1 change: 1 addition & 0 deletions packages/cli-shared/src/pkg/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './isLerna'
6 changes: 6 additions & 0 deletions packages/cli-shared/src/pkg/isLerna.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { existsSync } from 'fs'
import { join } from 'path'

export const isLernaPkg = (root: string) => {
return existsSync(join(root, 'lerna.json'))
}

0 comments on commit 0a62abe

Please sign in to comment.