@acryldev/plugin-share 0.1.3 → 0.1.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/README.md +6 -0
- package/index.ts +313 -16
- package/lib/index.js +341 -11
- package/lib/index.js.map +1 -1
- package/lib/package.js +3 -3
- package/lib/package.js.map +1 -1
- package/lib/types/index.d.ts +26 -1
- package/lib/types/merge-edits.d.ts +16 -0
- package/lib/types/npm-login.d.ts +7 -0
- package/lib/types/package.d.ts +5 -2
- package/lib/types/publish.d.ts +16 -0
- package/lib/types/registry-source.d.ts +23 -0
- package/merge-edits.ts +85 -0
- package/npm-login.ts +43 -0
- package/package.json +1 -1
- package/package.ts +15 -7
- package/publish.ts +21 -0
- package/registry-source.ts +62 -0
- package/test/plugin-share.test.ts +357 -2
package/README.md
CHANGED
|
@@ -8,8 +8,14 @@ Share plugins between people. Pull a plugin someone released into your running a
|
|
|
8
8
|
npm run acryl -- plugin pull github:<owner>/<repo>@<version>
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
`registry:<id>` pulls what the public plugin registry (cordisplugins.github.io) lists under that id, checked against the integrity it lists.
|
|
12
|
+
|
|
11
13
|
The line someone gives you after publishing also carries `@sha256:<digest>`. Then a release with different bytes is refused, with exit 1, and nothing changes.
|
|
12
14
|
|
|
15
|
+
A newer release of a plugin you already pulled updates it in place. A folder you never touched is replaced. A folder you changed is merged three ways
|
|
16
|
+
(the release you pulled, your folder, the new release; `merge-edits.ts`): a file only you changed keeps your change, a file both changed is merged line by
|
|
17
|
+
line with git, and when the same lines changed nothing is written and the pull is refused naming the file. The answer lists the files that kept your changes (`kept`).
|
|
18
|
+
|
|
13
19
|
The app must be running (`npm run dev`), because the pull installs into it live. A stopped app never installs offline: the line exits 3 and asks you to start the app and run the same line again.
|
|
14
20
|
|
|
15
21
|
The app remembers where each pulled plugin came from, in the `plugin-share` row of `blend.yaml`. A copy of the app fetches the same bytes at its next start.
|
package/index.ts
CHANGED
|
@@ -15,29 +15,35 @@ import {
|
|
|
15
15
|
} from 'node:fs'
|
|
16
16
|
import { dirname, join, resolve } from 'node:path'
|
|
17
17
|
import { findSecrets } from '@acryldev/app-persistence'
|
|
18
|
-
import { digestFolder, type PluginFile } from '@acryldev/blends-core'
|
|
18
|
+
import { digestFolder, type PluginFile, pluginFiles } from '@acryldev/blends-core'
|
|
19
19
|
import { type ExtensionInfo, NAME } from '@acryldev/extensions'
|
|
20
20
|
import type {} from '@acryldev/instance'
|
|
21
21
|
import { LOCK_FILE, writeLock } from '@acryldev/manifest/lock'
|
|
22
22
|
import {
|
|
23
23
|
bumpVersion,
|
|
24
|
+
firstRegistryPort,
|
|
24
25
|
gitCli,
|
|
25
26
|
githubCli,
|
|
26
27
|
githubReleasesOverHttps,
|
|
27
28
|
httpsDownload,
|
|
28
29
|
InvalidPluginSourceError,
|
|
30
|
+
NPM_REGISTRY,
|
|
31
|
+
npmRegistryOverHttps,
|
|
32
|
+
npmRegistryViaCli,
|
|
29
33
|
type PluginSource,
|
|
30
34
|
PluginStore,
|
|
31
35
|
PullError,
|
|
32
36
|
packFolder,
|
|
33
37
|
parsePluginSource,
|
|
34
38
|
pullPlugin,
|
|
39
|
+
type RegistryPort,
|
|
35
40
|
type ReleasePort,
|
|
36
41
|
readTarball,
|
|
37
42
|
sharePlugin,
|
|
38
43
|
storeRootOf,
|
|
39
44
|
} from '@acryldev/plugin-hub-core'
|
|
40
45
|
import { type Context, Service } from '@deepseek-ai/cordis'
|
|
46
|
+
import { type MergedTrees, mergeTrees, type Tree } from './merge-edits.ts'
|
|
41
47
|
import {
|
|
42
48
|
ensureNative,
|
|
43
49
|
type NativeAction,
|
|
@@ -49,8 +55,10 @@ import {
|
|
|
49
55
|
nativesOf,
|
|
50
56
|
releaseNative,
|
|
51
57
|
} from './native.ts'
|
|
58
|
+
import { npmWithLogin } from './npm-login.ts'
|
|
52
59
|
import { packagePlugin } from './package.ts'
|
|
53
60
|
import {
|
|
61
|
+
npmPullLineOf,
|
|
54
62
|
type PublishOptions,
|
|
55
63
|
type PublishPlan,
|
|
56
64
|
type PublishPorts,
|
|
@@ -68,6 +76,7 @@ import {
|
|
|
68
76
|
} from './pull-check.ts'
|
|
69
77
|
import { LIST_MAX_BYTES, parsePullList, pullListLocation } from './pull-list.ts'
|
|
70
78
|
import { type PulledRecord, pulledRecords, withPulledRecord } from './pulled-records.ts'
|
|
79
|
+
import { lookUpRegistry, REGISTRY_INDEX_URL } from './registry-source.ts'
|
|
71
80
|
|
|
72
81
|
export type {
|
|
73
82
|
PublishOptions,
|
|
@@ -91,6 +100,10 @@ export interface PluginShareConfig {
|
|
|
91
100
|
publish?: PublishPorts
|
|
92
101
|
/** How an https address is read: a release `.tgz` and a list of sources. Defaults to HTTPS with the hub's size and time limits. */
|
|
93
102
|
download?: (url: string) => Promise<Uint8Array>
|
|
103
|
+
/** Where `registry:<id>` reads the plugin registry's index. Defaults to cordisplugins.github.io. */
|
|
104
|
+
registryIndex?: string
|
|
105
|
+
/** Where an `npm:` source reads packages. Defaults to the public registry over HTTPS, then the person's own npm (a private scope or registry). */
|
|
106
|
+
npm?: RegistryPort
|
|
94
107
|
/** How a plugin's native package is downloaded and unpacked (native.ts). Defaults to the npm registry and the system's tar. */
|
|
95
108
|
native?: NativePorts
|
|
96
109
|
}
|
|
@@ -108,6 +121,8 @@ export type PullResult =
|
|
|
108
121
|
readonly state: string
|
|
109
122
|
/** Whether the app's `blend.yaml` and lock now name this source, so a copy of the app can fetch it. A local file names nothing to fetch. */
|
|
110
123
|
readonly remembered: boolean
|
|
124
|
+
/** The files of an older pulled copy that kept the person's own changes when this release updated it in place. */
|
|
125
|
+
readonly kept?: readonly string[]
|
|
111
126
|
/** The native package the plugin brought for this computer (native.ts), and what installing it did. */
|
|
112
127
|
readonly native?: {
|
|
113
128
|
readonly package: string
|
|
@@ -186,6 +201,8 @@ function releaseOf(folder: string): string | undefined {
|
|
|
186
201
|
/** The license a new public plugin is offered under; the plan shows it before the person says yes. */
|
|
187
202
|
const DEFAULT_LICENSE = 'MIT'
|
|
188
203
|
const LICENSE_ID = /^[A-Za-z0-9][A-Za-z0-9.+-]*$/u
|
|
204
|
+
/** An npm scope: what follows `@`. */
|
|
205
|
+
const NPM_SCOPE = /^[a-z0-9][a-z0-9._~-]*$/u
|
|
189
206
|
|
|
190
207
|
const publishRefused = (reason: string, next: string): PublishRefused => ({
|
|
191
208
|
ok: false,
|
|
@@ -193,7 +210,8 @@ const publishRefused = (reason: string, next: string): PublishRefused => ({
|
|
|
193
210
|
next,
|
|
194
211
|
})
|
|
195
212
|
|
|
196
|
-
const SOURCE_FORMS =
|
|
213
|
+
const SOURCE_FORMS =
|
|
214
|
+
'github:<owner>/<repo>[@<version>], npm:<package>[@<version>], registry:<id> or a .tgz path or address'
|
|
197
215
|
|
|
198
216
|
/** Read what a person wrote. A `.tgz` path may be relative to the app folder; the hub only takes absolute ones. */
|
|
199
217
|
function readSource(
|
|
@@ -203,7 +221,12 @@ function readSource(
|
|
|
203
221
|
const { source, digest } = splitDigest(input.trim())
|
|
204
222
|
const isLocalTarball = /\.(?:tgz|tar\.gz)$/iu.test(source) && !/^[a-z][a-z0-9+]+:/iu.test(source)
|
|
205
223
|
const parsed = parsePluginSource(isLocalTarball ? resolve(root, source) : source)
|
|
206
|
-
if (
|
|
224
|
+
if (
|
|
225
|
+
parsed.kind !== 'github' &&
|
|
226
|
+
parsed.kind !== 'npm' &&
|
|
227
|
+
parsed.kind !== 'tarball-file' &&
|
|
228
|
+
parsed.kind !== 'tarball-url'
|
|
229
|
+
) {
|
|
207
230
|
const named = parsed.kind === 'name' ? 'a bare name' : parsed.kind
|
|
208
231
|
throw new InvalidPluginSourceError(
|
|
209
232
|
input,
|
|
@@ -216,9 +239,35 @@ function readSource(
|
|
|
216
239
|
/** The source as another machine can fetch it: the version pinned; a file on this machine names nothing. */
|
|
217
240
|
function recordedSource(source: PluginSource, version: string): string | undefined {
|
|
218
241
|
if (source.kind === 'github') return `github:${source.owner}/${source.repo}@${version}`
|
|
242
|
+
if (source.kind === 'npm') return `npm:${source.name.full}@${version}`
|
|
219
243
|
return source.kind === 'tarball-url' ? source.url : undefined
|
|
220
244
|
}
|
|
221
245
|
|
|
246
|
+
/** What two releases of one plugin share in a recorded source: `github:<owner>/<repo>` or `npm:<package>`, lower case. */
|
|
247
|
+
const repoOf = (source: string): string | undefined => {
|
|
248
|
+
const github = /^(github:[^/@]+\/[^/@]+)@/u.exec(source)?.[1]
|
|
249
|
+
const npm = /^(npm:(?:@[^/@]+\/)?[^/@]+)@/u.exec(source)?.[1]
|
|
250
|
+
return (github ?? npm)?.toLowerCase()
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** A package's folder in `plugins/`: the name after its scope (`@ana/clock` is `clock`). */
|
|
254
|
+
const folderOf = (packageName: string): string =>
|
|
255
|
+
packageName.includes('/') ? packageName.slice(packageName.lastIndexOf('/') + 1) : packageName
|
|
256
|
+
|
|
257
|
+
const treeOf = (files: readonly PluginFile[]): Tree =>
|
|
258
|
+
new Map(files.map((file) => [file.path, file.bytes]))
|
|
259
|
+
|
|
260
|
+
/** Make a plugin folder hold exactly `to`, having held `from`: files only `from` had go, every file of `to` is written. */
|
|
261
|
+
function applyTree(folder: string, from: Tree, to: Tree): void {
|
|
262
|
+
for (const path of from.keys())
|
|
263
|
+
if (!to.has(path)) rmSync(join(folder, ...path.split('/')), { force: true })
|
|
264
|
+
for (const [path, bytes] of to) {
|
|
265
|
+
const file = join(folder, ...path.split('/'))
|
|
266
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
267
|
+
writeFileSync(file, bytes)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
222
271
|
const reasonOf = (cause: unknown): string => {
|
|
223
272
|
if (cause instanceof PullError || cause instanceof InvalidPluginSourceError) return cause.reason
|
|
224
273
|
return cause instanceof Error ? cause.message : String(cause)
|
|
@@ -230,6 +279,8 @@ export class PluginShare extends Service {
|
|
|
230
279
|
private readonly dir: string
|
|
231
280
|
private readonly releases: ReleasePort
|
|
232
281
|
private readonly download: (url: string) => Promise<Uint8Array>
|
|
282
|
+
private readonly registryIndex: string
|
|
283
|
+
private readonly npmRegistry: RegistryPort
|
|
233
284
|
private readonly ports: PublishPorts
|
|
234
285
|
/** One at a time: two pulls of one name must not race for its folder, nor two publishes for its package. */
|
|
235
286
|
private queue: Promise<unknown> = Promise.resolve()
|
|
@@ -251,6 +302,8 @@ export class PluginShare extends Service {
|
|
|
251
302
|
this.dir = resolve(config.dir ?? join(ctx.appInstance.root, 'plugins'))
|
|
252
303
|
this.releases = config.releases ?? githubReleasesOverHttps()
|
|
253
304
|
this.download = config.download ?? httpsDownload()
|
|
305
|
+
this.registryIndex = config.registryIndex ?? REGISTRY_INDEX_URL
|
|
306
|
+
this.npmRegistry = config.npm ?? firstRegistryPort(npmRegistryOverHttps(), npmRegistryViaCli())
|
|
254
307
|
this.nativeWork = config.native ?? nativePorts()
|
|
255
308
|
this.ports = config.publish ?? { host: githubCli(), git: gitCli() }
|
|
256
309
|
// Removing a plugin removes its record. Listened to here, not asked of each caller: every way of removing it goes through extensions.
|
|
@@ -394,6 +447,7 @@ export class PluginShare extends Service {
|
|
|
394
447
|
'look at the new plan and say yes again',
|
|
395
448
|
)
|
|
396
449
|
}
|
|
450
|
+
if (plan.target === 'npm') return this.publishNpm(name, plan)
|
|
397
451
|
const { home } = this.own.appInstance
|
|
398
452
|
try {
|
|
399
453
|
const shared = await sharePlugin(
|
|
@@ -568,6 +622,7 @@ export class PluginShare extends Service {
|
|
|
568
622
|
`this app has no plugin "${name}" in plugins/`,
|
|
569
623
|
'name a folder of plugins/',
|
|
570
624
|
)
|
|
625
|
+
if (options.to === 'npm') return this.planNpm(name, source, options)
|
|
571
626
|
try {
|
|
572
627
|
const { host } = this.ports
|
|
573
628
|
const owner = options.owner ?? host.login()
|
|
@@ -621,8 +676,174 @@ export class PluginShare extends Service {
|
|
|
621
676
|
}
|
|
622
677
|
}
|
|
623
678
|
|
|
679
|
+
/**
|
|
680
|
+
* What publishing to npm would do, read without sending anything: the account is the person's own npm login (`npm whoami`), the package is
|
|
681
|
+
* `@<scope>/<name>` (the scope defaults to that account), and a version npm already has makes the plan name the next patch, as a GitHub release does.
|
|
682
|
+
*/
|
|
683
|
+
private async planNpm(
|
|
684
|
+
name: string,
|
|
685
|
+
source: string,
|
|
686
|
+
options: PublishOptions,
|
|
687
|
+
): Promise<PublishPlan | PublishRefused> {
|
|
688
|
+
const npm = this.ports.npm ?? npmWithLogin()
|
|
689
|
+
const registry = options.registry ?? NPM_REGISTRY
|
|
690
|
+
try {
|
|
691
|
+
const account = npm.login(registry)
|
|
692
|
+
const scope = (options.scope ?? account).replace(/^@/u, '').toLowerCase()
|
|
693
|
+
if (!NPM_SCOPE.test(scope))
|
|
694
|
+
return publishRefused(
|
|
695
|
+
`"${scope}" is not an npm scope`,
|
|
696
|
+
'write letters, digits and dashes, for example --scope acme',
|
|
697
|
+
)
|
|
698
|
+
const license = options.license ?? DEFAULT_LICENSE
|
|
699
|
+
if (!LICENSE_ID.test(license))
|
|
700
|
+
return publishRefused(
|
|
701
|
+
`"${license}" is not a license id`,
|
|
702
|
+
'write one such as MIT or Apache-2.0',
|
|
703
|
+
)
|
|
704
|
+
const packageName = `@${scope}/${name}`
|
|
705
|
+
const pack = (version?: string) => this.packNpm(source, name, packageName, license, version)
|
|
706
|
+
let packaged = await pack()
|
|
707
|
+
const released = npm.hasVersion(packageName, packaged.version, registry)
|
|
708
|
+
? packaged.version
|
|
709
|
+
: undefined
|
|
710
|
+
if (released !== undefined) packaged = await pack(bumpVersion(released, 'patch'))
|
|
711
|
+
const secrets = findSecrets(
|
|
712
|
+
readTarball(packFolder(packaged.dir).tarball).map((file) => ({
|
|
713
|
+
path: file.path,
|
|
714
|
+
text: Buffer.from(file.content).toString('utf8'),
|
|
715
|
+
})),
|
|
716
|
+
)
|
|
717
|
+
if (secrets.length > 0) {
|
|
718
|
+
return publishRefused(
|
|
719
|
+
`the secret check found ${String(secrets.length)} possible secret(s) in ${secrets.map((secret) => secret.path).join(', ')}; nothing was sent`,
|
|
720
|
+
"take them out of the plugin and read them from the app's settings",
|
|
721
|
+
)
|
|
722
|
+
}
|
|
723
|
+
return {
|
|
724
|
+
target: 'npm',
|
|
725
|
+
package: packageName,
|
|
726
|
+
registry,
|
|
727
|
+
owner: scope,
|
|
728
|
+
repo: name,
|
|
729
|
+
version: packaged.version,
|
|
730
|
+
visibility: 'public',
|
|
731
|
+
license,
|
|
732
|
+
createsRepository: false,
|
|
733
|
+
...(released === undefined ? {} : { released }),
|
|
734
|
+
files: packaged.files,
|
|
735
|
+
integrity: packaged.integrity,
|
|
736
|
+
digest: packaged.digest,
|
|
737
|
+
pullLine: npmPullLineOf(packageName, packaged.version, packaged.digest),
|
|
738
|
+
}
|
|
739
|
+
} catch (cause) {
|
|
740
|
+
return publishRefused(
|
|
741
|
+
reasonOf(cause),
|
|
742
|
+
'fix it, or sign in with: npm login (--registry <address> for another registry); nothing was sent',
|
|
743
|
+
)
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/** The plugin as the npm package `packageName`: a package of its own folder, the same bytes whenever it is made again for the same version. */
|
|
748
|
+
private packNpm(
|
|
749
|
+
source: string,
|
|
750
|
+
name: string,
|
|
751
|
+
packageName: string,
|
|
752
|
+
license: string,
|
|
753
|
+
version?: string,
|
|
754
|
+
) {
|
|
755
|
+
return packagePlugin(source, this.packages, name, `The ${name} Cordis plugin.`, {
|
|
756
|
+
release: { license },
|
|
757
|
+
packageName,
|
|
758
|
+
...(version === undefined ? {} : { version }),
|
|
759
|
+
})
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/** The confirmed npm plan, published with the person's own npm: the package is made again, and only the bytes that were planned are sent. */
|
|
763
|
+
private async publishNpm(name: string, plan: PublishPlan): Promise<PublishResult> {
|
|
764
|
+
const registry = plan.registry ?? NPM_REGISTRY
|
|
765
|
+
const packageName = plan.package ?? `@${plan.owner}/${plan.repo}`
|
|
766
|
+
try {
|
|
767
|
+
const packed = await this.packNpm(
|
|
768
|
+
join(this.dir, name),
|
|
769
|
+
name,
|
|
770
|
+
packageName,
|
|
771
|
+
plan.license,
|
|
772
|
+
plan.version,
|
|
773
|
+
)
|
|
774
|
+
if (packed.integrity !== plan.integrity)
|
|
775
|
+
return publishRefused(
|
|
776
|
+
`the package made now is not the one that was planned (${packageName} v${plan.version}); nothing was sent`,
|
|
777
|
+
'look at the new plan and say yes again',
|
|
778
|
+
)
|
|
779
|
+
;(this.ports.npm ?? npmWithLogin()).publish(packed.tarball, registry, 'public')
|
|
780
|
+
const url =
|
|
781
|
+
registry === NPM_REGISTRY
|
|
782
|
+
? `https://www.npmjs.com/package/${packageName}`
|
|
783
|
+
: `${registry.replace(/\/$/u, '')}/${packageName}`
|
|
784
|
+
return {
|
|
785
|
+
ok: true,
|
|
786
|
+
url,
|
|
787
|
+
releaseUrl: url,
|
|
788
|
+
version: packed.version,
|
|
789
|
+
integrity: packed.integrity,
|
|
790
|
+
digest: plan.digest,
|
|
791
|
+
pullLine: plan.pullLine,
|
|
792
|
+
}
|
|
793
|
+
} catch (cause) {
|
|
794
|
+
return publishRefused(
|
|
795
|
+
reasonOf(cause),
|
|
796
|
+
'look at the registry; part of this publish may have been sent',
|
|
797
|
+
)
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/** A `registry:<id>` source turned into the release it lists, with the integrity the registry lists for it; any other source as it is. */
|
|
802
|
+
private async resolveRegistry(
|
|
803
|
+
input: string,
|
|
804
|
+
expectIntegrity?: string,
|
|
805
|
+
): Promise<{ readonly input: string; readonly expectIntegrity?: string } | PullRefused> {
|
|
806
|
+
const text = input.trim()
|
|
807
|
+
if (!/^registry:/iu.test(text))
|
|
808
|
+
return { input, ...(expectIntegrity === undefined ? {} : { expectIntegrity }) }
|
|
809
|
+
// A digest written after the id (`registry:<id>@sha256:<digest>`) is not part of the id: it travels on to the release's source.
|
|
810
|
+
let named: ReturnType<typeof splitDigest>
|
|
811
|
+
try {
|
|
812
|
+
named = splitDigest(text)
|
|
813
|
+
} catch (cause) {
|
|
814
|
+
return refuse(
|
|
815
|
+
'source',
|
|
816
|
+
reasonOf(cause),
|
|
817
|
+
'write the 64 hex digits the plugin page shows after @sha256:',
|
|
818
|
+
)
|
|
819
|
+
}
|
|
820
|
+
let index: string
|
|
821
|
+
try {
|
|
822
|
+
index = Buffer.from(await this.download(this.registryIndex)).toString('utf8')
|
|
823
|
+
} catch (cause) {
|
|
824
|
+
return refuse(
|
|
825
|
+
'fetch',
|
|
826
|
+
`the registry could not be read: ${reasonOf(cause)}`,
|
|
827
|
+
'check the internet connection and pull again, or pull github:<owner>/<repo>',
|
|
828
|
+
)
|
|
829
|
+
}
|
|
830
|
+
const found = lookUpRegistry(index, named.source.slice('registry:'.length))
|
|
831
|
+
if (!found.ok) return refuse('source', found.reason, found.next)
|
|
832
|
+
return {
|
|
833
|
+
input:
|
|
834
|
+
named.digest === undefined
|
|
835
|
+
? found.hit.source
|
|
836
|
+
: `${found.hit.source}@sha256:${named.digest}`,
|
|
837
|
+
expectIntegrity: expectIntegrity ?? found.hit.integrity,
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
624
841
|
private async pullNow(input: string, expectIntegrity?: string): Promise<PullResult> {
|
|
625
842
|
const { home, root } = this.own.appInstance
|
|
843
|
+
const resolved = await this.resolveRegistry(input, expectIntegrity)
|
|
844
|
+
if ('refusal' in resolved) return resolved
|
|
845
|
+
input = resolved.input
|
|
846
|
+
expectIntegrity = resolved.expectIntegrity
|
|
626
847
|
let read: ReturnType<typeof readSource>
|
|
627
848
|
try {
|
|
628
849
|
read = readSource(input, root)
|
|
@@ -643,6 +864,7 @@ export class PluginShare extends Service {
|
|
|
643
864
|
{
|
|
644
865
|
store: new PluginStore(scratch),
|
|
645
866
|
releases: this.releases,
|
|
867
|
+
registry: this.npmRegistry,
|
|
646
868
|
download: this.download,
|
|
647
869
|
},
|
|
648
870
|
expectIntegrity === undefined ? {} : { expectIntegrity },
|
|
@@ -654,18 +876,21 @@ export class PluginShare extends Service {
|
|
|
654
876
|
'check the source with the person who released it; nothing was installed',
|
|
655
877
|
)
|
|
656
878
|
}
|
|
879
|
+
// An npm package is `@scope/name` and lives in the folder `name`; a package from any other source must already be named for a folder.
|
|
880
|
+
const folderIn = (packageName: string): string =>
|
|
881
|
+
read.source.kind === 'npm' ? folderOf(packageName) : packageName
|
|
657
882
|
const files: PluginFile[] = readTarball(readFileSync(pulled.stored.path)).map(
|
|
658
883
|
({ path, content }) => ({ path, bytes: content, kind: 'file' }),
|
|
659
884
|
)
|
|
660
885
|
const refused = pullRefusal(pulled.manifest, files, {
|
|
661
886
|
digest: read.digest,
|
|
662
|
-
nameFits: (name) => NAME.test(name),
|
|
887
|
+
nameFits: (name) => NAME.test(folderIn(name)),
|
|
663
888
|
hasService: (service) => this.own.get(service as never) !== undefined,
|
|
664
889
|
})
|
|
665
890
|
if (refused !== undefined) return refused
|
|
666
891
|
const declared = nativesOf(pulled.manifest)
|
|
667
892
|
return await this.install(
|
|
668
|
-
pulled.stored.name,
|
|
893
|
+
folderIn(pulled.stored.name),
|
|
669
894
|
pulled.stored.version,
|
|
670
895
|
pulled.stored.integrity,
|
|
671
896
|
files,
|
|
@@ -677,6 +902,60 @@ export class PluginShare extends Service {
|
|
|
677
902
|
}
|
|
678
903
|
}
|
|
679
904
|
|
|
905
|
+
/**
|
|
906
|
+
* What the folder holds once a newer release of its pulled plugin comes in, the person's own changes kept: the release they pulled is the base of a
|
|
907
|
+
* three-way merge (fetched again from where it came from, checked against its recorded integrity, unless the folder is untouched and is the base).
|
|
908
|
+
* Refused, and nothing written, when the release and the person changed the same lines.
|
|
909
|
+
*/
|
|
910
|
+
private async planUpdate(
|
|
911
|
+
name: string,
|
|
912
|
+
before: PulledRecord,
|
|
913
|
+
held: string,
|
|
914
|
+
release: readonly PluginFile[],
|
|
915
|
+
target: string,
|
|
916
|
+
): Promise<{ readonly mine: Tree; readonly merged: MergedTrees } | PullRefused> {
|
|
917
|
+
const mine = treeOf(pluginFiles(target))
|
|
918
|
+
let base = mine
|
|
919
|
+
if (held !== before.digest) {
|
|
920
|
+
const scratch = mkdtempSync(join(this.own.appInstance.home, 'plugin-base-'))
|
|
921
|
+
try {
|
|
922
|
+
const pulled = await pullPlugin(
|
|
923
|
+
readSource(before.source, this.own.appInstance.root).source,
|
|
924
|
+
{
|
|
925
|
+
store: new PluginStore(scratch),
|
|
926
|
+
releases: this.releases,
|
|
927
|
+
registry: this.npmRegistry,
|
|
928
|
+
download: this.download,
|
|
929
|
+
},
|
|
930
|
+
{ expectIntegrity: before.integrity },
|
|
931
|
+
)
|
|
932
|
+
base = treeOf(
|
|
933
|
+
readTarball(readFileSync(pulled.stored.path)).map(({ path, content }) => ({
|
|
934
|
+
path,
|
|
935
|
+
bytes: content,
|
|
936
|
+
kind: 'file' as const,
|
|
937
|
+
})),
|
|
938
|
+
)
|
|
939
|
+
} catch (cause) {
|
|
940
|
+
return refuse(
|
|
941
|
+
'folder-exists',
|
|
942
|
+
`plugins/${name} has changes of your own, and the release it was pulled from could not be fetched to keep them: ${reasonOf(cause)}`,
|
|
943
|
+
`check the internet connection and pull again, or move plugins/${name} out of plugins/ and pull again`,
|
|
944
|
+
)
|
|
945
|
+
} finally {
|
|
946
|
+
rmSync(scratch, { recursive: true, force: true })
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
const merged = mergeTrees(base, mine, treeOf(release))
|
|
950
|
+
if (merged.conflicts.length > 0)
|
|
951
|
+
return refuse(
|
|
952
|
+
'folder-exists',
|
|
953
|
+
`plugins/${name} has changes of your own to ${merged.conflicts.join(', ')}, and the new release changes ${merged.conflicts.length === 1 ? 'it' : 'them'} too`,
|
|
954
|
+
`plugins/${name} is untouched: move it out of plugins/ and pull again, then bring your changes back by hand`,
|
|
955
|
+
)
|
|
956
|
+
return { mine, merged }
|
|
957
|
+
}
|
|
958
|
+
|
|
680
959
|
/** Put the files in `plugins/<name>/` and install them; whatever does not end running takes its folder with it. */
|
|
681
960
|
private async install(
|
|
682
961
|
name: string,
|
|
@@ -700,22 +979,36 @@ export class PluginShare extends Service {
|
|
|
700
979
|
const wrote = !existsSync(target)
|
|
701
980
|
const before = this.recorded(name)
|
|
702
981
|
const held = wrote ? undefined : digestFolder(target)
|
|
982
|
+
let update: { readonly mine: Tree; readonly merged: MergedTrees } | undefined
|
|
703
983
|
if (held !== undefined && held !== digest) {
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
984
|
+
// A newer release of the plugin pulled here updates it in place, the person's own changes kept; anything else in the folder is someone's work.
|
|
985
|
+
const sameRepo =
|
|
986
|
+
before !== undefined &&
|
|
987
|
+
source !== undefined &&
|
|
988
|
+
repoOf(before.source) !== undefined &&
|
|
989
|
+
repoOf(before.source) === repoOf(source)
|
|
990
|
+
if (before !== undefined && sameRepo && before.digest !== digest) {
|
|
991
|
+
const planned = await this.planUpdate(name, before, held, files, target)
|
|
992
|
+
if ('refusal' in planned) return planned
|
|
993
|
+
update = planned
|
|
994
|
+
} else {
|
|
995
|
+
const holder = before === undefined ? 'made in this app' : `pulled from ${before.source}`
|
|
996
|
+
// Only an untouched pulled copy goes with `plugin remove`; anything else is someone's work, which the person moves themselves.
|
|
997
|
+
const next =
|
|
998
|
+
before !== undefined && held === before.digest
|
|
999
|
+
? `keep it, or remove it (npm run acryl -- plugin remove ${name}) and pull again`
|
|
1000
|
+
: `keep it, or move plugins/${name} out of plugins/ and pull again`
|
|
1001
|
+
return refuse(
|
|
1002
|
+
'folder-exists',
|
|
1003
|
+
`plugins/${name} holds a different copy of this plugin, ${holder}`,
|
|
1004
|
+
next,
|
|
1005
|
+
)
|
|
1006
|
+
}
|
|
715
1007
|
}
|
|
716
1008
|
const liveBefore = this.live.get(name)
|
|
717
1009
|
this.live.set(name, digest)
|
|
718
1010
|
if (wrote) renameSync(incoming, target)
|
|
1011
|
+
else if (update !== undefined) applyTree(target, update.mine, update.merged.files)
|
|
719
1012
|
// Remembered as the folder appears, before it is installed, so whoever sees the folder can tell it is these released bytes (the frame then does
|
|
720
1013
|
// not load the app again for a plugin the app installs live); a pull that does not end running puts back what was remembered before.
|
|
721
1014
|
const remembered = source !== undefined && this.remember(name, { source, integrity, digest })
|
|
@@ -726,6 +1019,7 @@ export class PluginShare extends Service {
|
|
|
726
1019
|
if (native !== undefined && (nativeAction === 'downloaded' || nativeAction === 'linked'))
|
|
727
1020
|
releaseNative(native, this.places, nativeAction === 'linked')
|
|
728
1021
|
if (wrote) rmSync(target, { recursive: true, force: true })
|
|
1022
|
+
else if (update !== undefined) applyTree(target, update.merged.files, update.mine)
|
|
729
1023
|
if (remembered) this.remember(name, before)
|
|
730
1024
|
if (liveBefore === undefined) this.live.delete(name)
|
|
731
1025
|
else this.live.set(name, liveBefore)
|
|
@@ -771,6 +1065,9 @@ export class PluginShare extends Service {
|
|
|
771
1065
|
action: result.action,
|
|
772
1066
|
state: result.state,
|
|
773
1067
|
remembered,
|
|
1068
|
+
...(update === undefined || update.merged.kept.length === 0
|
|
1069
|
+
? {}
|
|
1070
|
+
: { kept: update.merged.kept }),
|
|
774
1071
|
...this.keepNative(name, native, nativeBefore, nativeAction),
|
|
775
1072
|
}
|
|
776
1073
|
} finally {
|