diff --git a/packages/address-book/package.json b/packages/address-book/package.json index 86de6244d..0a1d886e9 100644 --- a/packages/address-book/package.json +++ b/packages/address-book/package.json @@ -13,9 +13,7 @@ "directory": "packages/address-book" }, "exports": { - "./horizon/addresses.json": "./src/horizon/addresses.json", - "./issuance/addresses.json": "./src/issuance/addresses.json", - "./subgraph-service/addresses.json": "./src/subgraph-service/addresses.json" + "./*/addresses.json": "./src/*/addresses.json" }, "files": [ "src/**/*.json", diff --git a/packages/address-book/scripts/copy-addresses-for-publish.js b/packages/address-book/scripts/copy-addresses-for-publish.js index 6335f7dc5..0665359d4 100755 --- a/packages/address-book/scripts/copy-addresses-for-publish.js +++ b/packages/address-book/scripts/copy-addresses-for-publish.js @@ -3,67 +3,52 @@ /** * Copy Addresses for Publishing * - * This script copies the actual addresses.json files from horizon and subgraph-service - * packages to replace the symlinks before npm publish. - * - * Why we need this: - * - Development uses symlinks (committed to git) for convenience - * - npm publish doesn't include symlinks in the published package - * - We need actual files in the published package for consumers - * - * The postpublish script will restore the symlinks after publishing. + * Replaces the dev-time symlinks under src//addresses.json with real + * file copies before npm publish — npm does not include symlinks in the + * published tarball. restore-symlinks.js puts the symlinks back afterwards. */ const fs = require('fs') const path = require('path') +const SOURCES = require('./sources') -const FILES_TO_COPY = [ - { - source: '../../../horizon/addresses.json', - target: 'src/horizon/addresses.json', - }, - { - source: '../../../issuance/addresses.json', - target: 'src/issuance/addresses.json', - }, - { - source: '../../../subgraph-service/addresses.json', - target: 'src/subgraph-service/addresses.json', - }, -] +const ROOT = path.resolve(__dirname, '..') +const SRC = path.join(ROOT, 'src') -function copyFileForPublish(source, target) { - const targetPath = path.resolve(__dirname, '..', target) - const sourcePath = path.resolve(path.dirname(targetPath), source) +function copyOne(name) { + const sourcePath = path.resolve(ROOT, '..', name, 'addresses.json') + const targetDir = path.join(SRC, name) + const targetPath = path.join(targetDir, 'addresses.json') - // Ensure source exists if (!fs.existsSync(sourcePath)) { console.error(`❌ Source file ${sourcePath} does not exist`) process.exit(1) } - // Remove existing symlink - if (fs.existsSync(targetPath)) { - fs.unlinkSync(targetPath) - } + fs.mkdirSync(targetDir, { recursive: true }) + fs.rmSync(targetPath, { force: true }) + fs.copyFileSync(sourcePath, targetPath) + console.log(`✅ Copied for publish: src/${name}/addresses.json`) +} - // Copy actual file - try { - fs.copyFileSync(sourcePath, targetPath) - console.log(`✅ Copied for publish: ${target} <- ${source}`) - } catch (error) { - console.error(`❌ Failed to copy ${source} to ${target}:`, error.message) +function checkDrift() { + const dirs = fs + .readdirSync(SRC) + .filter((d) => fs.statSync(path.join(SRC, d)).isDirectory()) + .sort() + const expected = [...SOURCES].sort() + if (JSON.stringify(dirs) !== JSON.stringify(expected)) { + console.error(`❌ Drift between SOURCES and src/`) + console.error(` SOURCES: [${expected.join(', ')}]`) + console.error(` src/ : [${dirs.join(', ')}]`) process.exit(1) } } function main() { console.log('📦 Copying address files for npm publish...') - - for (const { source, target } of FILES_TO_COPY) { - copyFileForPublish(source, target) - } - + for (const name of SOURCES) copyOne(name) + checkDrift() console.log('✅ Address files copied for publish!') } diff --git a/packages/address-book/scripts/restore-symlinks.js b/packages/address-book/scripts/restore-symlinks.js index 2f3a871f2..eb2f1f5df 100755 --- a/packages/address-book/scripts/restore-symlinks.js +++ b/packages/address-book/scripts/restore-symlinks.js @@ -3,54 +3,32 @@ /** * Restore Symlinks After Publishing * - * This script restores the symlinks after npm publish completes. - * The prepublishOnly script replaces symlinks with actual files for publishing, - * and this script puts the symlinks back for development. + * Restores the dev-time symlinks under src//addresses.json after + * npm publish. copy-addresses-for-publish.js replaces them with real files + * for the publish step; this puts them back. */ const fs = require('fs') const path = require('path') +const SOURCES = require('./sources') -const SYMLINKS_TO_RESTORE = [ - { - target: '../../../horizon/addresses.json', - link: 'src/horizon/addresses.json', - }, - { - target: '../../../issuance/addresses.json', - link: 'src/issuance/addresses.json', - }, - { - target: '../../../subgraph-service/addresses.json', - link: 'src/subgraph-service/addresses.json', - }, -] +const ROOT = path.resolve(__dirname, '..') +const SRC = path.join(ROOT, 'src') -function restoreSymlink(target, link) { - const linkPath = path.resolve(__dirname, '..', link) +function restoreOne(name) { + const linkTarget = `../../../${name}/addresses.json` + const linkDir = path.join(SRC, name) + const linkPath = path.join(linkDir, 'addresses.json') - // Remove the copied file - if (fs.existsSync(linkPath)) { - fs.unlinkSync(linkPath) - } - - // Restore symlink - try { - fs.symlinkSync(target, linkPath) - console.log(`✅ Restored symlink: ${link} -> ${target}`) - } catch (error) { - console.error(`❌ Failed to restore symlink ${link}:`, error.message) - process.exit(1) - } + fs.mkdirSync(linkDir, { recursive: true }) + fs.rmSync(linkPath, { force: true }) + fs.symlinkSync(linkTarget, linkPath) + console.log(`✅ Restored symlink: src/${name}/addresses.json -> ${linkTarget}`) } function main() { console.log('🔗 Restoring symlinks after publish...') - - for (const { target, link } of SYMLINKS_TO_RESTORE) { - restoreSymlink(target, link) - } - + for (const name of SOURCES) restoreOne(name) console.log('✅ Symlinks restored!') } diff --git a/packages/address-book/scripts/sources.js b/packages/address-book/scripts/sources.js new file mode 100644 index 000000000..6885c1a2b --- /dev/null +++ b/packages/address-book/scripts/sources.js @@ -0,0 +1,4 @@ +// Source packages exported from @graphprotocol/address-book. +// Each name corresponds to packages//addresses.json (source of truth) +// and packages/address-book/src//addresses.json (publish symlink). +module.exports = ['horizon', 'issuance', 'subgraph-service']