Skip to content

Commit 7f6e0c7

Browse files
feat: added npm registry readme (#68)
1 parent be03e06 commit 7f6e0c7

10 files changed

Lines changed: 124 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ node_modules
1212
oclif.manifest.json
1313
.env
1414
.codify-files
15+
.npm-readme-backup.md

.npmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
min-release-age=10080
1+
min-release-age=7

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
> ⚠️ **This package has been migrated and re-purposed.** The original package by [Andrew Stone](https://www.npmjs.com/~andrewjstone)
2+
> for transforming integers to base36 strings. To continue to use that functionality, please pin to version 0.3.0 or below.
3+
14
# Codify - Your Development Environment as Code
25

36
**Stop manually setting up your development environment. Define it once, replicate it everywhere.**

README.npm.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
> ⚠️ **This package has been migrated and re-purposed.** The original package by [Andrew Stone](https://www.npmjs.com/~andrewjstone)
2+
> for transforming integers to base36 strings. To continue to use that functionality, please pin to version 0.3.0 or below.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@
144144
"start:dev": "./bin/dev.js",
145145
"start:vm": "npm run build && npm run pack:macos && npm run start:vm",
146146
"deploy": "npm run pkg && npm run notarize && npm run upload",
147-
"prepublishOnly": "npm run build"
147+
"build:npm": "tsx ./scripts/build-npm.ts",
148+
"restore-npm": "tsx ./scripts/restore-npm.ts",
149+
"prepublishOnly": "npm run build && npm run build:npm",
150+
"postpublish": "npm run restore-npm"
148151
},
149152
"version": "1.2.2",
150153
"bugs": "https://github.com/codifycli/codify/issues",

scripts/build-npm.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// npm-publish prep. Runs from `prepublishOnly` (after `npm run build`).
2+
//
3+
// Two npm-specific concerns the plain `tsc` build doesn't handle:
4+
//
5+
// 1. dist/patch-ink.mjs — the postinstall hook runs `node dist/patch-ink.mjs`
6+
// to patch node_modules/ink at the user's install time (needed for the raw-PTY
7+
// handoff, e.g. `gh auth login` — see scripts/patch-ink.ts). `tsc -b` only
8+
// compiles src/**/*, so this file is otherwise absent from the npm tarball and
9+
// the postinstall guard silently skips it. Compile it here (same command as
10+
// scripts/pkg.ts uses for the binary build).
11+
//
12+
// 2. README.md — npm should show a "migrated and re-purposed" blurb that the
13+
// git-tracked README must NOT carry. README.npm.md holds ONLY the blurb; we
14+
// back up the real README and prepend the blurb to it (so the body is never
15+
// duplicated). `npm run restore-npm` (via postpublish, and defensively on
16+
// failure) restores it from the backup.
17+
//
18+
// The backup file (README.md.orig) is the single source of truth for restoration,
19+
// so a failed publish never leaves README.md dirty.
20+
21+
import chalk from 'chalk'
22+
import { execSync } from 'node:child_process'
23+
import { existsSync } from 'node:fs'
24+
import fs from 'node:fs/promises'
25+
26+
const README = 'README.md'
27+
const README_BLURB = 'README.npm.md'
28+
// The transient backup is intentionally NOT named README* and is a dotfile — npm
29+
// force-includes any README* file into the tarball regardless of the `files`
30+
// allowlist, and this backup is just build scratch that shouldn't ship.
31+
const README_BACKUP = '.npm-readme-backup.md'
32+
33+
// ── 1. Compile patch-ink.ts → dist/patch-ink.mjs ─────────────────────────────
34+
console.log(chalk.magenta('Compiling patch-ink.ts to dist/patch-ink.mjs'))
35+
execSync(
36+
'tsc --module nodenext --moduleResolution nodenext --target es2022 --outDir dist scripts/patch-ink.ts',
37+
{ shell: 'zsh' },
38+
)
39+
await fs.rename('dist/patch-ink.js', 'dist/patch-ink.mjs')
40+
41+
// ── 2. Prepend the npm blurb to README.md ────────────────────────────────────
42+
// README.npm.md holds ONLY the blurb; we prepend it to the real README so the
43+
// body never has to be duplicated/kept in sync. `npm run restore-npm` puts the
44+
// original README.md back from the backup.
45+
if (!existsSync(README_BLURB)) {
46+
console.error(chalk.red(`ERROR: ${README_BLURB} not found. Cannot build npm README.`))
47+
process.exit(1)
48+
}
49+
50+
// If a stale backup exists (previous publish aborted before restore), the real
51+
// README was already saved there — use it as the base so we don't prepend twice.
52+
if (!existsSync(README_BACKUP)) {
53+
console.log(chalk.magenta(`Backing up ${README}${README_BACKUP}`))
54+
await fs.copyFile(README, README_BACKUP)
55+
} else {
56+
console.log(chalk.yellow(`${README_BACKUP} already exists (stale?); using it as the base`))
57+
}
58+
59+
console.log(chalk.magenta(`Prepending ${README_BLURB} to ${README}`))
60+
const blurb = await fs.readFile(README_BLURB, 'utf8')
61+
const body = await fs.readFile(README_BACKUP, 'utf8')
62+
await fs.writeFile(README, `${blurb.trimEnd()}\n\n${body}`, 'utf8')

scripts/patch-ink.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,35 @@
1616
import fs from 'node:fs/promises';
1717
import path from 'node:path';
1818
import { fileURLToPath } from 'node:url';
19+
import { createRequire } from 'node:module';
1920
import { existsSync } from 'node:fs';
2021

2122
const __dirname = path.dirname(fileURLToPath(import.meta.url));
22-
const INK_DIR = path.join(__dirname, '../node_modules/ink/build');
23+
24+
// Locate ink's install dir via Node module resolution rather than a fixed
25+
// relative path. Under an npm install, ink is hoisted to the top-level
26+
// node_modules (not nested under codify), so `../node_modules/ink` misses it and
27+
// the patch silently skips. Resolve ink's entry point (ink/package.json can't be
28+
// resolved directly — ink's "exports" map doesn't expose it) and walk up to the
29+
// package root, which finds ink wherever npm/pnpm placed it (hoisted, nested, or
30+
// symlinked). Falls back to the old relative path (used by the self-contained
31+
// binary build) if resolution somehow fails.
32+
function resolveInkDir(): string {
33+
try {
34+
const require = createRequire(import.meta.url);
35+
let dir = path.dirname(require.resolve('ink')); // .../ink/build/index.js → .../ink/build
36+
while (dir !== path.dirname(dir)) {
37+
if (existsSync(path.join(dir, 'package.json'))) return path.join(dir, 'build');
38+
dir = path.dirname(dir);
39+
}
40+
} catch {
41+
// fall through to the relative-path default below
42+
}
43+
44+
return path.join(__dirname, '../node_modules/ink/build');
45+
}
46+
47+
const INK_DIR = resolveInkDir();
2348
const APP_JS = path.join(INK_DIR, 'components/App.js');
2449
const INK_JS = path.join(INK_DIR, 'ink.js');
2550
const RENDER_JS = path.join(INK_DIR, 'render.js');

scripts/restore-npm.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Restores the git-tracked README.md after an npm publish.
2+
//
3+
// scripts/build-npm.ts swaps in README.npm.md and saves the real README to
4+
// README.md.orig. This restores it from that backup and removes the backup.
5+
// Runs via `postpublish` (success) and can be run manually if a publish aborts.
6+
// Idempotent: a no-op if there's no backup to restore.
7+
8+
import chalk from 'chalk'
9+
import { existsSync } from 'node:fs'
10+
import fs from 'node:fs/promises'
11+
12+
const README = 'README.md'
13+
const README_BACKUP = '.npm-readme-backup.md'
14+
15+
if (existsSync(README_BACKUP)) {
16+
console.log(chalk.magenta(`Restoring ${README} from ${README_BACKUP}`))
17+
await fs.copyFile(README_BACKUP, README)
18+
await fs.rm(README_BACKUP)
19+
} else {
20+
console.log(chalk.gray(`No ${README_BACKUP} to restore; nothing to do.`))
21+
}

src/api/dashboard/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ export const DashboardApiClient = {
1010
throw new Error('Not logged in');
1111
}
1212

13+
// The api worker owns the Yjs decode and returns { id, contents }.
1314
const res = await fetch(
14-
`${config.dashboardUrl}/api/v1/documents/${id}`,
15+
`${config.apiUrl}/v1/documents/${id}/contents`,
1516
{
1617
method: 'GET',
1718
headers: { 'Content-Type': 'application/json', 'authorization': `Bearer ${login.accessToken}` }
@@ -24,7 +25,7 @@ export const DashboardApiClient = {
2425
}
2526

2627
const json = await res.json();
27-
return json.defaultDocumentId;
28+
return json as CloudDocument;
2829
},
2930

3031
async getDefaultDocumentId(): Promise<null | string> {

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const config = {
1111
],
1212

1313
dashboardUrl: 'https://dashboard.codifycli.com',
14+
apiUrl: 'https://api.codifycli.com',
1415
supabaseUrl: 'https://kdctbvqvqjfquplxhqrm.supabase.co',
1516

1617
isBeta: VERSION.includes('beta'),

0 commit comments

Comments
 (0)