@ctzy-web-client/create-web-client 1.0.4
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/bin/create-web-client.js +225 -0
- package/bin/utils/index.js +105 -0
- package/index.js +2 -0
- package/lib/download.js +184 -0
- package/lib/generator.js +142 -0
- package/package.json +30 -0
- package/templates/qiankun-subapp-vue-template/.env +5 -0
- package/templates/qiankun-subapp-vue-template/index.html +16 -0
- package/templates/qiankun-subapp-vue-template/jsconfig.json +5 -0
- package/templates/qiankun-subapp-vue-template/mocks/test.js +60 -0
- package/templates/qiankun-subapp-vue-template/package.json +34 -0
- package/templates/qiankun-subapp-vue-template/public/vite.svg +1 -0
- package/templates/qiankun-subapp-vue-template/src/interceptors/AppRequestInterceptors.js +50 -0
- package/templates/qiankun-subapp-vue-template/src/interceptors/AppRouterInterceptors.js +25 -0
- package/templates/qiankun-subapp-vue-template/src/main.js +23 -0
- package/templates/qiankun-subapp-vue-template/src/models/TestDataModel.js +45 -0
- package/templates/qiankun-subapp-vue-template/src/pages/form/index.vue +120 -0
- package/templates/qiankun-subapp-vue-template/src/pages/home/index.vue +57 -0
- package/templates/qiankun-subapp-vue-template/src/services/TestServices.js +21 -0
- package/templates/qiankun-subapp-vue-template/vite.config.ts +54 -0
- package/templates/qiankun-vue-template/.env +4 -0
- package/templates/qiankun-vue-template/index.html +16 -0
- package/templates/qiankun-vue-template/jsconfig.json +5 -0
- package/templates/qiankun-vue-template/package.json +33 -0
- package/templates/qiankun-vue-template/public/vite.svg +1 -0
- package/templates/qiankun-vue-template/src/main.js +21 -0
- package/templates/qiankun-vue-template/src/pages/app/index.vue +15 -0
- package/templates/qiankun-vue-template/src/services/ApplicaitionService.js +6 -0
- package/templates/qiankun-vue-template/src/services/UserService.js +4 -0
- package/templates/qiankun-vue-template/vite.config.ts +35 -0
- package/templates/web-client-vue-template/package.json +18 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
// import { fileURLToPath } from 'url';
|
|
4
|
+
import minimist from 'minimist';
|
|
5
|
+
import prompts from 'prompts';
|
|
6
|
+
import { blue, green, red, reset, yellow } from 'kolorist';
|
|
7
|
+
import {
|
|
8
|
+
formatTargetDir,
|
|
9
|
+
copy,
|
|
10
|
+
isValidPackageName,
|
|
11
|
+
toValidPackageName,
|
|
12
|
+
isEmpty,
|
|
13
|
+
emptyDir,
|
|
14
|
+
pkgFromUserAgent,
|
|
15
|
+
} from './utils/index.js';
|
|
16
|
+
|
|
17
|
+
//参数列表
|
|
18
|
+
const argv = minimist(process.argv.slice(2), { string: ['_'] });
|
|
19
|
+
|
|
20
|
+
//命令行
|
|
21
|
+
const cwd = process.cwd();
|
|
22
|
+
|
|
23
|
+
//模板列表
|
|
24
|
+
const templates = [
|
|
25
|
+
{
|
|
26
|
+
name: 'web-base-client-vue',
|
|
27
|
+
display: '单体web客户端应用(web-client-app)',
|
|
28
|
+
color: yellow,
|
|
29
|
+
// variants: [
|
|
30
|
+
// {
|
|
31
|
+
// name: 'vue-app',
|
|
32
|
+
// display: 'JavaScript',
|
|
33
|
+
// color: yellow
|
|
34
|
+
// }
|
|
35
|
+
// ]
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: 'qiankun-subapp-vue',
|
|
39
|
+
display: 'VUE版本微前端子项目',
|
|
40
|
+
color: blue,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'qiankun-vue',
|
|
44
|
+
display: '乾坤+VUE基座项目',
|
|
45
|
+
color: green,
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
//模板名称
|
|
49
|
+
const templateNames = templates
|
|
50
|
+
.map((f) => (f.variants && f.variants.map((v) => v.name)) || [f.name])
|
|
51
|
+
.reduce((a, b) => a.concat(b), []);
|
|
52
|
+
//默认项目名称
|
|
53
|
+
const defaultTargetDir = 'web-client-app';
|
|
54
|
+
//renameFiles
|
|
55
|
+
const renameFiles = {
|
|
56
|
+
_gitignore: '.gitignore',
|
|
57
|
+
};
|
|
58
|
+
//初始化
|
|
59
|
+
async function init() {
|
|
60
|
+
const argTargetDir = formatTargetDir(argv._[0]);
|
|
61
|
+
|
|
62
|
+
const argTemplate = argv.template || argv.t;
|
|
63
|
+
|
|
64
|
+
let targetDir = argTargetDir || defaultTargetDir;
|
|
65
|
+
|
|
66
|
+
//获取项目名称
|
|
67
|
+
const getProjectName = () =>
|
|
68
|
+
targetDir === '.' ? path.basename(path.resolve()) : targetDir;
|
|
69
|
+
|
|
70
|
+
let result;
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
result = await prompts(
|
|
74
|
+
[
|
|
75
|
+
{
|
|
76
|
+
type: argTargetDir ? null : 'text',
|
|
77
|
+
name: 'projectName',
|
|
78
|
+
message: reset('Project name:'),
|
|
79
|
+
initial: defaultTargetDir,
|
|
80
|
+
onState: (state) => {
|
|
81
|
+
targetDir = formatTargetDir(state.value) || defaultTargetDir;
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
type: () =>
|
|
86
|
+
!fs.existsSync(targetDir) || isEmpty(targetDir) ? null : 'confirm',
|
|
87
|
+
name: 'overwrite',
|
|
88
|
+
message: () =>
|
|
89
|
+
(targetDir === '.'
|
|
90
|
+
? 'Current directory'
|
|
91
|
+
: `Target directory "${targetDir}"`) +
|
|
92
|
+
` is not empty. Remove existing files and continue?`,
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
type: (_, { overwrite }) => {
|
|
96
|
+
if (overwrite === false) {
|
|
97
|
+
throw new Error(red('✖') + ' Operation cancelled');
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
},
|
|
101
|
+
name: 'overwriteChecker',
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
type: () => (isValidPackageName(getProjectName()) ? null : 'text'),
|
|
105
|
+
name: 'packageName',
|
|
106
|
+
message: reset('Package name:'),
|
|
107
|
+
initial: () => toValidPackageName(getProjectName()),
|
|
108
|
+
validate: (dir) =>
|
|
109
|
+
isValidPackageName(dir) || 'Invalid package.json name',
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
type:
|
|
113
|
+
argTemplate && templateNames.includes(argTemplate)
|
|
114
|
+
? null
|
|
115
|
+
: 'select',
|
|
116
|
+
name: 'template',
|
|
117
|
+
message:
|
|
118
|
+
typeof argTemplate === 'string' &&
|
|
119
|
+
!templateNames.includes(argTemplate)
|
|
120
|
+
? reset(
|
|
121
|
+
`"${argTemplate}" isn't a valid template. Please choose from below: `
|
|
122
|
+
)
|
|
123
|
+
: reset('Select a template:'),
|
|
124
|
+
initial: 0,
|
|
125
|
+
choices: templates.map((template) => {
|
|
126
|
+
const color = template.color;
|
|
127
|
+
return {
|
|
128
|
+
title: color(template.display || template.name),
|
|
129
|
+
value: template,
|
|
130
|
+
};
|
|
131
|
+
}),
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
type: (template) => (template && template.variants ? 'select' : null),
|
|
135
|
+
name: 'variant',
|
|
136
|
+
message: reset('Select a variant:'),
|
|
137
|
+
choices: (template) =>
|
|
138
|
+
template.variants.map((variant) => {
|
|
139
|
+
const variantColor = variant.color;
|
|
140
|
+
return {
|
|
141
|
+
title: variantColor(variant.display || variant.name),
|
|
142
|
+
value: variant.name,
|
|
143
|
+
};
|
|
144
|
+
}),
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
{
|
|
148
|
+
onCancel: () => {
|
|
149
|
+
throw new Error(red('✖') + ' Operation cancelled');
|
|
150
|
+
},
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
} catch (cancelled) {
|
|
154
|
+
console.log(cancelled.message);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// user choice associated with prompts
|
|
159
|
+
const { template, overwrite, packageName, variant } = result;
|
|
160
|
+
|
|
161
|
+
const root = path.join(cwd, targetDir);
|
|
162
|
+
|
|
163
|
+
if (overwrite) {
|
|
164
|
+
emptyDir(root);
|
|
165
|
+
} else if (!fs.existsSync(root)) {
|
|
166
|
+
fs.mkdirSync(root, { recursive: true });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// determine template
|
|
170
|
+
const installTemplate = variant || (template && template.name) || argTemplate;
|
|
171
|
+
|
|
172
|
+
const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent);
|
|
173
|
+
const pkgManager = pkgInfo ? pkgInfo.name : 'npm';
|
|
174
|
+
|
|
175
|
+
// 该提示不输出
|
|
176
|
+
// console.log(`\nScaffolding project in ${root}...`)
|
|
177
|
+
|
|
178
|
+
const templateDir = path.resolve(
|
|
179
|
+
// fileURLToPath(__dirname),
|
|
180
|
+
__dirname,
|
|
181
|
+
'../templates',
|
|
182
|
+
`${installTemplate}-template`
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
const write = (file, content) => {
|
|
186
|
+
const targetPath = path.join(root, renameFiles[file] ?? file);
|
|
187
|
+
if (content) {
|
|
188
|
+
fs.writeFileSync(targetPath, content);
|
|
189
|
+
} else {
|
|
190
|
+
copy(path.join(templateDir, file), targetPath);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const files = fs.readdirSync(templateDir);
|
|
195
|
+
for (const file of files.filter((f) => f !== 'package.json')) {
|
|
196
|
+
write(file);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const pkg = JSON.parse(
|
|
200
|
+
fs.readFileSync(path.join(templateDir, `package.json`), 'utf-8')
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
pkg.name = packageName || getProjectName();
|
|
204
|
+
|
|
205
|
+
write('package.json', JSON.stringify(pkg, null, 2));
|
|
206
|
+
|
|
207
|
+
console.log(`\nDone. Now run:\n`);
|
|
208
|
+
if (root !== cwd) {
|
|
209
|
+
console.log(` cd ${path.relative(cwd, root)}`);
|
|
210
|
+
}
|
|
211
|
+
switch (pkgManager) {
|
|
212
|
+
case 'yarn':
|
|
213
|
+
console.log(' yarn');
|
|
214
|
+
console.log(' yarn dev');
|
|
215
|
+
break;
|
|
216
|
+
default:
|
|
217
|
+
console.log(` ${pkgManager} install`);
|
|
218
|
+
console.log(` ${pkgManager} run dev`);
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
console.log();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
//初始化
|
|
225
|
+
init().catch((e) => console.error(e));
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
/**
|
|
4
|
+
* 格式化目标目录
|
|
5
|
+
* @param {*} targetDir
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export const formatTargetDir = function formatTargetDir(targetDir) {
|
|
9
|
+
return targetDir?.trim().replace(/\/+$/g, '');
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 拷贝
|
|
14
|
+
* @param {*} src
|
|
15
|
+
* @param {*} dest
|
|
16
|
+
*/
|
|
17
|
+
export const copy = function copy(src, dest) {
|
|
18
|
+
const stat = fs.statSync(src);
|
|
19
|
+
if (stat.isDirectory()) {
|
|
20
|
+
copyDir(src, dest);
|
|
21
|
+
} else {
|
|
22
|
+
fs.copyFileSync(src, dest);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 检测项目名称是否有效
|
|
28
|
+
* @param {*} projectName
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
export const isValidPackageName = function isValidPackageName(projectName) {
|
|
32
|
+
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
|
|
33
|
+
projectName
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 将项目名称处理成有效的名称
|
|
39
|
+
* @param {*} projectName
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
export const toValidPackageName = function toValidPackageName(projectName) {
|
|
43
|
+
return projectName
|
|
44
|
+
.trim()
|
|
45
|
+
.toLowerCase()
|
|
46
|
+
.replace(/\s+/g, '-')
|
|
47
|
+
.replace(/^[._]/, '')
|
|
48
|
+
.replace(/[^a-z0-9-~]+/g, '-');
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 拷贝目录
|
|
53
|
+
* @param {*} srcDir
|
|
54
|
+
* @param {*} destDir
|
|
55
|
+
*/
|
|
56
|
+
export const copyDir = function copyDir(srcDir, destDir) {
|
|
57
|
+
fs.mkdirSync(destDir, { recursive: true });
|
|
58
|
+
for (const file of fs.readdirSync(srcDir)) {
|
|
59
|
+
const srcFile = path.resolve(srcDir, file);
|
|
60
|
+
const destFile = path.resolve(destDir, file);
|
|
61
|
+
copy(srcFile, destFile);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 是否是空目录
|
|
67
|
+
* @param {*} path
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
export const isEmpty = function isEmpty(path) {
|
|
71
|
+
const files = fs.readdirSync(path);
|
|
72
|
+
return files.length === 0 || (files.length === 1 && files[0] === '.git');
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 是否是空目录
|
|
77
|
+
* @param {*} dir
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
export const emptyDir = function emptyDir(dir) {
|
|
81
|
+
if (!fs.existsSync(dir)) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
for (const file of fs.readdirSync(dir)) {
|
|
85
|
+
if (file === '.git') {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
fs.rmSync(path.resolve(dir, file), { recursive: true, force: true });
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 更新packagejson
|
|
94
|
+
* @param {*} userAgent
|
|
95
|
+
* @returns
|
|
96
|
+
*/
|
|
97
|
+
export const pkgFromUserAgent = function pkgFromUserAgent(userAgent) {
|
|
98
|
+
if (!userAgent) return undefined;
|
|
99
|
+
const pkgSpec = userAgent.split(' ')[0];
|
|
100
|
+
const pkgSpecArr = pkgSpec.split('/');
|
|
101
|
+
return {
|
|
102
|
+
name: pkgSpecArr[0],
|
|
103
|
+
version: pkgSpecArr[1],
|
|
104
|
+
};
|
|
105
|
+
};
|
package/index.js
ADDED
package/lib/download.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
var downloadUrl = require('download');
|
|
2
|
+
var gitclone = require('git-clone');
|
|
3
|
+
var rm = require('rimraf').sync;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Expose `download`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
module.exports = download;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Download `repo` to `dest` and callback `fn(err)`.
|
|
13
|
+
*
|
|
14
|
+
* @param {String} repo
|
|
15
|
+
* @param {String} dest
|
|
16
|
+
* @param {Object} opts
|
|
17
|
+
* @param {Function} fn
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
function download(repo, dest, opts, fn) {
|
|
21
|
+
if (typeof opts === 'function') {
|
|
22
|
+
fn = opts;
|
|
23
|
+
opts = null;
|
|
24
|
+
}
|
|
25
|
+
opts = opts || {};
|
|
26
|
+
var clone = opts.clone || false;
|
|
27
|
+
|
|
28
|
+
repo = normalize(repo);
|
|
29
|
+
|
|
30
|
+
var url = repo.url || getUrl(repo, clone);
|
|
31
|
+
|
|
32
|
+
if (clone) {
|
|
33
|
+
gitclone(
|
|
34
|
+
url,
|
|
35
|
+
dest,
|
|
36
|
+
{
|
|
37
|
+
checkout: repo.checkout,
|
|
38
|
+
shallow: repo.checkout === 'master',
|
|
39
|
+
},
|
|
40
|
+
function (err) {
|
|
41
|
+
if (err === undefined) {
|
|
42
|
+
rm(dest + '/.git');
|
|
43
|
+
fn();
|
|
44
|
+
} else {
|
|
45
|
+
fn(err);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
} else {
|
|
50
|
+
downloadUrl(url, dest, {
|
|
51
|
+
extract: true,
|
|
52
|
+
strip: 1,
|
|
53
|
+
mode: '666',
|
|
54
|
+
headers: {
|
|
55
|
+
accept: 'application/zip',
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
.then(function (data) {
|
|
59
|
+
fn();
|
|
60
|
+
})
|
|
61
|
+
.catch(function (err) {
|
|
62
|
+
fn(err);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Normalize a repo string.
|
|
69
|
+
*
|
|
70
|
+
* @param {String} repo
|
|
71
|
+
* @return {Object}
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
function normalize(repo) {
|
|
75
|
+
var regex = /^(?:(direct):([^#]+)(?:#(.+))?)$/;
|
|
76
|
+
var match = regex.exec(repo);
|
|
77
|
+
|
|
78
|
+
if (match) {
|
|
79
|
+
var url = match[2];
|
|
80
|
+
var checkout = match[3] || 'master';
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
type: 'direct',
|
|
84
|
+
url: url,
|
|
85
|
+
checkout: checkout,
|
|
86
|
+
};
|
|
87
|
+
} else {
|
|
88
|
+
regex =
|
|
89
|
+
/^(?:(github|gitlab|bitbucket):)?(?:(.+):)?([^\/]+)\/([^#]+)(?:#(.+))?$/;
|
|
90
|
+
match = regex.exec(repo);
|
|
91
|
+
var type = match[1] || 'baswebapp';
|
|
92
|
+
var origin = match[2] || null;
|
|
93
|
+
var owner = match[3];
|
|
94
|
+
var name = match[4];
|
|
95
|
+
var checkout = match[5] || 'master';
|
|
96
|
+
|
|
97
|
+
if (origin == null) {
|
|
98
|
+
if (type === 'github') origin = 'github.com';
|
|
99
|
+
else if (type === 'gitlab') origin = 'gitlab.com';
|
|
100
|
+
else if (type === 'bitbucket') origin = 'bitbucket.com';
|
|
101
|
+
else if (type === 'baswebapp') origin = 'gitlab.baswebapp.cn';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
type: type,
|
|
106
|
+
origin: origin,
|
|
107
|
+
owner: owner,
|
|
108
|
+
name: name,
|
|
109
|
+
checkout: checkout,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Adds protocol to url in none specified
|
|
116
|
+
*
|
|
117
|
+
* @param {String} url
|
|
118
|
+
* @return {String}
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
function addProtocol(origin, clone) {
|
|
122
|
+
if (!/^(f|ht)tps?:\/\//i.test(origin)) {
|
|
123
|
+
if (clone) origin = 'git@' + origin;
|
|
124
|
+
else origin = 'https://' + origin;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return origin;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Return a zip or git url for a given `repo`.
|
|
132
|
+
*
|
|
133
|
+
* @param {Object} repo
|
|
134
|
+
* @return {String}
|
|
135
|
+
*/
|
|
136
|
+
|
|
137
|
+
function getUrl(repo, clone) {
|
|
138
|
+
var url;
|
|
139
|
+
|
|
140
|
+
// Get origin with protocol and add trailing slash or colon (for ssh)
|
|
141
|
+
var origin = addProtocol(repo.origin, clone);
|
|
142
|
+
if (/^git\@/i.test(origin)) origin = origin + ':';
|
|
143
|
+
else origin = origin + '/';
|
|
144
|
+
|
|
145
|
+
// Build url
|
|
146
|
+
if (clone) {
|
|
147
|
+
url = origin + repo.owner + '/' + repo.name + '.git';
|
|
148
|
+
} else {
|
|
149
|
+
if (repo.type === 'github')
|
|
150
|
+
url =
|
|
151
|
+
origin +
|
|
152
|
+
repo.owner +
|
|
153
|
+
'/' +
|
|
154
|
+
repo.name +
|
|
155
|
+
'/archive/' +
|
|
156
|
+
repo.checkout +
|
|
157
|
+
'.zip';
|
|
158
|
+
else if (repo.type === 'gitlab')
|
|
159
|
+
url =
|
|
160
|
+
origin +
|
|
161
|
+
repo.owner +
|
|
162
|
+
'/' +
|
|
163
|
+
repo.name +
|
|
164
|
+
'/repository/archive.zip?ref=' +
|
|
165
|
+
repo.checkout;
|
|
166
|
+
else if (repo.type === 'baswebapp')
|
|
167
|
+
url =
|
|
168
|
+
'http://gitlab.baswebapp.cn/bwa//' +
|
|
169
|
+
repo.name +
|
|
170
|
+
'/repository/archive.zip?ref=' +
|
|
171
|
+
repo.checkout;
|
|
172
|
+
else if (repo.type === 'bitbucket')
|
|
173
|
+
url =
|
|
174
|
+
origin +
|
|
175
|
+
repo.owner +
|
|
176
|
+
'/' +
|
|
177
|
+
repo.name +
|
|
178
|
+
'/get/' +
|
|
179
|
+
repo.checkout +
|
|
180
|
+
'.zip';
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return url;
|
|
184
|
+
}
|
package/lib/generator.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const isBinary = require('isbinaryfile');
|
|
4
|
+
const ora = require('ora');
|
|
5
|
+
const home = require('user-home');
|
|
6
|
+
const download = require('./download');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 生成
|
|
10
|
+
* @param {String} dir 目录
|
|
11
|
+
* @param {*} files
|
|
12
|
+
* @param {*} base
|
|
13
|
+
* @param {*} rootOptions
|
|
14
|
+
*/
|
|
15
|
+
async function generate(dir, files, base = '', rootOptions = {}) {
|
|
16
|
+
const glob = require('glob');
|
|
17
|
+
|
|
18
|
+
glob
|
|
19
|
+
.sync('**/*', {
|
|
20
|
+
cwd: dir,
|
|
21
|
+
nodir: true,
|
|
22
|
+
})
|
|
23
|
+
.forEach((rawPath) => {
|
|
24
|
+
const sourcePath = path.resolve(dir, rawPath);
|
|
25
|
+
|
|
26
|
+
const filename = path.join(base, rawPath);
|
|
27
|
+
|
|
28
|
+
//是否是二进制文件
|
|
29
|
+
if (isBinary.sync(sourcePath)) {
|
|
30
|
+
//如果是直接返回流
|
|
31
|
+
files[filename] = fs.readFileSync(sourcePath); // return buffer
|
|
32
|
+
} else {
|
|
33
|
+
//读取文件内容
|
|
34
|
+
let content = fs.readFileSync(sourcePath, 'utf-8');
|
|
35
|
+
|
|
36
|
+
if (path.basename(filename) === 'manifest.json') {
|
|
37
|
+
content = content.replace('{{name}}', rootOptions.projectName || '');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
//_aaa.js => aaa.js
|
|
41
|
+
if (filename.charAt(0) === '_' && filename.charAt(1) !== '_') {
|
|
42
|
+
files[`.${filename.slice(1)}`] = content;
|
|
43
|
+
}
|
|
44
|
+
//__aaa.js => _aaa.js
|
|
45
|
+
else if (filename.charAt(0) === '_' && filename.charAt(1) === '_') {
|
|
46
|
+
files[`${filename.slice(1)}`] = content;
|
|
47
|
+
}
|
|
48
|
+
//原模原样拷贝
|
|
49
|
+
else {
|
|
50
|
+
files[filename] = content;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = (api, options, rootOptions) => {
|
|
57
|
+
//扩展package配置
|
|
58
|
+
// api.extendPackage(pkg => {
|
|
59
|
+
// return {
|
|
60
|
+
// dependencies: {
|
|
61
|
+
// "axios": "^0.19.2",
|
|
62
|
+
// "core-js": "^3.6.5",
|
|
63
|
+
// "vue": "^2.6.11",
|
|
64
|
+
// "vue-router": "^3.2.0",
|
|
65
|
+
// "vuex": "^3.4.0"
|
|
66
|
+
// },
|
|
67
|
+
// devDependencies: {
|
|
68
|
+
// "@vue/cli-plugin-babel": "^4.5.0",
|
|
69
|
+
// "@vue/cli-service": "^4.5.0",
|
|
70
|
+
// "babel-plugin-import": "^1.13.0",
|
|
71
|
+
// "less": "^3.0.4",
|
|
72
|
+
// "less-loader": "^5.0.0",
|
|
73
|
+
// "vue-template-compiler": "^2.6.11"
|
|
74
|
+
// }
|
|
75
|
+
// }
|
|
76
|
+
// });
|
|
77
|
+
|
|
78
|
+
// if (options.template === 'bwa/bwa-template-h5') {
|
|
79
|
+
// api.extendPackage(pkg => {
|
|
80
|
+
// return {
|
|
81
|
+
// dependencies: {
|
|
82
|
+
// "vant": "^2.10.3"
|
|
83
|
+
// },
|
|
84
|
+
// devDependencies: {
|
|
85
|
+
|
|
86
|
+
// }
|
|
87
|
+
// }
|
|
88
|
+
// })
|
|
89
|
+
// }
|
|
90
|
+
// else if (options.template === 'bwa/bwa-template-news') {
|
|
91
|
+
// api.extendPackage(pkg => {
|
|
92
|
+
// return {
|
|
93
|
+
// dependencies: {
|
|
94
|
+
// "vue-i18n": "^8.18.2"
|
|
95
|
+
// },
|
|
96
|
+
// devDependencies: {
|
|
97
|
+
|
|
98
|
+
// }
|
|
99
|
+
// }
|
|
100
|
+
// })
|
|
101
|
+
// }
|
|
102
|
+
|
|
103
|
+
api.render(async function (files) {
|
|
104
|
+
Object.keys(files).forEach((name) => {
|
|
105
|
+
delete files[name];
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
let _template = options.repo || options.template;
|
|
109
|
+
let _base = '';
|
|
110
|
+
|
|
111
|
+
//获取公共模版内容
|
|
112
|
+
// await generate(path.resolve(__dirname, './template/common'), files);
|
|
113
|
+
|
|
114
|
+
let _spinner = ora('模板下载中...');
|
|
115
|
+
|
|
116
|
+
//下载到根目录
|
|
117
|
+
let _tmpDir = path.join(
|
|
118
|
+
home,
|
|
119
|
+
'.bwa-app/templates',
|
|
120
|
+
_template.replace(/[/:]/g, '-')
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
_spinner.start();
|
|
124
|
+
|
|
125
|
+
await new Promise((resolve, reject) => {
|
|
126
|
+
//http://gitlab.baswebapp.cn/bwa/bwa-template-h5.git
|
|
127
|
+
//开始下载 /bwa/bwa-template-h5/repository/archive.zip?ref=master
|
|
128
|
+
download(_template, _tmpDir, (err) => {
|
|
129
|
+
//下载完成
|
|
130
|
+
_spinner.stop();
|
|
131
|
+
|
|
132
|
+
if (err) {
|
|
133
|
+
return reject(err);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
resolve();
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
await generate(_tmpDir, files, _base);
|
|
141
|
+
});
|
|
142
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ctzy-web-client/create-web-client",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "创建 web client 项目模板工具",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"bin/",
|
|
8
|
+
"lib/",
|
|
9
|
+
"index.js",
|
|
10
|
+
"templates/"
|
|
11
|
+
],
|
|
12
|
+
"bin": {
|
|
13
|
+
"create-web-client": "index.js"
|
|
14
|
+
},
|
|
15
|
+
"author": "tommy",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"cross-spawn": "^7.0.3",
|
|
19
|
+
"download": "^8.0.0",
|
|
20
|
+
"git-clone": "^0.2.0",
|
|
21
|
+
"glob": "^8.0.3",
|
|
22
|
+
"isbinaryfile": "^5.0.0",
|
|
23
|
+
"kolorist": "^1.6.0",
|
|
24
|
+
"minimist": "^1.2.7",
|
|
25
|
+
"ora": "^6.1.2",
|
|
26
|
+
"prompts": "^2.4.2",
|
|
27
|
+
"rimraf": "^3.0.2",
|
|
28
|
+
"user-home": "^3.0.0"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
+
<title>test</title>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<script>
|
|
11
|
+
window.__DELAY_REGISTER_SERVICES__ = [];
|
|
12
|
+
</script>
|
|
13
|
+
<div id="app"></div>
|
|
14
|
+
<script type="module" src="./src/main.js"></script>
|
|
15
|
+
</body>
|
|
16
|
+
</html>
|