saturn-core-cli 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/spice +3 -0
- package/commands/error.js +13 -0
- package/commands/index.js +24 -0
- package/commands/publish.js +85 -0
- package/index.js +97 -0
- package/lib/status.js +19 -0
- package/mapper.js +67 -0
- package/package.json +38 -0
package/bin/spice
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var program = null;
|
|
2
|
+
function command(prog) {
|
|
3
|
+
'use strict';
|
|
4
|
+
program = prog;
|
|
5
|
+
program.on('command:*', function () {
|
|
6
|
+
console.error('Invalid command: %s\nsc --help for a list of available commands.', program.args.join(' '));
|
|
7
|
+
process.exit(1);
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
command: command
|
|
13
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
var fs = require('fs');
|
|
2
|
+
var path = require('path');
|
|
3
|
+
process.setMaxListeners(10000)
|
|
4
|
+
|
|
5
|
+
module.exports = function commandLoader(program) {
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
var commands = {};
|
|
9
|
+
var loadPath = path.dirname(__filename);
|
|
10
|
+
// Loop though command files
|
|
11
|
+
fs.readdirSync(loadPath).filter(function (filename) {
|
|
12
|
+
return (/\.js$/.test(filename) && filename !== 'index.js');
|
|
13
|
+
}).forEach(function (filename) {
|
|
14
|
+
var name = filename.substr(0, filename.lastIndexOf('.'));
|
|
15
|
+
|
|
16
|
+
// Require command
|
|
17
|
+
var command = require(path.join(loadPath, filename)).command;
|
|
18
|
+
|
|
19
|
+
// Initialize command
|
|
20
|
+
commands[name] = command(program);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return commands;
|
|
24
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
var exec = require("child-process-promise").exec;
|
|
2
|
+
var spawn = require("child-process-promise").spawn;
|
|
3
|
+
var status = require("../lib/status");
|
|
4
|
+
let axios = require("axios");
|
|
5
|
+
let path = require("path");
|
|
6
|
+
let _ = require("lodash");
|
|
7
|
+
let fs = require("fs-extra");
|
|
8
|
+
var program = null;
|
|
9
|
+
var mapper = require("../mapper");
|
|
10
|
+
let saturnignore = path.join(path.resolve("./"), ".saturnignore.js");
|
|
11
|
+
let exemptions = null;
|
|
12
|
+
const SATURN_URL = "https://managerapi.saturn.gd/api";
|
|
13
|
+
const SATURN_API_TOKEN =
|
|
14
|
+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InVzZXItZGM5MGJhN2ItZjY0MS00OGVjLWEwZjgtNDNlZjk3MzA5MTk2IiwiZ3JvdXAiOiJ1c2VyX2dyb3VwLTBiNDNjYjdhLTY5NzgtNGYwMy1iNGRkLTJiODVjMzBkYzVkOSIsImlhdCI6MTY0NzMzNjQzOX0.qigpTkuHyK9ovX-e4JhybxqcGa2Y-7miR29HOp_Vjdg";
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
exemptions = require(saturnignore);
|
|
18
|
+
} catch (e) {
|
|
19
|
+
exemptions = [];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function action(version, options) {
|
|
23
|
+
try {
|
|
24
|
+
const packageObj = await fs.readJson(`package.json`);
|
|
25
|
+
if (version == undefined) {
|
|
26
|
+
version = packageObj.version;
|
|
27
|
+
}
|
|
28
|
+
status.start();
|
|
29
|
+
let map = await new mapper({ exemptions: exemptions }).createMap("./");
|
|
30
|
+
if (options.dry) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
let core = await getCoreId(packageObj.saturn.core);
|
|
34
|
+
console.log(await createInstance(map, core.id, version));
|
|
35
|
+
status.stop();
|
|
36
|
+
} catch (e) {
|
|
37
|
+
console.log(e.stack);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function getCoreId(core) {
|
|
42
|
+
let response = await axios.get(`${SATURN_URL}/cores`, {
|
|
43
|
+
headers: {
|
|
44
|
+
Authorization: `Bearer ${SATURN_API_TOKEN}`,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
let mapped_result = _.find(response.data.data, ["name", core]);
|
|
48
|
+
return mapped_result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function createInstance(map, core, version) {
|
|
52
|
+
try {
|
|
53
|
+
var formData = {
|
|
54
|
+
map: JSON.stringify({ version, map }),
|
|
55
|
+
core,
|
|
56
|
+
version,
|
|
57
|
+
};
|
|
58
|
+
let response = await axios.post(
|
|
59
|
+
`${SATURN_URL}/saturn_instances`,
|
|
60
|
+
formData,
|
|
61
|
+
{
|
|
62
|
+
headers: {
|
|
63
|
+
Authorization: `Bearer ${SATURN_API_TOKEN}`,
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
return response.data.data;
|
|
68
|
+
} catch (e) {
|
|
69
|
+
console.log("ERROR", e.message);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function command(prog) {
|
|
74
|
+
"use strict";
|
|
75
|
+
program = prog;
|
|
76
|
+
program
|
|
77
|
+
.command("publish [version]", { isDefault: true })
|
|
78
|
+
.option("-d, --dry <template>", "Dry Run", false)
|
|
79
|
+
.description("Publish Saturn Version")
|
|
80
|
+
.action(action);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
module.exports = {
|
|
84
|
+
command: command,
|
|
85
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
require("babel-polyfill");
|
|
3
|
+
|
|
4
|
+
var fs = require("fs");
|
|
5
|
+
var util = require("util");
|
|
6
|
+
var status = require("./lib/status");
|
|
7
|
+
var program = require("commander");
|
|
8
|
+
var commands = require("./commands")(program);
|
|
9
|
+
var packageJson = require("./package.json");
|
|
10
|
+
|
|
11
|
+
program.LOG_PATH = process.env.HOME + "/.cli-log";
|
|
12
|
+
|
|
13
|
+
// Initialize cli options
|
|
14
|
+
program
|
|
15
|
+
.version(packageJson.version)
|
|
16
|
+
.usage("<command> [options]")
|
|
17
|
+
.option("-d, --debug", "show debug info");
|
|
18
|
+
|
|
19
|
+
// Initialize prompt
|
|
20
|
+
program.prompt = require("prompt");
|
|
21
|
+
program.prompt.message = "";
|
|
22
|
+
program.prompt.delimiter = "";
|
|
23
|
+
program.prompt.colors = false;
|
|
24
|
+
|
|
25
|
+
// Turn off colors when non-interactive
|
|
26
|
+
var colors = require("colors");
|
|
27
|
+
colors.mode = process.stdout.isTTY ? colors.mode : "none";
|
|
28
|
+
|
|
29
|
+
// Setup logging and messaging
|
|
30
|
+
var logMessages = [];
|
|
31
|
+
program.log = (function (debugMode) {
|
|
32
|
+
return function _log(logEntry, noPrint) {
|
|
33
|
+
logMessages.push(logEntry);
|
|
34
|
+
if (!noPrint && debugMode) {
|
|
35
|
+
console.log("--debug-- ".cyan + logEntry);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
})(process.argv.indexOf("--debug") >= 0);
|
|
39
|
+
|
|
40
|
+
program.successMessage = function successMessage() {
|
|
41
|
+
var msg = util.format.apply(this, arguments);
|
|
42
|
+
program.log("Success: " + msg, true);
|
|
43
|
+
console.log(msg.green);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
program.errorMessage = function errorMessage() {
|
|
47
|
+
var msg = util.format.apply(this, arguments);
|
|
48
|
+
program.log("Error: " + msg, true);
|
|
49
|
+
console.log(msg.red);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
program.handleError = function handleError(err, exitCode) {
|
|
53
|
+
if (err) {
|
|
54
|
+
if (err.message) {
|
|
55
|
+
program.errorMessage(err.message);
|
|
56
|
+
} else {
|
|
57
|
+
program.errorMessage(err);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
fs.writeFileSync(program.LOG_PATH, logMessages.join("\n") + "\n");
|
|
62
|
+
|
|
63
|
+
process.exit(exitCode || 1);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// Create request wrapper
|
|
67
|
+
program.request = function (opts, next) {
|
|
68
|
+
if (program.debug) {
|
|
69
|
+
program.log("REQUEST: ".bold + JSON.stringify(opts, null, 2));
|
|
70
|
+
} else {
|
|
71
|
+
program.log(opts.uri);
|
|
72
|
+
}
|
|
73
|
+
status.start();
|
|
74
|
+
return request(opts, function (err, res, body) {
|
|
75
|
+
status.stop();
|
|
76
|
+
if (err) {
|
|
77
|
+
if (program.debug) {
|
|
78
|
+
program.errorMessage(err.message);
|
|
79
|
+
}
|
|
80
|
+
return next(err, res, body);
|
|
81
|
+
} else {
|
|
82
|
+
if (program.debug) {
|
|
83
|
+
program.log("RESPONSE: ".bold + JSON.stringify(res.headers, null, 2));
|
|
84
|
+
program.log("BODY: ".bold + JSON.stringify(res.body, null, 2));
|
|
85
|
+
}
|
|
86
|
+
return next(err, res, body);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
program.on("*", function () {
|
|
92
|
+
console.log("Unknown Command: " + program.args.join(" "));
|
|
93
|
+
program.help();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Process Commands
|
|
97
|
+
program.parse(process.argv);
|
package/lib/status.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
var _intervalId = null;
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
start: function () {
|
|
5
|
+
process.stdout.write('.');
|
|
6
|
+
if (process.stdout.isTTY) {
|
|
7
|
+
_intervalId = _intervalId || setInterval(function () {
|
|
8
|
+
process.stdout.write('.');
|
|
9
|
+
}, 500);
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
stop: function () {
|
|
13
|
+
if (process.stdout.isTTY && _intervalId) {
|
|
14
|
+
clearInterval(_intervalId);
|
|
15
|
+
_intervalId = null;
|
|
16
|
+
console.log('');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
};
|
package/mapper.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
let klawSync = require("klaw-sync");
|
|
2
|
+
let _ = require("lodash");
|
|
3
|
+
let path = require("path");
|
|
4
|
+
let crypto = require("crypto");
|
|
5
|
+
let fs = require("fs");
|
|
6
|
+
|
|
7
|
+
let exemptions = [];
|
|
8
|
+
let dir_path = ".";
|
|
9
|
+
|
|
10
|
+
function generateChecksum(file) {
|
|
11
|
+
return new Promise(function (resolve, reject) {
|
|
12
|
+
try {
|
|
13
|
+
var hash = crypto.createHash("md5");
|
|
14
|
+
var stream = fs.createReadStream(file);
|
|
15
|
+
stream.on("data", function (data) {
|
|
16
|
+
hash.update(data);
|
|
17
|
+
});
|
|
18
|
+
stream.on("end", function () {
|
|
19
|
+
var sha = hash.digest("hex");
|
|
20
|
+
resolve(sha);
|
|
21
|
+
});
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isExemptedDirectory(item) {
|
|
29
|
+
let file = _.last(item.path.split(dir_path + "/"));
|
|
30
|
+
let included = _.includes(
|
|
31
|
+
_.map(exemptions, (exem) => _.startsWith(file, exem)),
|
|
32
|
+
true
|
|
33
|
+
);
|
|
34
|
+
return !included;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = class Mapper {
|
|
38
|
+
constructor(options) {
|
|
39
|
+
exemptions = options.exemptions;
|
|
40
|
+
}
|
|
41
|
+
async createMap(root_path) {
|
|
42
|
+
try {
|
|
43
|
+
dir_path = path.resolve(root_path);
|
|
44
|
+
const files = klawSync(dir_path, {
|
|
45
|
+
filter: isExemptedDirectory,
|
|
46
|
+
nodir: true,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return _.groupBy(
|
|
50
|
+
await Promise.all(
|
|
51
|
+
files.map(async function (file) {
|
|
52
|
+
return {
|
|
53
|
+
file: _.last(file.path.split(dir_path + "/")),
|
|
54
|
+
size: file.stats.size,
|
|
55
|
+
|
|
56
|
+
checksum: await generateChecksum(file.path),
|
|
57
|
+
creation_date: file.stats.birthtime,
|
|
58
|
+
};
|
|
59
|
+
})
|
|
60
|
+
),
|
|
61
|
+
"file"
|
|
62
|
+
);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
console.log(e);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "saturn-core-cli",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Saturn Core CLI",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"patch": "npm version patch",
|
|
8
|
+
"publish": "npm publish"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"sc": "index.js"
|
|
12
|
+
},
|
|
13
|
+
"preferGlobal": true,
|
|
14
|
+
"author": "Chad Fraser <chad@sonover.com> (http://sonover.com)",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"babel-polyfill": "^6.26.0",
|
|
18
|
+
"child-process-promise": "^2.2.1",
|
|
19
|
+
"colors": "^1.4.0",
|
|
20
|
+
"commander": "^9.2.0",
|
|
21
|
+
"axios": "^0.27.2",
|
|
22
|
+
"chokidar": "^3.5.2",
|
|
23
|
+
"fs-extra": "^8.1.0",
|
|
24
|
+
"pluralize": "^8.0.0",
|
|
25
|
+
"prompt": "^1.0.0",
|
|
26
|
+
"klaw-sync": "^6.0.0",
|
|
27
|
+
"lodash": "^4.17.21"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@babel/core": "^7.8.4",
|
|
31
|
+
"@babel/plugin-proposal-class-properties": "^7.8.3",
|
|
32
|
+
"@babel/plugin-proposal-export-default-from": "^7.8.3",
|
|
33
|
+
"@babel/plugin-proposal-export-namespace-from": "^7.8.3",
|
|
34
|
+
"@babel/preset-env": "^7.8.4",
|
|
35
|
+
"gulp": "^4.0.2",
|
|
36
|
+
"gulp-babel": "^8.0.0"
|
|
37
|
+
}
|
|
38
|
+
}
|