33 *
44 * Runs automatically on `npm install` (npm "install" lifecycle script).
55 *
6- * electerm-ios reuses 100 % of the source code from electerm-android.
7- * Instead of keeping a duplicate copy in this repo, we download the latest
8- * source archive from https://github.com/electerm/electerm-android and copy
9- * its `src/` directory into ours. This repo only keeps its own package.json,
10- * build scripts and iOS-specific build configuration.
6+ * electerm-ios reuses the source tree of electerm-web. Instead of keeping a
7+ * duplicate copy in this repo, we download the latest source archive from
8+ * https://github.com/electerm/electerm-web and copy its `src/` directory into
9+ * ours. This repo only keeps its own package.json, build scripts and
10+ * iOS-specific build configuration.
1111 *
1212 * After the source sync we also copy the @electerm/electerm-react client
1313 * from node_modules (same as the original install step), then apply the
1414 * iOS-specific src overrides from build/replace/src on top of src/.
15+ *
16+ * `install-records.ref` (repo root, gitignored) records what src/ was built
17+ * from: the upstream ref, the @electerm/electerm-react version, and a
18+ * fingerprint of the iOS delta. A run whose ref *and* version *and* delta all
19+ * match what is recorded — and whose src/ and client dir are present — skips the
20+ * download, the client copy and the overrides entirely, so a plain `npm install`
21+ * no longer has to wipe src/. Delete that file to force a full re-install.
22+ *
23+ * All paths are cwd-relative: this is the npm `install` lifecycle script, so npm
24+ * runs it from the package root, and the rest of this file already assumes that.
1525 */
1626import { writeFile , mkdir } from 'node:fs/promises'
17- import { existsSync } from 'node:fs'
18- import { resolve } from 'node:path'
27+ import { existsSync , readFileSync , readdirSync } from 'node:fs'
28+ import { resolve , relative , join , sep } from 'node:path'
29+ import { createHash } from 'node:crypto'
30+ import { execFileSync } from 'node:child_process'
1931import pkg from 'shelljs'
2032import { x as tarX } from 'tar'
2133import { applySrcOverrides } from './apply-src-overrides.mjs'
@@ -25,75 +37,226 @@ const { echo, rm: shellRm, cp } = pkg
2537const REPO = 'electerm/electerm-web'
2638const BRANCH = 'main'
2739const URL = `https://codeload.github.com/${ REPO } /tar.gz/refs/heads/${ BRANCH } `
28- const TMP = resolve ( 'temp/electerm-android-src' )
29- const TMP_FILE = resolve ( TMP , 'electerm-android.tar.gz' )
40+
41+ const RECORD = resolve ( 'install-records.ref' )
42+ const REPLACE_SRC = resolve ( 'build/replace/src' )
43+ const CLIENT_DIR = resolve ( 'src/client/electerm-react' )
44+ const CLIENT_PKG_JSON = resolve ( 'node_modules/@electerm/electerm-react/package.json' )
45+
46+ const TMP = resolve ( 'temp/electerm-web-src' )
47+ const TMP_FILE = resolve ( TMP , 'electerm-web.tar.gz' )
3048
3149echo ( 'install required modules' )
3250
3351// ---------------------------------------------------------------------------
34- // 1. Download the latest electerm-android source archive
52+ // What are we building from, and do we already have it?
3553// ---------------------------------------------------------------------------
36- echo ( `downloading latest ${ REPO } (${ BRANCH } branch)…` )
3754
38- shellRm ( '-rf' , TMP )
39- await mkdir ( TMP , { recursive : true } )
55+ /**
56+ * The commit at the tip of the source branch, via `git ls-remote`: one line, no
57+ * token, no API rate limit, and — unlike the tarball — nothing to download.
58+ *
59+ * Returns null when it cannot be resolved (offline, no git). That is not fatal
60+ * here: the run falls through to the download, which has its own fallback.
61+ */
62+ function remoteRef ( ) {
63+ try {
64+ const out = execFileSync (
65+ 'git' ,
66+ [ 'ls-remote' , `https://github.com/${ REPO } .git` , `refs/heads/${ BRANCH } ` ] ,
67+ { encoding : 'utf8' , stdio : [ 'ignore' , 'pipe' , 'ignore' ] , timeout : 30000 }
68+ )
69+ return ( out . split ( '\n' ) [ 0 ] || '' ) . split ( '\t' ) [ 0 ] . trim ( ) || null
70+ } catch ( e ) {
71+ return null
72+ }
73+ }
4074
41- let downloaded = false
42- try {
43- const res = await fetch ( URL )
44- if ( ! res . ok ) {
45- throw new Error ( `HTTP ${ res . status } ${ res . statusText } ` )
75+ /**
76+ * Fingerprint of the iOS delta: every file under build/replace/src (path +
77+ * contents).
78+ *
79+ * The skip needs this, not just the ref. `applySrcOverrides()` only ever copies,
80+ * it never removes — so deleting an override leaves the upstream ref unchanged,
81+ * a ref-only check would skip, and the stale overlaid file would sit in src/
82+ * forever. Hashing the delta makes that deletion force a real re-sync.
83+ *
84+ * `.DS_Store` is excluded because applySrcOverrides filters it out of the copy,
85+ * so it cannot change what lands in src/.
86+ */
87+ function deltaHash ( ) {
88+ if ( ! existsSync ( REPLACE_SRC ) ) {
89+ return 'none'
4690 }
47- const buf = Buffer . from ( await res . arrayBuffer ( ) )
48- await writeFile ( TMP_FILE , buf )
49- echo ( 'download complete' )
50- downloaded = true
51- } catch ( e ) {
52- echo ( `WARNING: failed to download source — ${ e . message } ` )
53- if ( existsSync ( 'src' ) ) {
54- echo ( 'keeping existing src/ folder' )
55- } else {
56- echo ( 'ERROR: src/ does not exist and download failed — cannot continue' )
57- process . exit ( 1 )
91+ const files = [ ]
92+ const walk = ( dir ) => {
93+ for ( const entry of readdirSync ( dir , { withFileTypes : true } ) ) {
94+ if ( entry . name === '.DS_Store' ) {
95+ continue
96+ }
97+ const full = join ( dir , entry . name )
98+ if ( entry . isDirectory ( ) ) {
99+ walk ( full )
100+ } else if ( entry . isFile ( ) ) {
101+ files . push ( relative ( REPLACE_SRC , full ) . split ( sep ) . join ( '/' ) )
102+ }
103+ }
58104 }
59- }
105+ walk ( REPLACE_SRC )
60106
61- // ---------------------------------------------------------------------------
62- // 2. Extract archive and replace src/
63- // ---------------------------------------------------------------------------
64- if ( downloaded ) {
65- echo ( 'extracting…' )
66- await tarX ( {
67- file : TMP_FILE ,
68- cwd : TMP ,
69- strip : 1 // remove the top-level "electerm-android-main/" directory
70- } )
71-
72- echo ( 'syncing src/ from electerm-android…' )
73- shellRm ( '-rf' , 'src' )
74- cp ( '-r' , resolve ( TMP , 'src' ) , resolve ( 'src' ) )
107+ const h = createHash ( 'sha256' )
108+ for ( const rel of files . sort ( ) ) {
109+ h . update ( rel ) . update ( '\0' )
110+ h . update ( createHash ( 'sha256' ) . update ( readFileSync ( join ( REPLACE_SRC , rel ) ) ) . digest ( 'hex' ) )
111+ h . update ( '\n' )
112+ }
113+ return h . digest ( 'hex' )
75114}
76115
77- // ---------------------------------------------------------------------------
78- // 3. Copy @electerm/electerm-react client from node_modules
79- // ---------------------------------------------------------------------------
80- echo ( 'installing electerm-react module' )
81- shellRm ( '-rf' , 'src/client/electerm-react' )
82- cp ( '-r' , 'node_modules/@electerm/electerm-react/client' , 'src/client/electerm-react' )
116+ /** Version of the @electerm/electerm-react package sitting in node_modules. */
117+ function clientVersion ( ) {
118+ try {
119+ return JSON . parse ( readFileSync ( CLIENT_PKG_JSON , 'utf8' ) ) . version || null
120+ } catch ( e ) {
121+ return null
122+ }
123+ }
83124
84- // ---------------------------------------------------------------------------
85- // 4. Apply iOS-specific src overrides from build/replace/src. Uses the same
86- // shared function as build/ios/build.mjs so the patched sources are
87- // identical whether the build runs from `npm i` or `npm run build:ios`.
88- // ---------------------------------------------------------------------------
89- const n = applySrcOverrides ( )
90- if ( n > 0 ) {
91- echo ( `applied ${ n } src override file(s) from build/replace/src` )
125+ /** What install-records.ref currently says, or null. */
126+ function readRecord ( ) {
127+ try {
128+ return JSON . parse ( readFileSync ( RECORD , 'utf8' ) )
129+ } catch ( e ) {
130+ return null
131+ }
92132}
93133
94- // ---------------------------------------------------------------------------
95- // 5. Cleanup temp files
96- // ---------------------------------------------------------------------------
97- shellRm ( '-rf' , TMP )
134+ const ref = remoteRef ( )
135+ const delta = deltaHash ( )
136+ const client = clientVersion ( )
137+ const recorded = readRecord ( )
138+
139+ const upToDate = Boolean (
140+ ref &&
141+ client &&
142+ recorded &&
143+ recorded . repo === REPO &&
144+ recorded . branch === BRANCH &&
145+ recorded . ref === ref &&
146+ recorded . electermReact === client &&
147+ recorded . delta === delta &&
148+ existsSync ( 'src' ) &&
149+ existsSync ( CLIENT_DIR )
150+ )
151+
152+ if ( upToDate ) {
153+ echo ( `up to date: ${ REPO } @${ ref . slice ( 0 , 7 ) } (${ BRANCH } ), electerm-react ${ client } , delta unchanged — skipping download` )
154+ } else {
155+ if ( ! ref ) {
156+ echo ( 'WARNING: could not resolve the source ref — cannot skip, downloading' )
157+ }
158+
159+ // -------------------------------------------------------------------------
160+ // 1. Download the latest source archive
161+ // -------------------------------------------------------------------------
162+ echo ( `downloading latest ${ REPO } (${ BRANCH } branch)…` )
163+
164+ shellRm ( '-rf' , TMP )
165+ await mkdir ( TMP , { recursive : true } )
166+
167+ let downloaded = false
168+ try {
169+ const res = await fetch ( URL )
170+ if ( ! res . ok ) {
171+ throw new Error ( `HTTP ${ res . status } ${ res . statusText } ` )
172+ }
173+ const buf = Buffer . from ( await res . arrayBuffer ( ) )
174+ await writeFile ( TMP_FILE , buf )
175+ echo ( 'download complete' )
176+ downloaded = true
177+ } catch ( e ) {
178+ echo ( `WARNING: failed to download source — ${ e . message } ` )
179+ if ( existsSync ( 'src' ) ) {
180+ echo ( 'keeping existing src/ folder' )
181+ } else {
182+ echo ( 'ERROR: src/ does not exist and download failed — cannot continue' )
183+ process . exit ( 1 )
184+ }
185+ }
186+
187+ // -------------------------------------------------------------------------
188+ // 2. Extract archive and replace src/
189+ // -------------------------------------------------------------------------
190+ if ( downloaded ) {
191+ echo ( 'extracting…' )
192+ await tarX ( {
193+ file : TMP_FILE ,
194+ cwd : TMP ,
195+ strip : 1 // remove the top-level "<repo>-<branch>/" directory
196+ } )
197+
198+ echo ( 'syncing src/ from the source archive…' )
199+ shellRm ( '-rf' , 'src' )
200+ // shelljs `rm -rf` reports failure only as a warning, so an intercepted
201+ // delete (a bulk-delete guard, a read-only mount) leaves src/ in place and
202+ // the copy below would land *inside* it — producing src/src and a tree that
203+ // is silently half old, half new. Refuse instead: no record is written, so
204+ // the next run tries again.
205+ if ( existsSync ( 'src' ) ) {
206+ echo ( 'ERROR: could not remove src/ — refusing to copy into it (that would nest the tree at src/src)' )
207+ echo ( ' a bulk-delete guard or read-only mount can cause this; remove src/ by hand and retry' )
208+ process . exit ( 1 )
209+ }
210+ cp ( '-r' , resolve ( TMP , 'src' ) , resolve ( 'src' ) )
211+ if ( ! existsSync ( resolve ( 'src/app' ) ) || ! existsSync ( resolve ( 'src/client' ) ) ) {
212+ echo ( 'ERROR: src/ does not look like a source tree after the copy — not recording it as installed' )
213+ process . exit ( 1 )
214+ }
215+ }
216+
217+ // -------------------------------------------------------------------------
218+ // 3. Copy @electerm/electerm-react client from node_modules
219+ // -------------------------------------------------------------------------
220+ echo ( 'installing electerm-react module' )
221+ shellRm ( '-rf' , 'src/client/electerm-react' )
222+ cp ( '-r' , 'node_modules/@electerm/electerm-react/client' , 'src/client/electerm-react' )
223+
224+ // -------------------------------------------------------------------------
225+ // 4. Apply iOS-specific src overrides from build/replace/src. Uses the same
226+ // shared function as build/ios/build.mjs so the patched sources are
227+ // identical whether the build runs from `npm i` or `npm run build:ios`.
228+ // -------------------------------------------------------------------------
229+ const n = applySrcOverrides ( )
230+ if ( n > 0 ) {
231+ echo ( `applied ${ n } src override file(s) from build/replace/src` )
232+ }
233+
234+ // -------------------------------------------------------------------------
235+ // 5. Record what src/ was built from, so the next run can skip all of the
236+ // above. Written last, and only after a real download: recording a ref we
237+ // never materialised would make the next run skip a sync that never
238+ // happened. No timestamp, so re-running does not churn the file.
239+ // -------------------------------------------------------------------------
240+ if ( downloaded && ref && client ) {
241+ await writeFile (
242+ RECORD ,
243+ JSON . stringify ( {
244+ repo : REPO ,
245+ branch : BRANCH ,
246+ ref,
247+ electermReact : client ,
248+ delta
249+ } , null , 2 ) + '\n'
250+ )
251+ echo ( `wrote install-records.ref: ${ REPO } @${ ref . slice ( 0 , 7 ) } , electerm-react ${ client } ` )
252+ } else if ( downloaded ) {
253+ echo ( 'WARNING: not writing install-records.ref (missing ref or client version) — the next run will download again' )
254+ }
255+
256+ // -------------------------------------------------------------------------
257+ // 6. Cleanup temp files
258+ // -------------------------------------------------------------------------
259+ shellRm ( '-rf' , TMP )
260+ }
98261
99262echo ( 'done install required modules' )
0 commit comments