Skip to content

Commit

Permalink
feat: pkg as interface
Browse files Browse the repository at this point in the history
  • Loading branch information
musdotdigital committed Nov 30, 2023
1 parent a441092 commit ec07312
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 75 deletions.
5 changes: 3 additions & 2 deletions __tests__/data/dependencies.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[
{
"name": "git",
"version": "2.34.1-r0"
"version": "2.34.1-r0",
"license": "GPL-2.0-or-later"
},
{
"name": "make",
Expand All @@ -15,4 +16,4 @@
"name": "protobuf-dev",
"version": "3.18.1-r1"
}
]
]
4 changes: 2 additions & 2 deletions __tests__/dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {mkdtemp} from 'fs/promises'
import * as path from 'path'

test('save dependencies', async () => {
const pkg = new Package('test', 'latest', {extra: 'field'})
const pkg = {name: 'test', version: 'latest', extra: 'field'}
const tmpdir = await mkdtemp(path.join(os.tmpdir(), 'test-save-dep-'))
const outPath = path.join(tmpdir, 'deps.json')
save(outPath, [pkg])
Expand All @@ -25,7 +25,7 @@ test('save dependencies', async () => {
test('save and load dependencies', async () => {
const tmpdir = await mkdtemp(path.join(os.tmpdir(), 'test-load-dep'))
const depPath = path.join(tmpdir, 'deps.json')
const pkg = new Package('test', 'latest', {extra: 'field'})
const pkg = {name: 'test', version: 'latest', extra: 'field'}

save(depPath, [pkg])
const packages = load(depPath)
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dockerfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('load debian dockerfile', () => {
expect(dockerfile.name).toBe('debian:bullseye-slim')
})

test ('load cuda dockerfile', () => {
test('load cuda dockerfile', () => {
const dockerfilePath = path.join(__dirname, 'data', 'cudaDockerfile')
const dockerfile = load(dockerfilePath)
expect(dockerfile).toBeInstanceOf(DebImage)
Expand Down
32 changes: 4 additions & 28 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Package = exports.save = exports.load = void 0;
exports.save = exports.load = void 0;
const fs_1 = __importDefault(__nccwpck_require__(147));
function load(dependencies_path) {
const content = fs_1.default.readFileSync(dependencies_path).toString('utf-8');
const jsonContent = JSON.parse(content);
return packages_from_dict(jsonContent);
return JSON.parse(content);
}
exports.load = load;
function save(dependencies_path, dependencies) {
const jsonContent = JSON.stringify(dependencies, null, 2);
fs_1.default.writeFileSync(dependencies_path, jsonContent);
}
exports.save = save;
class Package {
constructor(name, version, extraFields) {
this.name = name;
this.version = version;
if (extraFields) {
for (const [key, value] of Object.entries(extraFields)) {
if (key !== 'name' && key !== 'version') {
Object.assign(this, { [key]: value });
}
}
}
}
}
exports.Package = Package;
function packages_from_dict(dict) {
const packages = [];
for (const storedPackage of dict) {
packages.push(new Package(storedPackage.name, storedPackage.version, Object.assign({}, storedPackage)));
}
return packages;
}


/***/ }),
Expand Down Expand Up @@ -122,7 +100,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.factory = exports.DebImage = exports.AlpineImage = exports.Image = void 0;
const docker_cli_js_1 = __nccwpck_require__(771);
const dependencies_1 = __nccwpck_require__(31);
class Image {
constructor(name) {
this.name = name;
Expand All @@ -141,8 +118,7 @@ class AlpineImage extends Image {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.docker.command(`run ${this.name} sh -c "apk update > /dev/null && apk info ${installed_package.name}"`);
const updated_version = remove_prefix(response.raw.split(' ')[0], `${installed_package.name}-`);
// return name and new version, with extra fields
return new dependencies_1.Package(installed_package.name, updated_version, Object.assign({}, installed_package));
return Object.assign(Object.assign({}, installed_package), { version: updated_version });
});
}
}
Expand All @@ -160,7 +136,7 @@ class DebImage extends Image {
}
}
if (updated_version !== undefined) {
return new dependencies_1.Package(installed_package.name, updated_version, Object.assign({}, installed_package));
return Object.assign(Object.assign({}, installed_package), { version: updated_version });
}
throw Error('Unable to extract new version from package infos');
});
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

36 changes: 2 additions & 34 deletions src/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,15 @@ import fs from 'fs'

export function load(dependencies_path: string): Package[] {
const content = fs.readFileSync(dependencies_path).toString('utf-8')
const jsonContent = JSON.parse(content)
return packages_from_dict(jsonContent)
return JSON.parse(content)
}

export function save(dependencies_path: string, dependencies: Package[]): void {
const jsonContent = JSON.stringify(dependencies, null, 2)
fs.writeFileSync(dependencies_path, jsonContent)
}

interface StoredJSON {
export interface Package {
name: string
version: string
}

export class Package {
name: string
version: string

constructor(
name: string,
version: string,
extraFields?: {[key: string]: string}
) {
this.name = name
this.version = version
if (extraFields) {
for (const [key, value] of Object.entries(extraFields)) {
if (key !== 'name' && key !== 'version') {
Object.assign(this, {[key]: value})
}
}
}
}
}

function packages_from_dict(dict: StoredJSON[]): Package[] {
const packages: Package[] = []
for (const storedPackage of dict) {
packages.push(
new Package(storedPackage.name, storedPackage.version, {...storedPackage})
)
}
return packages
}
9 changes: 2 additions & 7 deletions src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ export class AlpineImage extends Image {
response.raw.split(' ')[0],
`${installed_package.name}-`
)
// return name and new version, with extra fields
return new Package(installed_package.name, updated_version, {
...installed_package
})
return {...installed_package, version: updated_version}
}
}

Expand All @@ -54,9 +51,7 @@ export class DebImage extends Image {
}
}
if (updated_version !== undefined) {
return new Package(installed_package.name, updated_version, {
...installed_package
})
return {...installed_package, version: updated_version}
}
throw Error('Unable to extract new version from package infos')
}
Expand Down

0 comments on commit ec07312

Please sign in to comment.