Skip to content

Commit

Permalink
feat: provide different package entrypoint (cli + lib)
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Jul 17, 2024
1 parent 0682b4d commit 8c81980
Show file tree
Hide file tree
Showing 9 changed files with 740 additions and 33 deletions.
617 changes: 616 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
"url": "https://github.com/tada5hi"
},
"scripts": {
"build": "rm -rf ./dist && tsc",
"cli": "node dist/index.js",
"cli-dev": "ts-node src/index.ts",
"build:types": "tsc --emitDeclarationOnly -p tsconfig.json",
"build:js": "rollup -c",
"build": "rimraf dist && rimraf bin && npm run build:types && npm run build:js",
"cli": "node dist/cli.js",
"cli-dev": "ts-node src/cli.ts",
"lint": "eslint --ext .ts,.vue,.js ./src/",
"lint:fix": "npm run lint -- --fix"
},
Expand All @@ -24,7 +26,19 @@
"docker",
"image-scan"
],
"bin": "dist/index.js",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./bin/*": "./bin/*"
},
"bin": "dist/cli.js",
"license": "MIT",
"dependencies": {
"@routup/body": "^2.4.0",
Expand All @@ -48,13 +62,16 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-swc": "^0.3.1",
"@tada5hi/eslint-config-typescript": "^1.2.11",
"@types/cross-spawn": "^6.0.2",
"@types/dockerode": "^3.3.30",
"@types/node": "^20.4.4",
"@types/semver": "^7.5.8",
"@types/tar-fs": "^2.0.4",
"eslint": "^8.48.0",
"rollup": "^4.18.1",
"typescript": "^5.5.2"
}
}
66 changes: 66 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2022.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

import resolve from '@rollup/plugin-node-resolve';
import swc from '@rollup/plugin-swc';
import { builtinModules } from 'node:module';
import fs from "node:fs";

const extensions = [
'.js', '.mjs', '.cjs', '.ts', '.mts', '.cts'
];

const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), {encoding: 'utf-8'}));
const external = Object.keys(pkg.dependencies || {})
.concat(Object.keys(pkg.peerDependencies || {}))
.concat(builtinModules)

export default [
{
input: 'src/index.ts',
external,
output: [
{
format: 'cjs',
file: pkg.main,
exports: 'named',
sourcemap: true
},
{
format: 'es',
file: pkg.module,
sourcemap: true
}
],
plugins: [
// Allows node_modules resolution
resolve({ extensions}),

// Compile TypeScript/JavaScript files
swc()
]
},
{
input: 'src/cli.ts',
external,
output: [
{
format: 'cjs',
file: pkg.bin,
exports: 'named',
sourcemap: true
}
],
plugins: [
// Allows node_modules resolution
resolve({ extensions}),

// Compile TypeScript/JavaScript files
swc()
]
}
]
23 changes: 23 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node
/*
* Copyright (c) 2023.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

import cac from 'cac';
import { registerCLIBuildCommand } from './commands/build';
import { registerCLIListCommand } from './commands/list';
import { registerCLIPushCommand } from './commands/push';
import { registerCLIWebhookCommand } from './commands/webhook';

const cli = cac('master-images');

registerCLIListCommand(cli);
registerCLIBuildCommand(cli);
registerCLIPushCommand(cli);
registerCLIWebhookCommand(cli);

cli.help();
cli.parse();
5 changes: 3 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export enum RegistryEnv {
PASSWORD = 'REGISTRY_PASSWORD',
}

export const PACKAGE_PATH = path.join(__dirname, '..');

export const SCAN_IMAGE_PATH = path.join(
__dirname,
'..',
PACKAGE_PATH,
'data',
);

Expand Down
4 changes: 4 additions & 0 deletions src/core/docker/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ export function useDockerDaemon() {

return instance;
}

export function setDockerDaemon(input: DockerClient) {
instance = input;
}
27 changes: 4 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
#!/usr/bin/env node
/*
* Copyright (c) 2023.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

import cac from 'cac';
import { registerCLIBuildCommand } from './commands/build';
import { registerCLIListCommand } from './commands/list';
import { registerCLIPushCommand } from './commands/push';
import { registerCLIWebhookCommand } from './commands/webhook';

const cli = cac('master-images');

registerCLIListCommand(cli);
registerCLIBuildCommand(cli);
registerCLIPushCommand(cli);
registerCLIWebhookCommand(cli);

cli.help();
cli.parse();
export * from './constants';
export * from './config';
export * from './core';
export * from './utils';
4 changes: 2 additions & 2 deletions src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import fs from 'node:fs';
import path from 'node:path';
import semver from 'semver';
import { VersionTag } from '../constants';
import { PACKAGE_PATH, VersionTag } from '../constants';

export async function getPackageJsonVersionTag() : Promise<`${VersionTag}`> {
const filePath = path.join(__dirname, '..', '..', 'package.json');
const filePath = path.join(PACKAGE_PATH, 'package.json');

const stat = await fs.promises.stat(filePath);
if (!stat.isFile()) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"module": "commonjs",
"outDir": "dist",
"target": "ES2020",
"target": "ESNext",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strictPropertyInitialization": false
Expand Down

0 comments on commit 8c81980

Please sign in to comment.