UNPKG

atlas-app-services-cli

Version:

The Atlas App Services Command Line Interface

109 lines (95 loc) 2.81 kB
#!/usr/bin/env node import os from 'os'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { spawnSync } from 'child_process'; function directoryExists(file) { try { const stat = fs.lstatSync(file); return stat.isDirectory(); } catch (err) { return false; } } function fileExists(file) { try { const stat = fs.lstatSync(file); return stat.isFile(); } catch (err) { return false; } } function removeFolder(dir) { if (!fs.existsSync(dir)) return; fs.readdirSync(dir).forEach(function(file) { const curPath = dir + path.sep + file; if (fs.lstatSync(curPath).isDirectory()) { removeFolder(curPath); } else { fs.unlinkSync(curPath); } }); fs.rmdirSync(dir); } function checkSpawn(spawnInfo) { if (spawnInfo.stdout) { if (typeof spawnInfo.stdout !== 'string') { console.log(spawnInfo.stdout.toString('utf8')); } else { console.log(spawnInfo.stdout); } } if (spawnInfo.stderr) { if (typeof spawnInfo.error !== 'string') { console.error(spawnInfo.stderr.toString('utf8')); } else { console.error(spawnInfo.stderr); } } if (spawnInfo.status !== 0 || spawnInfo.error) { console.error('Failed when spawning.'); process.exit(1); } if (typeof spawnInfo.stdout !== 'string') { return spawnInfo.stdout.toString('utf8'); } return spawnInfo.stdout; } function sleep(milliseconds) { const inAFewMilliseconds = new Date(new Date().getTime() + milliseconds); while (inAFewMilliseconds > new Date()) { // wait for completion } } const tempInstallPath = path.resolve(os.tmpdir(), 'baas-cli-test'); if (directoryExists(tempInstallPath)) { console.log(`Deleting directory '${tempInstallPath}'.`); removeFolder(tempInstallPath); } fs.mkdirSync(tempInstallPath); const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file const __dirname = path.dirname(__filename); // get the name of the directory if (process.platform === 'win32') { sleep(2000); // wait 2 seconds until everything is in place checkSpawn(spawnSync('cmd.exe', ['/c', `npm i ${__dirname}`], { cwd: tempInstallPath })); } else { checkSpawn(spawnSync('npm', ['i', `${__dirname}`], { cwd: tempInstallPath })); } const executable = path.resolve( tempInstallPath, 'node_modules', 'atlas-app-services-cli', `appservices${os.platform() === 'win32' ? '.exe' : ''}` ); if (fileExists(executable)) { console.log(`Atlas App Services CLI installed fine.`); } else { console.error(`Atlas App Services CLI did not install correctly, file '${executable}' was not found.`); process.exit(2); } try { removeFolder(tempInstallPath); } catch (err) { console.error(`Could not delete folder '${tempInstallPath}'.`); }