mirror of
https://github.com/nestjs/nest.git
synced 2026-10-28 09:45:43 +08:00
chore(scripts): support both docker compose v2 and docker-compose v1
Signed-off-by: Ali Hassan <ali-hassan27@outlook.com>
This commit is contained in:
1 parent
409f703c40
commit
557f964820
2 files changed
+63
-2
No files matched your search
+2
-2
@@ -33,8 +33,8 @@
|
||||
"pretest:cov": "npm run clean",
|
||||
"test:cov": "nyc mocha packages/**/*.spec.ts --reporter spec",
|
||||
"test:integration": "mocha --reporter-option maxDiffSize=0 \"integration/*/{,!(node_modules)/**/}/*.spec.ts\"",
|
||||
"test:docker:up": "docker-compose -f integration/docker-compose.yml up -d",
|
||||
"test:docker:down": "docker-compose -f integration/docker-compose.yml down",
|
||||
"test:docker:up": "node scripts/docker-compose.js -f integration/docker-compose.yml up -d",
|
||||
"test:docker:down": "node scripts/docker-compose.js -f integration/docker-compose.yml down",
|
||||
"lint": "concurrently 'npm run lint:packages' 'npm run lint:integration' 'npm run lint:spec'",
|
||||
"lint:fix": "concurrently 'npm run lint:packages -- --fix' 'npm run lint:integration -- --fix' 'npm run lint:spec -- --fix'",
|
||||
"lint:integration": "node --max-old-space-size=4096 ./node_modules/.bin/eslint 'integration/**/*.ts'",
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Cross-platform wrapper that forwards arguments to whichever Docker Compose
|
||||
* implementation is available on the host:
|
||||
*
|
||||
* 1. `docker compose` (Compose V2 plugin, shipped with modern Docker Desktop)
|
||||
* 2. `docker-compose` (legacy standalone V1 binary)
|
||||
*
|
||||
* This exists because contributors run a mix of both and neither is guaranteed
|
||||
* to be installed. The wrapper prefers V2 when available and transparently
|
||||
* falls back to V1, so npm scripts like `test:docker:up` work everywhere.
|
||||
*/
|
||||
|
||||
const { spawnSync } = require('node:child_process');
|
||||
|
||||
const forwardedArgs = process.argv.slice(2);
|
||||
const isWindows = process.platform === 'win32';
|
||||
|
||||
function hasComposeV2() {
|
||||
const result = spawnSync('docker', ['compose', 'version'], {
|
||||
stdio: 'ignore',
|
||||
shell: isWindows,
|
||||
});
|
||||
return result.status === 0;
|
||||
}
|
||||
|
||||
function hasComposeV1() {
|
||||
const probe = isWindows ? 'where' : 'which';
|
||||
const result = spawnSync(probe, ['docker-compose'], {
|
||||
stdio: 'ignore',
|
||||
shell: isWindows,
|
||||
});
|
||||
return result.status === 0;
|
||||
}
|
||||
|
||||
function run(command, args) {
|
||||
const result = spawnSync(command, args, {
|
||||
stdio: 'inherit',
|
||||
shell: isWindows,
|
||||
});
|
||||
if (result.error) {
|
||||
console.error(
|
||||
`[docker-compose] Failed to execute: ${result.error.message}`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
process.exit(result.status == null ? 1 : result.status);
|
||||
}
|
||||
|
||||
if (hasComposeV2()) {
|
||||
run('docker', ['compose', ...forwardedArgs]);
|
||||
} else if (hasComposeV1()) {
|
||||
run('docker-compose', forwardedArgs);
|
||||
} else {
|
||||
console.error(
|
||||
"[docker-compose] Neither 'docker compose' (V2) nor 'docker-compose' (V1) was found on PATH.\n" +
|
||||
'Install Docker Desktop or the Docker Compose plugin and try again.',
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
Reference in new issue
Block a user