-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathgenerateUsingComponent.ts
More file actions
63 lines (51 loc) · 2.07 KB
/
generateUsingComponent.ts
File metadata and controls
63 lines (51 loc) · 2.07 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
import path from 'path';
import fs from 'fs';
import { Config } from './getConfig';
import { getTemplate } from './getTemplate';
import {
replaceIsRpx,
replaceNames,
replacePlatform,
replaceSize,
replaceRelativePath,
replaceDesignWidth,
} from './replace';
export const generateUsingComponent = (config: Config, names: string[], platform?: string) => {
const saveDir = path.resolve(config.save_dir);
const jsxExtension = config.use_typescript ? '.tsx' : '.js';
let iconFile: string;
if (platform) {
if (fs.existsSync(path.join(__dirname, '../templates/index.' + platform + jsxExtension + '.template'))) {
iconFile = getTemplate('index.' + platform + jsxExtension);
} else {
iconFile = getTemplate(`index.platform${(platform && platform.includes("-vue")) ? "-vue" : ""}` + jsxExtension);
}
} else {
iconFile = getTemplate('index' + jsxExtension);
}
iconFile = replaceNames(iconFile, names);
iconFile = replaceSize(iconFile, config.default_icon_size);
if (platform === 'h5' && config.use_rpx) {
let designWidth = config.design_width || 750
iconFile = replaceDesignWidth(iconFile, designWidth);
}
iconFile = replaceIsRpx(iconFile, config.use_rpx);
if (platform) {
iconFile = replacePlatform(iconFile, platform);
}
if (!platform && !config.use_typescript) {
let definitionFile = getTemplate('index.d.ts');
definitionFile = replaceNames(definitionFile, names);
fs.writeFileSync(path.join(saveDir, 'index.d.ts'), definitionFile);
}
// index.config.ts only support commonJs
let helperFile = getTemplate('helper.js');
helperFile = replaceRelativePath(helperFile, config.save_dir);
fs.writeFileSync(path.join(saveDir, 'helper.js'), helperFile);
fs.writeFileSync(path.join(saveDir, 'helper.d.ts'), getTemplate('helper.d.ts'));
if (platform && platform.includes("-vue")) {
platform = platform.replace(/-vue/, "");
fs.writeFileSync(path.join(saveDir, "shims.d.ts"), getTemplate('shims.d.ts'));
}
fs.writeFileSync(path.join(saveDir, 'index' + (platform ? `.${platform}` : '') + jsxExtension), iconFile);
};