utils.js
2.09 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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.streamToString = streamToString;
exports.isTSFile = exports.isDynamicRoute = exports.getGlobalFile = void 0;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _fs() {
const data = require("fs");
_fs = function _fs() {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function _path() {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* get global file like (global.js, global.css)
* @param absSrcPath
* @param files default load global files
*/
const getGlobalFile = ({
absSrcPath,
files
}) => {
return files.map(file => (0, _path().join)(absSrcPath || '', file)).filter(file => (0, _fs().existsSync)(file)).slice(0, 1);
};
exports.getGlobalFile = getGlobalFile;
const isDynamicRoute = path => {
var _path$split, _path$split$some;
return !!(path === null || path === void 0 ? void 0 : (_path$split = path.split('/')) === null || _path$split === void 0 ? void 0 : (_path$split$some = _path$split.some) === null || _path$split$some === void 0 ? void 0 : _path$split$some.call(_path$split, snippet => snippet.startsWith(':')));
};
/**
* judge whether ts or tsx file exclude d.ts
* @param path
*/
exports.isDynamicRoute = isDynamicRoute;
const isTSFile = path => {
return typeof path === 'string' && !/\.d\.ts$/.test(path) && /\.(ts|tsx)$/.test(path);
};
/**
* stream convert string
* refs: https://stackoverflow.com/questions/10623798/how-do-i-read-the-contents-of-a-node-js-stream-into-a-string-variable
*
* @param stream
* @returns
*/
exports.isTSFile = isTSFile;
function streamToString(stream) {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on('data', chunk => chunks.push(Buffer.from(chunk)));
stream.on('error', err => reject(err));
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
});
}