BREAKING CHANGE(cli): remove legacy container test runner and make the default command show the man page
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@git.zone/tsdocker',
|
||||
version: '1.17.4',
|
||||
version: '2.0.0',
|
||||
description: 'develop npm modules cross platform with docker'
|
||||
}
|
||||
|
||||
@@ -1,15 +1,7 @@
|
||||
/**
|
||||
* Configuration interface for tsdocker
|
||||
* Extends legacy config with new Docker build capabilities
|
||||
*/
|
||||
export interface ITsDockerConfig {
|
||||
// Legacy (backward compatible)
|
||||
baseImage: string;
|
||||
command: string;
|
||||
dockerSock: boolean;
|
||||
keyValueObject: { [key: string]: any };
|
||||
|
||||
// New Docker build config
|
||||
registries?: string[];
|
||||
registryRepoMap?: { [registry: string]: string };
|
||||
buildArgEnvMap?: { [dockerArg: string]: string };
|
||||
|
||||
@@ -3,7 +3,6 @@ import * as paths from './tsdocker.paths.js';
|
||||
|
||||
// modules
|
||||
import * as ConfigModule from './tsdocker.config.js';
|
||||
import * as DockerModule from './tsdocker.docker.js';
|
||||
|
||||
import { logger, ora } from './tsdocker.logging.js';
|
||||
import { TsDockerManager } from './classes.tsdockermanager.js';
|
||||
@@ -14,16 +13,69 @@ import { commitinfo } from './00_commitinfo_data.js';
|
||||
const tsdockerCli = new plugins.smartcli.Smartcli();
|
||||
tsdockerCli.addVersion(commitinfo.version);
|
||||
|
||||
const printManPage = () => {
|
||||
const manPage = `
|
||||
TSDOCKER(1) User Commands TSDOCKER(1)
|
||||
|
||||
NAME
|
||||
tsdocker - build, test, and push Docker images
|
||||
|
||||
VERSION
|
||||
${commitinfo.version}
|
||||
|
||||
SYNOPSIS
|
||||
tsdocker <command> [options]
|
||||
|
||||
COMMANDS
|
||||
build [patterns...] [flags] Build Dockerfiles in dependency order
|
||||
push [patterns...] [flags] Build and push images to registries
|
||||
pull <registry-url> Pull images from a registry
|
||||
test [flags] Build and run container test scripts
|
||||
login Authenticate with configured registries
|
||||
list List discovered Dockerfiles
|
||||
clean [-y] [--all] Interactive Docker resource cleanup
|
||||
|
||||
BUILD / PUSH OPTIONS
|
||||
--platform=<p> Target platform (e.g. linux/arm64)
|
||||
--timeout=<s> Build timeout in seconds
|
||||
--no-cache Rebuild without Docker layer cache
|
||||
--cached Skip builds when Dockerfile is unchanged
|
||||
--verbose Stream raw docker build output
|
||||
--parallel[=<n>] Parallel builds (optional concurrency limit)
|
||||
--context=<name> Docker context to use
|
||||
|
||||
PUSH-ONLY OPTIONS
|
||||
--registry=<url> Push to a specific registry
|
||||
--no-build Push already-built images (skip build step)
|
||||
|
||||
CLEAN OPTIONS
|
||||
-y Auto-confirm all prompts
|
||||
--all Include all images and volumes (not just dangling)
|
||||
|
||||
CONFIGURATION
|
||||
Configure via npmextra.json under the "@git.zone/tsdocker" key:
|
||||
|
||||
registries Array of registry URLs to push to
|
||||
registryRepoMap Map of registry URL to repo path overrides
|
||||
buildArgEnvMap Map of Docker build-arg names to env var names
|
||||
platforms Array of target platforms (default: ["linux/amd64"])
|
||||
push Boolean, auto-push after build
|
||||
testDir Directory containing test_*.sh scripts
|
||||
|
||||
EXAMPLES
|
||||
tsdocker build
|
||||
tsdocker build Dockerfile_app --platform=linux/arm64
|
||||
tsdocker push --registry=ghcr.io
|
||||
tsdocker test --verbose
|
||||
tsdocker clean -y --all
|
||||
`;
|
||||
console.log(manPage);
|
||||
};
|
||||
|
||||
export let run = () => {
|
||||
// Default command: run tests in container (legacy behavior)
|
||||
tsdockerCli.standardCommand().subscribe(async argvArg => {
|
||||
const configArg = await ConfigModule.run().then(DockerModule.run);
|
||||
if (configArg.exitCode === 0) {
|
||||
logger.log('success', 'container ended all right!');
|
||||
} else {
|
||||
logger.log('error', `container ended with error! Exit Code is ${configArg.exitCode}`);
|
||||
process.exit(1);
|
||||
}
|
||||
// Default command: print man page
|
||||
tsdockerCli.standardCommand().subscribe(async () => {
|
||||
printManPage();
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -228,24 +280,6 @@ export let run = () => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* this command is executed inside docker and meant for use from outside docker
|
||||
*/
|
||||
tsdockerCli.addCommand('runinside').subscribe(async argvArg => {
|
||||
logger.log('ok', 'Allright. We are now in Docker!');
|
||||
ora.text('now trying to run your specified command');
|
||||
const configArg = await ConfigModule.run();
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
ora.stop();
|
||||
await smartshellInstance.exec(configArg.command).then(response => {
|
||||
if (response.exitCode !== 0) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
tsdockerCli.addCommand('clean').subscribe(async argvArg => {
|
||||
try {
|
||||
const autoYes = !!argvArg.y;
|
||||
@@ -443,19 +477,5 @@ export let run = () => {
|
||||
}
|
||||
});
|
||||
|
||||
tsdockerCli.addCommand('vscode').subscribe(async argvArg => {
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
logger.log('ok', `Starting vscode in cwd ${paths.cwd}`);
|
||||
await smartshellInstance.execAndWaitForLine(
|
||||
`docker run -p 127.0.0.1:8443:8443 -v "${
|
||||
paths.cwd
|
||||
}:/home/coder/project" registry.gitlab.com/hosttoday/ht-docker-vscode --allow-http --no-auth`,
|
||||
/Connected to shared process/
|
||||
);
|
||||
await plugins.smartopen.openUrl('testing-vscode.git.zone:8443');
|
||||
});
|
||||
|
||||
tsdockerCli.startParse();
|
||||
};
|
||||
|
||||
@@ -1,34 +1,10 @@
|
||||
import * as plugins from './tsdocker.plugins.js';
|
||||
import * as paths from './tsdocker.paths.js';
|
||||
import * as fs from 'fs';
|
||||
import type { ITsDockerConfig } from './interfaces/index.js';
|
||||
|
||||
// Re-export ITsDockerConfig as IConfig for backward compatibility
|
||||
export type IConfig = ITsDockerConfig & {
|
||||
exitCode?: number;
|
||||
};
|
||||
|
||||
const getQenvKeyValueObject = async () => {
|
||||
let qenvKeyValueObjectArray: { [key: string]: string | number };
|
||||
if (fs.existsSync(plugins.path.join(paths.cwd, 'qenv.yml'))) {
|
||||
qenvKeyValueObjectArray = new plugins.qenv.Qenv(paths.cwd, '.nogit/').keyValueObject;
|
||||
} else {
|
||||
qenvKeyValueObjectArray = {};
|
||||
}
|
||||
return qenvKeyValueObjectArray;
|
||||
};
|
||||
|
||||
const buildConfig = async (qenvKeyValueObjectArg: { [key: string]: string | number }) => {
|
||||
const buildConfig = async (): Promise<ITsDockerConfig> => {
|
||||
const npmextra = new plugins.npmextra.Npmextra(paths.cwd);
|
||||
const config = npmextra.dataFor<IConfig>('@git.zone/tsdocker', {
|
||||
// Legacy options (backward compatible)
|
||||
baseImage: 'hosttoday/ht-docker-node:npmdocker',
|
||||
init: 'rm -rf node_nodules/ && yarn install',
|
||||
command: 'npmci npm test',
|
||||
dockerSock: false,
|
||||
keyValueObject: qenvKeyValueObjectArg,
|
||||
|
||||
// New Docker build options
|
||||
const config = npmextra.dataFor<ITsDockerConfig>('@git.zone/tsdocker', {
|
||||
registries: [],
|
||||
registryRepoMap: {},
|
||||
buildArgEnvMap: {},
|
||||
@@ -39,7 +15,6 @@ const buildConfig = async (qenvKeyValueObjectArg: { [key: string]: string | numb
|
||||
return config;
|
||||
};
|
||||
|
||||
export let run = async (): Promise<IConfig> => {
|
||||
const config = await getQenvKeyValueObject().then(buildConfig);
|
||||
return config;
|
||||
export let run = async (): Promise<ITsDockerConfig> => {
|
||||
return buildConfig();
|
||||
};
|
||||
|
||||
@@ -1,169 +0,0 @@
|
||||
import * as plugins from './tsdocker.plugins.js';
|
||||
import * as paths from './tsdocker.paths.js';
|
||||
import * as snippets from './tsdocker.snippets.js';
|
||||
|
||||
import { logger, ora } from './tsdocker.logging.js';
|
||||
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
|
||||
// interfaces
|
||||
import type { IConfig } from './tsdocker.config.js';
|
||||
|
||||
let config: IConfig;
|
||||
|
||||
/**
|
||||
* the docker data used to build the internal testing container
|
||||
*/
|
||||
const dockerData = {
|
||||
imageTag: 'npmdocker-temp-image:latest',
|
||||
containerName: 'npmdocker-temp-container',
|
||||
dockerProjectMountString: '',
|
||||
dockerSockString: '',
|
||||
dockerEnvString: ''
|
||||
};
|
||||
|
||||
/**
|
||||
* check if docker is available
|
||||
*/
|
||||
const checkDocker = () => {
|
||||
const done = plugins.smartpromise.defer();
|
||||
ora.text('checking docker...');
|
||||
|
||||
if (smartshellInstance.exec('which docker')) {
|
||||
logger.log('ok', 'Docker found!');
|
||||
done.resolve();
|
||||
} else {
|
||||
done.reject(new Error('docker not found on this machine'));
|
||||
}
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* builds the Dockerfile according to the config in the project
|
||||
*/
|
||||
const buildDockerFile = async () => {
|
||||
const done = plugins.smartpromise.defer();
|
||||
ora.text('building Dockerfile...');
|
||||
const dockerfile: string = snippets.dockerfileSnippet({
|
||||
baseImage: config.baseImage,
|
||||
command: config.command
|
||||
});
|
||||
logger.log('info', `Base image is: ${config.baseImage}`);
|
||||
logger.log('info', `Command is: ${config.command}`);
|
||||
await plugins.smartfs.file(plugins.path.join(paths.cwd, 'npmdocker')).write(dockerfile);
|
||||
logger.log('ok', 'Dockerfile created!');
|
||||
ora.stop();
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* builds the Dockerimage from the built Dockerfile
|
||||
*/
|
||||
const buildDockerImage = async () => {
|
||||
logger.log('info', 'pulling latest base image from registry...');
|
||||
await smartshellInstance.exec(`docker pull ${config.baseImage}`);
|
||||
ora.text('building Dockerimage...');
|
||||
const execResult = await smartshellInstance.execSilent(
|
||||
`docker build --load -f npmdocker -t ${dockerData.imageTag} ${paths.cwd}`
|
||||
);
|
||||
if (execResult.exitCode !== 0) {
|
||||
console.log(execResult.stdout);
|
||||
process.exit(1);
|
||||
}
|
||||
logger.log('ok', 'Dockerimage built!');
|
||||
};
|
||||
|
||||
const buildDockerProjectMountString = async () => {
|
||||
if (process.env.CI !== 'true') {
|
||||
dockerData.dockerProjectMountString = `-v ${paths.cwd}:/workspace`;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* builds an environment string that docker cli understands
|
||||
*/
|
||||
const buildDockerEnvString = async () => {
|
||||
for (const key of Object.keys(config.keyValueObject)) {
|
||||
const envString = (dockerData.dockerEnvString =
|
||||
dockerData.dockerEnvString + `-e ${key}=${config.keyValueObject[key]} `);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* creates string to mount the docker.sock inside the testcontainer
|
||||
*/
|
||||
const buildDockerSockString = async () => {
|
||||
if (config.dockerSock) {
|
||||
dockerData.dockerSockString = `-v /var/run/docker.sock:/var/run/docker.sock`;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* creates a container by running the built Dockerimage
|
||||
*/
|
||||
const runDockerImage = async () => {
|
||||
const done = plugins.smartpromise.defer();
|
||||
ora.text('starting Container...');
|
||||
ora.stop();
|
||||
logger.log('info', 'now running Dockerimage');
|
||||
config.exitCode = (await smartshellInstance.exec(
|
||||
`docker run ${dockerData.dockerProjectMountString} ${dockerData.dockerSockString} ${
|
||||
dockerData.dockerEnvString
|
||||
} --name ${dockerData.containerName} ${dockerData.imageTag}`
|
||||
)).exitCode;
|
||||
};
|
||||
|
||||
/**
|
||||
* cleans up: deletes the test container
|
||||
*/
|
||||
const deleteDockerContainer = async () => {
|
||||
await smartshellInstance.execSilent(`docker rm -f ${dockerData.containerName}`);
|
||||
};
|
||||
|
||||
/**
|
||||
* cleans up deletes the test image
|
||||
*/
|
||||
const deleteDockerImage = async () => {
|
||||
await smartshellInstance.execSilent(`docker rmi ${dockerData.imageTag}`).then(async response => {
|
||||
if (response.exitCode !== 0) {
|
||||
console.log(response.stdout);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const preClean = async () => {
|
||||
await deleteDockerImage()
|
||||
.then(deleteDockerContainer)
|
||||
.then(async () => {
|
||||
logger.log('ok', 'ensured clean Docker environment!');
|
||||
});
|
||||
};
|
||||
|
||||
const postClean = async () => {
|
||||
await deleteDockerContainer()
|
||||
.then(deleteDockerImage)
|
||||
.then(async () => {
|
||||
logger.log('ok', 'cleaned up!');
|
||||
});
|
||||
await plugins.smartfs.file(paths.npmdockerFile).delete();
|
||||
};
|
||||
|
||||
export let run = async (configArg: IConfig): Promise<IConfig> => {
|
||||
config = configArg;
|
||||
const resultConfig = await checkDocker()
|
||||
.then(preClean)
|
||||
.then(buildDockerFile)
|
||||
.then(buildDockerImage)
|
||||
.then(buildDockerProjectMountString)
|
||||
.then(buildDockerEnvString)
|
||||
.then(buildDockerSockString)
|
||||
.then(runDockerImage)
|
||||
.then(postClean)
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
return config;
|
||||
};
|
||||
@@ -11,4 +11,3 @@ export let cwd = process.cwd();
|
||||
export let packageBase = plugins.path.join(__dirname, '../');
|
||||
export let assets = plugins.path.join(packageBase, 'assets/');
|
||||
fs.mkdirSync(assets, { recursive: true });
|
||||
export let npmdockerFile = plugins.path.join(cwd, 'npmdocker');
|
||||
|
||||
@@ -3,17 +3,13 @@ import * as lik from '@push.rocks/lik';
|
||||
import * as npmextra from '@push.rocks/npmextra';
|
||||
import * as path from 'path';
|
||||
import * as projectinfo from '@push.rocks/projectinfo';
|
||||
import * as smartpromise from '@push.rocks/smartpromise';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
import * as smartcli from '@push.rocks/smartcli';
|
||||
import { SmartFs, SmartFsProviderNode } from '@push.rocks/smartfs';
|
||||
import * as smartlog from '@push.rocks/smartlog';
|
||||
import * as smartlogDestinationLocal from '@push.rocks/smartlog-destination-local';
|
||||
import * as smartlogSouceOra from '@push.rocks/smartlog-source-ora';
|
||||
import * as smartopen from '@push.rocks/smartopen';
|
||||
import * as smartinteract from '@push.rocks/smartinteract';
|
||||
import * as smartshell from '@push.rocks/smartshell';
|
||||
import * as smartstring from '@push.rocks/smartstring';
|
||||
|
||||
// Create smartfs instance
|
||||
export const smartfs = new SmartFs(new SmartFsProviderNode());
|
||||
@@ -23,14 +19,10 @@ export {
|
||||
npmextra,
|
||||
path,
|
||||
projectinfo,
|
||||
smartpromise,
|
||||
qenv,
|
||||
smartcli,
|
||||
smartinteract,
|
||||
smartlog,
|
||||
smartlogDestinationLocal,
|
||||
smartlogSouceOra,
|
||||
smartopen,
|
||||
smartshell,
|
||||
smartstring
|
||||
};
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
import * as plugins from './tsdocker.plugins.js';
|
||||
|
||||
export interface IDockerfileSnippet {
|
||||
baseImage: string;
|
||||
command: string;
|
||||
}
|
||||
|
||||
let getMountSolutionString = (optionsArg: IDockerfileSnippet) => {
|
||||
if (process.env.CI) {
|
||||
return 'COPY ./ /workspace';
|
||||
} else {
|
||||
return '# not copying workspcae since not in CI';
|
||||
}
|
||||
};
|
||||
|
||||
let getGlobalPreparationString = (optionsArg: IDockerfileSnippet) => {
|
||||
// Always install tsdocker to ensure the latest version is available
|
||||
return 'RUN npm install -g @git.zone/tsdocker';
|
||||
};
|
||||
|
||||
export let dockerfileSnippet = (optionsArg: IDockerfileSnippet): string => {
|
||||
return plugins.smartstring.indent.normalize(
|
||||
`
|
||||
FROM ${optionsArg.baseImage}
|
||||
# For info about what tsdocker does read the docs at https://gitzone.github.io/tsdocker
|
||||
${getGlobalPreparationString(optionsArg)}
|
||||
${getMountSolutionString(optionsArg)}
|
||||
WORKDIR /workspace
|
||||
ENV CI=true
|
||||
ENTRYPOINT ["tsdocker"]
|
||||
CMD ["runinside"]
|
||||
`
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user