Skip to content

Commit

Permalink
🎨 refactor codes with @guanghechen/path and @guanghechen/filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
guanghechen committed Oct 16, 2023
1 parent 0fd45d2 commit 38b75ff
Show file tree
Hide file tree
Showing 113 changed files with 1,513 additions and 2,692 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"browserslist",
"Builtins",
"ciphertext",
"clazz",
"clipboardy",
"clsx",
"concat",
Expand All @@ -13,6 +14,7 @@
"editorconfig",
"eslintconfig",
"execa",
"filetree",
"flexbugs",
"funcs",
"gitmoji",
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ Package | Description
[@guanghechen/helper-mac][] | A collection of utility functions for generating MAC.
[@guanghechen/helper-npm][] | Utilities for handling npm repo and `package.json`.
[@guanghechen/helper-option][] | Utilities for resolving options or props.
[@guanghechen/helper-path][] | Utilities to handle url path and file path.
[@guanghechen/helper-plop][] | A collection of utility functions for plop templates
[@guanghechen/helper-storage][] | Utilities for read / write content.
[@guanghechen/helper-stream][] | Utilities for handing node streams.
Expand Down Expand Up @@ -107,7 +106,6 @@ node-scaffolds is [MIT licensed](https://github.com/guanghechen/node-scaffolds/t
[@guanghechen/helper-mac]: ./packages/helper-mac
[@guanghechen/helper-npm]: ./packages/helper-npm
[@guanghechen/helper-option]: ./packages/helper-option
[@guanghechen/helper-path]: ./packages/helper-path
[@guanghechen/helper-plop]: ./packages/helper-plop
[@guanghechen/helper-storage]: ./packages/helper-storage
[@guanghechen/helper-stream]: ./packages/helper-stream
Expand Down
66 changes: 34 additions & 32 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ const require = createRequire(import.meta.url)

export default async function () {
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
const packageDir = path.relative(__dirname, path.resolve()) + '/'
const { default: manifest } = await import(path.resolve('package.json'), {
assert: { type: 'json' },
})
const baseConfig = await tsMonorepoConfig(__dirname, {
useESM: true,
tsconfigFilepath: path.join(__dirname, 'tsconfig.test.esm.json'),
})

const config = {
...baseConfig,
collectCoverageFrom: [baseConfig.collectCoverageFrom ?? []].flat(),
collectCoverageFrom: [...(baseConfig.collectCoverageFrom ?? [])],
coveragePathIgnorePatterns: [
'packages/helper-commander/src/command/main.ts',
'packages/helper-commander/src/command/sub.ts',
Expand All @@ -26,38 +28,38 @@ export default async function () {
'packages/helper-npm/src/index.d.ts',
'packages/script-doc-link/src/cli.ts',
],
coverageThreshold: Object.fromEntries(
[
['global', { branches: 80, functions: 90, lines: 90, statements: 90 }],
['packages/helper-plop/src/cli.ts', { functions: 0, lines: 27, statements: 27 }],
['packages/helper-plop/src/cli.ts', { functions: 0, lines: 27, statements: 27 }],
[
'packages/helper-plop/src/run/types.ts',
{ branches: 0, functions: 0, lines: 0, statements: 0 },
],
[
'packages/helper-plop/src/run/util.ts',
{ branches: 10, functions: 50, lines: 50, statements: 50 },
],
['packages/rollup-config/src/config.ts', { branches: 66 }],
['packages/rollup-config/src/env.ts', { branches: 50 }],
['packages/rollup-config/src/external.ts', { branches: 70 }],
['packages/rollup-config/src/middleware/dts.ts', { branches: 60 }],
['packages/rollup-config/src/middleware/ts.ts', { branches: 50 }],
[
'packages/rollup-config/src/plugin/modify.ts',
{ branches: 0, functions: 0, lines: 0, statements: 0 },
],
['packages/rollup-config/src/preset/dts.ts', { branches: 60 }],
]
.filter(([p]) => !p.startsWith('packages/') || p.startsWith(packageDir))
.map(([p, val]) => (p.startsWith(packageDir) ? [path.join(__dirname, p), val] : [p, val])),
),
extensionsToTreatAsEsm: ['.ts', '.mts'],
moduleNameMapper: {
...baseConfig.moduleNameMapper,
coverageThreshold: {
global: {
branches: 80,
functions: 90,
lines: 90,
statements: 90,
},
...coverageMap[manifest.name],
},
extensionsToTreatAsEsm: ['.ts', '.mts'],
prettierPath: require.resolve('prettier-2'),
}
return config
}

const coverageMap = {
'@guanghechen/helper-plop': {
'src/cli.ts': { functions: 0, lines: 27, statements: 27 },
'src/run/types.ts': { branches: 0, functions: 0, lines: 0, statements: 0 },
'src/run/util.ts': { branches: 10, functions: 50, lines: 50, statements: 50 },
},
'@guanghechen/helper-string': {
'src/vender/change-case.ts': { branches: 53, functions: 82 },
'src/vender/title-case.ts': { branches: 50 },
},
'@guanghechen/rollup-config': {
'src/config.ts': { branches: 66 },
'src/env.ts': { branches: 50 },
'src/external.ts': { branches: 70 },
'src/middleware/dts.ts': { branches: 60 },
'src/middleware/ts.ts': { branches: 50 },
'src/plugin/modify.ts': { branches: 0, functions: 0, lines: 0, statements: 0 },
'src/preset/dts.ts': { branches: 60 },
},
}
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,30 @@
"node-plop/inquirer": "^9.1.4"
},
"devDependencies": {
"@babel/core": "7.23.0",
"@rollup/plugin-replace": "^5.0.2",
"@babel/core": "7.23.2",
"@rollup/plugin-replace": "^5.0.4",
"@types/command-exists": "^1.2.1",
"@types/jest": "29.5.5",
"@types/node": "^16.18.57",
"@types/node": "^16.18.58",
"ci-info": "^3.9.0",
"commander": "^11.0.0",
"commander": "^11.1.0",
"cross-env": "^7.0.3",
"depcheck": "^1.4.6",
"eslint": "8.50.0",
"eslint": "8.51.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-jest": "27.4.2",
"eslint-plugin-prettier": "5.0.0",
"eslint-plugin-prettier": "5.0.1",
"husky": "8.0.3",
"is-ci": "^3.0.1",
"jest": "^29.7.0",
"lerna": "7.3.0",
"lint-staged": "14.0.1",
"lerna": "7.3.1",
"lint-staged": "15.0.1",
"npm-run-all": "4.1.5",
"pinst": "3.0.0",
"prettier": "3.0.3",
"prettier-2": "npm:prettier@^2",
"rimraf": "^5.0.5",
"rollup": "^3.29.4",
"rollup": "^4.1.3",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"tsconfig-paths": "4.2.0",
Expand Down
8 changes: 1 addition & 7 deletions packages/chalk-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"README.md"
],
"scripts": {
"build": "rimraf lib/ && cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
"build": "../../node_modules/.bin/rimraf lib/ && ../../node_modules/.bin/cross-env NODE_ENV=production ../../node_modules/.bin/rollup -c ../../rollup.config.mjs",
"prepublishOnly": "yarn build",
"test": "node --experimental-vm-modules ../../node_modules/.bin/jest --config ../../jest.config.mjs --rootDir .",
"ts:run": "cross-env NODE_ENV=production node --no-warnings --experimental-specifier-resolution=node --loader ts-node/esm -r tsconfig-paths/register"
Expand All @@ -48,11 +48,5 @@
"chalk": "^5.3.0",
"dayjs": "^1.11.10",
"json5": "^2.2.3"
},
"devDependencies": {
"cross-env": "*",
"jest": "*",
"rimraf": "*",
"rollup": "*"
}
}
2 changes: 1 addition & 1 deletion packages/conventional-changelog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"README.md"
],
"dependencies": {
"gitmojis": "^3.13.5"
"gitmojis": "^3.14.0"
}
}
4 changes: 2 additions & 2 deletions packages/eslint-config-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
],
"dependencies": {
"@guanghechen/eslint-config": "^5.0.2",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4"
"@typescript-eslint/eslint-plugin": "^6.7.5",
"@typescript-eslint/parser": "^6.7.5"
},
"peerDependencies": {
"eslint": "^8.0.0"
Expand Down
8 changes: 1 addition & 7 deletions packages/event-bus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,11 @@
"README.md"
],
"scripts": {
"build": "rimraf lib/ && cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
"build": "../../node_modules/.bin/rimraf lib/ && ../../node_modules/.bin/cross-env NODE_ENV=production ../../node_modules/.bin/rollup -c ../../rollup.config.mjs",
"prepublishOnly": "yarn build",
"test": "node --experimental-vm-modules ../../node_modules/.bin/jest --config ../../jest.config.mjs --rootDir ."
},
"dependencies": {
"@guanghechen/helper-func": "^5.0.8"
},
"devDependencies": {
"cross-env": "*",
"jest": "*",
"rimraf": "*",
"rollup": "*"
}
}
8 changes: 1 addition & 7 deletions packages/helper-buffer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@
"README.md"
],
"scripts": {
"build": "rimraf lib/ && cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
"build": "../../node_modules/.bin/rimraf lib/ && ../../node_modules/.bin/cross-env NODE_ENV=production ../../node_modules/.bin/rollup -c ../../rollup.config.mjs",
"prepublishOnly": "yarn build",
"test": "node --experimental-vm-modules ../../node_modules/.bin/jest --config ../../jest.config.mjs --rootDir ."
},
"devDependencies": {
"cross-env": "*",
"jest": "*",
"rimraf": "*",
"rollup": "*"
}
}
34 changes: 17 additions & 17 deletions packages/helper-cipher-file/__test__/FileCipherBatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ChalkLogger from '@guanghechen/chalk-logger'
import { AesGcmCipherFactoryBuilder } from '@guanghechen/cipher'
import { FileSplitter } from '@guanghechen/file-split'
import { calcMac } from '@guanghechen/helper-mac'
import { FilepathResolver } from '@guanghechen/helper-path'
import { PhysicalWorkspacePathResolver as WorkspacePathResolver } from '@guanghechen/path'
import { mergeStreams, stream2buffer } from '@guanghechen/stream'
import {
assertPromiseNotThrow,
Expand Down Expand Up @@ -47,29 +47,29 @@ describe('FileCipherBatcher', () => {
const plainRootDir: string = path.join(workspaceDir, 'src')
const cryptRootDir: string = path.join(workspaceDir, 'src_encrypted')
const bakPlainRootDir: string = path.join(workspaceDir, 'src_backup')
const plainPathResolver = new FilepathResolver(plainRootDir)
const cryptPathResolver = new FilepathResolver(cryptRootDir)
const bakPlainPathResolver = new FilepathResolver(bakPlainRootDir)
const plainPathResolver = new WorkspacePathResolver(plainRootDir)
const cryptPathResolver = new WorkspacePathResolver(cryptRootDir)
const bakPlainPathResolver = new WorkspacePathResolver(bakPlainRootDir)
const logger = new ChalkLogger({ flights: { colorful: false, date: false } })

const filepathA: string = plainPathResolver.absolute(itemTable.A.plainFilepath)
const filepathB: string = plainPathResolver.absolute(itemTable.B.plainFilepath)
const filepathC: string = plainPathResolver.absolute(itemTable.C.plainFilepath)
const filepathD: string = plainPathResolver.absolute(itemTable.D.plainFilepath)
const filepathA: string = plainPathResolver.resolve(itemTable.A.plainFilepath)
const filepathB: string = plainPathResolver.resolve(itemTable.B.plainFilepath)
const filepathC: string = plainPathResolver.resolve(itemTable.C.plainFilepath)
const filepathD: string = plainPathResolver.resolve(itemTable.D.plainFilepath)

const filepath2A: string = bakPlainPathResolver.absolute(itemTable.A.plainFilepath)
const filepath2B: string = bakPlainPathResolver.absolute(itemTable.B.plainFilepath)
const filepath2C: string = bakPlainPathResolver.absolute(itemTable.C.plainFilepath)
const filepath2D: string = bakPlainPathResolver.absolute(itemTable.D.plainFilepath)
const filepath2A: string = bakPlainPathResolver.resolve(itemTable.A.plainFilepath)
const filepath2B: string = bakPlainPathResolver.resolve(itemTable.B.plainFilepath)
const filepath2C: string = bakPlainPathResolver.resolve(itemTable.C.plainFilepath)
const filepath2D: string = bakPlainPathResolver.resolve(itemTable.D.plainFilepath)

const encryptedFilepathA: string = cryptPathResolver.absolute(itemTable.A.cryptFilepath)
const encryptedFilepathA2: string = cryptPathResolver.absolute(itemTable.A2.cryptFilepath)
const encryptedFilepathB: string = cryptPathResolver.absolute(itemTable.B.cryptFilepath)
const encryptedFilepathA: string = cryptPathResolver.resolve(itemTable.A.cryptFilepath)
const encryptedFilepathA2: string = cryptPathResolver.resolve(itemTable.A2.cryptFilepath)
const encryptedFilepathB: string = cryptPathResolver.resolve(itemTable.B.cryptFilepath)
const encryptedFilepathsC: string[] = itemTable.C.cryptFilepathParts.map(part =>
cryptPathResolver.absolute(itemTable.C.cryptFilepath + part),
cryptPathResolver.resolve(itemTable.C.cryptFilepath + part),
)
const encryptedFilepathsD: string[] = itemTable.D.cryptFilepathParts.map(part =>
cryptPathResolver.absolute(itemTable.D.cryptFilepath + part),
cryptPathResolver.resolve(itemTable.D.cryptFilepath + part),
)

const contentA: string = contentTable.A
Expand Down
24 changes: 13 additions & 11 deletions packages/helper-cipher-file/__test__/FileCipherCatalog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChalkLogger } from '@guanghechen/chalk-logger'
import { AesGcmCipherFactoryBuilder } from '@guanghechen/cipher'
import { FileSplitter } from '@guanghechen/file-split'
import { iterable2map } from '@guanghechen/helper-func'
import { FilepathResolver } from '@guanghechen/helper-path'
import { PhysicalWorkspacePathResolver as WorkspacePathResolver } from '@guanghechen/path'
import {
assertPromiseNotThrow,
assertPromiseThrow,
Expand Down Expand Up @@ -42,14 +42,14 @@ describe('FileCipherCatalog', () => {
const workspaceDir: string = locateFixtures('__fictitious__.FileCipherCatalog')
const plainRootDir: string = path.join(workspaceDir, 'src')
const cryptRootDir: string = path.join(workspaceDir, 'src_encrypted')
const plainPathResolver = new FilepathResolver(plainRootDir)
const cryptPathResolver = new FilepathResolver(cryptRootDir)
const plainPathResolver = new WorkspacePathResolver(plainRootDir)
const cryptPathResolver = new WorkspacePathResolver(cryptRootDir)
const logger = new ChalkLogger({ name: 'FileCipherCatalog' })

const filepathA: string = plainPathResolver.absolute(itemTable.A.plainFilepath)
const filepathB: string = plainPathResolver.absolute(itemTable.B.plainFilepath)
const filepathC: string = plainPathResolver.absolute(itemTable.C.plainFilepath)
const filepathD: string = plainPathResolver.absolute(itemTable.D.plainFilepath)
const filepathA: string = plainPathResolver.resolve(itemTable.A.plainFilepath)
const filepathB: string = plainPathResolver.resolve(itemTable.B.plainFilepath)
const filepathC: string = plainPathResolver.resolve(itemTable.C.plainFilepath)
const filepathD: string = plainPathResolver.resolve(itemTable.D.plainFilepath)

const contentA: string = contentTable.A
const contentA2: string = contentTable.A2
Expand All @@ -64,6 +64,8 @@ describe('FileCipherCatalog', () => {
partCodePrefix,
contentHashAlgorithm: contentHashAlgorithm,
pathHashAlgorithm: pathHashAlgorithm,
plainPathResolver,
cryptPathResolver,
isKeepPlain: sourceFilepath => sourceFilepath === 'a.txt',
})
const catalog = new FileCipherCatalog({
Expand Down Expand Up @@ -184,8 +186,8 @@ describe('FileCipherCatalog', () => {
)

// Both plain files and crypt files are exist.
await writeFile(cryptPathResolver.absolute(itemTable.A.cryptFilepath), contentA, encoding)
await writeFile(cryptPathResolver.absolute(itemTable.B.cryptFilepath), contentB, encoding)
await writeFile(cryptPathResolver.resolve(itemTable.A.cryptFilepath), contentA, encoding)
await writeFile(cryptPathResolver.resolve(itemTable.B.cryptFilepath), contentB, encoding)
await assertPromiseNotThrow(() =>
Promise.all([
catalog.checkPlainIntegrity(plainFilepaths),
Expand Down Expand Up @@ -257,13 +259,13 @@ describe('FileCipherCatalog', () => {

// Both plain files and crypt files are exist.
for (const part of itemTable.C.cryptFilepathParts) {
const absoluteEncryptedFilepath = cryptPathResolver.absolute(
const absoluteEncryptedFilepath = cryptPathResolver.resolve(
itemTable.C.cryptFilepath + part,
)
await writeFile(absoluteEncryptedFilepath, contentC, encoding)
}
for (const part of itemTable.D.cryptFilepathParts) {
const absoluteEncryptedFilepath = cryptPathResolver.absolute(
const absoluteEncryptedFilepath = cryptPathResolver.resolve(
itemTable.D.cryptFilepath + part,
)
await writeFile(absoluteEncryptedFilepath, contentD, encoding)
Expand Down
16 changes: 6 additions & 10 deletions packages/helper-cipher-file/__test__/util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { calcMac, calcMacFromFile } from '@guanghechen/helper-mac'
import { FilepathResolver } from '@guanghechen/helper-path'
import { PhysicalWorkspacePathResolver as WorkspacePathResolver } from '@guanghechen/path'
import { locateFixtures } from 'jest.helper'
import type {
IFileCipherCatalogDiffItemDraft,
Expand All @@ -21,24 +21,20 @@ import { contentHashAlgorithm, itemTable, pathHashAlgorithm } from './_data'

describe('catalog', () => {
const plainRootDir = locateFixtures('basic')
const plainPathResolver = new FilepathResolver(plainRootDir)
const plainPathResolver = new WorkspacePathResolver(plainRootDir)

test('normalizePlainFilepath', () => {
expect(normalizePlainFilepath('a.txt', plainPathResolver)).toEqual('a.txt')
expect(normalizePlainFilepath('a.txt/', plainPathResolver)).toEqual('a.txt')
expect(normalizePlainFilepath('./a.txt', plainPathResolver)).toEqual('a.txt')
expect(normalizePlainFilepath('./a.txt/', plainPathResolver)).toEqual('a.txt')
expect(normalizePlainFilepath('a/b/c//d/e/a.txt', plainPathResolver)).toEqual('a/b/c/d/e/a.txt')
expect(() => normalizePlainFilepath('/a.txt', plainPathResolver)).toThrow(
'[FilepathResolver.relative] Not under the rootDir:',
)
expect(() => normalizePlainFilepath('/a.txt', plainPathResolver)).toThrow('not under the root')
expect(() => normalizePlainFilepath('../a.txt', plainPathResolver)).toThrow(
'[FilepathResolver.relative] Not under the rootDir:',
)
expect(() => normalizePlainFilepath('..', plainPathResolver)).toThrow(
'[FilepathResolver.relative] Not under the rootDir:',
'not under the root',
)
expect(normalizePlainFilepath(plainPathResolver.absolute('a.txt'), plainPathResolver)).toEqual(
expect(() => normalizePlainFilepath('..', plainPathResolver)).toThrow('not under the root')
expect(normalizePlainFilepath(plainPathResolver.resolve('a.txt'), plainPathResolver)).toEqual(
'a.txt',
)
})
Expand Down

0 comments on commit 38b75ff

Please sign in to comment.