@yyp92-cli/utils 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @yyp92-cli/utils
2
+
3
+ ## 0.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - utils 包
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@yyp92-cli/utils",
3
+ "version": "0.0.2",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "type": "module",
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "keywords": [],
11
+ "author": "",
12
+ "license": "ISC",
13
+ "description": "",
14
+ "dependencies": {
15
+ "axios": "^1.11.0",
16
+ "fs-extra": "^11.3.1",
17
+ "url-join": "^5.0.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/fs-extra": "^11.0.4"
21
+ },
22
+ "scripts": {
23
+ "test": "echo \"Error: no test specified\" && exit 1"
24
+ }
25
+ }
@@ -0,0 +1,89 @@
1
+ import fs from 'node:fs';
2
+ import fse from 'fs-extra';
3
+ // @ts-ignore
4
+ import npminstall from 'npminstall';
5
+ import { getLatestVersion, getNpmRegistry } from './versionUtils.js';
6
+ import path from 'node:path';
7
+
8
+ export interface NpmPackageOptions {
9
+ name: string;
10
+ targetPath: string;
11
+ }
12
+
13
+ class NpmPackage {
14
+ name: string;
15
+ version: string = '';
16
+ targetPath: string;
17
+ storePath: string;
18
+
19
+ constructor(options: NpmPackageOptions) {
20
+ this.targetPath = options.targetPath;
21
+ this.name = options.name;
22
+
23
+ this.storePath = path.resolve(options.targetPath, 'node_modules');
24
+ }
25
+
26
+ async prepare() {
27
+ if (!fs.existsSync(this.targetPath)) {
28
+ fse.mkdirpSync(this.targetPath);
29
+ }
30
+ const version = await getLatestVersion(this.name);
31
+ this.version = version;
32
+ }
33
+
34
+ async install() {
35
+ await this.prepare();
36
+
37
+ return npminstall({
38
+ pkgs: [
39
+ {
40
+ name: this.name,
41
+ version: this.version,
42
+ }
43
+ ],
44
+ registry: getNpmRegistry(),
45
+ root: this.targetPath
46
+ });
47
+ }
48
+
49
+ get npmFilePath() {
50
+ // 最外层目录包名中的 / 会被替换为 +
51
+ return path.resolve(this.storePath, `.store/${this.name.replace('/', '+')}@${this.version}/node_modules/${this.name}`);
52
+ }
53
+
54
+ // exists 方法就是判断这个目录是否存在,从而判断包又没有安装
55
+ async exists() {
56
+ await this.prepare();
57
+
58
+ return fs.existsSync(this.npmFilePath);
59
+ }
60
+
61
+ // getPackageJSON 方法就是用 readJSONSync 读取 package.json 文件
62
+ async getPackageJSON() {
63
+ if (await this.exists()) {
64
+ return fse.readJsonSync(path.resolve(this.npmFilePath, 'package.json'))
65
+ }
66
+
67
+ return null;
68
+ }
69
+
70
+ async getLatestVersion() {
71
+ return getLatestVersion(this.name);
72
+ }
73
+
74
+ async update() {
75
+ const latestVersion = await this.getLatestVersion();
76
+ return npminstall({
77
+ root: this.targetPath,
78
+ registry: getNpmRegistry(),
79
+ pkgs: [
80
+ {
81
+ name: this.name,
82
+ version: latestVersion,
83
+ }
84
+ ]
85
+ });
86
+ }
87
+ }
88
+
89
+ export default NpmPackage;
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import NpmPackage from "./NpmPackage.js";
2
+ import * as versionUtils from './versionUtils.js';
3
+
4
+ export {
5
+ NpmPackage,
6
+ versionUtils
7
+ }
@@ -0,0 +1,37 @@
1
+ import axios from 'axios';
2
+ import urlJoin from 'url-join';
3
+
4
+ function getNpmRegistry() {
5
+ return 'https://registry.npmmirror.com';
6
+ }
7
+
8
+ async function getNpmInfo(packageName: string) {
9
+ const register = getNpmRegistry();
10
+ const url = urlJoin(register, packageName);
11
+ try {
12
+ const response = await axios.get(url);
13
+
14
+ if (response.status === 200) {
15
+ return response.data;
16
+ }
17
+ } catch (e) {
18
+ return Promise.reject(e);
19
+ }
20
+ }
21
+
22
+ async function getLatestVersion(packageName: string) {
23
+ const data = await getNpmInfo(packageName);
24
+ return data['dist-tags'].latest;
25
+ }
26
+
27
+ async function getVersions(packageName: string) {
28
+ const data = await getNpmInfo(packageName);
29
+ return Object.keys(data.versions);
30
+ }
31
+
32
+ export {
33
+ getNpmRegistry,
34
+ getNpmInfo,
35
+ getLatestVersion,
36
+ getVersions
37
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "dist",
4
+ "types": [ "node" ],
5
+ "target": "es2016",
6
+ "module": "NodeNext",
7
+ "moduleResolution": "NodeNext",
8
+ "declaration": true,
9
+ "esModuleInterop": true,
10
+ "forceConsistentCasingInFileNames": true,
11
+ "strict": true,
12
+ "skipLibCheck": true,
13
+ "sourceMap": true
14
+ }
15
+ }