mirror of
https://github.com/engineer-man/piston.git
synced 2025-06-07 18:56:26 +02:00
Piston lint
This commit is contained in:
parent
d61fb8ec5b
commit
f2c91acbe6
57 changed files with 1121 additions and 893 deletions
|
@ -1,49 +1,53 @@
|
|||
const chalk = require('chalk');
|
||||
const fs = require('fs/promises');
|
||||
const minimatch = require("minimatch");
|
||||
const minimatch = require('minimatch');
|
||||
const semver = require('semver');
|
||||
|
||||
exports.command = ['spec <specfile>'];
|
||||
exports.aliases = ['s'];
|
||||
exports.describe = 'Install the packages described in the spec file, uninstalling packages which aren\'t in the list'
|
||||
exports.describe =
|
||||
"Install the packages described in the spec file, uninstalling packages which aren't in the list";
|
||||
|
||||
function does_match(package, rule){
|
||||
function does_match(package, rule) {
|
||||
const nameMatch = minimatch(package.language, rule.package_selector);
|
||||
const versionMatch = semver.satisfies(package.language_version, rule.version_selector)
|
||||
const versionMatch = semver.satisfies(
|
||||
package.language_version,
|
||||
rule.version_selector
|
||||
);
|
||||
|
||||
return nameMatch && versionMatch;
|
||||
}
|
||||
|
||||
exports.handler = async ({axios, specfile}) => {
|
||||
exports.handler = async ({ axios, specfile }) => {
|
||||
const spec_contents = await fs.readFile(specfile);
|
||||
const spec_lines = spec_contents.toString().split("\n");
|
||||
const spec_lines = spec_contents.toString().split('\n');
|
||||
|
||||
const rules = [];
|
||||
|
||||
for(const line of spec_lines){
|
||||
for (const line of spec_lines) {
|
||||
const rule = {
|
||||
_raw: line.trim(),
|
||||
comment: false,
|
||||
package_selector: null,
|
||||
version_selector: null,
|
||||
negate: false
|
||||
negate: false,
|
||||
};
|
||||
|
||||
if(line.starts_with("#")){
|
||||
if (line.starts_with('#')) {
|
||||
rule.comment = true;
|
||||
}else {
|
||||
} else {
|
||||
let l = line.trim();
|
||||
if(line.starts_with("!")){
|
||||
if (line.starts_with('!')) {
|
||||
rule.negate = true;
|
||||
l = line.slice(1).trim();
|
||||
}
|
||||
|
||||
const [pkg, ver] = l.split(" ", 2);
|
||||
const [pkg, ver] = l.split(' ', 2);
|
||||
rule.package_selector = pkg;
|
||||
rule.version_selector = ver;
|
||||
}
|
||||
|
||||
if(rule._raw.length != 0) rules.push(rule);
|
||||
if (rule._raw.length != 0) rules.push(rule);
|
||||
}
|
||||
|
||||
const packages_req = await axios.get('/api/v2/packages');
|
||||
|
@ -53,108 +57,127 @@ exports.handler = async ({axios, specfile}) => {
|
|||
|
||||
let ensure_packages = [];
|
||||
|
||||
for(const rule of rules){
|
||||
if(rule.comment) continue;
|
||||
for (const rule of rules) {
|
||||
if (rule.comment) continue;
|
||||
|
||||
const matches = [];
|
||||
|
||||
if(!rule.negate){
|
||||
for(const package of packages){
|
||||
if(does_match(package, rule))
|
||||
matches.push(package)
|
||||
if (!rule.negate) {
|
||||
for (const package of packages) {
|
||||
if (does_match(package, rule)) matches.push(package);
|
||||
}
|
||||
|
||||
const latest_matches = matches.filter(
|
||||
pkg => {
|
||||
const versions = matches
|
||||
.filter(x=>x.language == pkg.language)
|
||||
.map(x=>x.language_version).sort(semver.rcompare);
|
||||
return versions[0] == pkg.language_version
|
||||
}
|
||||
);
|
||||
const latest_matches = matches.filter(pkg => {
|
||||
const versions = matches
|
||||
.filter(x => x.language == pkg.language)
|
||||
.map(x => x.language_version)
|
||||
.sort(semver.rcompare);
|
||||
return versions[0] == pkg.language_version;
|
||||
});
|
||||
|
||||
for(const match of latest_matches){
|
||||
if(!ensure_packages.find(pkg => pkg.language == match.language && pkg.language_version == match.language_version))
|
||||
ensure_packages.push(match)
|
||||
for (const match of latest_matches) {
|
||||
if (
|
||||
!ensure_packages.find(
|
||||
pkg =>
|
||||
pkg.language == match.language &&
|
||||
pkg.language_version == match.language_version
|
||||
)
|
||||
)
|
||||
ensure_packages.push(match);
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
ensure_packages = ensure_packages.filter(
|
||||
pkg => !does_match(pkg, rule)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
const operations = [];
|
||||
|
||||
for(const package of ensure_packages){
|
||||
if(!package.installed)
|
||||
for (const package of ensure_packages) {
|
||||
if (!package.installed)
|
||||
operations.push({
|
||||
type: "install",
|
||||
type: 'install',
|
||||
package: package.language,
|
||||
version: package.language_version
|
||||
version: package.language_version,
|
||||
});
|
||||
}
|
||||
|
||||
for(const installed_package of installed){
|
||||
if(!ensure_packages.find(
|
||||
pkg => pkg.language == installed_package.language &&
|
||||
pkg.language_version == installed_package.language_version
|
||||
))
|
||||
for (const installed_package of installed) {
|
||||
if (
|
||||
!ensure_packages.find(
|
||||
pkg =>
|
||||
pkg.language == installed_package.language &&
|
||||
pkg.language_version == installed_package.language_version
|
||||
)
|
||||
)
|
||||
operations.push({
|
||||
type: "uninstall",
|
||||
type: 'uninstall',
|
||||
package: installed_package.language,
|
||||
version: installed_package.language_version
|
||||
})
|
||||
version: installed_package.language_version,
|
||||
});
|
||||
}
|
||||
|
||||
console.log(chalk.bold.yellow("Actions"))
|
||||
for(const op of operations){
|
||||
console.log((op.type == "install" ? chalk.green("Install") : chalk.red("Uninstall")) + ` ${op.package} ${op.version}`)
|
||||
console.log(chalk.bold.yellow('Actions'));
|
||||
for (const op of operations) {
|
||||
console.log(
|
||||
(op.type == 'install'
|
||||
? chalk.green('Install')
|
||||
: chalk.red('Uninstall')) + ` ${op.package} ${op.version}`
|
||||
);
|
||||
}
|
||||
|
||||
if(operations.length == 0){
|
||||
console.log(chalk.gray("None"))
|
||||
if (operations.length == 0) {
|
||||
console.log(chalk.gray('None'));
|
||||
}
|
||||
|
||||
for(const op of operations){
|
||||
if(op.type == "install"){
|
||||
try{
|
||||
for (const op of operations) {
|
||||
if (op.type == 'install') {
|
||||
try {
|
||||
const install = await axios.post(`/api/v2/packages`, {
|
||||
language: op.package,
|
||||
version: op.version
|
||||
version: op.version,
|
||||
});
|
||||
|
||||
if(!install.data.language)
|
||||
if (!install.data.language)
|
||||
throw new Error(install.data.message); // Go to exception handler
|
||||
|
||||
console.log(chalk.bold.green("Installed"), op.package, op.version)
|
||||
|
||||
}catch(e){
|
||||
console.log(chalk.bold.red("Failed to install") + ` ${op.package} ${op.version}:`, e.message)
|
||||
console.log(
|
||||
chalk.bold.green('Installed'),
|
||||
op.package,
|
||||
op.version
|
||||
);
|
||||
} catch (e) {
|
||||
console.log(
|
||||
chalk.bold.red('Failed to install') +
|
||||
` ${op.package} ${op.version}:`,
|
||||
e.message
|
||||
);
|
||||
}
|
||||
}
|
||||
else if(op.type == "uninstall"){
|
||||
try{
|
||||
} else if (op.type == 'uninstall') {
|
||||
try {
|
||||
const install = await axios.delete(`/api/v2/packages`, {
|
||||
data: {
|
||||
language: op.package,
|
||||
version: op.version
|
||||
}
|
||||
version: op.version,
|
||||
},
|
||||
});
|
||||
|
||||
if(!install.data.language)
|
||||
if (!install.data.language)
|
||||
throw new Error(install.data.message); // Go to exception handler
|
||||
|
||||
console.log(chalk.bold.green("Uninstalled"), op.package, op.version)
|
||||
|
||||
}catch(e){
|
||||
console.log(chalk.bold.red("Failed to uninstall") + ` ${op.package} ${op.version}:`, e.message)
|
||||
console.log(
|
||||
chalk.bold.green('Uninstalled'),
|
||||
op.package,
|
||||
op.version
|
||||
);
|
||||
} catch (e) {
|
||||
console.log(
|
||||
chalk.bold.red('Failed to uninstall') +
|
||||
` ${op.package} ${op.version}:`,
|
||||
e.message
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue