Skip to content

Commit 321d18a

Browse files
refactor: source has been reorganized and many features have been rewritten
1 parent 617e34e commit 321d18a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+5012
-3098
lines changed

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.github/
22

3-
type-gymnastics.ts
3+
type-gymnastics/
44

55
/tsconfig.base.json
66
/tsconfig.cjs.json

README-cn.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ yarn add path-nice
7272
- 提供: CommonJS, ESModule 和 TypeScript typings
7373
- ESModule 版本可以[直接在 Node 中使用](https://nodejs.org/api/esm.html#modules-ecmascript-modules).
7474

75-
## 用法
75+
## 3 分钟教程
7676

7777
> ⚠️ 这个库的 API 将在 2.0 版达到稳定, 在此之前请勿在生产中使用.
7878
@@ -85,15 +85,15 @@ import path from 'path-nice'
8585

8686
const pkg = path('./package.json')
8787

88+
// 一个 PathNice 实例是对 raw path string 的包装,
89+
// 用于方便地生成其他路径, 或操作文件
90+
pkg.raw === './package.json' // true
91+
8892
// 是 PathNice 的实例
8993
pkg instanceof path.PathNice // true
9094

9195
// 是一个不可变对象, 所有属性是只读的
9296
Object.isFrozen(pkg) // true
93-
94-
// 一个 PathNice 实例是对 raw path string 的包装,
95-
// 用于方便地生成其他路径, 或操作文件
96-
pkg.raw // './package.json'
9797
```
9898

9999
### Path 相关方法
@@ -132,6 +132,7 @@ f2.toAbsolute() // path('/work/path-nice/package.json'), suppose
132132
f2.toRelative('path-nice/docs') // path('../package.json')
133133
await f2.realpath() // path('/work/path-nice/package.json'), suppose cwd is '/work',
134134
// and there are no symbolic links here.
135+
f2.realpathSync() // Sync ver
135136

136137
const parsedF2 = f2.toAbsolute().parse()
137138

@@ -149,27 +150,48 @@ parsedF2.dir('/home/fuu').ext('.md')
149150

150151
### 文件系统相关方法
151152

153+
可以注意到, `fs` 模块中的函数, 例如 `readFile``writeFile`, 它们的第一个参数几乎都是 `path`, 如果
154+
将它们改写成路径类的成员方法, 调用起来能够更加方便快捷.
155+
156+
下面大多数方法返回一个 `Promise`. 在函数名加上后缀 `Sync`, 即为它们的同步版本.
157+
152158
#### Read and write
153159

154-
- readFile: 读取文件, 返回 string 或 Buffer, 返回哪个取决于是否设置编码
155-
- readString: 读取文件, 保证返回 string, 默认 UTF-8
156-
- readBuffer: 读取文件, 保证返回 Buffer
157-
- readJSON
160+
- readFile
161+
- readString: 等同于 readFile, 但保证返回 string, 默认 UTF-8
162+
- readBuffer: 等同于 readFile, 但保证返回 Buffer
163+
- readJSON: 读取文件, 再作为 json 解析
158164
- writeFile
159165
- writeJSON: 默认 UTF-8, 4 个空格缩进
160166
- outputFile: 等同于 writeFile, 但如果文件目录不存在, 自动创建
161167
- outputJSON
162-
- updateString: 例如 `path('README.md').updateString(str => str.replace(/path/g, 'path-nice'))`
163-
- updateJSON: 例如 `path('package.json').updateJSON(json => { json.version = '1.0.0' })`
168+
- updateString
169+
170+
例如:
171+
172+
```ts
173+
path('README.md')
174+
.updateString(str => str.replace(/path/g, 'path-nice'))
175+
```
176+
- updateJSON
177+
178+
例如:
179+
180+
```ts
181+
path('package.json')
182+
.updateJSON(json => { json.version = '1.0.0' })
183+
```
164184
- appendFile
165185
- createReadStream
166186
- createWriteStream
167187
- open
168188

169189
#### Copy, move and remove
170190

171-
- copyAs
172-
- copyToDir
191+
文件夹也可直接复制移动删除. 支持跨设备移动文件.
192+
193+
- copyAs: 复制为 ...
194+
- copyToDir: 复制到文件夹内
173195
- moveAs
174196
- moveToDir
175197
- remove

README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ yarn add path-nice
7272
- Provided: CommonJS, ESModule and TypeScript typings
7373
- ESModule version can be [used directly in Node](https://nodejs.org/api/esm.html#modules-ecmascript-modules).
7474

75-
## Usage
75+
## 3 minutes guide
7676

7777
> ⚠️ The API of this library will be stable in version 2.0, do not use it in production until then.
7878
@@ -85,15 +85,15 @@ import path from 'path-nice'
8585

8686
const pkg = path('./package.json')
8787

88+
// A PathNice instance is a wrapper of the raw path string, so that the path
89+
// can be easily used to generate additional paths or manipulate files.
90+
pkg.raw === './package.json' // true
91+
8892
// is the instance of PathNice
8993
pkg instanceof path.PathNice // true
9094

9195
// is an immutable object, all properties are read-only
9296
Object.isFrozen(pkg) // true
93-
94-
// A PathNice instance is a wrapper of the raw path string, so that the path
95-
// can be easily used to generate additional paths or manipulate files.
96-
pkg.raw // './package.json'
9797
```
9898

9999
### Path related methods
@@ -132,6 +132,7 @@ f2.toAbsolute() // path('/work/path-nice/package.json'), suppose
132132
f2.toRelative('path-nice/docs') // path('../package.json')
133133
await f2.realpath() // path('/work/path-nice/package.json'), suppose cwd is '/work',
134134
// and there are no symbolic links here.
135+
f2.realpathSync() // Sync ver
135136

136137
const parsedF2 = f2.toAbsolute().parse()
137138

@@ -149,27 +150,50 @@ parsedF2.dir('/home/fuu').ext('.md')
149150

150151
### File system related methods
151152

153+
It can be noted that, the functions in the `fs` module, such as `readFile` and `writeFile`,
154+
almost always have `path` as their first parameter, so it could be more convenient to call
155+
these functions if we rewrite them into the form of member functions of path class.
156+
157+
Most of the following methods return a `Promise`, and they also have a synchronized version,
158+
with the `Sync` suffix added to the function name.
159+
152160
#### Read and write
153161

154-
- readFile: Read the file, return either a string or a Buffer, depending on whether the encoding is set
155-
- readString: Read the file, guaranteed to return a string, UTF-8 by default
156-
- readBuffer: Read the file, guaranteed to return a Buffer
157-
- readJSON
162+
- readFile
163+
- readString: = readFile, but guaranteed to return a string, UTF-8 by default
164+
- readBuffer: = readFile, but guaranteed to return a Buffer
165+
- readJSON: read the file, then parse as json
158166
- writeFile
159167
- writeJSON: UTF-8, 4 spaces indent by default
160168
- outputFile: = writeFile, automatically create the parent directory if it does not exist
161169
- outputJSON
162-
- updateString: e.g. `path('README.md').updateString(str => str.replace(/path/g, 'path-nice'))`
163-
- updateJSON: e.g. `path('package.json').updateJSON(json => { json.version = '1.0.0' })`
170+
- updateString
171+
172+
e.g.
173+
174+
```ts
175+
path('README.md')
176+
.updateString(str => str.replace(/path/g, 'path-nice'))
177+
```
178+
- updateJSON
179+
180+
e.g.
181+
182+
```ts
183+
path('package.json')
184+
.updateJSON(json => { json.version = '1.0.0' })
185+
```
164186
- appendFile
165187
- createReadStream
166188
- createWriteStream
167189
- open
168190

169191
#### Copy, move and remove
170192

171-
- copyAs
172-
- copyToDir
193+
Directories can also be directly copied and moved for deletion. Support for moving files across devices.
194+
195+
- copyAs: copy **as** ...
196+
- copyToDir: copy **into** a directory
173197
- moveAs
174198
- moveToDir
175199
- remove

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,23 @@
2828
"docs": "node ./scripts/gen-docs.js",
2929
"release": "node ./scripts/release.js"
3030
},
31+
"dependencies": {
32+
"@types/node": "^16",
33+
"chokidar": "^3.5.3"
34+
},
3135
"devDependencies": {
3236
"@types/fs-extra": "^9.0.13",
3337
"@types/jest": "^28.1.4",
34-
"@types/node": "^12.12.6",
3538
"chalk": "^4.1.2",
3639
"commitizen": "^4.2.4",
3740
"conventional-changelog-cli": "^2.2.2",
3841
"cz-conventional-changelog": "3.3.0",
3942
"fs-extra": "^10.1.0",
4043
"jest": "^28.1.2",
41-
"memfs": "^3.4.6",
44+
"memfs": "^3.4.7",
4245
"prettier": "^2.7.1",
4346
"ts-jest": "^28.0.5",
44-
"typedoc": "^0.22.18",
47+
"typedoc": "^0.23.5",
4548
"typescript": "^4.7.4"
4649
},
4750
"sideEffects": false,
@@ -74,4 +77,4 @@
7477
"path": "./node_modules/cz-conventional-changelog"
7578
}
7679
}
77-
}
80+
}

scripts/gen-docs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function main() {
1818
await Promise.all([fixPath('docs/index.html'), fixPath('docs/index-cn.html')]);
1919

2020
async function fixPath(filepath) {
21-
await path(filepath).updateString((str) => {
21+
await path(filepath).updateFileAsString((str) => {
2222
str = str.replace('<a href="README-cn.md">', '<a href="index-cn.html">');
2323
str = str.replace('<a href="README.md">', '<a href="index.html">');
2424
return str;

scripts/release.js

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,57 @@ async function main() {
88

99
console.log('release: v' + version);
1010

11-
if ((await x('git branch --show-current')) !== 'dev') {
12-
console.log(`x('git branch --show-current') !== 'dev'`);
13-
return;
14-
}
15-
16-
if ((await x('git status --porcelain')) !== '') {
17-
console.log(`x('git status --porcelain') !== ''`);
18-
return;
19-
}
20-
21-
await sh('git rebase -i main');
22-
await sh('git checkout main');
23-
await sh('git merge dev');
24-
11+
// // at dev branch
12+
// if ((await x('git branch --show-current')) !== 'dev') {
13+
// console.log(`x('git branch --show-current') !== 'dev'`);
14+
// return;
15+
// }
16+
17+
// // working tree is clean
18+
// if ((await x('git status --porcelain')) !== '') {
19+
// console.log(`x('git status --porcelain') !== ''`);
20+
// return;
21+
// }
22+
23+
// // rebase & merge
24+
// await sh('git rebase -i main');
25+
// await sh('git checkout main');
26+
// await sh('git merge dev');
27+
28+
// build & test
2529
await fs.remove('./build');
2630
await sh('tsc -p tsconfig.cjs.json');
2731
await sh('tsc -p tsconfig.esm.json');
28-
await sh('jest');
32+
// await sh('jest');
2933

34+
// we can use path-nice now
3035
const path = require('../build/cjs/index.cjs.js');
3136

3237
await path('package.json').updateJSON((pkg) => {
3338
pkg.version = version;
3439
});
40+
// generate release/
3541
await genRelease();
42+
// generate docs/
3643
await sh('yarn docs');
44+
// generate CHANGLOG.md
3745
await sh('conventional-changelog -p angular -i CHANGELOG.md -s');
3846

39-
await sh('git add -A');
40-
await sh(`git commit -m "chore: release ${version}"`);
41-
await sh('git checkout dev');
42-
await sh('git merge main');
43-
await sh('git push --all');
44-
await sh(`git tag v${version}`);
45-
await sh(`git push origin v${version}`);
46-
47-
console.log(chalk.red(`Critical operation:`));
48-
console.log(chalk.red(` $ npm publish --dry-run ./release`));
49-
console.log(chalk.red(` $ npm publish ./release`));
50-
console.log(chalk.red(`Copy the command and execute it yourself.`));
47+
// // commit, back to dev branch, tag
48+
// await sh('git add -A');
49+
// await sh(`git commit -m "chore: release ${version}"`);
50+
// await sh(`git tag v${version}`);
51+
// await sh('git checkout dev');
52+
// await sh('git merge main');
53+
54+
// // sync
55+
// await sh('git push --all');
56+
// await sh(`git push origin v${version}`);
57+
58+
// console.log(chalk.red(`Critical operation:`));
59+
// console.log(chalk.red(` $ npm publish --dry-run ./release`));
60+
// console.log(chalk.red(` $ npm publish ./release`));
61+
// console.log(chalk.red(`Copy the command and execute it yourself.`));
5162
}
5263

5364
function getInput() {
@@ -75,23 +86,25 @@ function getInput() {
7586
async function genRelease() {
7687
const path = require('../build/cjs/index.cjs.js');
7788

78-
await path('release').remove();
79-
await path('release').ensureDir();
89+
await path('release').emptyDir();
8090

8191
await Promise.all([
82-
path('build/cjs')
83-
.copyToDir('release')
84-
.then(() => path('release/cjs/package.json').writeJSON({ type: 'commonjs' })),
85-
path('build/esm')
86-
.copyToDir('release')
87-
.then(() => path('release/esm/package.json').writeJSON({ type: 'module' })),
92+
path(
93+
'build/cjs',
94+
'build/esm',
95+
'package.json',
96+
'README.md',
97+
'README-cn.md',
98+
'LICENSE',
99+
).copyToDir('release'),
88100
path('release/posix/package.json').outputJSON(genSubPkgJSON('posix')),
89101
path('release/win32/package.json').outputJSON(genSubPkgJSON('win32')),
90-
path('package.json')
91-
.copyToDir('release')
92-
.then(() => path('release/package.json').updateJSON(updatePkgJSON)),
93-
path('README.md').copyToDir('release'),
94-
path('LICENSE').copyToDir('release'),
102+
]);
103+
104+
await Promise.all([
105+
path('release/cjs/package.json').writeJSON({ type: 'commonjs' }),
106+
path('release/esm/package.json').writeJSON({ type: 'module' }),
107+
path('release/package.json').updateJSON(updatePkgJSON),
95108
]);
96109

97110
/**
@@ -145,7 +158,7 @@ async function genRelease() {
145158
require: './cjs/win32.cjs.js',
146159
},
147160
},
148-
files: ['cjs/', 'esm/', 'posix/', 'win32/'],
161+
files: ['cjs/', 'esm/', 'posix/', 'win32/', 'README-cn.md'],
149162
};
150163
}
151164
}

0 commit comments

Comments
 (0)