-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: add scripts to run new yarn pack script * fix: configure new opts for yarn pack script * fix(types): abstract types for procs * fix: add script for checking npm releases * fix: abstract script logic into one central area * docs: update readme * ci: add ci test for npm releases * ci: fix error * ci: fix pre install * fix: setWsUrl * add sig interrupt * fix: error for sidecar * fix: comments docs * fix: refactor launchProcess params, add trim to stdout * fix: remove default value for the command on launchProcess * Update scripts/runYarnPack.ts Co-authored-by: David <dvdplm@gmail.com> * docs: update description, and give examples on to run it locally, and cleanup if there is an issue * fix: give error instructions on how to cleanup if theres an issue, fix doc to active voice * ci: description * ci: fix the description to a comment Co-authored-by: David <dvdplm@gmail.com>
- Loading branch information
Showing
7 changed files
with
303 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { defaultSasBuildOpts, defaultSasPackOpts } from './config'; | ||
import { killAll, launchProcess, setWsUrl } from './sidecarScriptApi'; | ||
import { ProcsType, StatusCode } from './types'; | ||
|
||
const procs: ProcsType = {}; | ||
|
||
/** | ||
* Cleans up the remaining deps and files that should be temps | ||
*/ | ||
const cleanup = async () => { | ||
const { Failed } = StatusCode; | ||
|
||
/** | ||
* Cleanup dep tree | ||
*/ | ||
console.log('Uninstalling Sidecar..'); | ||
const sasUnInstallPackOpts = { | ||
proc: 'sidecar-install-pack', | ||
resolver: 'YN0000: Done', | ||
args: ['remove', '@substrate/api-sidecar'], | ||
}; | ||
const sidecarUnInstallPack = await launchProcess('yarn', procs, sasUnInstallPackOpts); | ||
|
||
if (sidecarUnInstallPack === Failed) { | ||
console.error('UnInstalling sidecar package failed..'); | ||
console.error('Please uninstall the package using `yarn remove @substrate/api-sidecar`.'); | ||
killAll(procs); | ||
} | ||
|
||
/** | ||
* Delete tarball | ||
*/ | ||
console.log('Deleting tarball'); | ||
const sasDeleteTarballOpts = { | ||
proc: 'delete-tarball', | ||
resolver: '', | ||
args: ['-rf', `${__dirname}/../../package.tgz`], | ||
}; | ||
const deleteTarball = await launchProcess('rm', procs, sasDeleteTarballOpts); | ||
|
||
if (deleteTarball === Failed) { | ||
console.error('Error deleting tarball.'); | ||
console.error('In order to delete tarball run: `rm -rf ./package.tgz` from the root directory of the repository.'); | ||
killAll(procs); | ||
} | ||
}; | ||
|
||
/** | ||
* This script creates a dry-run npm release and checks if sidecar | ||
* launches succesfully. | ||
*/ | ||
const main = async () => { | ||
const { Failed, Success } = StatusCode; | ||
|
||
/** | ||
* Build sidecar | ||
*/ | ||
console.log('Building Sidecar'); | ||
const sidecarBuild = await launchProcess('yarn', procs, defaultSasBuildOpts); | ||
|
||
if (sidecarBuild === Failed) { | ||
console.error('Sidecar failed to build, exiting...'); | ||
killAll(procs); | ||
process.exit(1); | ||
} | ||
|
||
/** | ||
* Build Tarball via yarn pack | ||
*/ | ||
console.log('Building Local Npm release of Sidecar.'); | ||
const sidecarPack = await launchProcess('yarn', procs, defaultSasPackOpts); | ||
|
||
if (sidecarPack === Failed) { | ||
console.error('Sidecar failed to build an local npm tarball.'); | ||
killAll(procs); | ||
process.exit(1); | ||
} | ||
|
||
/** | ||
* Install tarball | ||
*/ | ||
console.log('Installing Sidecar as a package'); | ||
const sasInstallPackOpts = { | ||
proc: 'sidecar-install-pack', | ||
resolver: 'YN0000: Done', | ||
args: ['add', `${__dirname}/../../package.tgz`], | ||
}; | ||
const sidecarInstallPack = await launchProcess('yarn', procs, sasInstallPackOpts); | ||
|
||
if (sidecarInstallPack === Failed) { | ||
console.error('Installing the binary failed..'); | ||
killAll(procs); | ||
process.exit(1); | ||
} | ||
|
||
/** | ||
* Start sidecar and see if it works | ||
*/ | ||
setWsUrl('wss://kusama-rpc.polkadot.io'); | ||
console.log('Initializing Sidecar'); | ||
const sasStartPackOpts = { | ||
proc: 'sidecar', | ||
resolver: 'Check the root endpoint', | ||
resolverStartupErr: 'Error', | ||
args: [], | ||
}; | ||
const sidecarStart = await launchProcess( | ||
`${__dirname}/../../node_modules/.bin/substrate-api-sidecar`, | ||
procs, | ||
sasStartPackOpts | ||
); | ||
|
||
if (sidecarStart === Success) { | ||
console.log('Successful Release Build of Sidecar'); | ||
killAll(procs); | ||
await cleanup(); | ||
process.exit(0); | ||
} else { | ||
console.error('Release Build failed for Sidecar'); | ||
killAll(procs); | ||
await cleanup(); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
/** | ||
* Signal interrupt | ||
*/ | ||
process.on('SIGINT', function () { | ||
console.log('Caught interrupt signal'); | ||
killAll(procs); | ||
process.exit(); | ||
}); | ||
|
||
/** | ||
* Signal hangup terminal | ||
*/ | ||
process.on('SIGHUP', function () { | ||
console.log('Caught terminal termination'); | ||
killAll(procs); | ||
process.exit(); | ||
}); | ||
|
||
main().finally(() => process.exit()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { spawn } from 'child_process'; | ||
|
||
import { IProcOpts, ProcsType, StatusCode } from './types'; | ||
|
||
/** | ||
* Sets the url that sidecar will use in the env | ||
* | ||
* @param url ws url used in sidecar | ||
*/ | ||
export const setWsUrl = (url: string): void => { | ||
process.env.SAS_SUBSTRATE_WS_URL = url; | ||
}; | ||
|
||
/** | ||
* Kill all processes | ||
* | ||
* @param procs | ||
*/ | ||
export const killAll = (procs: ProcsType): void => { | ||
console.log('Killing all processes...'); | ||
for (const key of Object.keys(procs)) { | ||
if (!procs[key].killed) { | ||
try { | ||
console.log(`Killing ${key}`); | ||
// Kill child and all its descendants. | ||
process.kill(-procs[key].pid, 'SIGTERM'); | ||
process.kill(-procs[key].pid, 'SIGKILL'); | ||
} catch (e) { | ||
/** | ||
* The error we are catching here silently, is when `-procs[key].pid` takes | ||
* the range of all pid's inside of the subprocess group created with | ||
* `spawn`, and one of the process's is either already closed or doesn't exist anymore. | ||
* | ||
* ex: `Error: kill ESRCH` | ||
* | ||
* This is a very specific use case of an empty catch block and is used | ||
* outside of the scope of the API therefore justifiable, and should be used cautiously | ||
* elsewhere. | ||
*/ | ||
} | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Launch any given process. It accepts an options object. | ||
* | ||
* @param cmd Optional Command will default to 'yarn' | ||
* @param procs Object of saved processes | ||
* @param IProcOpts | ||