create-mapringg-repo 0.1.0 → 1.15.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.
Files changed (4) hide show
  1. package/cli.mjs +10 -1
  2. package/package.json +2 -2
  3. package/release.mjs +52 -0
  4. package/setup.mjs +69 -17
package/cli.mjs CHANGED
@@ -115,7 +115,15 @@ async function create(root, state, scratch) {
115
115
  if (remote) throw new Error(`GitHub already has ${state.repo}. Use an existing checkout to adopt it.`);
116
116
  if (!state.scaffolded) {
117
117
  if (state.kind === "app") copyTemplate(root, scratch);
118
- if (!existsSync(join(root, "README.md"))) writeFileSync(join(root, "README.md"), `# ${basename(root)}\n`);
118
+ const readme = join(root, "README.md");
119
+ const title = `# ${basename(root)}`;
120
+ if (!existsSync(readme))
121
+ writeFileSync(
122
+ readme,
123
+ `${title}\n\nAdd a short project overview here.\n\n## Quick start\n\nAdd setup steps here.\n`,
124
+ );
125
+ else if (state.kind === "app")
126
+ writeFileSync(readme, readFileSync(readme, "utf8").replace(/^# monorepo\r?$/im, title));
119
127
  saveState(root, { ...state, scaffolded: true });
120
128
  }
121
129
  const { check, updateLock } = configure(root, basename(root), { kind: state.kind, initial: true });
@@ -228,6 +236,7 @@ async function adopt(root, kind, scratch) {
228
236
  "pnpm-lock.yaml",
229
237
  "package-lock.json",
230
238
  ".github/workflows",
239
+ ".github/dependabot.yml",
231
240
  ]) {
232
241
  if (existsSync(join(work, file))) git(work, ["add", "--", file]);
233
242
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "create-mapringg-repo",
3
- "version": "0.1.0",
3
+ "version": "1.15.4",
4
4
  "description": "Create or adopt a GitHub repository with the Mapringg PR workflow.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/mapringg/standard.git",
9
- "directory": "create-mapringg-repo"
9
+ "directory": "packages/create-mapringg-repo"
10
10
  },
11
11
  "bin": {
12
12
  "create-mapringg-repo": "./cli.mjs"
package/release.mjs ADDED
@@ -0,0 +1,52 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import { existsSync, readFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+
5
+ const STANDARD_REMOTE = "https://github.com/mapringg/standard";
6
+ const STANDARD_MANIFEST = join(import.meta.dirname, "..", "..", "package.json");
7
+ const SEMVER_TAG = /^v(\d+)\.(\d+)\.(\d+)$/;
8
+
9
+ let tags;
10
+ let tag;
11
+
12
+ function remoteTags() {
13
+ tags ??= new Map(
14
+ execFileSync("git", ["ls-remote", "--tags", STANDARD_REMOTE], { encoding: "utf8" })
15
+ .trim()
16
+ .split("\n")
17
+ .filter(Boolean)
18
+ .map((line) => {
19
+ const [sha, ref] = line.split("\t");
20
+ return [ref.replace("refs/tags/", ""), sha];
21
+ }),
22
+ );
23
+ return tags;
24
+ }
25
+
26
+ function bundledTag() {
27
+ if (!existsSync(STANDARD_MANIFEST)) return null;
28
+ const { name, version } = JSON.parse(readFileSync(STANDARD_MANIFEST, "utf8"));
29
+ return name === "@mapringg/standard" ? `v${version}` : null;
30
+ }
31
+
32
+ function newestTag() {
33
+ const released = [...remoteTags().keys()].filter((name) => SEMVER_TAG.test(name));
34
+ if (released.length === 0) throw new Error(`No Standard release tags found at ${STANDARD_REMOTE}.`);
35
+ const parts = (name) => SEMVER_TAG.exec(name).slice(1).map(Number);
36
+ return released.toSorted((a, b) => {
37
+ const [x, y] = [parts(a), parts(b)];
38
+ return y[0] - x[0] || y[1] - x[1] || y[2] - x[2];
39
+ })[0];
40
+ }
41
+
42
+ export function releaseTag() {
43
+ tag ??= bundledTag() ?? newestTag();
44
+ return tag;
45
+ }
46
+
47
+ export function releaseSha() {
48
+ const name = releaseTag();
49
+ const sha = remoteTags().get(`${name}^{}`) ?? remoteTags().get(name);
50
+ if (!sha) throw new Error(`Standard ${name} is not tagged yet. Run a tagged release of the creator.`);
51
+ return sha;
52
+ }
package/setup.mjs CHANGED
@@ -3,9 +3,11 @@ import { dirname, join } from "node:path";
3
3
 
4
4
  import { isMap, isSeq, parseDocument } from "yaml";
5
5
 
6
- const RELEASE = "v1.11.0";
7
- const RELEASE_SHA = "2da38877f9f3361e0847f11aa0c7b581163b6b10";
8
- const STANDARD = "github:mapringg/standard#";
6
+ import { releaseSha, releaseTag } from "./release.mjs";
7
+
8
+ const PACKAGE = "@mapringg/standard";
9
+ const GROUP = "mapringg-standard";
10
+ const AUTOMERGE = join(".github", "workflows", "standard-updates.yml");
9
11
  const ACTION = "mapringg/standard/.github/actions/check-pr@";
10
12
  const CHECKOUT = "actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09";
11
13
  const EVENTS = ["opened", "synchronize", "reopened", "edited"];
@@ -22,12 +24,12 @@ function write(path, text) {
22
24
  writeFileSync(path, text);
23
25
  }
24
26
 
25
- function agents(root, name, installed) {
27
+ function agents(root, installed) {
26
28
  const path = join(root, "AGENTS.md");
27
29
  const skillsRoot = installed
28
30
  ? "node_modules/@mapringg/standard/.claude/skills"
29
- : `https://github.com/mapringg/standard/blob/${RELEASE}/.claude/skills`;
30
- const original = read(path) ?? `# ${name}\n`;
31
+ : "https://github.com/mapringg/standard/blob/main/.claude/skills";
32
+ const original = read(path) ?? "# AGENTS.md\n";
31
33
  const shared = original.split("\n").filter((line) => SKILLS.test(line));
32
34
  if (
33
35
  shared.length === 2 &&
@@ -63,7 +65,7 @@ function workflowFiles(root) {
63
65
  const path = join(dir, name);
64
66
  const doc = parseDocument(read(path));
65
67
  if (doc.errors.length > 0) throw new Error(`Fix invalid workflow YAML in ${name} before adopting Standard.`);
66
- return { path, doc, data: doc.toJS() ?? {} };
68
+ return { path, name, doc, data: doc.toJS() ?? {} };
67
69
  });
68
70
  }
69
71
 
@@ -110,21 +112,67 @@ function configureCheck(root, installed) {
110
112
  const actionIndex = job.steps.findIndex((s) => s.uses?.startsWith(ACTION));
111
113
  if (
112
114
  actionIndex !== -1 &&
113
- ![`${ACTION}${RELEASE}`, `${ACTION}${RELEASE_SHA}`].includes(job.steps[actionIndex].uses)
115
+ job.steps[actionIndex].uses !== `${ACTION}${releaseTag()}` &&
116
+ job.steps[actionIndex].uses !== `${ACTION}${releaseSha()}`
114
117
  ) {
115
- doc.setIn(["jobs", id, "steps", actionIndex, "uses"], `${ACTION}${RELEASE_SHA}`);
118
+ doc.setIn(["jobs", id, "steps", actionIndex, "uses"], `${ACTION}${releaseSha()}`);
116
119
  }
117
120
  if (doc.toString() !== before) write(path, doc.toString());
118
- return job.name ?? id;
121
+ return { name: job.name ?? id, workflow: check.data.name ?? `.github/workflows/${check.name}` };
119
122
  }
120
123
  const name = jobs.some(({ id, job }) => (job.name ?? id).toLowerCase() === "check") ? "Standard PR" : "Check";
121
124
  const path = join(root, ".github", "workflows", "standard-pr.yml");
122
125
  if (existsSync(path)) throw new Error("Resolve the existing standard-pr.yml before adopting Standard.");
123
126
  write(
124
127
  path,
125
- `name: Standard\n\non:\n pull_request:\n types: [${EVENTS.join(", ")}]\n push:\n branches: [main, develop]\n\npermissions:\n contents: read\n\njobs:\n check:\n name: ${name}\n runs-on: ubuntu-latest\n steps:\n - uses: ${CHECKOUT}\n with:\n persist-credentials: false\n - run: test -s AGENTS.md\n - uses: ${ACTION}${RELEASE_SHA} # ${RELEASE}\n`,
128
+ `name: Standard\n\non:\n pull_request:\n types: [${EVENTS.join(", ")}]\n push:\n branches: [main, develop]\n\npermissions:\n contents: read\n\njobs:\n check:\n name: ${name}\n runs-on: ubuntu-latest\n steps:\n - uses: ${CHECKOUT}\n with:\n persist-credentials: false\n - run: test -s AGENTS.md\n - uses: ${ACTION}${releaseSha()} # ${releaseTag()}\n`,
129
+ );
130
+ return { name, workflow: "Standard" };
131
+ }
132
+
133
+ function updateEntry(ecosystem, pattern) {
134
+ return {
135
+ "package-ecosystem": ecosystem,
136
+ directory: "/",
137
+ schedule: { interval: "daily" },
138
+ allow: [{ "dependency-name": pattern }],
139
+ groups: { [GROUP]: { patterns: [pattern] } },
140
+ };
141
+ }
142
+
143
+ function dependabot(root, hasPackage) {
144
+ const path = join(root, ".github", "dependabot.yml");
145
+ const doc = parseDocument(read(path) ?? "version: 2\nupdates: []\n");
146
+ if (doc.errors.length > 0) throw new Error("Fix invalid YAML in dependabot.yml before adopting Standard.");
147
+ const before = doc.toString({ flowCollectionPadding: false });
148
+ const wanted = [["github-actions", "mapringg/standard*"], ...(hasPackage ? [["npm", PACKAGE]] : [])];
149
+ const updates = doc.get("updates", true);
150
+ for (const [ecosystem, pattern] of wanted) {
151
+ const index = (updates?.items ?? []).findIndex((item) => {
152
+ const entry = item.toJSON();
153
+ const directories = entry.directories ?? [entry.directory];
154
+ return entry["package-ecosystem"] === ecosystem && directories.includes("/");
155
+ });
156
+ if (index === -1) {
157
+ doc.addIn(["updates"], doc.createNode(updateEntry(ecosystem, pattern)));
158
+ continue;
159
+ }
160
+ const groups = doc.getIn(["updates", index, "groups"], true);
161
+ if (isMap(groups) && groups.has(GROUP)) continue;
162
+ const rest = isMap(groups) ? groups.items : [];
163
+ const group = doc.createNode({ [GROUP]: { patterns: [pattern] } });
164
+ group.items.push(...rest);
165
+ doc.setIn(["updates", index, "groups"], group);
166
+ }
167
+ const options = { flowCollectionPadding: false };
168
+ if (doc.toString(options) !== before) write(path, doc.toString(options));
169
+ }
170
+
171
+ function automerge(root, workflow) {
172
+ write(
173
+ join(root, AUTOMERGE),
174
+ `name: Standard updates\n\non:\n workflow_run:\n workflows: [${JSON.stringify(workflow)}]\n types: [completed]\n\npermissions:\n contents: write\n pull-requests: write\n\njobs:\n merge:\n if: >-\n github.event.workflow_run.conclusion == 'success' &&\n github.event.workflow_run.event == 'pull_request' &&\n github.event.workflow_run.actor.login == 'dependabot[bot]' &&\n contains(github.event.workflow_run.head_branch, '${GROUP}')\n runs-on: ubuntu-latest\n steps:\n - env:\n GH_TOKEN: \${{ github.token }}\n BRANCH: \${{ github.event.workflow_run.head_branch }}\n SHA: \${{ github.event.workflow_run.head_sha }}\n run: gh pr merge "$BRANCH" --repo "$GITHUB_REPOSITORY" --squash --match-head-commit "$SHA"\n`,
126
175
  );
127
- return name;
128
176
  }
129
177
 
130
178
  function pinBrowser(root) {
@@ -149,11 +197,12 @@ export function configure(root, name, { kind = "plain", initial = false } = {})
149
197
  const original = read(path);
150
198
  const manifest = original ? JSON.parse(original) : {};
151
199
  const current = manifest.devDependencies?.["@mapringg/standard"] ?? manifest.dependencies?.["@mapringg/standard"];
152
- const installed = typeof current === "string" && current.startsWith(STANDARD);
153
- const updateLock = installed && current !== `${STANDARD}${RELEASE}`;
200
+ const installed = typeof current === "string";
201
+ const wanted = releaseTag().slice(1);
202
+ const updateLock = installed && current !== wanted;
154
203
  if (updateLock || isNewApp) {
155
204
  let next = original ?? "{}\n";
156
- if (updateLock) next = next.replaceAll(`"${current}"`, `"${STANDARD}${RELEASE}"`);
205
+ if (updateLock) next = next.replaceAll(`"${current}"`, `"${wanted}"`);
157
206
  if (isNewApp) next = next.replace(/"name":\s*"monorepo"/, `"name": ${JSON.stringify(name.toLowerCase())}`);
158
207
  write(path, next);
159
208
  }
@@ -167,6 +216,9 @@ export function configure(root, name, { kind = "plain", initial = false } = {})
167
216
  }
168
217
  }
169
218
  if (kind === "app") pinBrowser(root);
170
- agents(root, name, installed);
171
- return { check: configureCheck(root, installed), updateLock };
219
+ agents(root, installed);
220
+ const check = configureCheck(root, installed);
221
+ dependabot(root, Boolean(original));
222
+ automerge(root, check.workflow);
223
+ return { check: check.name, updateLock };
172
224
  }