rman 0.37.4 → 1.0.1
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 +364 -21
- package/cli.d.ts +1 -0
- package/cli.js +52 -28
- package/commands/build.command.d.ts +3 -0
- package/commands/build.command.js +14 -0
- package/commands/changed.command.d.ts +3 -0
- package/commands/changed.command.js +29 -0
- package/commands/changelog.command.d.ts +3 -0
- package/commands/changelog.command.js +63 -0
- package/commands/ci.command.d.ts +3 -0
- package/commands/ci.command.js +30 -0
- package/commands/clean.command.d.ts +3 -0
- package/commands/clean.command.js +36 -0
- package/commands/diff.command.d.ts +3 -0
- package/commands/diff.command.js +51 -0
- package/commands/exec.command.d.ts +3 -0
- package/commands/exec.command.js +83 -0
- package/commands/import.command.d.ts +3 -0
- package/commands/import.command.js +27 -0
- package/commands/info.command.d.ts +3 -0
- package/commands/info.command.js +61 -0
- package/commands/list.command.d.ts +3 -0
- package/commands/list.command.js +113 -0
- package/commands/publish.command.d.ts +3 -0
- package/commands/publish.command.js +147 -0
- package/commands/run.command.d.ts +6 -0
- package/commands/run.command.js +80 -0
- package/commands/test.command.d.ts +3 -0
- package/commands/test.command.js +14 -0
- package/commands/version.command.d.ts +3 -0
- package/commands/version.command.js +125 -0
- package/constants.js +1 -1
- package/core/config.d.ts +13 -0
- package/core/config.js +61 -0
- package/core/package.d.ts +2 -0
- package/core/package.js +2 -0
- package/core/repository.d.ts +36 -3
- package/core/repository.js +90 -45
- package/index.d.ts +17 -1
- package/index.js +15 -1
- package/package.json +11 -8
- package/services/changelog.service.d.ts +99 -0
- package/services/changelog.service.js +272 -0
- package/services/ci.service.d.ts +40 -0
- package/services/ci.service.js +204 -0
- package/services/clean.service.d.ts +42 -0
- package/services/clean.service.js +226 -0
- package/services/exec.service.d.ts +35 -0
- package/services/exec.service.js +114 -0
- package/services/import.service.d.ts +30 -0
- package/services/import.service.js +84 -0
- package/services/list.service.d.ts +27 -0
- package/services/list.service.js +25 -0
- package/services/publish.service.d.ts +79 -0
- package/services/publish.service.js +199 -0
- package/services/run.service.d.ts +99 -0
- package/services/run.service.js +432 -0
- package/services/system-info.d.ts +16 -0
- package/services/system-info.js +25 -0
- package/services/version.service.d.ts +95 -0
- package/services/version.service.js +450 -0
- package/services.d.ts +10 -0
- package/services.js +10 -0
- package/utils/branch-guard.d.ts +25 -0
- package/utils/branch-guard.js +61 -0
- package/utils/change-hash.d.ts +55 -0
- package/utils/change-hash.js +80 -0
- package/utils/conventional-commits.d.ts +39 -0
- package/utils/conventional-commits.js +43 -0
- package/utils/exec.d.ts +5 -6
- package/utils/exec.js +46 -60
- package/utils/git.d.ts +86 -0
- package/utils/git.js +276 -0
- package/utils/live-region.d.ts +16 -0
- package/utils/live-region.js +59 -0
- package/utils/logger.d.ts +24 -0
- package/utils/logger.js +39 -0
- package/utils/npm-run-path.d.ts +1 -1
- package/utils/npm-run-path.js +10 -10
- package/utils/package-filter.d.ts +32 -0
- package/utils/package-filter.js +73 -0
- package/utils/progress-panel.d.ts +59 -0
- package/utils/progress-panel.js +149 -0
- package/utils/workspace-range.d.ts +17 -0
- package/utils/workspace-range.js +28 -0
- package/bin/rman.mjs +0 -4
- package/commands/build-command.d.ts +0 -11
- package/commands/build-command.js +0 -24
- package/commands/changed-command.d.ts +0 -16
- package/commands/changed-command.js +0 -32
- package/commands/ci-command.d.ts +0 -24
- package/commands/ci-command.js +0 -68
- package/commands/execute-command.d.ts +0 -19
- package/commands/execute-command.js +0 -72
- package/commands/info-command.d.ts +0 -10
- package/commands/info-command.js +0 -55
- package/commands/list-command.d.ts +0 -38
- package/commands/list-command.js +0 -134
- package/commands/multi-task-command.d.ts +0 -22
- package/commands/multi-task-command.js +0 -65
- package/commands/publish-command.d.ts +0 -24
- package/commands/publish-command.js +0 -131
- package/commands/run-command.d.ts +0 -28
- package/commands/run-command.js +0 -131
- package/commands/version-command.d.ts +0 -25
- package/commands/version-command.js +0 -195
- package/core/command.d.ts +0 -35
- package/core/command.js +0 -116
- package/core/constants.d.ts +0 -1
- package/core/constants.js +0 -2
- package/core/logger.d.ts +0 -12
- package/core/logger.js +0 -2
- package/utils/file-utils.d.ts +0 -4
- package/utils/file-utils.js +0 -28
- package/utils/git-utils.d.ts +0 -25
- package/utils/git-utils.js +0 -66
- package/utils/npm-utils.d.ts +0 -11
- package/utils/npm-utils.js +0 -33
- package/utils/package-not-found-error.d.ts +0 -2
- package/utils/package-not-found-error.js +0 -2
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import colors from 'ansi-colors';
|
|
4
|
+
import fg from 'fast-glob';
|
|
5
|
+
import { Logger, resolveRootLogLevel } from '../utils/logger.js';
|
|
6
|
+
import { filterPackages } from '../utils/package-filter.js';
|
|
7
|
+
import { ProgressPanel } from '../utils/progress-panel.js';
|
|
8
|
+
export var CleanService;
|
|
9
|
+
(function (CleanService) {
|
|
10
|
+
/**
|
|
11
|
+
* `clean`: removes build output across every package (root included) - the built-in replacement
|
|
12
|
+
* for `ts-cleanup`, plus whatever else `.rmanrc clean.include`/`clean.exclude` says to remove.
|
|
13
|
+
*
|
|
14
|
+
* For every package not opted out via its own (cascaded) `clean.skip: true`:
|
|
15
|
+
* - deletes compiled `.js`/`.js.map`/`.d.ts` files under its `src`/`test` (see `cleanTsArtifacts`);
|
|
16
|
+
* - deletes any `*.tsbuildinfo` incremental-build cache file anywhere in it;
|
|
17
|
+
* - deletes anything matching its own (cascaded) `clean.include` glob(s), minus `clean.exclude`.
|
|
18
|
+
*
|
|
19
|
+
* `include`/`exclude` are resolved relative to *that* package's own directory - a root-level
|
|
20
|
+
* `.rmanrc` pattern like `packages/*\/build` is naturally evaluated from the repository root
|
|
21
|
+
* (spanning every package in one pass), while a package's own override in its own `.rmanrc`
|
|
22
|
+
* (e.g. `include: ./cache`) only ever reaches that one package, since a package normally
|
|
23
|
+
* overrides rather than merges with the root's value for the same key.
|
|
24
|
+
*
|
|
25
|
+
* Never touches `node_modules` - that's `ci`'s job, not this one.
|
|
26
|
+
*
|
|
27
|
+
* Run from inside a single package's own directory, it only cleans that package unless
|
|
28
|
+
* `options.root` says otherwise (see `Repository.currentPackage`).
|
|
29
|
+
*
|
|
30
|
+
* Uses the same live progress panel as `run`/`build` (see `../utils/progress-panel.ts`) - unlike
|
|
31
|
+
* `ci`, cleaning genuinely is independent per-package work, so the final per-package tally stays.
|
|
32
|
+
*/
|
|
33
|
+
async function clean(repository, options = {}) {
|
|
34
|
+
/** Standing inside a single package's own directory scopes cleaning to just that package
|
|
35
|
+
* (root's own artifacts included) unless `--root` asks for the whole repository anyway - a
|
|
36
|
+
* no-op when already at the root, or outside any known package. */
|
|
37
|
+
const cwdScope = options.root ? undefined : repository.currentPackage;
|
|
38
|
+
const packages = repository.getPackages().filter(p => p !== repository.rootPackage);
|
|
39
|
+
const allTargets = cwdScope ? [cwdScope] : [repository.rootPackage, ...packages];
|
|
40
|
+
const targets = filterPackages(allTargets, options).filter(pkg => !cleanConfig(pkg).skip);
|
|
41
|
+
const dryRun = options.dryRun ?? false;
|
|
42
|
+
const progress = options.progress ?? true;
|
|
43
|
+
const logger = new Logger(options.logLevel ?? resolveRootLogLevel(repository));
|
|
44
|
+
const panel = new ProgressPanel(dryRun ? 'CLEAN (dry-run)' : 'CLEAN', !!process.stdout.isTTY && progress);
|
|
45
|
+
panel.start();
|
|
46
|
+
let failed = false;
|
|
47
|
+
try {
|
|
48
|
+
const results = await Promise.allSettled(targets.map(pkg => {
|
|
49
|
+
const label = pkg === repository.rootPackage ? 'root' : pkg.name;
|
|
50
|
+
return cleanPackage(pkg, panel.addItem(label), panel.enabled, dryRun, logger);
|
|
51
|
+
}));
|
|
52
|
+
if (results.some(r => r.status === 'rejected'))
|
|
53
|
+
failed = true;
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
panel.stop();
|
|
57
|
+
}
|
|
58
|
+
panel.printSummary();
|
|
59
|
+
if (failed) {
|
|
60
|
+
const err = new Error('"clean" failed');
|
|
61
|
+
err.logged = true;
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
CleanService.clean = clean;
|
|
66
|
+
})(CleanService || (CleanService = {}));
|
|
67
|
+
/** Directories a package's TypeScript output gets cleaned from, mirroring `ts-cleanup -s src`
|
|
68
|
+
* and `-s test` (the only way it was ever actually invoked in this project's own scripts). */
|
|
69
|
+
const TS_SOURCE_DIRS = ['src', 'test'];
|
|
70
|
+
/** A package's (cascaded) `.rmanrc clean.include`/`clean.exclude`/`clean.skip` - a package that
|
|
71
|
+
* declares its own `clean` block replaces the root's entirely for itself (same as any other
|
|
72
|
+
* rman config key), rather than combining with it. `include`/`exclude` accept a single glob
|
|
73
|
+
* string or an array of them. */
|
|
74
|
+
function cleanConfig(pkg) {
|
|
75
|
+
const cfg = pkg.config?.clean;
|
|
76
|
+
const normalize = (v) => {
|
|
77
|
+
if (Array.isArray(v))
|
|
78
|
+
return v.map(String);
|
|
79
|
+
return typeof v === 'string' && v ? [v] : [];
|
|
80
|
+
};
|
|
81
|
+
return { include: normalize(cfg?.include), exclude: normalize(cfg?.exclude), skip: cfg?.skip === true };
|
|
82
|
+
}
|
|
83
|
+
/** Deletes `file`, unless `dryRun` - either way the caller treats it as removed for reporting. */
|
|
84
|
+
async function remove(file, dryRun) {
|
|
85
|
+
if (!dryRun)
|
|
86
|
+
await fs.promises.rm(file, { force: true });
|
|
87
|
+
}
|
|
88
|
+
/** Removes now-empty directories under `dir`, deepest first (so a parent left empty once its
|
|
89
|
+
* only child is removed gets caught in the same pass). Also removes `dir` itself once empty
|
|
90
|
+
* when `includeSelf` is set - used when `dir` is itself something the user asked to delete
|
|
91
|
+
* (e.g. an `include: build` match), as opposed to a source root like `src`/`test` that should
|
|
92
|
+
* stay even if everything inside it was cleaned out. No-op in dry-run mode: nothing was actually
|
|
93
|
+
* deleted, so there's nothing that could have been left empty. */
|
|
94
|
+
function pruneEmptyDirs(dir, includeSelf, dryRun) {
|
|
95
|
+
if (dryRun || !fs.existsSync(dir))
|
|
96
|
+
return;
|
|
97
|
+
const entries = fg.sync('**', { cwd: dir, onlyDirectories: true, dot: true });
|
|
98
|
+
entries.sort((a, b) => b.split(path.sep).length - a.split(path.sep).length);
|
|
99
|
+
for (const rel of entries) {
|
|
100
|
+
const abs = path.join(dir, rel);
|
|
101
|
+
if (fs.existsSync(abs) && fs.readdirSync(abs).length === 0)
|
|
102
|
+
fs.rmdirSync(abs);
|
|
103
|
+
}
|
|
104
|
+
if (includeSelf && fs.readdirSync(dir).length === 0)
|
|
105
|
+
fs.rmdirSync(dir);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Deletes every path matched by `include` (minus `exclude`) under `dirname` - files and
|
|
109
|
+
* directories alike, gulp-`src()`-style. Returns what was actually removed (or, in dry-run mode,
|
|
110
|
+
* what *would* be), relative to `dirname`, so the caller only logs what really happened.
|
|
111
|
+
*
|
|
112
|
+
* `exclude` protects at two levels: a pattern matching a whole `include` result directly (e.g.
|
|
113
|
+
* `packages/pkg1/*` against a matched `packages/pkg1/build` directory) drops that result before
|
|
114
|
+
* anything under it is touched; a finer pattern (e.g. `build/*.json`) instead protects just the
|
|
115
|
+
* matching files *inside* an otherwise-deleted directory, leaving the rest of it gone and the
|
|
116
|
+
* protected files (and their now non-empty parent) in place.
|
|
117
|
+
*/
|
|
118
|
+
async function cleanGlobs(dirname, include, exclude, dryRun) {
|
|
119
|
+
if (!include.length)
|
|
120
|
+
return [];
|
|
121
|
+
const matches = await fg(include, { cwd: dirname, ignore: exclude, onlyFiles: false, dot: true, absolute: true });
|
|
122
|
+
const protectedFiles = exclude.length
|
|
123
|
+
? new Set(await fg(exclude, { cwd: dirname, onlyFiles: true, dot: true, absolute: true }))
|
|
124
|
+
: new Set();
|
|
125
|
+
const removed = [];
|
|
126
|
+
for (const m of matches) {
|
|
127
|
+
const stat = await fs.promises.stat(m).catch(() => undefined);
|
|
128
|
+
if (!stat)
|
|
129
|
+
continue;
|
|
130
|
+
if (stat.isDirectory()) {
|
|
131
|
+
const filesInside = await fg('**', { cwd: m, onlyFiles: true, dot: true, absolute: true });
|
|
132
|
+
for (const f of filesInside) {
|
|
133
|
+
if (protectedFiles.has(f))
|
|
134
|
+
continue;
|
|
135
|
+
await remove(f, dryRun);
|
|
136
|
+
removed.push(path.relative(dirname, f));
|
|
137
|
+
}
|
|
138
|
+
pruneEmptyDirs(m, true, dryRun);
|
|
139
|
+
}
|
|
140
|
+
else if (!protectedFiles.has(m)) {
|
|
141
|
+
await remove(m, dryRun);
|
|
142
|
+
removed.push(path.relative(dirname, m));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return removed;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Removes compiled TypeScript output (`.js`, `.js.map`, `.d.ts`) sitting next to its `.ts`
|
|
149
|
+
* source under `src`/`test` - the same job `ts-cleanup -s <dir> --all` did (the only mode this
|
|
150
|
+
* project ever actually used it in). A `.d.ts` with no matching `.ts`/`.tsx` is left alone - it's
|
|
151
|
+
* presumably a hand-written declaration file, not build output. Prunes directories left empty
|
|
152
|
+
* afterward.
|
|
153
|
+
*/
|
|
154
|
+
async function cleanTsArtifacts(dirname, dryRun) {
|
|
155
|
+
const removed = [];
|
|
156
|
+
for (const sub of TS_SOURCE_DIRS) {
|
|
157
|
+
const dir = path.join(dirname, sub);
|
|
158
|
+
if (!fs.existsSync(dir))
|
|
159
|
+
continue;
|
|
160
|
+
const files = await fg('**/*.{js,js.map,d.ts}', { cwd: dir, onlyFiles: true, dot: true, absolute: true });
|
|
161
|
+
for (const f of files) {
|
|
162
|
+
if (f.endsWith('.d.ts')) {
|
|
163
|
+
const base = f.slice(0, -'.d.ts'.length);
|
|
164
|
+
if (!fs.existsSync(base + '.ts') && !fs.existsSync(base + '.tsx'))
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
await remove(f, dryRun);
|
|
168
|
+
removed.push(path.relative(dirname, f));
|
|
169
|
+
}
|
|
170
|
+
pruneEmptyDirs(dir, false, dryRun);
|
|
171
|
+
}
|
|
172
|
+
return removed;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Removes TypeScript's incremental-build cache files (`*.tsbuildinfo`) anywhere under `dirname` -
|
|
176
|
+
* `tsc --build`/`composite`/`incremental` output that `ts-cleanup` never touched, but which can
|
|
177
|
+
* leave a project in a stale state if a clean rebuild is expected to start from nothing. Skips
|
|
178
|
+
* `node_modules` (dependencies may ship their own, irrelevant to this package's own build).
|
|
179
|
+
*/
|
|
180
|
+
async function cleanTsBuildInfo(dirname, dryRun) {
|
|
181
|
+
const files = await fg('**/*.tsbuildinfo', {
|
|
182
|
+
cwd: dirname,
|
|
183
|
+
ignore: ['**/node_modules/**'],
|
|
184
|
+
onlyFiles: true,
|
|
185
|
+
dot: true,
|
|
186
|
+
absolute: true,
|
|
187
|
+
});
|
|
188
|
+
for (const f of files)
|
|
189
|
+
await remove(f, dryRun);
|
|
190
|
+
return files.map(f => path.relative(dirname, f));
|
|
191
|
+
}
|
|
192
|
+
async function cleanPackage(pkg, item, panelEnabled, dryRun, logger) {
|
|
193
|
+
item.status = 'running';
|
|
194
|
+
item.startedAt = Date.now();
|
|
195
|
+
try {
|
|
196
|
+
item.currentStep = 'ts';
|
|
197
|
+
const tsRemoved = await cleanTsArtifacts(pkg.dirname, dryRun);
|
|
198
|
+
const buildInfoRemoved = await cleanTsBuildInfo(pkg.dirname, dryRun);
|
|
199
|
+
item.currentStep = 'glob';
|
|
200
|
+
const { include, exclude } = cleanConfig(pkg);
|
|
201
|
+
const globRemoved = await cleanGlobs(pkg.dirname, include, exclude, dryRun);
|
|
202
|
+
const removed = [...tsRemoved, ...buildInfoRemoved, ...globRemoved];
|
|
203
|
+
const verb = dryRun ? 'would rm' : 'rm';
|
|
204
|
+
if (panelEnabled) {
|
|
205
|
+
item.lastLine = removed.length
|
|
206
|
+
? `${dryRun ? 'would remove' : 'removed'} ${removed.length} item(s)`
|
|
207
|
+
: 'already clean';
|
|
208
|
+
}
|
|
209
|
+
else if (removed.length) {
|
|
210
|
+
for (const r of removed)
|
|
211
|
+
logger.info(colors.yellow(verb), colors.cyan(item.name), r);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
logger.info(colors.gray('clean'), colors.cyan(item.name));
|
|
215
|
+
}
|
|
216
|
+
item.status = 'success';
|
|
217
|
+
}
|
|
218
|
+
catch (e) {
|
|
219
|
+
item.status = 'failed';
|
|
220
|
+
item.log.push(e?.message ?? String(e));
|
|
221
|
+
throw e;
|
|
222
|
+
}
|
|
223
|
+
finally {
|
|
224
|
+
item.finishedAt = Date.now();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Repository } from '../core/repository.js';
|
|
2
|
+
import { type LogLevel } from '../utils/logger.js';
|
|
3
|
+
import { type PackageFilterOptions } from '../utils/package-filter.js';
|
|
4
|
+
export declare namespace ExecService {
|
|
5
|
+
interface Options extends PackageFilterOptions {
|
|
6
|
+
/** Max packages at once: `true`/omitted = CPU count, a number = that many, `false` = serial (1). */
|
|
7
|
+
parallel?: boolean | number;
|
|
8
|
+
/** Respect the package dependency graph: a package waits for its dependencies and is skipped
|
|
9
|
+
* if one fails. Default true. Set false to run in every matching package independently,
|
|
10
|
+
* alphabetically, regardless of the dependency graph. */
|
|
11
|
+
topo?: boolean;
|
|
12
|
+
bail?: boolean;
|
|
13
|
+
changed?: boolean;
|
|
14
|
+
changedSince?: string;
|
|
15
|
+
/** Show the live progress panel. Default true; auto-disabled when stdout isn't a TTY. */
|
|
16
|
+
progress?: boolean;
|
|
17
|
+
/** Verbosity of the classic per-package log (only applies when the live panel is off). Falls
|
|
18
|
+
* back to the root's `.rmanrc logLevel`, then 'info' - see `resolveRootLogLevel`. */
|
|
19
|
+
logLevel?: LogLevel;
|
|
20
|
+
/** Run across the whole repository even when the current directory is inside a single package
|
|
21
|
+
* (which otherwise scopes the run to just that package). Has no effect when already at the
|
|
22
|
+
* repository root, or outside any known package. */
|
|
23
|
+
root?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* `exec`: runs `command` (an arbitrary shell command, not an npm script) directly in every
|
|
27
|
+
* matching package's own directory - unlike `run`, there's no `package.json` script to resolve
|
|
28
|
+
* and no `pre<script>`/`post<script>` npm lifecycle convention to honor, since the command isn't
|
|
29
|
+
* tied to any script name at all. Everything else about how packages are selected and scheduled
|
|
30
|
+
* matches `run`: topological order and per-package bail by default, the same live progress panel
|
|
31
|
+
* (falling back to a classic one-line-per-package log when it's off), and the same
|
|
32
|
+
* `--scope`/`--ignore`/`--deps`/`--dependents`/`--changed` package filtering.
|
|
33
|
+
*/
|
|
34
|
+
function exec(repository: Repository, command: string, options?: Options): Promise<void>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import colors from 'ansi-colors';
|
|
3
|
+
import { Task } from 'power-tasks';
|
|
4
|
+
import { exec as execCommand } from '../utils/exec.js';
|
|
5
|
+
import { Logger, resolveRootLogLevel } from '../utils/logger.js';
|
|
6
|
+
import { filterPackages } from '../utils/package-filter.js';
|
|
7
|
+
import { ProgressPanel } from '../utils/progress-panel.js';
|
|
8
|
+
export var ExecService;
|
|
9
|
+
(function (ExecService) {
|
|
10
|
+
/**
|
|
11
|
+
* `exec`: runs `command` (an arbitrary shell command, not an npm script) directly in every
|
|
12
|
+
* matching package's own directory - unlike `run`, there's no `package.json` script to resolve
|
|
13
|
+
* and no `pre<script>`/`post<script>` npm lifecycle convention to honor, since the command isn't
|
|
14
|
+
* tied to any script name at all. Everything else about how packages are selected and scheduled
|
|
15
|
+
* matches `run`: topological order and per-package bail by default, the same live progress panel
|
|
16
|
+
* (falling back to a classic one-line-per-package log when it's off), and the same
|
|
17
|
+
* `--scope`/`--ignore`/`--deps`/`--dependents`/`--changed` package filtering.
|
|
18
|
+
*/
|
|
19
|
+
async function exec(repository, command, options = {}) {
|
|
20
|
+
const logLevelDefault = resolveRootLogLevel(repository);
|
|
21
|
+
const logger = new Logger(options.logLevel ?? logLevelDefault);
|
|
22
|
+
const cwdScope = options.root ? undefined : repository.currentPackage;
|
|
23
|
+
const topo = options.topo ?? true;
|
|
24
|
+
let packages = repository.getPackages({ toposort: topo, scope: cwdScope?.name });
|
|
25
|
+
if (!topo)
|
|
26
|
+
packages = [...packages].sort((a, b) => a.name.localeCompare(b.name));
|
|
27
|
+
packages = filterPackages(packages, options);
|
|
28
|
+
if (options.changed || options.changedSince) {
|
|
29
|
+
const status = await repository.listStatus({ hash: options.changedSince });
|
|
30
|
+
packages = packages.filter(p => status[p.name] !== 'clean');
|
|
31
|
+
}
|
|
32
|
+
if (!packages.length) {
|
|
33
|
+
logger.info(colors.gray('No package matched.'));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const concurrency = options.parallel === false
|
|
37
|
+
? 1
|
|
38
|
+
: typeof options.parallel === 'number'
|
|
39
|
+
? options.parallel
|
|
40
|
+
: options.parallel === true
|
|
41
|
+
? os.cpus().length
|
|
42
|
+
: os.cpus().length;
|
|
43
|
+
const progress = options.progress ?? true;
|
|
44
|
+
const panel = new ProgressPanel('EXEC', !!process.stdout.isTTY && progress);
|
|
45
|
+
const bail = options.bail ?? true;
|
|
46
|
+
let rootTask;
|
|
47
|
+
const names = new Set(packages.map(p => p.name));
|
|
48
|
+
const children = packages.map(pkg => {
|
|
49
|
+
const ctx = panel.addItem(pkg.name);
|
|
50
|
+
const dependencies = topo ? pkg.dependencies.filter(d => names.has(d)) : [];
|
|
51
|
+
return new Task(() => execForPackage(ctx, panel.enabled, pkg.dirname, command, logger, bail ? () => rootTask?.abort() : () => { }), { name: pkg.name, dependencies });
|
|
52
|
+
});
|
|
53
|
+
panel.start();
|
|
54
|
+
let failed = false;
|
|
55
|
+
try {
|
|
56
|
+
/** bail:false here - each item's own task decides whether to call `rootTask.abort()`
|
|
57
|
+
* itself (see `execForPackage`), mirroring `run`'s own reasoning: power-tasks' own `bail`
|
|
58
|
+
* is a single blanket policy, it can't be conditional on `options.bail`. `toPromise()`
|
|
59
|
+
* still rejects on any child's failure regardless, whether or not it triggered an abort. */
|
|
60
|
+
rootTask = new Task(children, { concurrency, bail: false });
|
|
61
|
+
await rootTask.toPromise();
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
failed = true;
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
panel.stop();
|
|
68
|
+
}
|
|
69
|
+
panel.printSummary();
|
|
70
|
+
if (failed) {
|
|
71
|
+
const err = new Error('"exec" failed');
|
|
72
|
+
err.logged = true;
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
ExecService.exec = exec;
|
|
77
|
+
})(ExecService || (ExecService = {}));
|
|
78
|
+
async function execForPackage(ctx, panelEnabled, cwd, command, logger, abort) {
|
|
79
|
+
ctx.status = 'running';
|
|
80
|
+
ctx.startedAt = Date.now();
|
|
81
|
+
try {
|
|
82
|
+
if (panelEnabled) {
|
|
83
|
+
await execCommand(command, {
|
|
84
|
+
cwd,
|
|
85
|
+
stdio: 'pipe',
|
|
86
|
+
onLine: line => {
|
|
87
|
+
ctx.log.push(line);
|
|
88
|
+
ctx.lastLine = line;
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
logger.info(colors.cyan('exec'), colors.cyan(ctx.name), command);
|
|
94
|
+
const t = Date.now();
|
|
95
|
+
try {
|
|
96
|
+
await execCommand(command, { cwd, stdio: 'inherit' });
|
|
97
|
+
logger.info(colors.green('success'), colors.cyan(ctx.name), colors.yellow(`(${Date.now() - t} ms)`));
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
logger.error(colors.red('failed'), colors.cyan(ctx.name), colors.yellow(`(${Date.now() - t} ms)`));
|
|
101
|
+
throw e;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
ctx.status = 'success';
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
ctx.status = 'failed';
|
|
108
|
+
abort();
|
|
109
|
+
throw e;
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
ctx.finishedAt = Date.now();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Repository } from '../core/repository.js';
|
|
2
|
+
export declare namespace ImportService {
|
|
3
|
+
interface Options {
|
|
4
|
+
/** Subdirectory (relative to the repository root) the new package is placed under - default `'packages'`. */
|
|
5
|
+
dest?: string;
|
|
6
|
+
}
|
|
7
|
+
interface Result {
|
|
8
|
+
/** The imported package's name (from its own `package.json`, or its directory basename). */
|
|
9
|
+
name: string;
|
|
10
|
+
/** Where it was placed, absolute. */
|
|
11
|
+
targetDir: string;
|
|
12
|
+
commitCount: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Imports `sourcePath` (a local clone of some other git repository) as a new package under this
|
|
16
|
+
* repository, preserving its **entire commit history** - every original commit, author, date and
|
|
17
|
+
* message stays intact, just as if the package had always lived at its new path. Unlike copying
|
|
18
|
+
* the files over and making one "import" commit, `git blame`/`git log --follow` keep working on
|
|
19
|
+
* the imported files afterward.
|
|
20
|
+
*
|
|
21
|
+
* How: every commit reachable from `sourcePath`'s HEAD is turned into a patch (`git format-patch
|
|
22
|
+
* --root`, oldest first), each patch's file paths are rewritten to be prefixed with the new
|
|
23
|
+
* subdirectory, then replayed onto this repository via `git am` (preserving authorship). A
|
|
24
|
+
* source repo with merge commits or binary-file renames can still trip up a patch here or there -
|
|
25
|
+
* same caveat `lerna import` has, since both use this same replay approach rather than a real
|
|
26
|
+
* merge (which real `git subtree` would give, at the cost of far less predictable behavior
|
|
27
|
+
* across git versions - the reason this - and lerna - avoid it).
|
|
28
|
+
*/
|
|
29
|
+
function importRepo(repository: Repository, sourcePath: string, options?: Options): Promise<Result>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { GitHelper } from '../utils/git.js';
|
|
5
|
+
export var ImportService;
|
|
6
|
+
(function (ImportService) {
|
|
7
|
+
/**
|
|
8
|
+
* Imports `sourcePath` (a local clone of some other git repository) as a new package under this
|
|
9
|
+
* repository, preserving its **entire commit history** - every original commit, author, date and
|
|
10
|
+
* message stays intact, just as if the package had always lived at its new path. Unlike copying
|
|
11
|
+
* the files over and making one "import" commit, `git blame`/`git log --follow` keep working on
|
|
12
|
+
* the imported files afterward.
|
|
13
|
+
*
|
|
14
|
+
* How: every commit reachable from `sourcePath`'s HEAD is turned into a patch (`git format-patch
|
|
15
|
+
* --root`, oldest first), each patch's file paths are rewritten to be prefixed with the new
|
|
16
|
+
* subdirectory, then replayed onto this repository via `git am` (preserving authorship). A
|
|
17
|
+
* source repo with merge commits or binary-file renames can still trip up a patch here or there -
|
|
18
|
+
* same caveat `lerna import` has, since both use this same replay approach rather than a real
|
|
19
|
+
* merge (which real `git subtree` would give, at the cost of far less predictable behavior
|
|
20
|
+
* across git versions - the reason this - and lerna - avoid it).
|
|
21
|
+
*/
|
|
22
|
+
async function importRepo(repository, sourcePath, options = {}) {
|
|
23
|
+
const absSource = path.resolve(sourcePath);
|
|
24
|
+
if (!fs.existsSync(path.join(absSource, '.git'))) {
|
|
25
|
+
throw new Error(`"${sourcePath}" is not a git repository (no .git found) - clone it locally first`);
|
|
26
|
+
}
|
|
27
|
+
const name = resolveSourceName(absSource);
|
|
28
|
+
const dirName = name.includes('/') ? name.slice(name.lastIndexOf('/') + 1) : name;
|
|
29
|
+
const destDir = options.dest ?? 'packages';
|
|
30
|
+
const targetDir = path.join(repository.dirname, destDir, dirName);
|
|
31
|
+
if (fs.existsSync(targetDir)) {
|
|
32
|
+
throw new Error(`"${path.relative(repository.dirname, targetDir)}" already exists`);
|
|
33
|
+
}
|
|
34
|
+
const sourceGit = new GitHelper({ cwd: absSource });
|
|
35
|
+
const patchDir = fs.mkdtempSync(path.join(os.tmpdir(), 'rman-import-'));
|
|
36
|
+
try {
|
|
37
|
+
const patches = await sourceGit.formatPatches(patchDir);
|
|
38
|
+
if (!patches.length)
|
|
39
|
+
throw new Error(`"${sourcePath}" has no commits to import`);
|
|
40
|
+
const relTargetDir = path.relative(repository.dirname, targetDir).split(path.sep).join('/');
|
|
41
|
+
for (const patchFile of patches)
|
|
42
|
+
rewritePatchPaths(patchFile, relTargetDir);
|
|
43
|
+
const targetGit = new GitHelper({ cwd: repository.dirname });
|
|
44
|
+
await targetGit.applyPatches(patches);
|
|
45
|
+
return { name, targetDir, commitCount: patches.length };
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
fs.rmSync(patchDir, { recursive: true, force: true });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
ImportService.importRepo = importRepo;
|
|
52
|
+
})(ImportService || (ImportService = {}));
|
|
53
|
+
/** The source repo's own `package.json` name, or its directory's basename when it has none (or
|
|
54
|
+
* isn't even a package yet - a plain library repo being folded in). */
|
|
55
|
+
function resolveSourceName(absSource) {
|
|
56
|
+
const pkgJsonFile = path.join(absSource, 'package.json');
|
|
57
|
+
if (fs.existsSync(pkgJsonFile)) {
|
|
58
|
+
try {
|
|
59
|
+
const json = JSON.parse(fs.readFileSync(pkgJsonFile, 'utf-8'));
|
|
60
|
+
if (typeof json.name === 'string' && json.name)
|
|
61
|
+
return json.name;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// malformed package.json - fall through to the directory name below.
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return path.basename(absSource);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Rewrites a `git format-patch`-produced file in place so every path it touches is prefixed with
|
|
71
|
+
* `prefix` (the new subdirectory, forward-slash-separated) - `diff --git`/`---`/`+++`/rename
|
|
72
|
+
* headers only; `--- /dev/null`/`+++ /dev/null` (new/deleted file markers) never match, since they
|
|
73
|
+
* have no `a/`/`b/` prefix to rewrite in the first place.
|
|
74
|
+
*/
|
|
75
|
+
function rewritePatchPaths(patchFile, prefix) {
|
|
76
|
+
const content = fs.readFileSync(patchFile, 'utf-8');
|
|
77
|
+
const rewritten = content
|
|
78
|
+
.replace(/^diff --git a\/(.+?) b\/(.+)$/gm, (_m, a, b) => `diff --git a/${prefix}/${a} b/${prefix}/${b}`)
|
|
79
|
+
.replace(/^--- a\/(.+)$/gm, (_m, p) => `--- a/${prefix}/${p}`)
|
|
80
|
+
.replace(/^\+\+\+ b\/(.+)$/gm, (_m, p) => `+++ b/${prefix}/${p}`)
|
|
81
|
+
.replace(/^rename from (.+)$/gm, (_m, p) => `rename from ${prefix}/${p}`)
|
|
82
|
+
.replace(/^rename to (.+)$/gm, (_m, p) => `rename to ${prefix}/${p}`);
|
|
83
|
+
fs.writeFileSync(patchFile, rewritten);
|
|
84
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Repository } from '../core/repository.js';
|
|
2
|
+
import { type PackageFilterOptions } from '../utils/package-filter.js';
|
|
3
|
+
export declare namespace ListService {
|
|
4
|
+
interface Options extends PackageFilterOptions {
|
|
5
|
+
/** Topological order (dependencies before dependents) instead of lexical by directory. */
|
|
6
|
+
toposort?: boolean;
|
|
7
|
+
/** Only include packages that have changed since the last publish (dirty or committed but
|
|
8
|
+
* not yet published) - or, with `changedSince`, since that specific commit/hash. */
|
|
9
|
+
changed?: boolean;
|
|
10
|
+
changedSince?: string;
|
|
11
|
+
}
|
|
12
|
+
interface Item {
|
|
13
|
+
name: string;
|
|
14
|
+
version: string;
|
|
15
|
+
location: string;
|
|
16
|
+
private: boolean;
|
|
17
|
+
status: Repository.PackageStatus;
|
|
18
|
+
/** In-repo package names this one depends on - enough to build a dependency graph without a
|
|
19
|
+
* second call, e.g. `Object.fromEntries(items.map(i => [i.name, i.dependencies]))`. */
|
|
20
|
+
dependencies: string[];
|
|
21
|
+
}
|
|
22
|
+
/** `list`: every package in the repository (or, with `changed`/`changedSince`, only the ones
|
|
23
|
+
* that have actually changed), each with its version, location, private flag, and change
|
|
24
|
+
* status relative to upstream. Pure data - no console output; `rman list`'s own command
|
|
25
|
+
* decides how to present it (table, JSON, parseable, names only, or a dependency graph). */
|
|
26
|
+
function getPackages(repository: Repository, options?: Options): Promise<Item[]>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { filterPackages } from '../utils/package-filter.js';
|
|
3
|
+
export var ListService;
|
|
4
|
+
(function (ListService) {
|
|
5
|
+
/** `list`: every package in the repository (or, with `changed`/`changedSince`, only the ones
|
|
6
|
+
* that have actually changed), each with its version, location, private flag, and change
|
|
7
|
+
* status relative to upstream. Pure data - no console output; `rman list`'s own command
|
|
8
|
+
* decides how to present it (table, JSON, parseable, names only, or a dependency graph). */
|
|
9
|
+
async function getPackages(repository, options = {}) {
|
|
10
|
+
const packages = filterPackages(repository.getPackages({ toposort: options.toposort }), options);
|
|
11
|
+
const status = await repository.listStatus({ hash: options.changedSince });
|
|
12
|
+
let items = packages.map(p => ({
|
|
13
|
+
name: p.name,
|
|
14
|
+
version: p.version,
|
|
15
|
+
location: path.relative(repository.dirname, p.dirname) || '.',
|
|
16
|
+
private: p.isPrivate,
|
|
17
|
+
status: status[p.name],
|
|
18
|
+
dependencies: [...p.dependencies],
|
|
19
|
+
}));
|
|
20
|
+
if (options.changed || options.changedSince)
|
|
21
|
+
items = items.filter(it => it.status !== 'clean');
|
|
22
|
+
return items;
|
|
23
|
+
}
|
|
24
|
+
ListService.getPackages = getPackages;
|
|
25
|
+
})(ListService || (ListService = {}));
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Package } from '../core/package.js';
|
|
2
|
+
import type { Repository } from '../core/repository.js';
|
|
3
|
+
import { type PackageFilterOptions } from '../utils/package-filter.js';
|
|
4
|
+
import { CiService } from './ci.service.js';
|
|
5
|
+
export declare namespace PublishService {
|
|
6
|
+
/** Injectable registry lookup - mainly for tests, so they don't depend on network access or a
|
|
7
|
+
* real published package. Same shape as `detectChangeHash`'s own `npmViewVersion`. */
|
|
8
|
+
interface Deps {
|
|
9
|
+
npmViewVersion?: (name: string, cwd: string) => Promise<string | undefined>;
|
|
10
|
+
}
|
|
11
|
+
interface Options extends PackageFilterOptions {
|
|
12
|
+
/** A package with uncommitted local changes is excluded (status `'skip'`) instead of aborting
|
|
13
|
+
* the whole plan (status `'error'`) - same as `version`'s own option. Default false. */
|
|
14
|
+
ignoreDirty?: boolean;
|
|
15
|
+
/** Registry to check against (and, in `applyPlan`, publish to) - `.npmrc`'s own configured
|
|
16
|
+
* registry is used when omitted. */
|
|
17
|
+
registry?: string;
|
|
18
|
+
/** Path to a custom `.npmrc`, for both the registry check and the actual publish. */
|
|
19
|
+
userconfig?: string;
|
|
20
|
+
}
|
|
21
|
+
interface ApplyOptions extends Options {
|
|
22
|
+
packageManager?: CiService.PackageManager;
|
|
23
|
+
/** `npm publish --access <access>` - required by the registry for a *new* scoped package. */
|
|
24
|
+
access?: 'public' | 'restricted';
|
|
25
|
+
/** `npm publish --tag <tag>` - the dist-tag this version is published under (default `latest`). */
|
|
26
|
+
tag?: string;
|
|
27
|
+
/** `npm publish --otp <otp>` - a 2FA one-time password, for registries that require it. */
|
|
28
|
+
otp?: string;
|
|
29
|
+
/** Subdirectory to publish from, relative to the package's own directory - only consulted when
|
|
30
|
+
* the package doesn't already declare its own `package.json` `publishConfig.directory` (npm's
|
|
31
|
+
* native mechanism for this, which always wins when present). */
|
|
32
|
+
contents?: string;
|
|
33
|
+
}
|
|
34
|
+
/** One package's outcome in a publish plan - see `getPlan`. */
|
|
35
|
+
interface Entry {
|
|
36
|
+
package: Package;
|
|
37
|
+
version: string;
|
|
38
|
+
status: 'publish' | 'skip' | 'up-to-date' | 'error';
|
|
39
|
+
/** What's currently on the registry, if anything - only set once a registry check actually ran. */
|
|
40
|
+
registryVersion?: string;
|
|
41
|
+
reason?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Computes what `publish` *would* do, across every non-private package (topological order,
|
|
45
|
+
* dependencies before dependents) - never touches the registry to publish anything, just
|
|
46
|
+
* queries it to decide, so it's safe to call any time, including as the plan a bare `rman
|
|
47
|
+
* publish` shows before asking for confirmation.
|
|
48
|
+
*
|
|
49
|
+
* A `private: true` package is always `'skip'`ped outright. A package with uncommitted local
|
|
50
|
+
* changes is `'error'` (aborts the whole plan) unless `options.ignoreDirty` downgrades it to
|
|
51
|
+
* `'skip'` instead - same rule `version` uses, since publishing untracked local edits is worse
|
|
52
|
+
* than a bad commit. Otherwise, its currently-published registry version (via `npm view`,
|
|
53
|
+
* queried concurrently across every remaining package) decides the rest: identical to the local
|
|
54
|
+
* `package.json` version is `'up-to-date'`; anything else (including never having been
|
|
55
|
+
* published at all) is `'publish'`.
|
|
56
|
+
*
|
|
57
|
+
* Deliberately decoupled from `version`: this only ever looks at what's *currently* on disk and
|
|
58
|
+
* on the registry, never at whether `version` was just run - so it works equally well right
|
|
59
|
+
* after a version bump, or standing alone in a release pipeline that bumped days earlier.
|
|
60
|
+
*
|
|
61
|
+
* A monorepo's root package is never a candidate at all - `repository.getPackages()` already
|
|
62
|
+
* excludes it for a real monorepo (it only doubles as "the" package in a single-package repo,
|
|
63
|
+
* where it's a normal candidate like any other).
|
|
64
|
+
*/
|
|
65
|
+
function getPlan(repository: Repository, options?: Options, deps?: Deps): Promise<Entry[]>;
|
|
66
|
+
/**
|
|
67
|
+
* Publishes every `'publish'` entry in `plan`, topological order (already `plan`'s own order -
|
|
68
|
+
* see `getPlan`), via the configured `packageManager`'s own `publish` command. Sequential, not
|
|
69
|
+
* concurrent: unlike `run`/`build`, a package genuinely needs its own dependencies to have
|
|
70
|
+
* landed on the registry first, and publishing is rare enough (once per release) that the
|
|
71
|
+
* simplicity is worth more than the parallelism `run` gets from `power-tasks`.
|
|
72
|
+
*
|
|
73
|
+
* If a package fails, every other still-pending entry depending on it (transitively) is marked
|
|
74
|
+
* `'error'` too and never attempted - publishing a package whose own new dependency range points
|
|
75
|
+
* at a version that never actually reached the registry would hand consumers a broken install.
|
|
76
|
+
* A package's own failure doesn't stop unrelated packages elsewhere in the plan, though.
|
|
77
|
+
*/
|
|
78
|
+
function applyPlan(repository: Repository, plan: Entry[], options?: ApplyOptions): Promise<Entry[]>;
|
|
79
|
+
}
|