@es-pkg/publish 1.0.6 → 1.0.7
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 +1 -1
- package/cjs/index.d.ts +3 -0
- package/cjs/index.js +242 -0
- package/esm/index.d.ts +3 -0
- package/{src/index.ts → esm/index.js} +238 -242
- package/esm/type.d.ts +46 -0
- package/package.json +6 -4
- /package/{src → cjs}/type.d.ts +0 -0
package/README.md
CHANGED
|
@@ -29,4 +29,4 @@
|
|
|
29
29
|
|
|
30
30
|
#### default
|
|
31
31
|
|
|
32
|
-
* default(done:`Undertaker.TaskCallback`): `void` | `EventEmitter`<`DefaultEventMap`\> | `internal.Stream` | `"child_process".ChildProcess` | `asyncDone.Observable`<`any`\>
|
|
32
|
+
* default(done:`Undertaker.TaskCallback`): `void` | `EventEmitter`<`DefaultEventMap`\> | `internal.Stream` | `PromiseLike`<`any`\> | `"child_process".ChildProcess` | `asyncDone.Observable`<`any`\>
|
package/cjs/index.d.ts
ADDED
package/cjs/index.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var gulp = require('@es-pkg/gulp');
|
|
6
|
+
var chalk = require('chalk');
|
|
7
|
+
var logger = require('@es-pkg/gulp-logger');
|
|
8
|
+
var fs = require('fs');
|
|
9
|
+
var path = require('path');
|
|
10
|
+
var utils = require('@es-pkg/utils');
|
|
11
|
+
var config = require('@es-pkg/config');
|
|
12
|
+
var prompts = require('prompts');
|
|
13
|
+
|
|
14
|
+
const scoped = /^@[a-zA-Z0-9-]+\/.+$/;
|
|
15
|
+
const REGISTRY = config.config.publishRegistry;
|
|
16
|
+
const json = config.pkg;
|
|
17
|
+
const publishDir = path.join(config.config.publishDir, './__npm__');
|
|
18
|
+
gulp.task('clean', async () => {
|
|
19
|
+
utils.log(`清除${publishDir}开始`);
|
|
20
|
+
try {
|
|
21
|
+
await utils.remove(publishDir);
|
|
22
|
+
utils.log(`清除${publishDir}完成`);
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
utils.log(`清除${publishDir}失败:`, e);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
gulp.task('del-cjs-iife-es', async () => {
|
|
29
|
+
if (config.config.iife) {
|
|
30
|
+
utils.log(`删除 ${path.join(`${publishDir}`, config.config.iife)} 开始`);
|
|
31
|
+
await utils.remove(`${path.join(`${publishDir}`, config.config.iife)}`);
|
|
32
|
+
utils.log(`删除 ${path.join(`${publishDir}`, config.config.iife)} 结束`);
|
|
33
|
+
}
|
|
34
|
+
utils.log(`删除 ${path.join(`${publishDir}`, config.config.cjs)} 开始`);
|
|
35
|
+
await utils.remove(`${path.join(`${publishDir}`, config.config.cjs)}`);
|
|
36
|
+
utils.log(`删除 ${path.join(`${publishDir}`, config.config.cjs)} 结束`);
|
|
37
|
+
utils.log(`删除 ${path.join(`${publishDir}`, config.config.es)} 开始`);
|
|
38
|
+
await utils.remove(`${path.join(`${publishDir}`, config.config.es)}`);
|
|
39
|
+
utils.log(`删除 ${path.join(`${publishDir}`, config.config.es)} 结束`);
|
|
40
|
+
});
|
|
41
|
+
gulp.task('copy-info', gulp.series(async () => {
|
|
42
|
+
utils.log(`生成 package 开始`);
|
|
43
|
+
let errored = false;
|
|
44
|
+
let version = "";
|
|
45
|
+
try {
|
|
46
|
+
const { stdout } = await utils.run(`npm`, ['view', config.pkg.name, 'version'], {
|
|
47
|
+
stdio: undefined,
|
|
48
|
+
cwd: path.join(process.cwd(), config.config.publishDir),
|
|
49
|
+
});
|
|
50
|
+
if (stdout) {
|
|
51
|
+
version = stdout;
|
|
52
|
+
}
|
|
53
|
+
if (!version) {
|
|
54
|
+
const controller = new AbortController();
|
|
55
|
+
const timer = setTimeout(() => {
|
|
56
|
+
controller.abort();
|
|
57
|
+
}, 3000);
|
|
58
|
+
const start = Date.now();
|
|
59
|
+
const url = `https://img.shields.io/npm/v/${config.pkg.name}`;
|
|
60
|
+
const response = await utils.fetch(url, {
|
|
61
|
+
signal: controller.signal
|
|
62
|
+
}).finally(async () => {
|
|
63
|
+
utils.log.warn(`远程获取版本花费时间:${Date.now() - start}`);
|
|
64
|
+
});
|
|
65
|
+
const htmlString = await response.text();
|
|
66
|
+
const regex = /<title>npm: v([\d.]+)<\/title>/;
|
|
67
|
+
version = htmlString.match(regex)?.[1];
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
}
|
|
70
|
+
utils.log(`远程获取版本信息 tag:`, version);
|
|
71
|
+
if (version) {
|
|
72
|
+
json.version = utils.compare(config.pkg.version, version) <= 0 ? utils.autoUpgrade(version) : config.pkg.version;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
errored = true;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
errored = true;
|
|
80
|
+
}
|
|
81
|
+
if (errored) {
|
|
82
|
+
const { version } = await prompts({
|
|
83
|
+
type: 'text',
|
|
84
|
+
name: 'version',
|
|
85
|
+
message: '远程获取版本失败!请输入版本号',
|
|
86
|
+
initial: config.pkg.version
|
|
87
|
+
});
|
|
88
|
+
json.version = version;
|
|
89
|
+
}
|
|
90
|
+
delete json.devDependencies;
|
|
91
|
+
delete json.scripts;
|
|
92
|
+
if (!json.publishConfig) {
|
|
93
|
+
json.publishConfig = {
|
|
94
|
+
access: "public",
|
|
95
|
+
registry: REGISTRY
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const es = path.basename(config.config.es);
|
|
99
|
+
const cjs = path.basename(config.config.cjs);
|
|
100
|
+
const iife = config.config.iife ? path.basename(config.config.iife) : "";
|
|
101
|
+
const CJSExists = fs.existsSync(path.join(`${config.config.publishDir}`, cjs));
|
|
102
|
+
const ESExists = fs.existsSync(path.join(`${config.config.publishDir}`, es));
|
|
103
|
+
const IIFEExists = iife ? fs.existsSync(path.join(`${config.config.publishDir}`, iife)) : false;
|
|
104
|
+
const mainExists = !!json.main && fs.existsSync(path.join(config.resolveApp(''), json.main));
|
|
105
|
+
const browserExists = !!json.browser && fs.existsSync(path.join(config.resolveApp(''), json.main));
|
|
106
|
+
const moduleExists = !!json.module && fs.existsSync(path.join(config.resolveApp(''), json.main));
|
|
107
|
+
const _es = config.getEntrypoint(config.config.es);
|
|
108
|
+
const _cjs = config.getEntrypoint(config.config.cjs);
|
|
109
|
+
if (!mainExists) {
|
|
110
|
+
if (ESExists) {
|
|
111
|
+
json.main = _es || _cjs;
|
|
112
|
+
}
|
|
113
|
+
if (CJSExists) {
|
|
114
|
+
json.main = _cjs || _es;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (!moduleExists && CJSExists) {
|
|
118
|
+
json.module = _es || _cjs;
|
|
119
|
+
}
|
|
120
|
+
if (!browserExists && IIFEExists) {
|
|
121
|
+
json.browser = config.getEntrypoint(config.config.iife);
|
|
122
|
+
}
|
|
123
|
+
if (!json.types) {
|
|
124
|
+
const type = config.getEntrypoint(config.config.es, config.config.typings) || _es || _cjs;
|
|
125
|
+
const { dir, name, ext } = path.parse(type);
|
|
126
|
+
json.types = ['.ts', '.tsx'].includes(ext) ? type : `${dir}/${name}.d.ts`;
|
|
127
|
+
}
|
|
128
|
+
json.files = Array.from(new Set([ESExists && es, CJSExists && cjs, IIFEExists && iife])).filter(Boolean);
|
|
129
|
+
json.dependencies = Object.fromEntries(Object.entries(json.dependencies).map(([key, value]) => {
|
|
130
|
+
if (value.startsWith('file://') || value.startsWith('workspace:')) {
|
|
131
|
+
return [key, 'latest'];
|
|
132
|
+
}
|
|
133
|
+
return [key, value];
|
|
134
|
+
}));
|
|
135
|
+
let jsonStr = JSON.stringify(json, null, "\t");
|
|
136
|
+
const ex = fs.existsSync(`${config.config.publishDir}/`);
|
|
137
|
+
if (!ex) {
|
|
138
|
+
fs.mkdirSync(`${config.config.publishDir}/`, { recursive: true });
|
|
139
|
+
}
|
|
140
|
+
fs.writeFileSync(`${config.config.publishDir}/package.json`, jsonStr);
|
|
141
|
+
utils.log(`生成 package完成`, chalk.green(json.version));
|
|
142
|
+
utils.log(`生成 .npmrc 开始`);
|
|
143
|
+
fs.writeFileSync(`${config.config.publishDir}/.npmrc`, `registry=${REGISTRY}`);
|
|
144
|
+
utils.log(`生成 .npmrc 完成`);
|
|
145
|
+
utils.log(`拷贝 README 开始`);
|
|
146
|
+
}, () => {
|
|
147
|
+
return gulp.src([`./README.md`])
|
|
148
|
+
.pipe(logger({
|
|
149
|
+
before: 'copy README...',
|
|
150
|
+
after: 'copy README complete!',
|
|
151
|
+
showChange: false
|
|
152
|
+
}))
|
|
153
|
+
.pipe(gulp.dest(`${config.config.publishDir}/`));
|
|
154
|
+
}));
|
|
155
|
+
gulp.task('copy-iife', (c) => {
|
|
156
|
+
if (!config.config.iife) {
|
|
157
|
+
return c();
|
|
158
|
+
}
|
|
159
|
+
utils.log(`拷贝 '/iife/**' 开始`);
|
|
160
|
+
return gulp.src([`${config.config.iife}/.**`, `${config.config.iife}/**`])
|
|
161
|
+
.pipe(logger({
|
|
162
|
+
before: 'copy iife...',
|
|
163
|
+
after: 'copy iife complete!',
|
|
164
|
+
showChange: false
|
|
165
|
+
}))
|
|
166
|
+
.pipe(gulp.dest(path.join(`${publishDir}`, path.basename(config.config.iife))));
|
|
167
|
+
});
|
|
168
|
+
gulp.task('copy-cjs', () => {
|
|
169
|
+
return gulp.src([`${config.config.cjs}/.**`, `${config.config.cjs}/**`])
|
|
170
|
+
.pipe(logger({
|
|
171
|
+
before: 'copy cjs...',
|
|
172
|
+
after: 'copy cjs complete!',
|
|
173
|
+
showChange: false
|
|
174
|
+
}))
|
|
175
|
+
.pipe(gulp.dest(path.join(`${publishDir}`, path.basename(config.config.cjs))));
|
|
176
|
+
});
|
|
177
|
+
gulp.task('copy-es', () => {
|
|
178
|
+
return gulp.src([`${config.config.es}/.**`, `${config.config.es}/**`])
|
|
179
|
+
.pipe(logger({
|
|
180
|
+
before: 'copy es...',
|
|
181
|
+
after: 'copy es complete!',
|
|
182
|
+
showChange: false
|
|
183
|
+
}))
|
|
184
|
+
.pipe(gulp.dest(path.join(`${publishDir}`, path.basename(config.config.es))));
|
|
185
|
+
});
|
|
186
|
+
gulp.task('remove-__npm__', gulp.series(() => {
|
|
187
|
+
let promises = [];
|
|
188
|
+
const includes = [config.config.es, config.config.cjs, config.config.iife].flatMap(val => {
|
|
189
|
+
if (!val) {
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
const some = config.getIncludeFiles().some(item => path.resolve(val).startsWith(path.resolve(item.path)));
|
|
193
|
+
return some ? [] : [val];
|
|
194
|
+
});
|
|
195
|
+
promises = includes.map(item => utils.remove(item));
|
|
196
|
+
return Promise.all(promises);
|
|
197
|
+
}, () => {
|
|
198
|
+
return gulp.src([`${publishDir}/**`, `${publishDir}/.**`])
|
|
199
|
+
.pipe(logger({
|
|
200
|
+
before: 'copy __npm__...',
|
|
201
|
+
after: 'copy __npm__ complete!',
|
|
202
|
+
showChange: false
|
|
203
|
+
}))
|
|
204
|
+
.pipe(gulp.dest(`${config.config.publishDir}/`));
|
|
205
|
+
}, async () => {
|
|
206
|
+
await utils.remove(publishDir);
|
|
207
|
+
}));
|
|
208
|
+
gulp.task('npm-publish', async function () {
|
|
209
|
+
let publishAccess = [];
|
|
210
|
+
//公共包
|
|
211
|
+
if (scoped.test(config.pkg.name)) {
|
|
212
|
+
publishAccess = ["--access", "public"];
|
|
213
|
+
}
|
|
214
|
+
if (config.config.publishAccess) {
|
|
215
|
+
publishAccess = config.config.publishAccess;
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
const { stdout } = await utils.run(`npm`, ["whoami", '--registry', REGISTRY], { stdio: undefined });
|
|
219
|
+
utils.log.warn(`===npm登录信息===>${stdout}`);
|
|
220
|
+
}
|
|
221
|
+
catch (e) {
|
|
222
|
+
utils.log.warn(`===npm${REGISTRY}未登录!!请登录`);
|
|
223
|
+
await utils.run(`npm`, ["login", '--registry', REGISTRY]);
|
|
224
|
+
}
|
|
225
|
+
const { confirm } = await prompts({
|
|
226
|
+
type: 'confirm',
|
|
227
|
+
name: 'confirm',
|
|
228
|
+
message: '是否发布?',
|
|
229
|
+
});
|
|
230
|
+
if (confirm) {
|
|
231
|
+
await utils.run(`npm`, ['publish', ...publishAccess, '--registry', REGISTRY], {
|
|
232
|
+
cwd: path.join(process.cwd(), config.config.publishDir),
|
|
233
|
+
});
|
|
234
|
+
utils.success(`${json.name}@${json.version}:发布成功!`);
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
utils.error(`${json.name}@${json.version}:取消成功!`);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
var index = gulp.series('clean', 'del-cjs-iife-es', 'copy-cjs', 'copy-es', 'copy-iife', 'remove-__npm__', 'copy-info', 'npm-publish');
|
|
241
|
+
|
|
242
|
+
exports.default = index;
|
package/esm/index.d.ts
ADDED
|
@@ -1,242 +1,238 @@
|
|
|
1
|
-
import gulp, {series} from '@es-pkg/gulp'
|
|
2
|
-
import chalk from 'chalk'
|
|
3
|
-
import logger from '@es-pkg/gulp-logger'
|
|
4
|
-
import fs from
|
|
5
|
-
import path from
|
|
6
|
-
import {
|
|
7
|
-
import {config,
|
|
8
|
-
import prompts from
|
|
9
|
-
|
|
10
|
-
const scoped = /^@[a-zA-Z0-9-]+\/.+$/;
|
|
11
|
-
const REGISTRY = config.publishRegistry
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
log(`清除${publishDir}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
log(`删除 ${path.join(`${publishDir}`, config.
|
|
34
|
-
|
|
35
|
-
log(`删除 ${path.join(`${publishDir}`, config.es)}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
const
|
|
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
|
-
const
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
.pipe(
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
export default gulp.series('clean', 'del-cjs-iife-es', 'copy-cjs', 'copy-es', 'copy-iife', 'remove-__npm__', 'copy-info', 'npm-publish')
|
|
1
|
+
import gulp, { series } from '@es-pkg/gulp';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import logger from '@es-pkg/gulp-logger';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { log, remove, run, fetch, autoUpgrade, compare, success, error } from '@es-pkg/utils';
|
|
7
|
+
import { config, pkg, resolveApp, getEntrypoint, getIncludeFiles } from '@es-pkg/config';
|
|
8
|
+
import prompts from 'prompts';
|
|
9
|
+
|
|
10
|
+
const scoped = /^@[a-zA-Z0-9-]+\/.+$/;
|
|
11
|
+
const REGISTRY = config.publishRegistry;
|
|
12
|
+
const json = pkg;
|
|
13
|
+
const publishDir = path.join(config.publishDir, './__npm__');
|
|
14
|
+
gulp.task('clean', async () => {
|
|
15
|
+
log(`清除${publishDir}开始`);
|
|
16
|
+
try {
|
|
17
|
+
await remove(publishDir);
|
|
18
|
+
log(`清除${publishDir}完成`);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
log(`清除${publishDir}失败:`, e);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
gulp.task('del-cjs-iife-es', async () => {
|
|
25
|
+
if (config.iife) {
|
|
26
|
+
log(`删除 ${path.join(`${publishDir}`, config.iife)} 开始`);
|
|
27
|
+
await remove(`${path.join(`${publishDir}`, config.iife)}`);
|
|
28
|
+
log(`删除 ${path.join(`${publishDir}`, config.iife)} 结束`);
|
|
29
|
+
}
|
|
30
|
+
log(`删除 ${path.join(`${publishDir}`, config.cjs)} 开始`);
|
|
31
|
+
await remove(`${path.join(`${publishDir}`, config.cjs)}`);
|
|
32
|
+
log(`删除 ${path.join(`${publishDir}`, config.cjs)} 结束`);
|
|
33
|
+
log(`删除 ${path.join(`${publishDir}`, config.es)} 开始`);
|
|
34
|
+
await remove(`${path.join(`${publishDir}`, config.es)}`);
|
|
35
|
+
log(`删除 ${path.join(`${publishDir}`, config.es)} 结束`);
|
|
36
|
+
});
|
|
37
|
+
gulp.task('copy-info', series(async () => {
|
|
38
|
+
log(`生成 package 开始`);
|
|
39
|
+
let errored = false;
|
|
40
|
+
let version = "";
|
|
41
|
+
try {
|
|
42
|
+
const { stdout } = await run(`npm`, ['view', pkg.name, 'version'], {
|
|
43
|
+
stdio: undefined,
|
|
44
|
+
cwd: path.join(process.cwd(), config.publishDir),
|
|
45
|
+
});
|
|
46
|
+
if (stdout) {
|
|
47
|
+
version = stdout;
|
|
48
|
+
}
|
|
49
|
+
if (!version) {
|
|
50
|
+
const controller = new AbortController();
|
|
51
|
+
const timer = setTimeout(() => {
|
|
52
|
+
controller.abort();
|
|
53
|
+
}, 3000);
|
|
54
|
+
const start = Date.now();
|
|
55
|
+
const url = `https://img.shields.io/npm/v/${pkg.name}`;
|
|
56
|
+
const response = await fetch(url, {
|
|
57
|
+
signal: controller.signal
|
|
58
|
+
}).finally(async () => {
|
|
59
|
+
log.warn(`远程获取版本花费时间:${Date.now() - start}`);
|
|
60
|
+
});
|
|
61
|
+
const htmlString = await response.text();
|
|
62
|
+
const regex = /<title>npm: v([\d.]+)<\/title>/;
|
|
63
|
+
version = htmlString.match(regex)?.[1];
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
}
|
|
66
|
+
log(`远程获取版本信息 tag:`, version);
|
|
67
|
+
if (version) {
|
|
68
|
+
json.version = compare(pkg.version, version) <= 0 ? autoUpgrade(version) : pkg.version;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
errored = true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
errored = true;
|
|
76
|
+
}
|
|
77
|
+
if (errored) {
|
|
78
|
+
const { version } = await prompts({
|
|
79
|
+
type: 'text',
|
|
80
|
+
name: 'version',
|
|
81
|
+
message: '远程获取版本失败!请输入版本号',
|
|
82
|
+
initial: pkg.version
|
|
83
|
+
});
|
|
84
|
+
json.version = version;
|
|
85
|
+
}
|
|
86
|
+
delete json.devDependencies;
|
|
87
|
+
delete json.scripts;
|
|
88
|
+
if (!json.publishConfig) {
|
|
89
|
+
json.publishConfig = {
|
|
90
|
+
access: "public",
|
|
91
|
+
registry: REGISTRY
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const es = path.basename(config.es);
|
|
95
|
+
const cjs = path.basename(config.cjs);
|
|
96
|
+
const iife = config.iife ? path.basename(config.iife) : "";
|
|
97
|
+
const CJSExists = fs.existsSync(path.join(`${config.publishDir}`, cjs));
|
|
98
|
+
const ESExists = fs.existsSync(path.join(`${config.publishDir}`, es));
|
|
99
|
+
const IIFEExists = iife ? fs.existsSync(path.join(`${config.publishDir}`, iife)) : false;
|
|
100
|
+
const mainExists = !!json.main && fs.existsSync(path.join(resolveApp(''), json.main));
|
|
101
|
+
const browserExists = !!json.browser && fs.existsSync(path.join(resolveApp(''), json.main));
|
|
102
|
+
const moduleExists = !!json.module && fs.existsSync(path.join(resolveApp(''), json.main));
|
|
103
|
+
const _es = getEntrypoint(config.es);
|
|
104
|
+
const _cjs = getEntrypoint(config.cjs);
|
|
105
|
+
if (!mainExists) {
|
|
106
|
+
if (ESExists) {
|
|
107
|
+
json.main = _es || _cjs;
|
|
108
|
+
}
|
|
109
|
+
if (CJSExists) {
|
|
110
|
+
json.main = _cjs || _es;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (!moduleExists && CJSExists) {
|
|
114
|
+
json.module = _es || _cjs;
|
|
115
|
+
}
|
|
116
|
+
if (!browserExists && IIFEExists) {
|
|
117
|
+
json.browser = getEntrypoint(config.iife);
|
|
118
|
+
}
|
|
119
|
+
if (!json.types) {
|
|
120
|
+
const type = getEntrypoint(config.es, config.typings) || _es || _cjs;
|
|
121
|
+
const { dir, name, ext } = path.parse(type);
|
|
122
|
+
json.types = ['.ts', '.tsx'].includes(ext) ? type : `${dir}/${name}.d.ts`;
|
|
123
|
+
}
|
|
124
|
+
json.files = Array.from(new Set([ESExists && es, CJSExists && cjs, IIFEExists && iife])).filter(Boolean);
|
|
125
|
+
json.dependencies = Object.fromEntries(Object.entries(json.dependencies).map(([key, value]) => {
|
|
126
|
+
if (value.startsWith('file://') || value.startsWith('workspace:')) {
|
|
127
|
+
return [key, 'latest'];
|
|
128
|
+
}
|
|
129
|
+
return [key, value];
|
|
130
|
+
}));
|
|
131
|
+
let jsonStr = JSON.stringify(json, null, "\t");
|
|
132
|
+
const ex = fs.existsSync(`${config.publishDir}/`);
|
|
133
|
+
if (!ex) {
|
|
134
|
+
fs.mkdirSync(`${config.publishDir}/`, { recursive: true });
|
|
135
|
+
}
|
|
136
|
+
fs.writeFileSync(`${config.publishDir}/package.json`, jsonStr);
|
|
137
|
+
log(`生成 package完成`, chalk.green(json.version));
|
|
138
|
+
log(`生成 .npmrc 开始`);
|
|
139
|
+
fs.writeFileSync(`${config.publishDir}/.npmrc`, `registry=${REGISTRY}`);
|
|
140
|
+
log(`生成 .npmrc 完成`);
|
|
141
|
+
log(`拷贝 README 开始`);
|
|
142
|
+
}, () => {
|
|
143
|
+
return gulp.src([`./README.md`])
|
|
144
|
+
.pipe(logger({
|
|
145
|
+
before: 'copy README...',
|
|
146
|
+
after: 'copy README complete!',
|
|
147
|
+
showChange: false
|
|
148
|
+
}))
|
|
149
|
+
.pipe(gulp.dest(`${config.publishDir}/`));
|
|
150
|
+
}));
|
|
151
|
+
gulp.task('copy-iife', (c) => {
|
|
152
|
+
if (!config.iife) {
|
|
153
|
+
return c();
|
|
154
|
+
}
|
|
155
|
+
log(`拷贝 '/iife/**' 开始`);
|
|
156
|
+
return gulp.src([`${config.iife}/.**`, `${config.iife}/**`])
|
|
157
|
+
.pipe(logger({
|
|
158
|
+
before: 'copy iife...',
|
|
159
|
+
after: 'copy iife complete!',
|
|
160
|
+
showChange: false
|
|
161
|
+
}))
|
|
162
|
+
.pipe(gulp.dest(path.join(`${publishDir}`, path.basename(config.iife))));
|
|
163
|
+
});
|
|
164
|
+
gulp.task('copy-cjs', () => {
|
|
165
|
+
return gulp.src([`${config.cjs}/.**`, `${config.cjs}/**`])
|
|
166
|
+
.pipe(logger({
|
|
167
|
+
before: 'copy cjs...',
|
|
168
|
+
after: 'copy cjs complete!',
|
|
169
|
+
showChange: false
|
|
170
|
+
}))
|
|
171
|
+
.pipe(gulp.dest(path.join(`${publishDir}`, path.basename(config.cjs))));
|
|
172
|
+
});
|
|
173
|
+
gulp.task('copy-es', () => {
|
|
174
|
+
return gulp.src([`${config.es}/.**`, `${config.es}/**`])
|
|
175
|
+
.pipe(logger({
|
|
176
|
+
before: 'copy es...',
|
|
177
|
+
after: 'copy es complete!',
|
|
178
|
+
showChange: false
|
|
179
|
+
}))
|
|
180
|
+
.pipe(gulp.dest(path.join(`${publishDir}`, path.basename(config.es))));
|
|
181
|
+
});
|
|
182
|
+
gulp.task('remove-__npm__', series(() => {
|
|
183
|
+
let promises = [];
|
|
184
|
+
const includes = [config.es, config.cjs, config.iife].flatMap(val => {
|
|
185
|
+
if (!val) {
|
|
186
|
+
return [];
|
|
187
|
+
}
|
|
188
|
+
const some = getIncludeFiles().some(item => path.resolve(val).startsWith(path.resolve(item.path)));
|
|
189
|
+
return some ? [] : [val];
|
|
190
|
+
});
|
|
191
|
+
promises = includes.map(item => remove(item));
|
|
192
|
+
return Promise.all(promises);
|
|
193
|
+
}, () => {
|
|
194
|
+
return gulp.src([`${publishDir}/**`, `${publishDir}/.**`])
|
|
195
|
+
.pipe(logger({
|
|
196
|
+
before: 'copy __npm__...',
|
|
197
|
+
after: 'copy __npm__ complete!',
|
|
198
|
+
showChange: false
|
|
199
|
+
}))
|
|
200
|
+
.pipe(gulp.dest(`${config.publishDir}/`));
|
|
201
|
+
}, async () => {
|
|
202
|
+
await remove(publishDir);
|
|
203
|
+
}));
|
|
204
|
+
gulp.task('npm-publish', async function () {
|
|
205
|
+
let publishAccess = [];
|
|
206
|
+
//公共包
|
|
207
|
+
if (scoped.test(pkg.name)) {
|
|
208
|
+
publishAccess = ["--access", "public"];
|
|
209
|
+
}
|
|
210
|
+
if (config.publishAccess) {
|
|
211
|
+
publishAccess = config.publishAccess;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
const { stdout } = await run(`npm`, ["whoami", '--registry', REGISTRY], { stdio: undefined });
|
|
215
|
+
log.warn(`===npm登录信息===>${stdout}`);
|
|
216
|
+
}
|
|
217
|
+
catch (e) {
|
|
218
|
+
log.warn(`===npm${REGISTRY}未登录!!请登录`);
|
|
219
|
+
await run(`npm`, ["login", '--registry', REGISTRY]);
|
|
220
|
+
}
|
|
221
|
+
const { confirm } = await prompts({
|
|
222
|
+
type: 'confirm',
|
|
223
|
+
name: 'confirm',
|
|
224
|
+
message: '是否发布?',
|
|
225
|
+
});
|
|
226
|
+
if (confirm) {
|
|
227
|
+
await run(`npm`, ['publish', ...publishAccess, '--registry', REGISTRY], {
|
|
228
|
+
cwd: path.join(process.cwd(), config.publishDir),
|
|
229
|
+
});
|
|
230
|
+
success(`${json.name}@${json.version}:发布成功!`);
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
error(`${json.name}@${json.version}:取消成功!`);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
var index = gulp.series('clean', 'del-cjs-iife-es', 'copy-cjs', 'copy-es', 'copy-iife', 'remove-__npm__', 'copy-info', 'npm-publish');
|
|
237
|
+
|
|
238
|
+
export { index as default };
|
package/esm/type.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
declare module "rename" {
|
|
4
|
+
export = rename;
|
|
5
|
+
|
|
6
|
+
function rename(filepath: string | rename.FileObject, transformer: rename.Transformer): rename.FilePath;
|
|
7
|
+
|
|
8
|
+
namespace rename {
|
|
9
|
+
interface FileObject {
|
|
10
|
+
// using package's terminology
|
|
11
|
+
dirname?: string | undefined;
|
|
12
|
+
basename?: string | undefined;
|
|
13
|
+
extname?: string | undefined;
|
|
14
|
+
path?: string | undefined;
|
|
15
|
+
hash?: string | undefined; // not populated by package
|
|
16
|
+
origin?: string | undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface Specification {
|
|
20
|
+
dirname?: string | undefined;
|
|
21
|
+
prefix?: string | undefined;
|
|
22
|
+
basename?: string | undefined;
|
|
23
|
+
suffix?: string | undefined;
|
|
24
|
+
extname?: string | undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type FilePath =
|
|
28
|
+
| string
|
|
29
|
+
| Specification;
|
|
30
|
+
|
|
31
|
+
type Transformer =
|
|
32
|
+
| ((spec: FileObject) => FilePath)
|
|
33
|
+
| FilePath;
|
|
34
|
+
|
|
35
|
+
interface ParsedFileObject {
|
|
36
|
+
dirname: string;
|
|
37
|
+
extname: string;
|
|
38
|
+
basename: string;
|
|
39
|
+
origin: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function parse(filename: string | Partial<ParsedFileObject>): ParsedFileObject;
|
|
43
|
+
|
|
44
|
+
function stringify(obj: FileObject): string;
|
|
45
|
+
}
|
|
46
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@es-pkg/publish",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "组件打包工具",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "cjs/index.js",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=14.6.0"
|
|
8
8
|
},
|
|
@@ -28,8 +28,10 @@
|
|
|
28
28
|
"access": "public",
|
|
29
29
|
"registry": "https://registry.npmjs.org"
|
|
30
30
|
},
|
|
31
|
-
"
|
|
31
|
+
"module": "esm/index.js",
|
|
32
|
+
"types": "esm/index.d.ts",
|
|
32
33
|
"files": [
|
|
33
|
-
"
|
|
34
|
+
"esm",
|
|
35
|
+
"cjs"
|
|
34
36
|
]
|
|
35
37
|
}
|
/package/{src → cjs}/type.d.ts
RENAMED
|
File without changes
|