-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalize.ts
More file actions
126 lines (117 loc) · 3.17 KB
/
localize.ts
File metadata and controls
126 lines (117 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import spawn from 'cross-spawn-promise';
import { ensureDir, ensureFile, writeFile } from 'fs-extra';
import { join, resolve } from 'path';
import uuid from 'short-uuid';
import { debug } from './core';
import { IMaterializeOnlineOptions, IMaterializeOnlinePackageAndVersionOptions } from './types';
const log = debug.extend('localize');
/**
* 创建组件包
*
* @private
* @param {{
* pkgName: string;
* pkgVersion: string;
* }} params
* @returns {Promise<void>}
* @memberof OnlineAccesser
*/
export async function createFakePackage(params: {
workDir: string;
pkgName: string;
pkgVersion: string;
npmClient?: string;
}): Promise<void> {
// 创建临时组件包
const { workDir } = params;
const pkgJsonFilePath = join(workDir, 'package.json');
await ensureFile(pkgJsonFilePath);
await writeFile(
pkgJsonFilePath,
JSON.stringify(
{
name: params.pkgName,
version: params.pkgVersion || '0.0.0',
dependencies: {
[params.pkgName]: params.pkgVersion || 'latest',
react: 'latest',
'react-dom': 'latest',
'parse-prop-types': '^0.3.0',
typesync: 'latest',
},
},
null,
2,
),
);
// 安装依赖
const npmClient = params.npmClient || 'tnpm';
await spawn(npmClient, ['i'], { stdio: 'inherit', cwd: workDir } as any);
}
/**
* 创建临时目录
*
* @private
* @returns {Promise<string>} 返回临时文件夹路径
* @memberof LocalGenerator
*/
export async function createworkDir(tempDir?: string): Promise<string> {
const workDirName = uuid.generate();
const workDir = resolve(tempDir || '../../node_modules/.temp/', workDirName);
await ensureDir(workDir);
log('create temp dir successfully', workDir);
return workDir;
}
/**
* 分离物料组件名称和版本号
*
* @private
* @param {string} pkgNameWithVersion
* @returns {{ [key: string]: any }}
* @memberof OnlineAccesser
*/
export function getPkgNameAndVersion(pkgNameWithVersion: string): { [key: string]: any } {
const matches = pkgNameWithVersion.match(/(@[^/]+)$/);
if (!matches) {
return {
name: pkgNameWithVersion,
};
}
const name = pkgNameWithVersion.replace(matches[0], '');
return {
version: matches[0].slice(1),
name,
};
}
// 将问题转化为本地物料化场景
export default async function localize(options: IMaterializeOnlineOptions): Promise<{
workDir: string;
moduleDir: string;
entry?: string;
}> {
// 创建临时目录
const workDir = await createworkDir(options.tempDir);
await ensureDir(workDir);
let { name, version = 'latest' } = options as IMaterializeOnlinePackageAndVersionOptions;
if (!name) {
const pkgNameAndVersion = getPkgNameAndVersion(options.entry);
name = pkgNameAndVersion.name;
version = pkgNameAndVersion.version;
}
// 创建组件包
await createFakePackage({
pkgName: name,
pkgVersion: version,
workDir,
npmClient: options.npmClient,
});
const result = {
workDir,
moduleDir: join(workDir, 'node_modules', name),
entry: undefined,
};
if ((options as IMaterializeOnlinePackageAndVersionOptions)?.name) {
result.entry = options.entry;
}
return result;
}