diff --git a/packages/shared/dist/cjs/constants.d.ts b/packages/shared/dist/cjs/constants.d.ts index bb8a04ef3..7fec4dd81 100644 --- a/packages/shared/dist/cjs/constants.d.ts +++ b/packages/shared/dist/cjs/constants.d.ts @@ -7,14 +7,16 @@ export declare namespace Constants { prdrr = "mainnet-beta-round-robin", dev = "devnet", test = "testnet", - localhost = "localhost-devnet" + localhost = "localhost-devnet", + custom = "custom" } const currentCluster: string; + const customUrl: string; const isDebugging: boolean; const nftstorageApikey: string; } export declare namespace ConstantsFunc { - const switchApi: (env: string | undefined) => string; + const switchApi: (env: string | undefined, customUrl?: string) => string; } export declare namespace Constants { const WRAPPED_TOKEN_PROGRAM_ID: PublicKey; diff --git a/packages/shared/dist/cjs/constants.js b/packages/shared/dist/cjs/constants.js index 5936d0586..ffa02cc76 100644 --- a/packages/shared/dist/cjs/constants.js +++ b/packages/shared/dist/cjs/constants.js @@ -17,14 +17,16 @@ var Constants; Cluster["dev"] = "devnet"; Cluster["test"] = "testnet"; Cluster["localhost"] = "localhost-devnet"; + Cluster["custom"] = "custom"; })(Cluster = Constants.Cluster || (Constants.Cluster = {})); - Constants.currentCluster = solana_suite_json_1.default.cluster; + Constants.currentCluster = solana_suite_json_1.default.cluster.type; + Constants.customUrl = solana_suite_json_1.default.cluster.customUrl; Constants.isDebugging = solana_suite_json_1.default.debugging; Constants.nftstorageApikey = solana_suite_json_1.default.nftstorage.apikey; })(Constants = exports.Constants || (exports.Constants = {})); var ConstantsFunc; (function (ConstantsFunc) { - ConstantsFunc.switchApi = (env) => { + ConstantsFunc.switchApi = (env, customUrl = Constants.customUrl) => { switch (env) { case Constants.Cluster.prd: return 'https://api.mainnet-beta.solana.com'; @@ -44,6 +46,8 @@ var ConstantsFunc; 'https://solana-api.projectserum.com', ]; return clusters[index]; + case Constants.Cluster.custom: + return customUrl; default: return 'http://api.devnet.solana.com'; } diff --git a/packages/shared/dist/cjs/solana-suite.json b/packages/shared/dist/cjs/solana-suite.json index ba397d354..75694202d 100644 --- a/packages/shared/dist/cjs/solana-suite.json +++ b/packages/shared/dist/cjs/solana-suite.json @@ -1,5 +1,8 @@ { - "cluster": "localhost-devnet", + "cluster": { + "type": "localhost-devnet", + "customUrl": "" + }, "debugging": false, "nftstorage": { "apikey": "" diff --git a/packages/shared/dist/esm/solana-suite.json b/packages/shared/dist/esm/solana-suite.json index ba397d354..2a4dae66c 100644 --- a/packages/shared/dist/esm/solana-suite.json +++ b/packages/shared/dist/esm/solana-suite.json @@ -1,7 +1 @@ -{ - "cluster": "localhost-devnet", - "debugging": false, - "nftstorage": { - "apikey": "" - } -} +{"cluster":{"type":"custom","customUrl":"https://www.google.com"},"debugging":false,"nftstorage":{"apikey":""}} \ No newline at end of file diff --git a/packages/shared/solana-suite-config.js b/packages/shared/solana-suite-config.js index bfd6304ab..a838548b9 100755 --- a/packages/shared/solana-suite-config.js +++ b/packages/shared/solana-suite-config.js @@ -20,11 +20,23 @@ const loadConfigFile = () => { } } +const successMessage = () => console.log('Update solana suite config.'); + const updateConfigFile = (key, value) => { const parsed = JSON.parse(cjs); parsed[key] = value; fs.writeFileSync(CJS_JSON, JSON.stringify(parsed)); fs.writeFileSync(ESM_JSON, JSON.stringify(parsed)); + successMessage(); +} + +const updateClusterConfigFile = (key, value, customUrl = '') => { + const parsed = JSON.parse(cjs); + parsed[key].type = value; + parsed[key].customUrl = customUrl; + fs.writeFileSync(CJS_JSON, JSON.stringify(parsed)); + fs.writeFileSync(ESM_JSON, JSON.stringify(parsed)); + successMessage(); } const updateNftStorageConfigFile = (key, value) => { @@ -32,6 +44,7 @@ const updateNftStorageConfigFile = (key, value) => { parsed.nftstorage[key] = value; fs.writeFileSync(CJS_JSON, JSON.stringify(parsed)); fs.writeFileSync(ESM_JSON, JSON.stringify(parsed)); + successMessage(); } loadConfigFile(); @@ -49,7 +62,8 @@ program.command('cluster') .option('dev', 'Connect to devnet') .option('test', 'Connect to testnet') .option('localhost', 'Connect to devnet in localhost') - .action(arg => { + .option('custom', 'Use custom url connect to devnet(mainnet-beta). e.g: custom `https://....`') + .action((arg, option) => { let value; switch (arg) { case 'prd': @@ -70,13 +84,18 @@ program.command('cluster') case 'localhost': value = 'localhost-devnet'; break; + case 'custom': + if (!option || !/https?:\/\/[-_.!~*\\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+/g.test(option)) { + console.warn('Not found custom cluster url. e.g: custom `https://....`'); + return; + } + value = 'custom'; + customUrl = option; + break; default: - console.warn(` - No match parameter: need parameter is - "prd", "prd2", "prdrr", "dev", "test", "localhost". any one of them - `); + console.warn(`No match parameter: need parameter is\n"prd", "prd2", "prdrr", "dev", "test", "localhost", "custom". any one of them`); } - updateConfigFile('cluster', value); + updateClusterConfigFile('cluster', value, customUrl); }); program.command('debug') diff --git a/packages/shared/src/constants.ts b/packages/shared/src/constants.ts index d7d865dc9..4bc1f1911 100644 --- a/packages/shared/src/constants.ts +++ b/packages/shared/src/constants.ts @@ -14,15 +14,17 @@ export namespace Constants { dev = 'devnet', test = 'testnet', localhost = 'localhost-devnet', + custom = 'custom', } - export const currentCluster = Config.cluster; + export const currentCluster = Config.cluster.type; + export const customUrl = Config.cluster.customUrl; export const isDebugging = Config.debugging; export const nftstorageApikey = Config.nftstorage.apikey; } export namespace ConstantsFunc { - export const switchApi = (env: string | undefined) => { + export const switchApi = (env: string | undefined, customUrl = Constants.customUrl) => { switch (env) { case Constants.Cluster.prd: return 'https://api.mainnet-beta.solana.com'; @@ -42,6 +44,8 @@ export namespace ConstantsFunc { 'https://solana-api.projectserum.com', ]; return clusters[index]; + case Constants.Cluster.custom: + return customUrl; default: return 'http://api.devnet.solana.com'; } diff --git a/packages/shared/src/solana-suite.json b/packages/shared/src/solana-suite.json index fd77d5714..4ae62e263 100644 --- a/packages/shared/src/solana-suite.json +++ b/packages/shared/src/solana-suite.json @@ -1,5 +1,8 @@ { - "cluster": "localhost-devnet", + "cluster": { + "type": "localhost-devnet", + "customUrl": "" + }, "debugging": false, "nftstorage": { "apikey": "" diff --git a/packages/shared/test/constants.test.ts b/packages/shared/test/constants.test.ts index a652aa77b..97867e7a9 100644 --- a/packages/shared/test/constants.test.ts +++ b/packages/shared/test/constants.test.ts @@ -15,4 +15,10 @@ describe('Constants', () => { assert.isNotEmpty(cluster); } }); + it('ConstantsFunc use custom and customUrl', () => { + const dummyUrl = 'https://hoge.hoge'; + const cluster = ConstantsFunc.switchApi(Constants.Cluster.custom, dummyUrl); + console.log('# cluster url: ', cluster); + assert.equal(cluster, dummyUrl); + }); })