ko 5.2.1 → 5.2.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/README.md +2 -128
- package/lib/actions/build.js +74 -0
- package/lib/actions/creator.js +42 -0
- package/lib/actions/dev.js +110 -0
- package/lib/cli.js +41 -0
- package/lib/interfaces.js +2 -0
- package/lib/utils/config.js +42 -0
- package/lib/utils/config.test.js +30 -0
- package/lib/webpack/index.js +55 -0
- package/lib/webpack/loaders/asset.js +19 -0
- package/lib/webpack/loaders/index.js +12 -0
- package/lib/webpack/loaders/script.js +59 -0
- package/lib/webpack/loaders/style.js +72 -0
- package/lib/webpack/plugins.js +61 -0
- package/package.json +43 -85
- package/{preinstall.js → scripts/preinstall.js} +6 -5
- package/.eslintignore +0 -3
- package/.eslintrc.js +0 -96
- package/.github/workflows/auto-publish.yml +0 -32
- package/.github/workflows/ci.yml +0 -67
- package/.husky/pre-commit +0 -4
- package/.prettierignore +0 -5
- package/CHANGELOG.md +0 -115
- package/bin/ko-build.js +0 -30
- package/bin/ko-create.js +0 -14
- package/bin/ko-dev.js +0 -31
- package/bin/ko-init.js +0 -14
- package/bin/ko-install.js +0 -27
- package/bin/ko-lint.js +0 -18
- package/bin/ko-move.js +0 -14
- package/bin/ko-preview.js +0 -27
- package/bin/ko-swagger.js +0 -41
- package/bin/ko.js +0 -29
- package/commitlint.config.js +0 -7
- package/config/defaultPaths.js +0 -26
- package/config/lint/getEslintConf.js +0 -23
- package/config/lint/getPrettierConf.js +0 -14
- package/config/lint/index.js +0 -14
- package/config/webpack/base.js +0 -77
- package/config/webpack/devServer.js +0 -39
- package/config/webpack/index.js +0 -63
- package/config/webpack/loaders.js +0 -142
- package/config/webpack/plugins.js +0 -70
- package/constants/default.js +0 -7
- package/constants/env.js +0 -7
- package/jest.config.js +0 -44
- package/prettier.config.js +0 -8
- package/script/build.js +0 -23
- package/script/create.js +0 -31
- package/script/dev.js +0 -96
- package/script/init.js +0 -129
- package/script/install.js +0 -91
- package/script/lint.js +0 -24
- package/script/move.js +0 -38
- package/script/preview.js +0 -22
- package/script/swagger.js +0 -239
- package/template/index.html +0 -0
- package/template/pageTemplate.mustache +0 -21
- package/template/pageTemplateTS.mustache +0 -29
- package/template/restful.hbs +0 -14
- package/template/restfulTemplate.mustache +0 -12
- package/template/routerConfTemplate.mustache +0 -15
- package/template/styleTemplate.mustache +0 -3
- package/template/tsconfig.json +0 -22
- package/util/__tests__/file.test.js +0 -16
- package/util/__tests__/fileService.test.js +0 -101
- package/util/__tests__/index.test.js +0 -51
- package/util/__tests__/userConfig.test.js +0 -83
- package/util/__tests__/verifyHtml.test.js +0 -81
- package/util/createComp.js +0 -71
- package/util/createPage.js +0 -141
- package/util/defaultData.js +0 -64
- package/util/file.js +0 -9
- package/util/fileService.js +0 -55
- package/util/index.js +0 -25
- package/util/prepareUrl.js +0 -69
- package/util/program.js +0 -20
- package/util/request.js +0 -18
- package/util/stdout.js +0 -36
- package/util/userConfig.js +0 -40
- package/util/verifyHtml.js +0 -63
package/script/swagger.js
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Description: 文件
|
|
3
|
-
* @version: 1.0.0
|
|
4
|
-
* @Company: 袋鼠云
|
|
5
|
-
* @Author: xiuneng
|
|
6
|
-
* @Date: 2019-01-07 17:06:53
|
|
7
|
-
* @LastEditors: Charles
|
|
8
|
-
* @LastEditTime: 2019-02-20 16:36:10
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const Colors = require('colors');
|
|
12
|
-
const Log = console.log;
|
|
13
|
-
const fs = require('fs');
|
|
14
|
-
const { resolveApp, parsePath } = require('../config/defaultPaths');
|
|
15
|
-
const { get } = require('../util/request');
|
|
16
|
-
const { existsSync, mkdir } = require('../util/fileService');
|
|
17
|
-
const Mustache = require('mustache');
|
|
18
|
-
const Path = require('path');
|
|
19
|
-
const cloneDeep = require('lodash').cloneDeep;
|
|
20
|
-
const DEFAULT_FILE_NAME = 'swagger.js';
|
|
21
|
-
const DEFAULT_TS_FILE_NAME = 'swagger.ts';
|
|
22
|
-
const DEFAULT_TEMPLATE_PATH = '../template/restfulTemplate.mustache';
|
|
23
|
-
/**
|
|
24
|
-
* @description 对象数组去重
|
|
25
|
-
* @param {Array} arr
|
|
26
|
-
*/
|
|
27
|
-
const unique = (arr = []) => {
|
|
28
|
-
const obj = {};
|
|
29
|
-
const result = arr.reduce((item, next) => {
|
|
30
|
-
obj[next.operationId]
|
|
31
|
-
? ''
|
|
32
|
-
: (obj[next.operationId] = true && item.push(next));
|
|
33
|
-
return item;
|
|
34
|
-
}, []);
|
|
35
|
-
return result;
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* @description 字符串专成驼峰命名
|
|
39
|
-
* @param {string} str
|
|
40
|
-
*/
|
|
41
|
-
const parseCamelCase = (str = '') => {
|
|
42
|
-
const reg = /(?<=Using)[^,]*/;
|
|
43
|
-
if (reg.test(str)) {
|
|
44
|
-
let substr = str.match(reg)[0].toLowerCase();
|
|
45
|
-
substr = substr.charAt(0).toUpperCase() + substr.substring(1);
|
|
46
|
-
return str.replace(reg, substr);
|
|
47
|
-
} else {
|
|
48
|
-
return str;
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
*
|
|
53
|
-
* @param {string} url -url地址
|
|
54
|
-
*/
|
|
55
|
-
const getSwaggerPath = (url = '', prefixPath) => {
|
|
56
|
-
return new Promise((resolve, reject) => {
|
|
57
|
-
get({}, url)
|
|
58
|
-
.then((res) => res[0].location)
|
|
59
|
-
.then((res) => {
|
|
60
|
-
prefixPath
|
|
61
|
-
? resolve(`${prefixPath.protocol}//${prefixPath.host}${res}`)
|
|
62
|
-
: resolve(res);
|
|
63
|
-
})
|
|
64
|
-
.catch((err) => {
|
|
65
|
-
reject(err);
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* @description 用于合并两个数组对象
|
|
71
|
-
* @param {Array} arr1
|
|
72
|
-
* @param {Array} arr2
|
|
73
|
-
*/
|
|
74
|
-
const handleResolveArray = (arr1, arr2) => {
|
|
75
|
-
arr2 = arr2.map((item) => {
|
|
76
|
-
// 合并data数据
|
|
77
|
-
const index = item.name + ' ' + item.description;
|
|
78
|
-
const object = arr1.find((o) => o.name == index) || {};
|
|
79
|
-
item.data = item.data.concat(object.data || []);
|
|
80
|
-
return item;
|
|
81
|
-
});
|
|
82
|
-
// 去重
|
|
83
|
-
arr2 = arr2.map((item) => {
|
|
84
|
-
item.data = unique(item.data);
|
|
85
|
-
return item;
|
|
86
|
-
});
|
|
87
|
-
return arr2;
|
|
88
|
-
};
|
|
89
|
-
/**
|
|
90
|
-
* @description 反向解析字符串
|
|
91
|
-
* @param {string} str
|
|
92
|
-
*/
|
|
93
|
-
const parseRenderString = (str) => {
|
|
94
|
-
const parseArr = str
|
|
95
|
-
.trim()
|
|
96
|
-
.split('},')
|
|
97
|
-
.filter((o) => o.trim() != '{' && o.trim() != '}' && o.trim());
|
|
98
|
-
const result = [];
|
|
99
|
-
let obj = {};
|
|
100
|
-
const tagRegex = /(?<=(-------- )).*?(?=( --------)) /;
|
|
101
|
-
const methodRegex = /(?<=method:(\s*)\')[^\',]*/;
|
|
102
|
-
const urlRegex = /(?<=url:(\s*)(\`|\'|\"))[^(\`|\'|\"),]*/;
|
|
103
|
-
const keyRegex = /[^:\`,]*/;
|
|
104
|
-
const commentRegex = /(?<=\/\/)[^(\n)]*/;
|
|
105
|
-
parseArr.forEach((item) => {
|
|
106
|
-
if (tagRegex.test(item)) {
|
|
107
|
-
result.push(obj);
|
|
108
|
-
obj = {};
|
|
109
|
-
obj.name = item.match(tagRegex)[0].trim();
|
|
110
|
-
obj.description = '';
|
|
111
|
-
obj.data = [];
|
|
112
|
-
} else {
|
|
113
|
-
try {
|
|
114
|
-
const method = methodRegex.test(item)
|
|
115
|
-
? item.match(methodRegex)[0].trim()
|
|
116
|
-
: '';
|
|
117
|
-
const url = urlRegex.test(item) ? item.match(urlRegex)[0].trim() : '';
|
|
118
|
-
const key = keyRegex.test(item) ? item.match(keyRegex)[0].trim() : '';
|
|
119
|
-
const comment = commentRegex.test(item)
|
|
120
|
-
? item.match(commentRegex)[0].trim()
|
|
121
|
-
: '';
|
|
122
|
-
Array.isArray(obj.data) &&
|
|
123
|
-
obj.data.push({
|
|
124
|
-
path: url,
|
|
125
|
-
operationId: key,
|
|
126
|
-
summary: comment,
|
|
127
|
-
method,
|
|
128
|
-
});
|
|
129
|
-
} catch (err) {
|
|
130
|
-
Log(`${item}失败:`, err);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
return result.filter((o) => JSON.stringify(o) != '{}');
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* 处理并合并已存在的文件的内容
|
|
138
|
-
* @param {string}path-文件地址
|
|
139
|
-
* @param {Array<object>}data-需要合并的旧数据
|
|
140
|
-
* */
|
|
141
|
-
function handleFileData(path, data) {
|
|
142
|
-
const fileContent = fs.readFileSync(path).toString();
|
|
143
|
-
const contentRegex = /\{((?:[^{}]*\{[^{}]*\})*[^{}]*?)\}/;
|
|
144
|
-
let fileResult = parseRenderString(fileContent.match(contentRegex)[0]);
|
|
145
|
-
const interfaceData = handleResolveArray(fileResult, data); // 将已存在的文件数据内容和需要新添加的数据内容合并
|
|
146
|
-
const renderString = renderMustache(interfaceData);
|
|
147
|
-
fileResult = fileContent.replace(
|
|
148
|
-
contentRegex,
|
|
149
|
-
renderString.match(contentRegex)[0]
|
|
150
|
-
); // 将正文部分替换成合并的数据部分,非正文部分不变
|
|
151
|
-
return fileResult;
|
|
152
|
-
}
|
|
153
|
-
/* 生成文件 */
|
|
154
|
-
function writerFile(filePath, renderString) {
|
|
155
|
-
fs.writeFile(filePath, renderString, function (err) {
|
|
156
|
-
if (err) Log(Colors.red('生成api文件操作失败'));
|
|
157
|
-
else Log(Colors.green(`生成api文件操作成功,生成目录: ${filePath} `));
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/* 渲染获取字符串 */
|
|
162
|
-
function renderMustache(interfaceData) {
|
|
163
|
-
const temp = fs
|
|
164
|
-
.readFileSync(require.resolve(DEFAULT_TEMPLATE_PATH), 'utf-8')
|
|
165
|
-
.toString();
|
|
166
|
-
const renderString = Mustache.render(temp, { result: interfaceData });
|
|
167
|
-
return renderString;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/* 根据接口类型分类 */
|
|
171
|
-
function sortByTags(paths, result) {
|
|
172
|
-
const cloneResult = cloneDeep(result);
|
|
173
|
-
Object.keys(paths).map((reqApi) => {
|
|
174
|
-
const method = Object.keys(paths[reqApi])[0];
|
|
175
|
-
const info = paths[reqApi][method];
|
|
176
|
-
info.operationId = parseCamelCase(info.operationId);
|
|
177
|
-
const object = cloneResult.find((o) => o.name == info.tags[0]);
|
|
178
|
-
!/(?<=\{)[^\},]*/.test(reqApi) &&
|
|
179
|
-
object.data.push({ ...info, path: reqApi, method });
|
|
180
|
-
});
|
|
181
|
-
return cloneResult;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/* 获取接口类型 */
|
|
185
|
-
function getTags(tags) {
|
|
186
|
-
const result = [];
|
|
187
|
-
tags.map((item) => {
|
|
188
|
-
result.push({ name: item.name, description: item.description, data: [] });
|
|
189
|
-
});
|
|
190
|
-
return result;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* @param {string} path-swagger地址
|
|
194
|
-
* @param {string} filePath-生成的api接口文件地址
|
|
195
|
-
* @param {boolean} ts-是否生成ts文件
|
|
196
|
-
*/
|
|
197
|
-
module.exports = (path, filePath, ts = false) => {
|
|
198
|
-
const parseUrl = parsePath(path);
|
|
199
|
-
const requestSwaggerPath = `${parseUrl.protocol}//${parseUrl.host}/swagger-resources`;
|
|
200
|
-
getSwaggerPath(requestSwaggerPath, parseUrl)
|
|
201
|
-
.then((api) => {
|
|
202
|
-
get({}, encodeURI(api))
|
|
203
|
-
.then((res) => {
|
|
204
|
-
const FILE_NAME = ts ? DEFAULT_TS_FILE_NAME : DEFAULT_FILE_NAME;
|
|
205
|
-
const folderExist = existsSync(filePath); // 文件夹是否存在
|
|
206
|
-
let interfaceData = getTags(res.tags);
|
|
207
|
-
interfaceData = sortByTags(res.paths, interfaceData);
|
|
208
|
-
const renderString = renderMustache(interfaceData);
|
|
209
|
-
if (folderExist) {
|
|
210
|
-
const fileExist = existsSync(Path.join(filePath, FILE_NAME));
|
|
211
|
-
if (fileExist) {
|
|
212
|
-
// 若文件存在,则处理内容
|
|
213
|
-
const fileResult = handleFileData(
|
|
214
|
-
Path.join(filePath, FILE_NAME),
|
|
215
|
-
interfaceData
|
|
216
|
-
);
|
|
217
|
-
writerFile(Path.join(filePath, FILE_NAME), fileResult);
|
|
218
|
-
} else {
|
|
219
|
-
// 文件夹存在,但文件不存在
|
|
220
|
-
writerFile(Path.join(filePath, FILE_NAME), renderString);
|
|
221
|
-
}
|
|
222
|
-
} else {
|
|
223
|
-
// 文件夹不存在,则文件必不存在;
|
|
224
|
-
try {
|
|
225
|
-
mkdir(filePath);
|
|
226
|
-
writerFile(Path.join(filePath, FILE_NAME), renderString);
|
|
227
|
-
} catch (err) {
|
|
228
|
-
Log(Colors.red(`请检查文件路径 ${filePath} 是否合法`));
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
})
|
|
232
|
-
.catch((err) => {
|
|
233
|
-
Log(Colors.red('请检查Swagger地址是否包含特殊字符'));
|
|
234
|
-
});
|
|
235
|
-
})
|
|
236
|
-
.catch((err) => {
|
|
237
|
-
Log(Colors.red('请检查Swagger地址是否合法'));
|
|
238
|
-
});
|
|
239
|
-
};
|
package/template/index.html
DELETED
|
File without changes
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import './style.scss';
|
|
3
|
-
|
|
4
|
-
export default class {{name}} extends React.Component {
|
|
5
|
-
|
|
6
|
-
state = {
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
componentDidMount () {
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
render(){
|
|
15
|
-
return (
|
|
16
|
-
<div className="{{className}}">
|
|
17
|
-
|
|
18
|
-
</div>
|
|
19
|
-
)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import './style.scss';
|
|
3
|
-
|
|
4
|
-
interface IProps{
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
interface IState{
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default class {{name}} extends React.Component<IProps, IState> {
|
|
13
|
-
|
|
14
|
-
state: IState = {
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
componentDidMount () {
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
render(){
|
|
23
|
-
return (
|
|
24
|
-
<div className="{{className}}">
|
|
25
|
-
|
|
26
|
-
</div>
|
|
27
|
-
)
|
|
28
|
-
}
|
|
29
|
-
}
|
package/template/restful.hbs
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
var myData = {
|
|
2
|
-
students : [
|
|
3
|
-
{name : null, years : 20, sex: 'man'},
|
|
4
|
-
{name : 'jiahua', years : 21, sex: 'man'},
|
|
5
|
-
{name : 'qinjie', years : 20, sex: 'woman'}
|
|
6
|
-
]
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
var template = "<div><table><tr><td>name</td><td>years</td><td>sex</td></tr>
|
|
10
|
-
{{#students}}<tr><td>{{#name}}{{name}}</td><td>{{years}}</td><td>{{sex}}{{/name}}</td></tr>{{/students}}
|
|
11
|
-
</table></div>";
|
|
12
|
-
var viewTemplate = Mustache.render(template, myData);
|
|
13
|
-
container.innerHTML = viewTemplate;
|
|
14
|
-
---------------------
|
package/template/tsconfig.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"module": "commonjs",
|
|
4
|
-
"target": "es6",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"allowSyntheticDefaultImports": true,
|
|
7
|
-
"sourceMap": true,
|
|
8
|
-
"removeComments": false,
|
|
9
|
-
"rootDirs": ["./src"],
|
|
10
|
-
"baseUrl": "./src",
|
|
11
|
-
"paths": {},
|
|
12
|
-
"jsx": "react",
|
|
13
|
-
"alwaysStrict": true,
|
|
14
|
-
"noUnusedLocals": true,
|
|
15
|
-
"importHelpers": false,//tslib
|
|
16
|
-
"experimentalDecorators": true,
|
|
17
|
-
"lib": ["es7", "dom"],
|
|
18
|
-
"skipLibCheck": true,
|
|
19
|
-
"typeRoots": ["node", "node_modules/@types", "./typings"]
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
const { getFileRealPath } = require('../file');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
|
|
4
|
-
describe('file utils:', () => {
|
|
5
|
-
it('module import:', () => {
|
|
6
|
-
expect(getFileRealPath).toBeInstanceOf(Function);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it('getFileRealPath', () => {
|
|
10
|
-
const workspace = process.cwd();
|
|
11
|
-
const dirname = 'build';
|
|
12
|
-
|
|
13
|
-
const realPath = getFileRealPath(dirname);
|
|
14
|
-
expect(realPath).toBe(path.resolve(workspace, dirname));
|
|
15
|
-
});
|
|
16
|
-
});
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
const {
|
|
2
|
-
createFileSync,
|
|
3
|
-
createWriteStream,
|
|
4
|
-
readFileSync,
|
|
5
|
-
existsSync,
|
|
6
|
-
mkdir,
|
|
7
|
-
getCurFilePath,
|
|
8
|
-
isAbsolute,
|
|
9
|
-
} = require('../fileService');
|
|
10
|
-
|
|
11
|
-
const fs = require('fs');
|
|
12
|
-
const path = require('path');
|
|
13
|
-
|
|
14
|
-
// global data filed
|
|
15
|
-
const mockContent = 'this is string test string...';
|
|
16
|
-
const mockFilename = 'test.txt';
|
|
17
|
-
const completePath = path.resolve(__dirname, mockFilename);
|
|
18
|
-
const errorFileNotPrepared = new Error('testing file is not prepared...');
|
|
19
|
-
const mockDirname = 'mock';
|
|
20
|
-
const completeDirPath = path.resolve(__dirname, mockDirname);
|
|
21
|
-
|
|
22
|
-
describe('file service:', () => {
|
|
23
|
-
it('create file sync:', () => {
|
|
24
|
-
createFileSync(completePath, mockContent);
|
|
25
|
-
const content = fs.readFileSync(completePath).toString();
|
|
26
|
-
expect(content).toBe(mockContent);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('create write stream:', (done) => {
|
|
30
|
-
fs.writeFileSync(completePath, mockContent);
|
|
31
|
-
if (!fs.existsSync(completePath)) throw errorFileNotPrepared;
|
|
32
|
-
const writeStream = createWriteStream(completePath);
|
|
33
|
-
const newLabel = 'hello, this is new label test...';
|
|
34
|
-
writeStream.write(newLabel, 'utf8');
|
|
35
|
-
writeStream.end();
|
|
36
|
-
writeStream.on('finish', () => {
|
|
37
|
-
const content = fs.readFileSync(completePath).toString();
|
|
38
|
-
expect(content).toBe(newLabel);
|
|
39
|
-
done();
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('read file sync:', () => {
|
|
44
|
-
fs.writeFileSync(completePath, mockContent);
|
|
45
|
-
if (!fs.existsSync(completePath)) throw errorFileNotPrepared;
|
|
46
|
-
const result = readFileSync(completePath);
|
|
47
|
-
const content = fs.readFileSync(completePath).toString();
|
|
48
|
-
expect(result).toBe(content);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('exists sync:', () => {
|
|
52
|
-
const notExist = existsSync(completePath);
|
|
53
|
-
expect(notExist).toBe(false);
|
|
54
|
-
fs.writeFileSync(completePath, mockContent);
|
|
55
|
-
if (!fs.existsSync(completePath)) throw errorFileNotPrepared;
|
|
56
|
-
const exist = existsSync(completePath);
|
|
57
|
-
expect(exist).toBe(true);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('mkdir', () => {
|
|
61
|
-
mkdir(completeDirPath);
|
|
62
|
-
const result = fs.existsSync(completeDirPath);
|
|
63
|
-
expect(result).toBe(true);
|
|
64
|
-
try {
|
|
65
|
-
mkdir(completeDirPath);
|
|
66
|
-
} catch (error) {
|
|
67
|
-
expect(error.message).toMatch(/EEXIST: file already exists/);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('mkdir access control:', () => {
|
|
72
|
-
mkdir(completeDirPath);
|
|
73
|
-
fs.accessSync(completeDirPath, fs.constants.W_OK | fs.constants.R_OK);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('getCurFilePath', () => {
|
|
77
|
-
const dirname = 'mock';
|
|
78
|
-
const result = getCurFilePath(dirname);
|
|
79
|
-
const workspace = process.cwd();
|
|
80
|
-
expect(result).toBe(path.resolve(workspace, dirname));
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('get absolute path:', () => {
|
|
84
|
-
const relationPath = './build';
|
|
85
|
-
const absolutePath = '/Users';
|
|
86
|
-
const relationResult = isAbsolute(relationPath);
|
|
87
|
-
const absoluteResult = isAbsolute(absolutePath);
|
|
88
|
-
const workspace = process.cwd();
|
|
89
|
-
expect(relationResult).toBe(path.resolve(workspace, relationPath));
|
|
90
|
-
expect(absoluteResult).toBe(absoluteResult);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
afterEach(() => {
|
|
94
|
-
if (fs.existsSync(completePath)) {
|
|
95
|
-
fs.unlinkSync(completePath);
|
|
96
|
-
}
|
|
97
|
-
if (fs.existsSync(completeDirPath)) {
|
|
98
|
-
fs.rmdirSync(completeDirPath);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
});
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
const { formatBundle, getCurDirPath, getCurFilePath } = require('../');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
|
|
5
|
-
describe('index.js:', () => {
|
|
6
|
-
it('format bundle:', () => {
|
|
7
|
-
const mockData = [1, 2, 3, 4, 5, 6, 7, 8];
|
|
8
|
-
const result = formatBundle(mockData, 3);
|
|
9
|
-
expect(result).toEqual({
|
|
10
|
-
vendor_1: [1, 2, 3],
|
|
11
|
-
vendor_2: [4, 5, 6],
|
|
12
|
-
vendor_3: [7, 8],
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
const result2 = formatBundle(mockData, 1);
|
|
16
|
-
expect(result2).toEqual({
|
|
17
|
-
vendor_1: [1],
|
|
18
|
-
vendor_2: [2],
|
|
19
|
-
vendor_3: [3],
|
|
20
|
-
vendor_4: [4],
|
|
21
|
-
vendor_5: [5],
|
|
22
|
-
vendor_6: [6],
|
|
23
|
-
vendor_7: [7],
|
|
24
|
-
vendor_8: [8],
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('getCurDirPath:', () => {
|
|
29
|
-
const mockDirname = 'build';
|
|
30
|
-
const workspace = process.cwd();
|
|
31
|
-
const completeDirPath = path.resolve(workspace, mockDirname);
|
|
32
|
-
try {
|
|
33
|
-
fs.mkdirSync(completeDirPath);
|
|
34
|
-
if (!fs.existsSync(completeDirPath))
|
|
35
|
-
throw new Error('testing directory is not prepared...');
|
|
36
|
-
const result = getCurDirPath(mockDirname);
|
|
37
|
-
expect(result).toBe(completeDirPath);
|
|
38
|
-
} catch (err) {
|
|
39
|
-
throw err;
|
|
40
|
-
} finally {
|
|
41
|
-
if (fs.existsSync(completeDirPath)) fs.rmdirSync(completeDirPath);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('getCurFilePath', () => {
|
|
46
|
-
const filename = 'test.txt';
|
|
47
|
-
const result = getCurFilePath(filename);
|
|
48
|
-
const workspace = process.cwd();
|
|
49
|
-
expect(result).toBe(path.resolve(workspace, filename));
|
|
50
|
-
});
|
|
51
|
-
});
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
const workspace = process.cwd();
|
|
3
|
-
const filename = 'ko.config.js';
|
|
4
|
-
const complatePath = path.resolve(workspace, filename);
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
|
|
7
|
-
const mockComfigTemplate = `
|
|
8
|
-
const path = require('path');
|
|
9
|
-
|
|
10
|
-
const isDev = process.env.NODE_ENV === 'development';
|
|
11
|
-
|
|
12
|
-
const copyConfig = [
|
|
13
|
-
{ from: path.resolve(__dirname, 'public/config'), to: 'config' },
|
|
14
|
-
{ from: path.resolve(__dirname, 'public/assets'), to: 'assets' },
|
|
15
|
-
];
|
|
16
|
-
|
|
17
|
-
if (process.env.NODE !== 'production') {
|
|
18
|
-
copyConfig.push({ from: path.resolve(__dirname, 'public/mock'), to: 'mock' });
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = () => {
|
|
22
|
-
return {
|
|
23
|
-
server: {
|
|
24
|
-
host: '127.0.0.1',
|
|
25
|
-
port: 8082,
|
|
26
|
-
},
|
|
27
|
-
proxy: [],
|
|
28
|
-
dll: [
|
|
29
|
-
'classnames',
|
|
30
|
-
],
|
|
31
|
-
webpack: {
|
|
32
|
-
entry: ['./src/app.tsx'],
|
|
33
|
-
output: {
|
|
34
|
-
publicPath: isDev ? '/' : '/publicService/',
|
|
35
|
-
},
|
|
36
|
-
plugins: [],
|
|
37
|
-
externals: {
|
|
38
|
-
APP_CONF: 'APP_CONF',
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
`;
|
|
44
|
-
|
|
45
|
-
describe('get user config:', () => {
|
|
46
|
-
it('whithout ko.config.js:', () => {
|
|
47
|
-
const userConfig = require('../userConfig');
|
|
48
|
-
expect(userConfig).toEqual({
|
|
49
|
-
webpack: {},
|
|
50
|
-
babel: {},
|
|
51
|
-
prettier: '',
|
|
52
|
-
eslint: '',
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('with ko.config.js', () => {
|
|
57
|
-
fs.writeFileSync(complatePath, mockComfigTemplate);
|
|
58
|
-
const userConfig = require('../userConfig');
|
|
59
|
-
expect(userConfig).toEqual({
|
|
60
|
-
webpack: {
|
|
61
|
-
entry: ['./src/app.tsx'],
|
|
62
|
-
output: {
|
|
63
|
-
publicPath: '/publicService/',
|
|
64
|
-
},
|
|
65
|
-
plugins: [],
|
|
66
|
-
externals: {
|
|
67
|
-
APP_CONF: 'APP_CONF',
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
babel: {},
|
|
71
|
-
prettier: '',
|
|
72
|
-
eslint: '',
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
beforeEach(() => {
|
|
77
|
-
jest.resetModules();
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
afterEach(() => {
|
|
81
|
-
if (fs.existsSync(complatePath)) fs.unlinkSync(complatePath);
|
|
82
|
-
});
|
|
83
|
-
});
|