Skip to content

Commit

Permalink
chore: Add the ability to override the default example repository URL…
Browse files Browse the repository at this point in the history
… and ref
  • Loading branch information
janjakubnanista committed Nov 22, 2023
1 parent 110f572 commit 381b81d
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/create-lz-oapp/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extensions": ["ts"],
"spec": ["**/*.test.*"],
"loader": "ts-node/esm"
}
6 changes: 5 additions & 1 deletion packages/create-lz-oapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"dev": "npx tsup --watch",
"lint": "npx eslint '**/*.{js,ts,json}'",
"prebuild": "tsc -noEmit",
"start": "./cli.cjs"
"start": "./cli.cjs",
"test": "mocha --parallel"
},
"files": [
"./cli.cjs",
Expand All @@ -40,9 +41,11 @@
"devDependencies": {
"@sindresorhus/tsconfig": "^5.0.0",
"@tanstack/react-query": "^5.8.3",
"@types/mocha": "^10.0.1",
"@types/prompts": "^2.4.8",
"@types/react": "^18.0.32",
"@types/which": "~3.0.2",
"chai": "^4.3.10",
"commander": "^11.1.0",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
Expand All @@ -51,6 +54,7 @@
"ink-select-input": "^5.0.0",
"ink-spinner": "^5.0.0",
"ink-text-input": "^5.0.1",
"mocha": "^10.0.0",
"prompts": "^2.4.2",
"react": "^18.2.0",
"react-devtools-core": "^4.28.5",
Expand Down
25 changes: 21 additions & 4 deletions packages/create-lz-oapp/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import type { Example, PackageManager } from "@/types.js"

/**
* To enable example development in a custom repository
* we open the repository URL field to be taken from the environment
*/
const repository = process.env["LAYERZERO_EXAMPLES_REPOSITORY_URL"] || "[email protected]:LayerZero-Labs/lz-utils"

/**
* To enable example development in a custom branch,
* we open up the ref field to be taken from the environment
*
* `LAYERZERO_EXAMPLES_REPOSITORY_REF` can then be set to something like `#develop` or `#my-custom-branch`
* to take the examples from a tag, a branch or a commit hash
*/
const ref = process.env["LAYERZERO_EXAMPLES_REPOSITORY_REF"] || ""

export const EXAMPLES: Example[] = [
{
id: "oft",
label: "OFT",
repository: "[email protected]:LayerZero-Labs/lz-examples",
directory: "packages/oft",
repository,
directory: "examples/oft",
ref,
},
{
id: "oapp",
label: "OApp",
repository: "[email protected]:LayerZero-Labs/lz-examples",
directory: "packages/oapp",
repository,
directory: "examples/oapp",
ref,
},
]

Expand Down
1 change: 1 addition & 0 deletions packages/create-lz-oapp/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Example {
label: string
repository: string
directory?: string
ref?: string
}

export interface PackageManager {
Expand Down
22 changes: 19 additions & 3 deletions packages/create-lz-oapp/src/utilities/cloning.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import { Config } from "@/types.js"
import { Config, Example } from "@/types.js"
import { rm } from "fs/promises"
import tiged from "tiged"

/**
* Helper function to satisfy the `tiged` repository URL specification
*
* @param example `Example`
* @returns `string` Repository URL compatible with `tiged`
*/
export const createExampleGitURL = (example: Example): string => {
return [
example.repository,
example.directory ? "/" + example.directory.replace(/^\//, "") : undefined,
example.ref ? "#" + example.ref.replace(/^#/, "") : undefined,
]
.filter(Boolean)
.join("")
}

export const cloneExample = async ({ example, destination }: Config) => {
const qualifier = example.directory ? `${example.repository}/${example.directory}` : example.repository
const emitter = tiged(qualifier, {
const url = createExampleGitURL(example)
const emitter = tiged(url, {
disableCache: true,
mode: "git",
verbose: true,
Expand Down
35 changes: 35 additions & 0 deletions packages/create-lz-oapp/test/utilities/cloning.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect } from "chai"
import { describe, it } from "mocha"
import { createExampleGitURL } from "../../src/utilities/cloning.js"

describe("utilities/cloning", () => {
describe("createExampleGitURL", () => {
const REPO_URL = "[email protected]:LayerZero-Labs/lz-utils"

it("should return the repository field if directory and ref are not specified", () => {
expect(createExampleGitURL({ repository: REPO_URL, id: "dummy", label: "Dummy" })).to.eql(REPO_URL)
})

it("should return the repository field with directory if directory is specified", () => {
expect(createExampleGitURL({ repository: REPO_URL, directory: "dir", id: "dummy", label: "Dummy" })).to.eql(`${REPO_URL}/dir`)
expect(createExampleGitURL({ repository: REPO_URL, directory: "/dir", id: "dummy", label: "Dummy" })).to.eql(`${REPO_URL}/dir`)
expect(createExampleGitURL({ repository: REPO_URL, directory: "dir", ref: "", id: "dummy", label: "Dummy" })).to.eql(
`${REPO_URL}/dir`
)
})

it("should return the repository field with directory and ref if directory and ref are specified", () => {
expect(createExampleGitURL({ repository: REPO_URL, directory: "dir", ref: "ref", id: "dummy", label: "Dummy" })).to.eql(
`${REPO_URL}/dir#ref`
)
expect(createExampleGitURL({ repository: REPO_URL, directory: "dir", ref: "#ref", id: "dummy", label: "Dummy" })).to.eql(
`${REPO_URL}/dir#ref`
)
})

it("should return the repository field with ref if only ref specified", () => {
expect(createExampleGitURL({ repository: REPO_URL, ref: "ref", id: "dummy", label: "Dummy" })).to.eql(`${REPO_URL}#ref`)
expect(createExampleGitURL({ repository: REPO_URL, ref: "#ref", id: "dummy", label: "Dummy" })).to.eql(`${REPO_URL}#ref`)
})
})
})
3 changes: 2 additions & 1 deletion packages/create-lz-oapp/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"outDir": "dist",
"declaration": false,
"target": "esnext",
"types": ["mocha", "node"],
"paths": {
"@/*": ["./src/*"]
}
},
"files": ["./types/tiged.d.ts"],
"include": ["src"]
"include": ["src", "test", "types"]
}

0 comments on commit 381b81d

Please sign in to comment.