@rife/cli 0.0.6-beta.1 → 0.0.6-beta.11
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/dist/1.js +24 -0
- package/dist/1.mjs +20 -0
- package/dist/cjs/1.js +24 -0
- package/dist/cli.js +4 -0
- package/dist/cli.js.map +1 -1
- package/dist/esm/1.mjs +20 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/logger.js +3 -11
- package/dist/logger.js.map +1 -1
- package/dist/plugin/commander.js +22 -17
- package/dist/plugin/commander.js.map +1 -1
- package/dist/plugin/compiler/index.js +11 -6
- package/dist/plugin/compiler/index.js.map +1 -1
- package/dist/plugin/compiler/swc.js +2 -1
- package/dist/plugin/compiler/swc.js.map +1 -1
- package/dist/plugin/compiler/swc2.js +65 -0
- package/dist/plugin/compiler/swc2.js.map +1 -0
- package/dist/plugin/config.js +25 -9
- package/dist/plugin/config.js.map +1 -1
- package/dist/plugin/index.js +1 -0
- package/dist/plugin/index.js.map +1 -1
- package/dist/plugin/package copy.js.map +1 -1
- package/dist/plugin/package.js.map +1 -1
- package/dist/plugin/release.js +84 -14
- package/dist/plugin/release.js.map +1 -1
- package/dist/runner.js +33 -5
- package/dist/runner.js.map +1 -1
- package/dist/sync.js +4 -3
- package/dist/sync.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/util.js +14 -0
- package/dist/util.js.map +1 -0
- package/package.json +28 -26
- package/src/cli.ts +23 -19
- package/src/index.ts +15 -3
- package/src/logger.ts +16 -34
- package/src/plugin/commander.ts +49 -44
- package/src/plugin/compiler/index.ts +115 -109
- package/src/plugin/compiler/swc.ts +56 -55
- package/src/plugin/compiler/tsc.ts +101 -101
- package/src/plugin/config.ts +78 -63
- package/src/plugin/index.ts +1 -0
- package/src/plugin/package.ts +0 -3
- package/src/plugin/release.ts +113 -32
- package/src/runner.ts +128 -88
- package/src/sync.ts +63 -62
- package/src/util.ts +9 -0
- package/dist/build.js +0 -3
- package/dist/build.js.map +0 -1
- package/dist/compiler.js +0 -82
- package/dist/compiler.js.map +0 -1
- package/dist/plugin.js +0 -161
- package/dist/plugin.js.map +0 -1
- package/dist/pnpmfile.js +0 -144
- package/dist/swc.js +0 -78
- package/dist/swc.js.map +0 -1
- package/dist/tsc.js +0 -94
- package/dist/tsc.js.map +0 -1
- package/src/build.ts +0 -0
- package/src/pnpmfile.ts +0 -121
- package/src/test/pnpmfile.test.ts +0 -223
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
import type { ChildProcess } from 'child_process';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
import type * as ts from 'typescript';
|
|
5
|
-
|
|
6
|
-
import type { TsxConfig } from './compiler';
|
|
7
|
-
|
|
8
|
-
type Status = 'startWatch' | 'reCompile' | 'error' | 'success' | '';
|
|
9
|
-
export interface TscConfig extends TsxConfig {
|
|
10
|
-
watch?: boolean;
|
|
11
|
-
options?: ts.CompilerOptions;
|
|
12
|
-
onWatchStatusChanged?: (data: { status: Status; message: string }) => any;
|
|
13
|
-
onReportDiagnostic?: (data: { diagnostic: ts.Diagnostic }) => any;
|
|
14
|
-
onEmitDiagnostics?: (data: { diagnostics: ts.Diagnostic[] }) => any;
|
|
15
|
-
}
|
|
16
|
-
export function tsc(config: TscConfig) {
|
|
17
|
-
const logger = console;
|
|
18
|
-
const ts: typeof import('typescript') = require('typescript');
|
|
19
|
-
// https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#writing-an-incremental-program-watcher
|
|
20
|
-
|
|
21
|
-
const rootDir = config.rootDir || '.';
|
|
22
|
-
|
|
23
|
-
const formatHost: ts.FormatDiagnosticsHost = {
|
|
24
|
-
getCanonicalFileName: path => path,
|
|
25
|
-
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
26
|
-
getNewLine: () => ts.sys.newLine,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
function reportDiagnostic(diagnostic: ts.Diagnostic) {
|
|
30
|
-
config.onReportDiagnostic?.({ diagnostic });
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Prints a diagnostic every time the watch status changes.
|
|
35
|
-
* This is mainly for messages like "Starting compilation" or "Compilation completed".
|
|
36
|
-
*/
|
|
37
|
-
let a: ChildProcess | any;
|
|
38
|
-
function reportWatchStatusChanged(diagnostic: ts.Diagnostic) {
|
|
39
|
-
const message = `${ts.formatDiagnostic(diagnostic, formatHost)}`.trimEnd();
|
|
40
|
-
let status: Status = '';
|
|
41
|
-
if (message.includes('message TS6031: Starting compilation in watch mode')) {
|
|
42
|
-
status = 'startWatch';
|
|
43
|
-
} else if (message.includes('message TS6032: File change detected')) {
|
|
44
|
-
status = 'reCompile';
|
|
45
|
-
} else if (message.includes('Found 0 errors')) {
|
|
46
|
-
status = 'success';
|
|
47
|
-
} else if (/Found \d+ error/.test(message)) {
|
|
48
|
-
status = 'error';
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
config.onWatchStatusChanged?.({ status, message });
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const configPath = ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.json');
|
|
55
|
-
if (!configPath) {
|
|
56
|
-
throw new Error('找不到tsconfig.json');
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
|
|
60
|
-
|
|
61
|
-
if (config.watch) {
|
|
62
|
-
const host = ts.createWatchCompilerHost(
|
|
63
|
-
configPath,
|
|
64
|
-
{
|
|
65
|
-
...config.options,
|
|
66
|
-
outDir: config.outDir,
|
|
67
|
-
noEmit: false,
|
|
68
|
-
incremental: true,
|
|
69
|
-
},
|
|
70
|
-
ts.sys,
|
|
71
|
-
createProgram,
|
|
72
|
-
reportDiagnostic,
|
|
73
|
-
reportWatchStatusChanged,
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
ts.createWatchProgram(host);
|
|
77
|
-
} else {
|
|
78
|
-
// 读取 tsconfig.json 文件
|
|
79
|
-
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
80
|
-
|
|
81
|
-
// 解析 tsconfig.json 文件
|
|
82
|
-
const compilerOptions = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(configPath), {
|
|
83
|
-
...config.options,
|
|
84
|
-
outDir: config.outDir,
|
|
85
|
-
noEmit: false,
|
|
86
|
-
incremental: false,
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// 获取编译器实例
|
|
90
|
-
const host = ts.createCompilerHost(compilerOptions.options);
|
|
91
|
-
const program = ts.createProgram(compilerOptions.fileNames, compilerOptions.options, host);
|
|
92
|
-
|
|
93
|
-
// 执行编译
|
|
94
|
-
const emitResult = program.emit();
|
|
95
|
-
|
|
96
|
-
// 处理编译结果
|
|
97
|
-
const diagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
98
|
-
|
|
99
|
-
config.onEmitDiagnostics?.({ diagnostics });
|
|
100
|
-
}
|
|
101
|
-
}
|
|
1
|
+
import type { ChildProcess } from 'child_process';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
import type * as ts from 'typescript';
|
|
5
|
+
|
|
6
|
+
import type { TsxConfig } from './compiler';
|
|
7
|
+
|
|
8
|
+
type Status = 'startWatch' | 'reCompile' | 'error' | 'success' | '';
|
|
9
|
+
export interface TscConfig extends TsxConfig {
|
|
10
|
+
watch?: boolean;
|
|
11
|
+
options?: ts.CompilerOptions;
|
|
12
|
+
onWatchStatusChanged?: (data: { status: Status; message: string }) => any;
|
|
13
|
+
onReportDiagnostic?: (data: { diagnostic: ts.Diagnostic }) => any;
|
|
14
|
+
onEmitDiagnostics?: (data: { diagnostics: ts.Diagnostic[] }) => any;
|
|
15
|
+
}
|
|
16
|
+
export function tsc(config: TscConfig) {
|
|
17
|
+
const logger = console;
|
|
18
|
+
const ts: typeof import('typescript') = require('typescript');
|
|
19
|
+
// https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#writing-an-incremental-program-watcher
|
|
20
|
+
|
|
21
|
+
const rootDir = config.rootDir || '.';
|
|
22
|
+
|
|
23
|
+
const formatHost: ts.FormatDiagnosticsHost = {
|
|
24
|
+
getCanonicalFileName: path => path,
|
|
25
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
26
|
+
getNewLine: () => ts.sys.newLine,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function reportDiagnostic(diagnostic: ts.Diagnostic) {
|
|
30
|
+
config.onReportDiagnostic?.({ diagnostic });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Prints a diagnostic every time the watch status changes.
|
|
35
|
+
* This is mainly for messages like "Starting compilation" or "Compilation completed".
|
|
36
|
+
*/
|
|
37
|
+
let a: ChildProcess | any;
|
|
38
|
+
function reportWatchStatusChanged(diagnostic: ts.Diagnostic) {
|
|
39
|
+
const message = `${ts.formatDiagnostic(diagnostic, formatHost)}`.trimEnd();
|
|
40
|
+
let status: Status = '';
|
|
41
|
+
if (message.includes('message TS6031: Starting compilation in watch mode')) {
|
|
42
|
+
status = 'startWatch';
|
|
43
|
+
} else if (message.includes('message TS6032: File change detected')) {
|
|
44
|
+
status = 'reCompile';
|
|
45
|
+
} else if (message.includes('Found 0 errors')) {
|
|
46
|
+
status = 'success';
|
|
47
|
+
} else if (/Found \d+ error/.test(message)) {
|
|
48
|
+
status = 'error';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
config.onWatchStatusChanged?.({ status, message });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const configPath = ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.json');
|
|
55
|
+
if (!configPath) {
|
|
56
|
+
throw new Error('找不到tsconfig.json');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
|
|
60
|
+
|
|
61
|
+
if (config.watch) {
|
|
62
|
+
const host = ts.createWatchCompilerHost(
|
|
63
|
+
configPath,
|
|
64
|
+
{
|
|
65
|
+
...config.options,
|
|
66
|
+
outDir: config.outDir,
|
|
67
|
+
noEmit: false,
|
|
68
|
+
incremental: true,
|
|
69
|
+
},
|
|
70
|
+
ts.sys,
|
|
71
|
+
createProgram,
|
|
72
|
+
reportDiagnostic,
|
|
73
|
+
reportWatchStatusChanged,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
ts.createWatchProgram(host);
|
|
77
|
+
} else {
|
|
78
|
+
// 读取 tsconfig.json 文件
|
|
79
|
+
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
80
|
+
|
|
81
|
+
// 解析 tsconfig.json 文件
|
|
82
|
+
const compilerOptions = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(configPath), {
|
|
83
|
+
...config.options,
|
|
84
|
+
outDir: config.outDir,
|
|
85
|
+
noEmit: false,
|
|
86
|
+
incremental: false,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// 获取编译器实例
|
|
90
|
+
const host = ts.createCompilerHost(compilerOptions.options);
|
|
91
|
+
const program = ts.createProgram(compilerOptions.fileNames, compilerOptions.options, host);
|
|
92
|
+
|
|
93
|
+
// 执行编译
|
|
94
|
+
const emitResult = program.emit();
|
|
95
|
+
|
|
96
|
+
// 处理编译结果
|
|
97
|
+
const diagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
98
|
+
|
|
99
|
+
config.onEmitDiagnostics?.({ diagnostics });
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/plugin/config.ts
CHANGED
|
@@ -1,63 +1,78 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
|
|
3
|
-
import type { Config, Plugin } from '../runner';
|
|
4
|
-
|
|
5
|
-
export function pluginConfig() {
|
|
6
|
-
const plugin: Plugin = {
|
|
7
|
-
name: pluginConfig.name,
|
|
8
|
-
apply: runner => {
|
|
9
|
-
const { logger, hook, config, fs, z } = runner;
|
|
10
|
-
const log = logger.withTag(pluginConfig.name);
|
|
11
|
-
|
|
12
|
-
hook.loadConfig.tapPromise(pluginConfig.name, async () => {
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
1
|
+
import path from 'path';
|
|
2
|
+
|
|
3
|
+
import type { Config, Plugin } from '../runner';
|
|
4
|
+
|
|
5
|
+
export function pluginConfig() {
|
|
6
|
+
const plugin: Plugin = {
|
|
7
|
+
name: pluginConfig.name,
|
|
8
|
+
apply: runner => {
|
|
9
|
+
const { logger, hook, config, command, fs, z } = runner;
|
|
10
|
+
const log = logger.withTag(pluginConfig.name);
|
|
11
|
+
|
|
12
|
+
hook.loadConfig.tapPromise(pluginConfig.name, async () => {
|
|
13
|
+
let filePath = '';
|
|
14
|
+
const mjs = 'app.mjs';
|
|
15
|
+
const fileNames = ['app.js', 'app.cjs', mjs];
|
|
16
|
+
let isEsm = false;
|
|
17
|
+
for (const fileName of fileNames) {
|
|
18
|
+
if (await fs.exists(fileName)) {
|
|
19
|
+
filePath = path.posix.resolve(fileName);
|
|
20
|
+
isEsm = fileName === mjs;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
log.debug(`filePath %s`, filePath);
|
|
25
|
+
if (!filePath) {
|
|
26
|
+
log.error(`找不到配置文件 app.(c|m)?js`);
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
let all: Config[] = [];
|
|
30
|
+
if (isEsm) {
|
|
31
|
+
all = (await import(filePath)).default;
|
|
32
|
+
} else {
|
|
33
|
+
all = require(filePath);
|
|
34
|
+
}
|
|
35
|
+
runner.config.all = all;
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
hook.validateConfig.tapPromise(pluginConfig.name, async () => {
|
|
39
|
+
const validation = z
|
|
40
|
+
.array(
|
|
41
|
+
z.object({
|
|
42
|
+
name: z.string().trim().min(1),
|
|
43
|
+
}),
|
|
44
|
+
)
|
|
45
|
+
.min(1)
|
|
46
|
+
.safeParse(config.all);
|
|
47
|
+
if (!validation.success) {
|
|
48
|
+
log.error(validation.error.message);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
hook.applyConfig.tapPromise(pluginConfig.name, async () => {
|
|
54
|
+
const result = z
|
|
55
|
+
.object({
|
|
56
|
+
name: z.string().trim().default(''),
|
|
57
|
+
})
|
|
58
|
+
.default({})
|
|
59
|
+
.safeParse(command.opts());
|
|
60
|
+
const name = result.data?.name;
|
|
61
|
+
if (!name) {
|
|
62
|
+
log.debug('没有指定 --name,默认使用列表第一个');
|
|
63
|
+
config.current = config.all[0];
|
|
64
|
+
} else {
|
|
65
|
+
log.debug(`指定 --name ${name}`);
|
|
66
|
+
const find = config.all.find(t => t.name === name);
|
|
67
|
+
if (!find) {
|
|
68
|
+
log.error(`未找到 name '%s'`, name);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
config.current = find;
|
|
72
|
+
}
|
|
73
|
+
config.current.plugins.forEach(plugin => plugin.apply(runner));
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
return plugin;
|
|
78
|
+
}
|
package/src/plugin/index.ts
CHANGED
package/src/plugin/package.ts
CHANGED
package/src/plugin/release.ts
CHANGED
|
@@ -1,32 +1,113 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
1
|
+
import { execSync } from 'child_process';
|
|
2
|
+
|
|
3
|
+
import type { Plugin } from '../runner';
|
|
4
|
+
|
|
5
|
+
export function pluginRelease() {
|
|
6
|
+
const plugin: Plugin = {
|
|
7
|
+
name: pluginRelease.name,
|
|
8
|
+
apply: runner => {
|
|
9
|
+
const { logger, hook, fs } = runner;
|
|
10
|
+
const log = logger.withTag(pluginRelease.name);
|
|
11
|
+
hook.release.tapPromise(pluginRelease.name, async () => {
|
|
12
|
+
runner.clear();
|
|
13
|
+
await hook.build.promise();
|
|
14
|
+
|
|
15
|
+
log.start('开始发布');
|
|
16
|
+
const packageVersion = runner.package.version;
|
|
17
|
+
const packageName = runner.package.name;
|
|
18
|
+
const url = `https://registry.npmjs.org/${packageName}`;
|
|
19
|
+
let nextVersion = packageVersion;
|
|
20
|
+
log.debug(`查询版本号 ${packageVersion} 是否存在`);
|
|
21
|
+
const res = await (await fetch(url)).json();
|
|
22
|
+
const exist = res?.versions?.[packageVersion];
|
|
23
|
+
if (exist) {
|
|
24
|
+
const split = packageVersion.split('.');
|
|
25
|
+
split[split.length - 1] = Number(split[split.length - 1]) + 1;
|
|
26
|
+
nextVersion = split.join('.');
|
|
27
|
+
log.debug(`版本号 ${packageVersion} 存在,更新版本号为 ${nextVersion}`);
|
|
28
|
+
const text = fs.readFileSync('./package.json').toString();
|
|
29
|
+
const nextText = text.replace(new RegExp(`"version":\\s*"${packageVersion}"`), `"version": "${nextVersion}"`);
|
|
30
|
+
fs.writeFileSync('./package.json', nextText);
|
|
31
|
+
}
|
|
32
|
+
log.debug(`版本号 ${packageVersion} 不存在`);
|
|
33
|
+
log.info(`版本号为 ${nextVersion}`);
|
|
34
|
+
|
|
35
|
+
const tempDir = `./node_modules/.rife/release/`;
|
|
36
|
+
log.info(`清空目录 ${tempDir}`);
|
|
37
|
+
fs.emptyDirSync(tempDir);
|
|
38
|
+
|
|
39
|
+
const pack = `pnpm pack --pack-destination ${tempDir} --json`;
|
|
40
|
+
log.info(`打包文件 ${pack}`);
|
|
41
|
+
const packOutput = JSON.parse(execSync(pack).toString());
|
|
42
|
+
const filename = packOutput.filename;
|
|
43
|
+
for (const item of packOutput.files) {
|
|
44
|
+
log.debug(item.path);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const debug = false;
|
|
48
|
+
const publish = `pnpm publish ${filename} --registry https://registry.npmjs.org --no-git-checks ${
|
|
49
|
+
debug ? '--dry-run' : ''
|
|
50
|
+
}`;
|
|
51
|
+
log.info(`发布文件 ${publish}`);
|
|
52
|
+
execSync(publish, { stdio: 'inherit' });
|
|
53
|
+
log.success(`发布成功,${runner.duration}`);
|
|
54
|
+
|
|
55
|
+
log.info('同步版本');
|
|
56
|
+
let hasSync = false;
|
|
57
|
+
while (true) {
|
|
58
|
+
const res = await queryVersion(packageName);
|
|
59
|
+
const nextManifest = res?.versions?.[nextVersion];
|
|
60
|
+
const exist = Boolean(nextManifest);
|
|
61
|
+
const latest = res?.['dist-tags']?.latest;
|
|
62
|
+
if (exist) {
|
|
63
|
+
log.debug(`同步版本存在`);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
log.info(`检查版本 ${nextVersion} 不存在,最新版本为 ${latest}`);
|
|
67
|
+
if (!hasSync) {
|
|
68
|
+
hasSync = true;
|
|
69
|
+
log.start(`开始同步`);
|
|
70
|
+
const sync = await syncVersion(packageName);
|
|
71
|
+
if (sync.ok === true) {
|
|
72
|
+
log.info(
|
|
73
|
+
`调用同步接口成功,日志 https://registry.npmmirror.com/-/package/${packageName}/syncs/${sync.id}/log`,
|
|
74
|
+
);
|
|
75
|
+
} else {
|
|
76
|
+
log.debug(`调用同步接口失败,%j`, sync);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
80
|
+
}
|
|
81
|
+
log.success(`同步成功,${runner.duration}`);
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
return plugin;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function queryVersion(packageName: string) {
|
|
89
|
+
const res = await (await fetch(`https://registry.npmmirror.com/${packageName}?t=${Math.random()}`)).json();
|
|
90
|
+
return res;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function syncVersion(packageName: string) {
|
|
94
|
+
const sync = await (
|
|
95
|
+
await fetch(`https://registry-direct.npmmirror.com/-/package/${packageName}/syncs`, {
|
|
96
|
+
headers: {
|
|
97
|
+
accept: '*/*',
|
|
98
|
+
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
99
|
+
'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
|
|
100
|
+
'sec-ch-ua-mobile': '?0',
|
|
101
|
+
'sec-ch-ua-platform': '"Windows"',
|
|
102
|
+
'sec-fetch-dest': 'empty',
|
|
103
|
+
'sec-fetch-mode': 'cors',
|
|
104
|
+
'sec-fetch-site': 'same-site',
|
|
105
|
+
Referer: 'https://npmmirror.com/',
|
|
106
|
+
'Referrer-Policy': 'strict-origin-when-cross-origin',
|
|
107
|
+
},
|
|
108
|
+
body: null,
|
|
109
|
+
method: 'PUT',
|
|
110
|
+
})
|
|
111
|
+
).json();
|
|
112
|
+
return sync;
|
|
113
|
+
}
|