@tegami/dart 0.0.0-tegami-trusted-publish-setup → 1.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fuma Nama
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,72 @@
1
+ import { BumpType, TegamiPlugin, WorkspacePackage } from "tegami";
2
+ import z from "zod";
3
+
4
+ //#region src/schema.d.ts
5
+ declare const pubspecSchema: z.ZodObject<{
6
+ name: z.ZodOptional<z.ZodString>;
7
+ version: z.ZodOptional<z.ZodString>;
8
+ publish_to: z.ZodOptional<z.ZodString>;
9
+ resolution: z.ZodOptional<z.ZodString>;
10
+ workspace: z.ZodOptional<z.ZodArray<z.ZodString>>;
11
+ dependencies: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodType<DartDependency, unknown, z.core.$ZodTypeInternals<DartDependency, unknown>>>>;
12
+ dev_dependencies: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodType<DartDependency, unknown, z.core.$ZodTypeInternals<DartDependency, unknown>>>>;
13
+ dependency_overrides: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodType<DartDependency, unknown, z.core.$ZodTypeInternals<DartDependency, unknown>>>>;
14
+ }, z.core.$loose>;
15
+ type DartDependency = string | {
16
+ version?: string;
17
+ hosted?: string | {
18
+ name?: string;
19
+ url: string;
20
+ };
21
+ path?: string;
22
+ git?: unknown;
23
+ sdk?: string;
24
+ [key: string]: unknown;
25
+ };
26
+ type Pubspec = z.infer<typeof pubspecSchema>;
27
+ //#endregion
28
+ //#region src/index.d.ts
29
+ declare const DEP_FIELDS: readonly ["dependencies", "dev_dependencies", "dependency_overrides"];
30
+ interface PubspecFile {
31
+ path: string;
32
+ data: Pubspec;
33
+ }
34
+ declare class DartPackage extends WorkspacePackage {
35
+ readonly path: string;
36
+ readonly file: PubspecFile;
37
+ readonly manager = "dart";
38
+ constructor(path: string, file: PubspecFile);
39
+ get name(): string;
40
+ get version(): string | undefined;
41
+ get publishTo(): string | undefined;
42
+ setVersion(version: string): void;
43
+ write(): Promise<void>;
44
+ }
45
+ interface DependentRef {
46
+ dependent: DartPackage;
47
+ kind: (typeof DEP_FIELDS)[number];
48
+ name: string;
49
+ spec: DartDependency;
50
+ table: Record<string, DartDependency>;
51
+ linked: DartPackage;
52
+ }
53
+ interface DartPluginOptions {
54
+ /**
55
+ * Update the pub workspace resolution after versioning.
56
+ *
57
+ * @default true
58
+ */
59
+ updateLockFile?: boolean;
60
+ /**
61
+ * Decide how to bump the dependents of a bumped package.
62
+ */
63
+ bumpDep?: (opts: DependentRef) => BumpType | false;
64
+ }
65
+ declare function dart({
66
+ updateLockFile,
67
+ bumpDep: getBumpDepType
68
+ }?: DartPluginOptions): TegamiPlugin;
69
+ declare function updateConstraintRange(range: string, version: string): string;
70
+ declare function isPackagePublished(name: string, version: string, hostedUrl: string): Promise<boolean>;
71
+ //#endregion
72
+ export { DartPackage, DartPluginOptions, dart, isPackagePublished, updateConstraintRange };
package/dist/index.js ADDED
@@ -0,0 +1,283 @@
1
+ import { readFile, writeFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { dump, load } from "js-yaml";
4
+ import * as semver from "semver";
5
+ import { glob } from "tinyglobby";
6
+ import { x } from "tinyexec";
7
+ import { WorkspacePackage } from "tegami";
8
+ import { execFailure, isCI, joinPath } from "tegami/utils";
9
+ import z from "zod";
10
+ //#region src/schema.ts
11
+ const dartDependencySchema = z.union([z.string(), z.looseObject({
12
+ version: z.string().optional(),
13
+ hosted: z.union([z.string(), z.looseObject({
14
+ name: z.string().optional(),
15
+ url: z.string()
16
+ })]).optional(),
17
+ path: z.string().optional(),
18
+ git: z.unknown().optional(),
19
+ sdk: z.string().optional()
20
+ })]);
21
+ const pubspecSchema = z.looseObject({
22
+ name: z.string().optional(),
23
+ version: z.string().optional(),
24
+ publish_to: z.string().optional(),
25
+ resolution: z.string().optional(),
26
+ workspace: z.array(z.string()).optional(),
27
+ dependencies: z.record(z.string(), dartDependencySchema).optional(),
28
+ dev_dependencies: z.record(z.string(), dartDependencySchema).optional(),
29
+ dependency_overrides: z.record(z.string(), dartDependencySchema).optional()
30
+ });
31
+ const hostedPackageSchema = z.object({ versions: z.array(z.object({ version: z.string() })).optional() });
32
+ //#endregion
33
+ //#region src/index.ts
34
+ const DEP_FIELDS = [
35
+ "dependencies",
36
+ "dev_dependencies",
37
+ "dependency_overrides"
38
+ ];
39
+ const DEFAULT_HOSTED_URL = "https://pub.dev";
40
+ var DartPackage = class extends WorkspacePackage {
41
+ path;
42
+ file;
43
+ manager = "dart";
44
+ constructor(path, file) {
45
+ super();
46
+ this.path = path;
47
+ this.file = file;
48
+ }
49
+ get name() {
50
+ return this.file.data.name;
51
+ }
52
+ get version() {
53
+ return this.file.data.version;
54
+ }
55
+ get publishTo() {
56
+ return this.file.data.publish_to;
57
+ }
58
+ setVersion(version) {
59
+ this.file.data.version = version;
60
+ }
61
+ async write() {
62
+ await writeFile(path.join(this.path, "pubspec.yaml"), dump(this.file.data, {
63
+ lineWidth: -1,
64
+ noRefs: true,
65
+ sortKeys: false
66
+ }));
67
+ }
68
+ };
69
+ function dart({ updateLockFile = true, bumpDep: getBumpDepType } = {}) {
70
+ let active = false;
71
+ return {
72
+ name: "dart",
73
+ enforce: "post",
74
+ async resolve() {
75
+ const packages = await discoverDartPackages(this.cwd);
76
+ for (const pkg of packages) this.graph.add(pkg);
77
+ active = packages.length > 0;
78
+ },
79
+ initDraft(draft) {
80
+ if (!active) return;
81
+ draft.addPolicy(depsPolicy(this, getBumpDepType));
82
+ },
83
+ publishPreflight({ pkg }) {
84
+ if (!(pkg instanceof DartPackage)) return;
85
+ const wait = dependencyRefs(this.graph, pkg).filter((ref) => ref.kind === "dependencies").map((ref) => ref.linked.id);
86
+ return {
87
+ shouldPublish: pkg.version !== void 0 && pkg.publishTo !== "none",
88
+ wait
89
+ };
90
+ },
91
+ resolvePlanStatus({ plan }) {
92
+ return Array.from(plan.packages, async ([id, { preflight }]) => {
93
+ if (!preflight.shouldPublish) return;
94
+ const pkg = this.graph.get(id);
95
+ if (!(pkg instanceof DartPackage) || !pkg.version) return;
96
+ if (!await isPackagePublished(pkg.name, pkg.version, pkg.publishTo ?? DEFAULT_HOSTED_URL)) return "pending";
97
+ });
98
+ },
99
+ async publish({ pkg }) {
100
+ if (!(pkg instanceof DartPackage)) return;
101
+ const result = await x("dart", [
102
+ "pub",
103
+ "publish",
104
+ ...isCI() ? ["--force"] : []
105
+ ], { nodeOptions: { cwd: pkg.path } });
106
+ if (result.exitCode !== 0) {
107
+ if (/already exists|already published|version already exists/i.test(`${result.stdout}\n${result.stderr}`)) return { type: "skipped" };
108
+ return {
109
+ type: "failed",
110
+ error: execFailure(`Failed to publish ${pkg.name}@${pkg.version}.`, result).message
111
+ };
112
+ }
113
+ return { type: "published" };
114
+ },
115
+ async applyDraft(draft) {
116
+ if (!active) return;
117
+ for (const pkg of this.graph.getPackages()) {
118
+ if (!(pkg instanceof DartPackage)) continue;
119
+ const bumped = draft.getPackageDraft(pkg.id)?.bumpVersion(pkg);
120
+ if (bumped) pkg.setVersion(bumped);
121
+ }
122
+ const writes = [];
123
+ for (const pkg of this.graph.getPackages()) {
124
+ if (!(pkg instanceof DartPackage)) continue;
125
+ for (const ref of dependencyRefs(this.graph, pkg)) {
126
+ if (!ref.linked.version) continue;
127
+ const range = getDependencyRange(ref.spec);
128
+ if (!range || satisfiesDartRange(ref.linked.version, range)) continue;
129
+ ref.table[ref.name] = setDependencyRange(ref.spec, updateConstraintRange(range, ref.linked.version));
130
+ }
131
+ writes.push(pkg.write());
132
+ }
133
+ await Promise.all(writes);
134
+ },
135
+ async applyCliDraft() {
136
+ if (!active || !updateLockFile) return;
137
+ const result = await x("dart", ["pub", "get"], { nodeOptions: { cwd: this.cwd } });
138
+ if (result.exitCode !== 0) throw execFailure("Failed to update Dart pub resolution.", result);
139
+ }
140
+ };
141
+ }
142
+ function depsPolicy({ graph }, getBumpDepType = ({ kind }) => {
143
+ switch (kind) {
144
+ case "dependencies":
145
+ case "dependency_overrides": return "patch";
146
+ case "dev_dependencies": return false;
147
+ }
148
+ }) {
149
+ const dependentMap = /* @__PURE__ */ new Map();
150
+ for (const pkg of graph.getPackages()) {
151
+ if (!(pkg instanceof DartPackage)) continue;
152
+ for (const ref of dependencyRefs(graph, pkg)) {
153
+ const refs = dependentMap.get(ref.linked.id);
154
+ if (refs) refs.push(ref);
155
+ else dependentMap.set(ref.linked.id, [ref]);
156
+ }
157
+ }
158
+ return {
159
+ id: "dart:deps",
160
+ onUpdate({ pkg, packageDraft: plan }) {
161
+ if (!(pkg instanceof DartPackage)) return;
162
+ const deps = dependentMap.get(pkg.id);
163
+ if (!deps) return;
164
+ const group = graph.getPackageGroup(pkg.id);
165
+ if (!plan.bumpVersion(pkg)) return;
166
+ for (const dep of deps) {
167
+ if (group?.options.syncBump && graph.getPackageGroup(dep.dependent.id) === group) continue;
168
+ const bumpType = getBumpDepType(dep);
169
+ if (bumpType === false) continue;
170
+ this.bumpPackage(dep.dependent, {
171
+ type: bumpType,
172
+ reason: `update dependency "${dep.name}"`
173
+ });
174
+ }
175
+ }
176
+ };
177
+ }
178
+ function dependencyRefs(graph, pkg) {
179
+ const refs = [];
180
+ for (const { kind, table } of dependencyTables(pkg.file.data)) for (const [name, spec] of Object.entries(table)) {
181
+ const linked = resolveLinkedPackage(graph, name);
182
+ if (!linked || linked === pkg) continue;
183
+ refs.push({
184
+ dependent: pkg,
185
+ kind,
186
+ name,
187
+ spec,
188
+ table,
189
+ linked
190
+ });
191
+ }
192
+ return refs;
193
+ }
194
+ function resolveLinkedPackage(graph, name) {
195
+ return graph.getPackages().find((candidate) => candidate instanceof DartPackage && candidate.name === name);
196
+ }
197
+ function dependencyTables(manifest) {
198
+ const tables = [];
199
+ for (const kind of DEP_FIELDS) {
200
+ const table = manifest[kind];
201
+ if (table) tables.push({
202
+ kind,
203
+ table
204
+ });
205
+ }
206
+ return tables;
207
+ }
208
+ function getDependencyRange(spec) {
209
+ if (typeof spec === "string") return spec;
210
+ return spec.version;
211
+ }
212
+ function setDependencyRange(spec, range) {
213
+ if (typeof spec !== "object" || spec === null) return range;
214
+ spec.version = range;
215
+ return spec;
216
+ }
217
+ function updateConstraintRange(range, version) {
218
+ const trimmed = range.trim();
219
+ if (trimmed.startsWith("^")) return `^${version}`;
220
+ if (trimmed.startsWith("~")) return `~${version}`;
221
+ const lowerBound = /^(>=|>)\s*([0-9A-Za-z.+-]+)/.exec(trimmed);
222
+ if (lowerBound) return trimmed.replace(lowerBound[0], `${lowerBound[1]}${version}`);
223
+ return version;
224
+ }
225
+ function satisfiesDartRange(version, range) {
226
+ const trimmed = range.trim();
227
+ if (trimmed === "any") return true;
228
+ if (!semver.validRange(trimmed, { loose: true })) return false;
229
+ return semver.satisfies(version, trimmed, {
230
+ includePrerelease: true,
231
+ loose: true
232
+ });
233
+ }
234
+ async function discoverDartPackages(cwd) {
235
+ const root = await readPubspec(cwd);
236
+ if (!root?.data.workspace?.length) return [];
237
+ const files = /* @__PURE__ */ new Map();
238
+ files.set(root.path, root);
239
+ await collectWorkspaceFiles(root, files);
240
+ return Array.from(files.values()).filter((file) => Boolean(file.data.name)).map((file) => new DartPackage(path.dirname(file.path), file));
241
+ }
242
+ async function collectWorkspaceFiles(root, files) {
243
+ const rootDir = path.dirname(root.path);
244
+ const members = root.data.workspace;
245
+ if (!members?.length) return;
246
+ const paths = await glob(members, {
247
+ absolute: true,
248
+ cwd: rootDir,
249
+ onlyDirectories: true,
250
+ onlyFiles: false,
251
+ ignore: ["**/.dart_tool/**", "**/build/**"]
252
+ });
253
+ await Promise.all(paths.map(async (dir) => {
254
+ const file = await readPubspec(dir);
255
+ if (!file || files.has(file.path)) return;
256
+ files.set(file.path, file);
257
+ await collectWorkspaceFiles(file, files);
258
+ }));
259
+ }
260
+ async function readPubspec(dir) {
261
+ const filePath = path.join(dir, "pubspec.yaml");
262
+ try {
263
+ const content = await readFile(filePath, "utf8");
264
+ return {
265
+ path: filePath,
266
+ data: pubspecSchema.parse(load(content) ?? {})
267
+ };
268
+ } catch {
269
+ return;
270
+ }
271
+ }
272
+ async function isPackagePublished(name, version, hostedUrl) {
273
+ const url = joinPath(hostedUrl, "api/packages", encodeURIComponent(name));
274
+ const response = await fetch(url, { headers: {
275
+ Accept: "application/vnd.pub.v2+json",
276
+ "User-Agent": "tegami-dart"
277
+ } });
278
+ if (response.status === 404) return false;
279
+ if (!response.ok) return false;
280
+ return hostedPackageSchema.parse(await response.json()).versions?.some((entry) => entry.version === version) ?? false;
281
+ }
282
+ //#endregion
283
+ export { DartPackage, dart, isPackagePublished, updateConstraintRange };
package/package.json CHANGED
@@ -1,5 +1,45 @@
1
1
  {
2
2
  "name": "@tegami/dart",
3
- "version": "0.0.0-tegami-trusted-publish-setup",
4
- "description": "Placeholder published by Tegami for npm trusted publishing setup."
5
- }
3
+ "version": "1.1.0",
4
+ "description": "Dart pub workspace support and pub.dev publishing for Tegami",
5
+ "license": "MIT",
6
+ "author": "Fuma Nama",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "github:fuma-nama/tegami"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "type": "module",
15
+ "exports": {
16
+ ".": "./dist/index.js",
17
+ "./package.json": "./package.json"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "dependencies": {
23
+ "js-yaml": "^5.2.0",
24
+ "semver": "^7.8.5",
25
+ "tinyexec": "^1.2.4",
26
+ "tinyglobby": "^0.2.17",
27
+ "zod": "^4.4.3"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^26.0.1",
31
+ "@types/semver": "^7.7.1",
32
+ "tsdown": "^0.22.3",
33
+ "typescript": "6.0.3",
34
+ "@repo/typescript-config": "0.0.0",
35
+ "tegami": "1.1.0"
36
+ },
37
+ "peerDependencies": {
38
+ "tegami": "1.1.0"
39
+ },
40
+ "scripts": {
41
+ "types:check": "tsc --noEmit",
42
+ "build": "tsdown",
43
+ "dev": "tsdown --watch"
44
+ }
45
+ }
package/README.md DELETED
@@ -1,5 +0,0 @@
1
- # Placeholder package
2
-
3
- This empty package was published by [Tegami](https://tegami.fuma-nama.dev) to configure npm trusted publishing.
4
-
5
- The real package contents will be published via CI with OIDC.