dsh-enterprise-market 0.1.0 → 0.3.0

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/lib/client.js CHANGED
@@ -24,6 +24,9 @@ window.__ModuleLoader__.load({
24
24
  install: '安装',
25
25
  installing: '安装中…',
26
26
  installed: '已安装',
27
+ update: '更新',
28
+ pendingApproval: '待 npm 批准',
29
+ updating: '更新中…',
27
30
  installedDisabled: '已安装 · 未启用',
28
31
  uploader: '上传人',
29
32
  unknownUploader: '未记录上传人',
@@ -31,8 +34,8 @@ window.__ModuleLoader__.load({
31
34
  updated: '更新',
32
35
  local: '本机发布',
33
36
  uploadHeading: '发布自建插件',
34
- uploadHint: '选择打包好的 .tgz,发布到企业 npm 仓库。上传人姓名取自当前企业身份并写入包元数据,同事在市场里就能看到。',
35
- chooseFile: '选择 .tgz 文件',
37
+ uploadHint: '选择 .zip 或 .tgz,发布到企业 npm 仓库。zip 由宿主自动解包打包,上传人姓名取自当前企业身份并写入包元数据,同事在市场里就能看到。',
38
+ chooseFile: '选择 .zip 或 .tgz 文件',
36
39
  noFile: '尚未选择文件',
37
40
  namePlaceholder: '包名(可留空,用包内 package.json)',
38
41
  descPlaceholder: '一句话用途(可留空)',
@@ -41,8 +44,8 @@ window.__ModuleLoader__.load({
41
44
  published: '已发布',
42
45
  publishFailed: '发布失败',
43
46
  needIdentity: '未认证企业身份,不能发布',
44
- needFile: '请先选择 .tgz 文件',
45
- packHint: '在插件目录执行 npm pack,再上传生成的 .tgz。',
47
+ needFile: '请先选择 .zip 或 .tgz 文件',
48
+ packHint: '直接上传插件目录压缩成的 .zip,或已打包的 .tgz;插件名必须以 traness- 开头。',
46
49
  npmSignedIn: 'npm 已登录',
47
50
  npmSignedOut: 'npm 未登录',
48
51
  npmLoginHint: '本机 npm 未登录,发布会被仓库拒绝。先在终端执行 npm login,再回来上传。',
@@ -56,7 +59,9 @@ window.__ModuleLoader__.load({
56
59
  'origin-refused': '请求来源不受信任,已拒绝。',
57
60
  'registry-unreachable': '企业仓库暂时不可达。',
58
61
  'publish-failed': 'npm 拒绝发布,请检查包名是否已被占用、是否已登录。',
59
- 'invalid-tarball': '这个 .tgz 读不出 package/package.json。',
62
+ 'invalid-tarball': '这个归档读不出 package.json,请确认压缩包里是插件目录本身。',
63
+ 'invalid-archive': '压缩包里找不到 package.json,请把插件目录(含 package.json)打成一个 zip。',
64
+ 'name-prefix-required': '插件名必须以 traness- 开头,请修改 package.json 的 name 后重新上传。',
60
65
  'invalid-manifest': '包内 package.json 缺少 name 或 version。',
61
66
  'identity-unverified': '未认证企业身份,不能发布。',
62
67
  'credential-missing': '没有可用的企业令牌。',
@@ -87,6 +92,9 @@ window.__ModuleLoader__.load({
87
92
  install: 'Install',
88
93
  installing: 'Installing…',
89
94
  installed: 'Installed',
95
+ update: 'Update',
96
+ pendingApproval: 'Pending npm approval',
97
+ updating: 'Updating…',
90
98
  installedDisabled: 'Installed · disabled',
91
99
  uploader: 'Uploaded by',
92
100
  unknownUploader: 'Uploader not recorded',
@@ -94,8 +102,8 @@ window.__ModuleLoader__.load({
94
102
  updated: 'Updated',
95
103
  local: 'Published locally',
96
104
  uploadHeading: 'Publish a self-built plugin',
97
- uploadHint: 'Pick the packed .tgz and publish it to the enterprise npm registry. Your verified name is written into the package metadata so colleagues see the uploader.',
98
- chooseFile: 'Choose a .tgz file',
105
+ uploadHint: 'Pick a .zip or .tgz and publish it to the enterprise npm registry. A zip is unpacked and packed by the host; your verified name is written into the package metadata so colleagues see the uploader.',
106
+ chooseFile: 'Choose a .zip or .tgz file',
99
107
  noFile: 'No file selected',
100
108
  namePlaceholder: 'Package name (optional; defaults to the package.json inside)',
101
109
  descPlaceholder: 'One-line purpose (optional)',
@@ -284,11 +292,24 @@ window.__ModuleLoader__.load({
284
292
  : h('span', null, t('unknownUploader')),
285
293
  item.publishedAt ? h('span', null, `${t('updated')} ${relativeTime(item.publishedAt, t)}`) : null,
286
294
  item.local ? h('span', { className: 'emk-tag' }, t('local')) : null,
295
+ item.pendingApproval ? h('span', { className: 'emk-tag' }, t('pendingApproval')) : null,
287
296
  ),
288
297
  h(
289
298
  'div',
290
299
  { className: 'emk-actions' },
291
- state !== null
300
+ state !== null && item.updateAvailable
301
+ ? h(
302
+ 'button',
303
+ {
304
+ className: 'emk-btn emk-btn-primary',
305
+ type: 'button',
306
+ 'data-update': item.name,
307
+ disabled: busy,
308
+ onClick: () => onInstall(item),
309
+ },
310
+ busy ? t('updating') : t('update'),
311
+ )
312
+ : state !== null
292
313
  ? h('button', { className: 'emk-btn', type: 'button', disabled: true }, state)
293
314
  : h(
294
315
  'button',
@@ -552,7 +573,7 @@ window.__ModuleLoader__.load({
552
573
  h('input', {
553
574
  ref: inputRef,
554
575
  type: 'file',
555
- accept: '.tgz,.tar.gz,application/gzip',
576
+ accept: '.zip,.tgz,.tar.gz,application/zip,application/gzip',
556
577
  className: 'emk-hidden',
557
578
  onChange: (event) => {
558
579
  setFile(event.target.files?.[0] ?? null)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-enterprise-market",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "TranESS 企业插件市场:员工把自建插件发布到企业 npm 仓库,市场按企业身份显示上传人姓名,同事可直接下载安装。",
@@ -133,6 +133,17 @@ export async function buildCatalog(input) {
133
133
  const doc = await readPackument({ registry: input.registry, name, fetchImpl: input.fetchImpl })
134
134
  if (doc === null) return
135
135
  item.version = doc.latest ?? item.version
136
+ // 本地回执有、registry 里没有的版本:多半还在暂存区等人工批准。
137
+ // 这种状态不能显示成“已发布”,也不能拿它当可更新目标。
138
+ if (
139
+ item.local === true &&
140
+ typeof item.localVersion === 'string' &&
141
+ item.localVersion !== '' &&
142
+ !(doc.versions && typeof doc.versions === 'object' && item.localVersion in doc.versions)
143
+ ) {
144
+ item.pendingApproval = true
145
+ item.updateAvailable = false
146
+ }
136
147
  item.description = item.description ?? doc.manifest?.description ?? null
137
148
  item.publishedAt = item.publishedAt ?? isoDay(doc.modified)
138
149
  item.uploader = uploaderOf(doc.manifest) ?? item.uploader
@@ -161,9 +172,14 @@ export async function buildCatalog(input) {
161
172
  const items = [...byName.values()]
162
173
  .map((item) => {
163
174
  const state = installed.get(item.name)
175
+ const installedVersion =
176
+ typeof state?.version === 'string' && state.version !== '' ? state.version : null
164
177
  return {
165
178
  ...item,
166
179
  installed: state?.installed === true,
180
+ installedVersion,
181
+ updateAvailable:
182
+ state?.installed === true && versionIsNewer(item.version, installedVersion),
167
183
  enabled: state?.enabled === true,
168
184
  installedVersion: state?.version ?? null,
169
185
  }
@@ -212,3 +228,26 @@ async function mapLimit(items, limit, worker) {
212
228
  })
213
229
  await Promise.all(runners)
214
230
  }
231
+
232
+
233
+ /**
234
+ * 判断仓库里的版本是否比本机已安装的新。只比较点分段数字,预发布后缀不参与;
235
+ * 任何无法解析的输入一律视为「不更新」,避免误报更新按钮。
236
+ *
237
+ * @param {string | null} candidate
238
+ * @param {string | null} current
239
+ */
240
+ export function versionIsNewer(candidate, current) {
241
+ if (typeof candidate !== 'string' || typeof current !== 'string') return false
242
+ const parse = (value) => value.split('-')[0].split('.').map((part) => Number.parseInt(part, 10))
243
+ const next = parse(candidate)
244
+ const have = parse(current)
245
+ if (next.some((n) => !Number.isFinite(n)) || have.some((n) => !Number.isFinite(n))) return false
246
+ const length = Math.max(next.length, have.length)
247
+ for (let index = 0; index < length; index += 1) {
248
+ const a = next[index] ?? 0
249
+ const b = have[index] ?? 0
250
+ if (a !== b) return a > b
251
+ }
252
+ return false
253
+ }
package/src/host/npm.js CHANGED
@@ -1,7 +1,16 @@
1
- import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
1
+ import { access, mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises'
2
2
  import { tmpdir } from 'node:os'
3
3
  import { join } from 'node:path'
4
4
  import { createHash } from 'node:crypto'
5
+
6
+ const exists = async (path) => {
7
+ try {
8
+ await access(path)
9
+ return true
10
+ } catch {
11
+ return false
12
+ }
13
+ }
5
14
  import { findCli, run } from './exec.js'
6
15
 
7
16
  /** The metadata field the market reads the uploader from. */
@@ -51,6 +60,18 @@ export function isTarballName(fileName) {
51
60
  return /\.(tgz|tar\.gz)$/i.test(fileName)
52
61
  }
53
62
 
63
+ /** Whether the spec names an archive this market can publish: tgz or zip. */
64
+ export function isArchiveName(fileName) {
65
+ return isTarballName(fileName) || /[.]zip$/i.test(fileName)
66
+ }
67
+
68
+ /** 企业插件命名硬约束:必须以 traness- 开头,不做自动改名。 */
69
+ export const PLUGIN_NAME_PREFIX = 'traness-'
70
+
71
+ /** @param {string} name */
72
+ export function nameMatchesPolicy(name) {
73
+ return typeof name === 'string' && name.startsWith(PLUGIN_NAME_PREFIX) && name.length > PLUGIN_NAME_PREFIX.length
74
+ }
54
75
  /**
55
76
  * Extract a tarball, inject the uploader metadata and the discovery keyword,
56
77
  * and publish the resulting directory with the user's npm. The directory is a
@@ -69,6 +90,25 @@ export function isTarballName(fileName) {
69
90
  * run?: typeof run,
70
91
  * }} input
71
92
  */
93
+
94
+ /**
95
+ * 找到插件根目录:npm tarball 固定在 'package/';zip 允许根目录直接是插件,
96
+ * 或只包一层文件夹(两种结构都支持)。
97
+ *
98
+ * @param {string} scratch
99
+ * @param {boolean} isZip
100
+ */
101
+ async function locatePluginRoot(scratch, isZip) {
102
+ const direct = join(scratch, isZip ? '' : 'package', 'package.json')
103
+ if (await exists(direct)) return join(scratch, isZip ? '' : 'package')
104
+ if (!isZip) return null
105
+ const entries = await readdir(scratch, { withFileTypes: true })
106
+ const dirs = entries.filter((entry) => entry.isDirectory())
107
+ if (dirs.length !== 1) return null
108
+ const nested = join(scratch, dirs[0].name)
109
+ if (!(await exists(join(nested, 'package.json')))) return null
110
+ return nested
111
+ }
72
112
  export async function publishTarball(input) {
73
113
  const registry = registryBase(input.registry)
74
114
  const npm = findCli(input.npmBin ?? 'npm')
@@ -79,14 +119,19 @@ export async function publishTarball(input) {
79
119
  let authFile
80
120
  try {
81
121
  await writeFile(tarballPath, input.tarball, { mode: 0o600 })
82
- const extracted = await (input.run ?? run)('tar', ['-xzf', tarballPath, '-C', scratch], {
122
+ const isZip = /[.]zip$/i.test(input.fileName)
123
+ const extracted = await (input.run ?? run)('tar', [isZip ? '-xf' : '-xzf', tarballPath, '-C', scratch], {
83
124
  timeoutMs: 30_000,
84
125
  })
85
126
  if (extracted.code !== 0) {
86
127
  return { ok: false, reason: 'invalid-tarball', detail: 'tar could not read the uploaded file' }
87
128
  }
88
- // A packed npm tarball puts everything under one `package/` directory.
89
- const pkgDir = join(scratch, 'package')
129
+ // A packed npm tarball puts everything under one 'package/' directory; a
130
+ // user zip may put package.json at the root or inside a single top folder.
131
+ const pkgDir = await locatePluginRoot(scratch, isZip)
132
+ if (pkgDir === null) {
133
+ return { ok: false, reason: 'invalid-archive', detail: 'the archive has no package.json at its root' }
134
+ }
90
135
  let manifest
91
136
  try {
92
137
  manifest = JSON.parse(await readFile(join(pkgDir, 'package.json'), 'utf8'))
@@ -99,6 +144,15 @@ export async function publishTarball(input) {
99
144
  if (typeof manifest.version !== 'string' || manifest.version.trim() === '') {
100
145
  return { ok: false, reason: 'invalid-manifest', detail: 'package.json has no version' }
101
146
  }
147
+ const requestedName = typeof input.name === 'string' ? input.name.trim() : ''
148
+ const effectiveName = requestedName !== '' ? requestedName : manifest.name
149
+ if (!nameMatchesPolicy(effectiveName)) {
150
+ return {
151
+ ok: false,
152
+ reason: 'name-prefix-required',
153
+ detail: '插件名必须以 ' + PLUGIN_NAME_PREFIX + ' 开头,当前为 ' + effectiveName,
154
+ }
155
+ }
102
156
  const name = typeof input.name === 'string' && input.name.trim() !== '' ? input.name.trim() : manifest.name
103
157
  const description =
104
158
  typeof input.description === 'string' && input.description.trim() !== ''