rman 2.31.0 → 2.32.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.
@@ -0,0 +1,2 @@
1
+ export * from './spm.target.js';
2
+ export * from './spm-publish.service.js';
@@ -0,0 +1,2 @@
1
+ export * from './spm.target.js';
2
+ export * from './spm-publish.service.js';
@@ -0,0 +1,74 @@
1
+ import type { Package } from '../../../core/classes/package.js';
2
+ import { Service } from '../../../core/classes/service.js';
3
+ import { type PublishTarget } from '../../../core/interfaces/publish-target.js';
4
+ import { type PackageFilterOptions } from '../../../utils/package-filter.js';
5
+ /** The name this target answers to in `publish.target` and `--target`. */
6
+ export declare const SPM_TARGET = "spm";
7
+ /**
8
+ * Publishes packages to the SyncBridge registry through the repository's own `spm`.
9
+ *
10
+ * **Question B, like every target**: is *this version* on the registry already? `spm view <name>
11
+ * --json` answers it, as `npm view` does for the npm target - `up-to-date` when the version is
12
+ * listed, `publish` when it is not, `error` when the lookup itself failed.
13
+ */
14
+ export declare class SpmPublishService extends Service {
15
+ /** What `spm publish` prints for a version the registry already has, under `--skip-existing`. */
16
+ protected readonly skippedPattern: RegExp;
17
+ /** `spm publish --no-wait`'s exit code when the registry would ask for approval. */
18
+ protected readonly approvalRequiredExit: number;
19
+ /**
20
+ * What `publish --target spm` would do. Only packages naming `"spm"` in their own `publish.target`
21
+ * are candidates; one with a dirty working tree is an `'error'` (or `'skip'` under
22
+ * `ignoreDirty`), and the rest are asked of the registry.
23
+ */
24
+ getPlan(options?: SpmPublishService.Options, deps?: SpmPublishService.Deps): Promise<SpmPublishService.Entry[]>;
25
+ /**
26
+ * Publishes every `'publish'` entry with `spm publish --skip-existing`, one package after another,
27
+ * in the package's own directory. A failure is that package's alone and is reported on its entry.
28
+ */
29
+ applyPlan(plan: SpmPublishService.Entry[]): Promise<SpmPublishService.Entry[]>;
30
+ /** The access and registry `pkg` publishes with: the flag, else its `.rmanrc "publish.spm"`. */
31
+ protected settingsOf(pkg: Package, options: SpmPublishService.Options): SpmPublishService.Settings;
32
+ /** The versions of `pkg` the registry lists - `spm view <name> --json`. Throws when the lookup
33
+ * failed, with spm's own reason. */
34
+ protected view(pkg: Package, registry: string | undefined): Promise<string[]>;
35
+ /** Runs the publish, its output shown on stderr as it comes. `true` when spm skipped the version
36
+ * because the registry has it. */
37
+ protected publish(entry: SpmPublishService.Entry): Promise<boolean>;
38
+ /**
39
+ * Whether nobody can approve a publish: not GitHub Actions (where spm publishes as the job, through
40
+ * its OIDC token) and no terminal. spm is then told `--no-wait`, and refuses at once rather than
41
+ * polling for an approval.
42
+ */
43
+ protected unattended(): boolean;
44
+ protected registryArgs(registry: string | undefined): string[];
45
+ /** `spm` from the package's own or the repository's `node_modules`. */
46
+ protected spm(pkg: Package, argv: string[], onLine: (line: string, stream: 'stdout' | 'stderr') => void): Promise<void>;
47
+ }
48
+ export declare namespace SpmPublishService {
49
+ /** Injectable registry lookup, so a spec needs neither network nor spm. */
50
+ interface Deps {
51
+ publishedVersions?(pkg: Package, registry: string | undefined): Promise<string[]>;
52
+ }
53
+ interface Options extends PackageFilterOptions {
54
+ /** `--ignore-dirty`: a package with uncommitted changes is skipped rather than an error. */
55
+ ignoreDirty?: boolean;
56
+ /** `--spm-access`, over every package's `publish.spm.access`. */
57
+ access?: 'public' | 'private';
58
+ /** `--spm-registry`, over every package's `publish.spm.registry`. */
59
+ registry?: string;
60
+ }
61
+ interface Settings {
62
+ access?: 'public' | 'private';
63
+ registry?: string;
64
+ }
65
+ interface Entry extends PublishTarget.Entry {
66
+ /** What the plan decided to publish with, read back by `applyPlan` rather than recomputed. */
67
+ settings: Settings;
68
+ }
69
+ }
70
+ declare module '../../../core/classes/service.js' {
71
+ interface ServiceMap {
72
+ spmPublish: SpmPublishService;
73
+ }
74
+ }
@@ -0,0 +1,186 @@
1
+ import { Service } from '../../../core/classes/service.js';
2
+ import { targetsOf } from '../../../core/interfaces/publish-target.js';
3
+ import { filesOwnedBy } from '../../../utils/bundled-packages.js';
4
+ import { dirtyReason, GitHelper } from '../../../utils/git.js';
5
+ import { filterPackages } from '../../../utils/package-filter.js';
6
+ import { runBin } from '../../../utils/run-bin.js';
7
+ /** The name this target answers to in `publish.target` and `--target`. */
8
+ export const SPM_TARGET = 'spm';
9
+ /**
10
+ * Publishes packages to the SyncBridge registry through the repository's own `spm`.
11
+ *
12
+ * **Question B, like every target**: is *this version* on the registry already? `spm view <name>
13
+ * --json` answers it, as `npm view` does for the npm target - `up-to-date` when the version is
14
+ * listed, `publish` when it is not, `error` when the lookup itself failed.
15
+ */
16
+ /* **A failed lookup is an `'error'`, never "not published"** - the opposite of the npm and
17
+ * cloudflare checks, on purpose. Their catch-everything shape is a measured cost on the npm side
18
+ * (`npmViewPackage` read a failed lookup as "never published" and proposed a republish every run),
19
+ * and `spm view` was asked for with a distinct failure exit exactly so this one need not guess.
20
+ *
21
+ * **The publish is `--skip-existing`**, which makes it exact even where the lookup cannot be: a
22
+ * private package is invisible to an anonymous read, and spm answers 409 from the write path. A
23
+ * version it skips comes back `up-to-date`, so `--json` does not call it published. */
24
+ export class SpmPublishService extends Service {
25
+ /** What `spm publish` prints for a version the registry already has, under `--skip-existing`. */
26
+ skippedPattern = /is already published to .*: skipped/;
27
+ /** `spm publish --no-wait`'s exit code when the registry would ask for approval. */
28
+ approvalRequiredExit = 3;
29
+ /**
30
+ * What `publish --target spm` would do. Only packages naming `"spm"` in their own `publish.target`
31
+ * are candidates; one with a dirty working tree is an `'error'` (or `'skip'` under
32
+ * `ignoreDirty`), and the rest are asked of the registry.
33
+ */
34
+ async getPlan(options = {}, deps = {}) {
35
+ const repository = this.repository;
36
+ const git = new GitHelper({ cwd: repository.dirname });
37
+ const packages = filterPackages(repository.getPackages({ toposort: true }), options).filter(pkg => targetsOf(this.app, pkg).some(t => t.name === SPM_TARGET) && !pkg.config.publish?.skip);
38
+ const dirtyFiles = await git.listDirtyFiles({ absolute: true });
39
+ const publishedVersions = deps.publishedVersions ?? ((pkg, registry) => this.view(pkg, registry));
40
+ return Promise.all(packages.map(async (pkg) => {
41
+ const settings = this.settingsOf(pkg, options);
42
+ const base = { package: pkg, version: pkg.version, settings, detail: settings.access };
43
+ const dirty = filesOwnedBy(dirtyFiles, pkg);
44
+ if (dirty.length) {
45
+ return {
46
+ ...base,
47
+ status: options.ignoreDirty ? 'skip' : 'error',
48
+ reason: dirtyReason(dirty, repository.dirname),
49
+ };
50
+ }
51
+ try {
52
+ const versions = await publishedVersions(pkg, settings.registry);
53
+ return versions.includes(pkg.version)
54
+ ? { ...base, status: 'up-to-date', reason: `the registry already has ${pkg.name}@${pkg.version}` }
55
+ : { ...base, status: 'publish', reason: versions.length ? `not published yet` : 'never published' };
56
+ }
57
+ catch (e) {
58
+ return { ...base, status: 'error', reason: e.message };
59
+ }
60
+ }));
61
+ }
62
+ /**
63
+ * Publishes every `'publish'` entry with `spm publish --skip-existing`, one package after another,
64
+ * in the package's own directory. A failure is that package's alone and is reported on its entry.
65
+ */
66
+ async applyPlan(plan) {
67
+ const result = [];
68
+ for (const entry of plan) {
69
+ if (entry.status !== 'publish') {
70
+ result.push(entry);
71
+ continue;
72
+ }
73
+ try {
74
+ const skipped = await this.publish(entry);
75
+ result.push(skipped
76
+ ? {
77
+ ...entry,
78
+ status: 'up-to-date',
79
+ reason: `the registry already has ${entry.package.name}@${entry.version}`,
80
+ }
81
+ : entry);
82
+ }
83
+ catch (e) {
84
+ result.push({ ...entry, status: 'error', reason: e.message });
85
+ }
86
+ }
87
+ return result;
88
+ }
89
+ /** The access and registry `pkg` publishes with: the flag, else its `.rmanrc "publish.spm"`. */
90
+ settingsOf(pkg, options) {
91
+ const config = pkg.config.publish?.spm;
92
+ return {
93
+ access: options.access ?? config?.access,
94
+ registry: options.registry ?? config?.registry,
95
+ };
96
+ }
97
+ /** The versions of `pkg` the registry lists - `spm view <name> --json`. Throws when the lookup
98
+ * failed, with spm's own reason. */
99
+ async view(pkg, registry) {
100
+ const stdout = [];
101
+ const stderr = [];
102
+ try {
103
+ await this.spm(pkg, ['view', pkg.name, '--json', ...this.registryArgs(registry)], (line, stream) => (stream === 'stdout' ? stdout : stderr).push(line));
104
+ }
105
+ catch (e) {
106
+ /** spm missing is its own sentence - "spm view failed" would hide how to fix it. */
107
+ if (!stderr.length)
108
+ throw e;
109
+ throw new Error(`spm view ${pkg.name} failed${tail(stderr)}`, { cause: e });
110
+ }
111
+ let answer;
112
+ try {
113
+ answer = JSON.parse(stdout.join('\n'));
114
+ }
115
+ catch {
116
+ throw new Error(`spm view ${pkg.name} did not answer JSON - @syncbridge/syncbuild 2.2.0 or later has the command`);
117
+ }
118
+ if (!Array.isArray(answer?.versions))
119
+ throw new Error(`spm view ${pkg.name} answered no "versions" list`);
120
+ return answer.versions.filter((v) => typeof v === 'string');
121
+ }
122
+ /** Runs the publish, its output shown on stderr as it comes. `true` when spm skipped the version
123
+ * because the registry has it. */
124
+ async publish(entry) {
125
+ const { access, registry } = entry.settings;
126
+ const argv = [
127
+ 'publish',
128
+ '--skip-existing',
129
+ ...(this.unattended() ? ['--no-wait'] : []),
130
+ ...(access ? ['--access', access] : []),
131
+ ...this.registryArgs(registry),
132
+ ];
133
+ const lines = [];
134
+ try {
135
+ await this.spm(entry.package, argv, line => {
136
+ lines.push(line);
137
+ process.stderr.write(line + '\n');
138
+ });
139
+ }
140
+ catch (e) {
141
+ if (e?.code === this.approvalRequiredExit) {
142
+ throw new Error('the registry asks for approval in Console - publish from a terminal, or from GitHub Actions ' +
143
+ 'with "permissions: id-token: write"', { cause: e });
144
+ }
145
+ throw new Error(`spm publish failed${tail(lines)}`, { cause: e });
146
+ }
147
+ return lines.some(l => this.skippedPattern.test(l));
148
+ }
149
+ /**
150
+ * Whether nobody can approve a publish: not GitHub Actions (where spm publishes as the job, through
151
+ * its OIDC token) and no terminal. spm is then told `--no-wait`, and refuses at once rather than
152
+ * polling for an approval.
153
+ */
154
+ /* `publish --yes` means "no prompt", and spm outside CI polls for up to ten minutes for a Console
155
+ * approval. `--no-wait` exists for this caller - asked of spm rather than decided here, so a
156
+ * registry that asks no approval (SB_PUBLISH_APPROVAL off) still publishes. */
157
+ unattended() {
158
+ const ci = !!process.env.ACTIONS_ID_TOKEN_REQUEST_URL && !!process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;
159
+ return !ci && !process.stdin.isTTY;
160
+ }
161
+ registryArgs(registry) {
162
+ return registry ? ['--registry', registry] : [];
163
+ }
164
+ /** `spm` from the package's own or the repository's `node_modules`. */
165
+ async spm(pkg, argv, onLine) {
166
+ try {
167
+ await runBin('spm', argv, { cwd: pkg.dirname, app: this.app, logLevel: 'silent', onLine });
168
+ }
169
+ catch (e) {
170
+ if (/was not found/.test(e?.message ?? '')) {
171
+ throw new Error('"spm" is not installed in this repository - add @syncbridge/syncbuild as a devDependency', {
172
+ cause: e,
173
+ });
174
+ }
175
+ throw e;
176
+ }
177
+ }
178
+ }
179
+ /** The lines of a failed spm run worth a reason - those saying "error", at most four; else the
180
+ * last two spm printed, which is where it says why (`... could not be reached: bad port`). */
181
+ function tail(lines) {
182
+ const said = lines.map(l => l.trim()).filter(Boolean);
183
+ const errors = [...new Set(said.filter(l => /error|refused|denied|not signed/i.test(l)))].slice(0, 4);
184
+ const picked = errors.length ? errors : said.slice(-2);
185
+ return picked.length ? ':\n' + picked.map(l => ` ${l}`).join('\n') : '';
186
+ }
@@ -0,0 +1,25 @@
1
+ import type { PublishFilesKeys, PublishTarget } from '../../../core/interfaces/publish-target.js';
2
+ import type { ConfigValue, ScopedVars } from '../../../interfaces/rman-config.interface.js';
3
+ /**
4
+ * **`publish.spm.*`** - this target's own config block, reaching `RmanConfig` through the
5
+ * `PublishTargetConfigs` slot `publish.command.ts` exports, as `publish.docker.*` does.
6
+ */
7
+ export interface SpmPublishOptions extends SpmPublishOptionsKeys, ScopedVars {
8
+ }
9
+ export interface SpmPublishOptionsKeys extends PublishFilesKeys {
10
+ /** Who may see the package: `"public"` or `"private"`. spm's own default - a new package is
11
+ * private - applies when unset. `--spm-access` overrides it. */
12
+ access?: ConfigValue<'public' | 'private'>;
13
+ /** The registry's address. spm's own default - `SB_REGISTRY_URL`, else registry.syncbridge.io -
14
+ * applies when unset. `--spm-registry` overrides it. */
15
+ registry?: ConfigValue<string>;
16
+ }
17
+ /**
18
+ * **The SyncBridge registry, as a publish target** - through `spm`, the way the `npm` target goes
19
+ * through `npm`.
20
+ *
21
+ * Opt-in, so no `claims`: a package ships here by naming `"spm"` in its own `publish.target`. `spm`
22
+ * itself comes from the repository - `@syncbridge/syncbuild` installed there - and is run from its
23
+ * `node_modules/.bin`.
24
+ */
25
+ export declare const spmPublishTarget: PublishTarget;
@@ -0,0 +1,47 @@
1
+ import { SPM_TARGET } from './spm-publish.service.js';
2
+ /**
3
+ * **The SyncBridge registry, as a publish target** - through `spm`, the way the `npm` target goes
4
+ * through `npm`.
5
+ *
6
+ * Opt-in, so no `claims`: a package ships here by naming `"spm"` in its own `publish.target`. `spm`
7
+ * itself comes from the repository - `@syncbridge/syncbuild` installed there - and is run from its
8
+ * `node_modules/.bin`.
9
+ */
10
+ /* **Built in rather than shipped by syncbuild - the user's call**: rman already carries the npm
11
+ * registry's whole half (`npm view`, `npm publish`), and the SyncBridge registry is the same kind of
12
+ * thing and ours. What is spm's stays in spm: the lookup is `spm view`, the publish `spm publish`,
13
+ * and anything rman needs that spm cannot answer is asked of spm rather than reimplemented here. */
14
+ export const spmPublishTarget = {
15
+ name: SPM_TARGET,
16
+ describe: 'Publish to the SyncBridge registry (spm publish)',
17
+ options: {
18
+ spmAccess: {
19
+ target: 'cli',
20
+ cliName: 'spm-access',
21
+ describe: 'Who may see a package published to the SyncBridge registry: public or private - over "publish.spm.access".',
22
+ type: 'string',
23
+ },
24
+ spmRegistry: {
25
+ target: 'cli',
26
+ cliName: 'spm-registry',
27
+ describe: 'The SyncBridge registry to publish to - over "publish.spm.registry" and SB_REGISTRY_URL.',
28
+ type: 'string',
29
+ },
30
+ },
31
+ getPlan(ctx) {
32
+ return ctx.app.getService('spmPublish').getPlan(optionsOf(ctx));
33
+ },
34
+ applyPlan(ctx, plan) {
35
+ /** The entries are this target's own - `getPlan` produced them. */
36
+ return ctx.app.getService('spmPublish').applyPlan(plan);
37
+ },
38
+ };
39
+ /** The shared filters plus this target's two flags - read here so the service takes a plain
40
+ * options object and stays callable without a CLI. */
41
+ function optionsOf(ctx) {
42
+ return {
43
+ ...ctx.options,
44
+ access: ctx.args.spmAccess,
45
+ registry: ctx.args.spmRegistry,
46
+ };
47
+ }
@@ -1,5 +1,6 @@
1
1
  import type { CloudflarePublishOptions } from '../builtins/publish-targets/cloudflare/cloudflare.target.js';
2
2
  import type { DockerPublishOptions } from '../builtins/publish-targets/docker/docker.target.js';
3
+ import type { SpmPublishOptions } from '../builtins/publish-targets/spm/spm.target.js';
3
4
  import type { RmanApplication } from '../core/application.js';
4
5
  import { type RmanConfig } from '../interfaces/rman-config.interface.js';
5
6
  /** Hoisted for `ArgsOf` - see `version.command.ts` and `RmanConfig.ArgsOf` for why. */
@@ -114,6 +115,8 @@ export interface PublishTargetConfigs {
114
115
  /** Required once `"cloudflare"` is one of a package's `publish.target`s - `kind` says Pages or
115
116
  * Workers. */
116
117
  cloudflare?: CloudflarePublishOptions;
118
+ /** `"spm"`'s settings - who may see the package, and which registry. Both optional. */
119
+ spm?: SpmPublishOptions;
117
120
  }
118
121
  type Args = RmanConfig.ArgsOf<typeof config, typeof COMMAND>;
119
122
  /**
package/constants.js CHANGED
@@ -1 +1 @@
1
- export const version = '2.31.0';
1
+ export const version = '2.32.0';
@@ -1,5 +1,6 @@
1
1
  import { CloudflarePublishService, cloudflarePublishTarget } from '../builtins/publish-targets/cloudflare/index.js';
2
2
  import { DockerPublishService, dockerPublishTarget } from '../builtins/publish-targets/docker/index.js';
3
+ import { SpmPublishService, spmPublishTarget } from '../builtins/publish-targets/spm/index.js';
3
4
  import { ChangelogService } from '../services/changelog.service.js';
4
5
  import { ConfigService } from '../services/config-service.js';
5
6
  import { ExecService } from '../services/exec.service.js';
@@ -101,10 +102,12 @@ export class RmanApplication {
101
102
  this.setService('import', a => new ImportService(a));
102
103
  this.setService('list', a => new ListService(a));
103
104
  this.setService('run', a => new RunService(a));
105
+ this.setService('spmPublish', a => new SpmPublishService(a));
104
106
  this.setService('version', a => new VersionService(a));
105
107
  // Register core targets.
106
108
  this.publishTargets.add(dockerPublishTarget);
107
109
  this.publishTargets.add(cloudflarePublishTarget);
110
+ this.publishTargets.add(spmPublishTarget);
108
111
  }
109
112
  /** Which platform claims a directory - the first whose manifest provider recognizes it, because
110
113
  * before a package is read there is nothing else to go on. `basePlatform` when none does, so the
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rman",
3
3
  "description": "Repository manager",
4
- "version": "2.31.0",
4
+ "version": "2.32.0",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
@@ -14,7 +14,7 @@
14
14
  "fast-glob": "^3.3.3",
15
15
  "fast-tokenizer": "^1.10.1",
16
16
  "ini": "^7.0.0",
17
- "js-yaml": "^5.4.1",
17
+ "js-yaml": "^5.5.0",
18
18
  "micromatch": "^4.0.8",
19
19
  "power-tasks": "^1.14.2",
20
20
  "semver": "^7.8.5",