Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clients/remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
"start": "yarn node dist/index.js",
"debug": "yarn node --inspect-brk dist/index.js",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"db:bootstrap": "tsx ./src/scripts/db.ts bootstrap",
"db:migrate:up": "tsx ./src/scripts/db.ts up",
"db:migrate:down": "tsx ./src/scripts/db.ts down",
Expand Down
1 change: 1 addition & 0 deletions core/ledger-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"build": "tsc -b",
"dev": "tsc -b --watch",
"clean": "tsc -b --clean; rm -rf dist",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"test": "yarn node --experimental-vm-modules $(yarn bin jest) --passWithNoTests"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions core/ledger-proto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"scripts": {
"build": "tsc -b",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions core/rpc-errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"scripts": {
"build": "tsc --build",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"scripts": {
"build": "tsc && typedoc src/index.ts --out docs --excludeExternals --excludePrivate --hideGenerator --readme none",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist"
},
"files": ["dist/**"],
Expand Down
1 change: 1 addition & 0 deletions core/signing-fireblocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
"build": "tsc -b",
"clean": "tsc -b --clean && rm -rf ./dist",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"test": "jest"
},
"main": "dist/index.js",
Expand Down
1 change: 1 addition & 0 deletions core/signing-internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"build": "tsc -b",
"clean": "tsc -b --clean && rm -rf ./dist",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"test": "jest"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions core/signing-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc -b",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean && rm -rf ./dist"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions core/signing-participant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"build": "tsc -b",
"clean": "tsc -b --clean && rm -rf ./dist",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"test": "yarn node --experimental-vm-modules $(yarn bin jest) --passWithNoTests"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions core/splice-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"build": "tsc -b",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions core/splice-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"scripts": {
"build": "tsc -b",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions core/token-standard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"build": "rollup -c",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions core/tx-visualizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"build": "tsc -b",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
"clean": "tsc -b --clean; rm -rf dist"
},
Expand Down
1 change: 1 addition & 0 deletions core/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"scripts": {
"build": "tsc -b",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions core/wallet-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"scripts": {
"build": "tsc -b",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist",
"test": "yarn node --experimental-vm-modules $(yarn bin jest) --passWithNoTests"
},
Expand Down
1 change: 1 addition & 0 deletions core/wallet-dapp-remote-rpc-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"scripts": {
"build": "tsc && typedoc src/index.ts --out docs --excludeExternals --excludePrivate --hideGenerator --readme none",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist"
},
"files": [
Expand Down
1 change: 1 addition & 0 deletions core/wallet-store-inmemory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"scripts": {
"build": "tsc -b",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)"
},
Expand Down
1 change: 1 addition & 0 deletions core/wallet-store-sql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"scripts": {
"build": "tsc -b",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)"
},
Expand Down
1 change: 1 addition & 0 deletions core/wallet-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"scripts": {
"build": "tsc -b",
"dev": "tsc -b --watch",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist",
"test": "yarn node --experimental-vm-modules $(yarn bin jest) --passWithNoTests"
},
Expand Down
1 change: 1 addition & 0 deletions core/wallet-ui-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build": "vite build && yarn workspace @canton-network/core-wallet-ui-components tsc --emitDeclarationOnly",
"build:watch": "vite build --watch & tsc --emitDeclarationOnly --watch",
"typecheck": "tsc --noEmit",
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
"clean": "tsc -b --clean; rm -rf dist",
"dev": "storybook dev -p 6006"
},
Expand Down
3 changes: 3 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
}
},
"targetDefaults": {
"flatpack": {
"dependsOn": ["build", "^flatpack"]
},
"build": {
"dependsOn": ["^build"],
"cache": true
Expand Down
121 changes: 45 additions & 76 deletions scripts/src/lib/flat-pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,19 @@ import { getRepoRoot } from './utils.js'

const repoRoot = getRepoRoot()

function run(cmd: string, opts: { cwd?: string } = {}) {
function run(
cmd: string,
opts: { cwd?: string; env?: Record<string, string> } = {}
) {
console.log(`$ ${cmd}`)
execSync(cmd, { stdio: 'inherit', ...opts })
}

interface DependencyPackage {
name: string
dir: string
}

interface DependencyPackageVendored {
name: string
dir: string
filename: string
}

/** Given a package directory within this repository: build, pack, and copy it as well as all its dependencies to an output directory */
export class FlatPack {
private outDir: string
private vendoredDir: string
private postInitHook: (() => void) | undefined = undefined

constructor(
private pkgDir: string,
Expand All @@ -41,58 +34,62 @@ export class FlatPack {
this.vendoredDir = path.join(this.outDir, '.vendored')
}

public postInit(callback: () => void): void {
this.postInitHook = callback
}

/**
* Build, pack, and copy the package and its dependencies to the output directory
* @returns The path to the output directory
*/
public pack(): string {
const mainPkgDir = this.pkgDir
const mainPkgName = this.readPackageJson(mainPkgDir).name
const mainPkgFileName = `${mainPkgName.replaceAll('/', '-')}.tgz`

console.log('Packing for: ' + mainPkgName)

console.log('packing for: ' + mainPkgName)
this.init()

const dependencies = this.getRecursiveDependencies()
run(`yarn nx --tui=false run ${mainPkgName}:flatpack`, {
cwd: repoRoot,
env: {
...process.env,
FLATPACK_OUTDIR: `${this.vendoredDir}/%s.tgz`,
},
})

for (const dep of dependencies) {
const vendored = this.buildDependency(dep)
this.overridePackageJson(vendored)
const overrides =
this.projectType === 'npm' ? 'overrides' : 'resolutions'

const resolvedDependencies = {} as Record<string, string>

const dependencies = fs
.readdirSync(this.vendoredDir)
.filter((f) => f.endsWith('.tgz'))
.filter((f) => f !== mainPkgFileName)

for (const file of dependencies) {
if (file.endsWith('.tgz')) {
const pkgName = file
.split('.tgz')[0]
.replaceAll('@canton-network-', '@canton-network/')
resolvedDependencies[pkgName] = `file:./.vendored/${file}`
}
}

const mainPkg = this.buildDependency({
name: this.readPackageJson(this.pkgDir).name,
dir: this.pkgDir,
})
this.overridePackageJson(mainPkg)
this.writePackageJson((pkgJson) => ({
...pkgJson,
dependencies: {
[mainPkgName]: `file:./.vendored/${mainPkg.filename}`,
...pkgJson.dependencies,
[mainPkgName]: `file:./.vendored/${mainPkgFileName}`,
},
[overrides]: resolvedDependencies,
}))

return this.outDir
}

public getRecursiveDependencies(): DependencyPackage[] {
const cmd =
'yarn workspaces foreach --no-private --topological -R exec \'echo "$npm_package_name $(pwd)"\''

const workspaces = execSync(cmd, {
encoding: 'utf8',
cwd: path.join(repoRoot, this.pkgDir),
})

return workspaces
.trim()
.split('\n')
.filter((l) => !l.startsWith('Done'))
.map((line) => {
const [name, dir] = line.split(' ')
return { name, dir }
})
}

private init() {
fs.mkdirSync(path.join(this.vendoredDir), { recursive: true })
fs.writeFileSync(
Expand All @@ -103,34 +100,19 @@ export class FlatPack {
private: true,
version: '0.0.0',
description: 'Temporary package for flat packing',
...(this.projectType === 'yarn'
? { packageManager: 'yarn@4.9.4' }
: {}),
dependencies: {},
},
null,
2
)
)
}

private pkgNameToFileName(name: string): string {
return (
name.replaceAll('/', '_').replaceAll('-', '_').replaceAll('@', '') +
'.tgz'
)
}

private buildDependency(dep: DependencyPackage): DependencyPackageVendored {
console.log('building package in: ' + dep.dir)

const filename = this.pkgNameToFileName(dep.name)
const outpath = path.join(
this.vendoredDir,
this.pkgNameToFileName(dep.name)
)

run('yarn build', { cwd: dep.dir })
run(`yarn pack --filename "${outpath}"`, { cwd: dep.dir })

return { ...dep, filename }
if (this.postInitHook) {
this.postInitHook()
}
}

private readPackageJson(parentDir: string): any {
Expand All @@ -151,17 +133,4 @@ export class FlatPack {
JSON.stringify(callback(pkgJson), null, 2)
)
}

private overridePackageJson(dep: DependencyPackageVendored) {
const overrides =
this.projectType === 'npm' ? 'overrides' : 'resolutions'

this.writePackageJson((pkgJson) => ({
...pkgJson,
[overrides]: {
...pkgJson[overrides],
[dep.name]: `file:./.vendored/${dep.filename}`,
},
}))
}
}
Loading
Loading