cli: setup scripts

This commit is contained in:
Koushik Dutta
2021-12-04 12:25:50 -08:00
parent 32e66738ef
commit b88c58960c
4 changed files with 50 additions and 10 deletions

4
cli/package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "scrypted",
"version": "1.0.30",
"version": "1.0.31",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "scrypted",
"version": "1.0.30",
"version": "1.0.31",
"license": "ISC",
"dependencies": {
"axios": "^0.21.4",

View File

@@ -1,6 +1,6 @@
{
"name": "scrypted",
"version": "1.0.30",
"version": "1.0.31",
"description": "",
"main": "dist/main.js",
"bin": {

View File

@@ -7,7 +7,7 @@ import util from 'util';
import readline, { BasicOptions } from 'readline-sync';
import https from 'https';
import mkdirp from 'mkdirp';
import { serveMain } from './service';
import { cwdInstallDir, getInstallDir, getRunServerArguments, installServe, runServer, serveMain } from './service';
function getUserHome() {
const ret = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
@@ -70,9 +70,26 @@ async function main() {
if (process.argv[2] === 'serve') {
await serveMain(false);
}
if (process.argv[2] === 'serve@latest') {
else if (process.argv[2] === 'serve@latest') {
await serveMain(true);
}
else if (process.argv[2] === 'install-server') {
const installDir = await installServe();
console.log('server installation successful:', installDir);
}
else if (process.argv[2] === 'run-server') {
cwdInstallDir();
await runServer();
}
else if (process.argv[2] === 'create-run-server-script') {
if (!process.argv[3])
throw new Error('no output file specified');
const args = getRunServerArguments();
const node = process.argv[0];
const script = `cd ${getInstallDir()} && ${node} ${args.join(' ')}\n`;
fs.writeFileSync(process.argv[3], script);
console.log(script);
}
else if (process.argv[2] === 'login') {
const ip = process.argv[3] || '127.0.0.1';
await doLogin(ip);

View File

@@ -31,27 +31,50 @@ async function runCommandEatError(command: string, ...args: string[]) {
}
}
export function getRunServerArguments() {
return ['--expose-gc', 'node_modules/@scrypted/server/dist/scrypted-main.js'];
}
export async function runServer() {
console.log('Starting scrypted main...');
await runCommand(process.argv[0], ...getRunServerArguments());
}
async function startServer() {
try {
console.log('Starting scrypted main...');
await runCommand(process.argv[0], '--expose-gc', 'node_modules/@scrypted/server/dist/scrypted-main.js');
await runServer();
}
catch (e) {
console.error('scrypted server exited with error', e);
}
}
export async function serveMain(install: boolean) {
const installDir = path.join(os.homedir(), '.scrypted');
export function getInstallDir() {
return path.join(os.homedir(), '.scrypted');
}
export function cwdInstallDir(): { volume: string, installDir: string } {
const installDir = getInstallDir();
const volume = path.join(installDir, 'volume');
mkdirp.sync(volume);
process.chdir(installDir);
return { volume, installDir };
}
export async function installServe() {
const { installDir } = cwdInstallDir();
await runCommandEatError('npm', '--prefix', installDir, 'install', '--production', '@scrypted/server@latest');
return installDir;
}
export async function serveMain(install: boolean) {
const { installDir, volume } = cwdInstallDir();
if (!fs.existsSync('node_modules/@scrypted/server')) {
install = true;
console.log('Package @scrypted/server not found. Installing.');
}
if (install) {
await runCommandEatError('npm', '--prefix', installDir, 'install', '--production', '@scrypted/server@latest');
await installServe();
}
process.env.SCRYPTED_NPM_SERVE = 'true';