@enspirit/emb
Version:
A replacement for our Makefile-for-monorepos
60 lines (59 loc) • 2.16 kB
JavaScript
import { getContext } from '../../../index.js';
import { Flags } from '@oclif/core';
import { printTable } from '@oclif/table';
import { FlavoredCommand, TABLE_DEFAULTS } from '../../index.js';
import { listImages, shortId } from '../../../docker/index.js';
import { timeAgo } from '../../../utils/index.js';
export default class ImagesIndex extends FlavoredCommand {
static description = 'List docker images.';
static enableJsonFlag = true;
static examples = ['<%= config.bin %> <%= command.id %>'];
static flags = {
all: Flags.boolean({
char: 'a',
default: false,
description: 'Show all images. Only images from a final layer (no children) are shown by default.',
name: 'all',
required: false,
}),
};
async run() {
const { flags } = await this.parse(ImagesIndex);
const context = await getContext();
const images = await listImages({
all: flags.all,
filters: {
label: [`emb/project=${context.monorepo.name}`],
},
});
const flatten = images.reduce((imgs, img) => {
const matches = (img.RepoTags || [])
?.filter((tag) => tag.indexOf(context.monorepo.name) === 0)
.map((m) => {
const [name, tag] = m.split(':');
return {
created: new Date(img.Created * 1000),
imageId: shortId(img.Id),
name,
size: img.Size,
tag,
};
});
return [...imgs, ...matches];
}, []);
if (!flags.json) {
printTable({
...TABLE_DEFAULTS,
columns: ['name', 'tag', 'imageId', 'created', 'size'],
data: flatten.map((f) => {
return {
...f,
created: timeAgo(f.created),
size: Math.floor(f.size / (1000 * 1000)) + 'MB',
};
}),
});
}
return flatten;
}
}