Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drop shelljs #23684

Merged
merged 6 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions generators/base-workspaces/internal/docker-prompts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* limitations under the License.
*/
import chalk from 'chalk';
import shelljs from 'shelljs';

import { readdirSync, statSync } from 'node:fs';
import { loadConfigs } from './docker-base.mjs';
import { applicationTypes, monitoringTypes, serviceDiscoveryTypes } from '../../../jdl/jhipster/index.mjs';
import { convertSecretToBase64 } from '../../base/support/index.mjs';
Expand Down Expand Up @@ -118,15 +118,21 @@ async function askForPath() {
name: 'directoryPath',
message: messageAskForPath,
default: this.directoryPath || '../',
validate: input => {
validate: async input => {
const path = this.destinationPath(input);
if (shelljs.test('-d', path)) {
const appsFolders = getAppFolders.call(this, input, deploymentApplicationType);

if (appsFolders.length === 0) {
return deploymentApplicationType === MONOLITH ? `No monolith found in ${path}` : `No microservice or gateway found in ${path}`;
try {
if (statSync(path).isDirectory) {
const appsFolders = getAppFolders.call(this, input, deploymentApplicationType);

if (appsFolders.length === 0) {
return deploymentApplicationType === MONOLITH
? `No monolith found in ${path}`
: `No microservice or gateway found in ${path}`;
}
return true;
}
return true;
} catch {
// Ignore error
}
return `${path} is not a directory or doesn't exist`;
},
Expand Down Expand Up @@ -366,27 +372,31 @@ async function askForDockerPushCommand() {
*/
export function getAppFolders(input, deploymentApplicationType) {
const destinationPath = this.destinationPath(input);
const files = shelljs.ls('-l', destinationPath);
const files = readdirSync(destinationPath);
const appsFolders = [];

files.forEach(file => {
if (file.isDirectory()) {
if (shelljs.test('-f', `${destinationPath}/${file.name}/.yo-rc.json`)) {
try {
const fileData = this.fs.readJSON(`${destinationPath}/${file.name}/.yo-rc.json`);
if (
fileData['generator-jhipster'].baseName !== undefined &&
(deploymentApplicationType === undefined ||
deploymentApplicationType === fileData['generator-jhipster'].applicationType ||
(deploymentApplicationType === MICROSERVICE && fileData['generator-jhipster'].applicationType === GATEWAY))
) {
appsFolders.push(file.name.match(/([^/]*)\/*$/)[1]);
try {
if (statSync(this.destinationPath(file)).isDirectory()) {
if (statSync(this.destinationPath(file, '.yo-rc.json')).isFile()) {
try {
const fileData = this.readDestinationJSON(`${file.name}/.yo-rc.json`);
if (
fileData['generator-jhipster'].baseName !== undefined &&
(deploymentApplicationType === undefined ||
deploymentApplicationType === fileData['generator-jhipster'].applicationType ||
(deploymentApplicationType === MICROSERVICE && fileData['generator-jhipster'].applicationType === GATEWAY))
) {
appsFolders.push(file.name.match(/([^/]*)\/*$/)[1]);
}
} catch (err) {
this.log.error(chalk.red(`${file}: this .yo-rc.json can't be read`));
this.log.debug('Error:', err);
}
} catch (err) {
this.log.error(chalk.red(`${file}: this .yo-rc.json can't be read`));
this.log.debug('Error:', err);
}
}
} catch {
// Not a file or directory
}
});

Expand Down
49 changes: 21 additions & 28 deletions generators/docker-compose/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import { existsSync } from 'fs';
import pathjs from 'path';
import chalk from 'chalk';
import shelljs from 'shelljs';
import jsyaml from 'js-yaml';
import normalize from 'normalize-path';
import runAsync from 'run-async';
import _ from 'lodash';

import BaseWorkspacesGenerator from '../base-workspaces/index.mjs';
Expand Down Expand Up @@ -73,36 +71,31 @@ export default class DockerComposeGenerator extends BaseWorkspacesGenerator {
this.parseJHipsterOptions(command.options);
},
checkDocker,
checkDockerCompose: runAsync(function () {
async checkDockerCompose() {
if (this.skipChecks) return;

const done = this.async();

shelljs.exec('docker compose version', { silent: true }, (code, stdout, stderr) => {
if (stderr) {
this.log.error(
chalk.red(
'Docker Compose 1.6.0 or later is not installed on your computer.\n' +
' Read https://docs.docker.com/compose/install/\n',
),
);
} else {
const composeVersion = stdout.split(' ')[2].replace(/,/g, '');
const composeVersionMajor = composeVersion.split('.')[0];
const composeVersionMinor = composeVersion.split('.')[1];
if (composeVersionMajor < 1 || (composeVersionMajor === 1 && composeVersionMinor < 6)) {
this.log.error(
chalk.red(
`$Docker Compose version 1.6.0 or later is not installed on your computer.
const { stdout, exitCode } = await this.spawnCommand('docker compose version', { reject: false, stdio: 'pipe' });
if (exitCode !== 0) {
this.log.error(
chalk.red(
'Docker Compose 1.6.0 or later is not installed on your computer.\n' +
' Read https://docs.docker.com/compose/install/\n',
),
);
}
const composeVersion = stdout.split(' ')[2].replace(/,/g, '');
const composeVersionMajor = composeVersion.split('.')[0];
const composeVersionMinor = composeVersion.split('.')[1];
if (composeVersionMajor < 1 || (composeVersionMajor === 1 && composeVersionMinor < 6)) {
this.log.error(
chalk.red(
`$Docker Compose version 1.6.0 or later is not installed on your computer.
Docker Compose version found: ${composeVersion}
Read https://docs.docker.com/compose/install`,
),
);
}
}
done();
});
}),
),
);
}
},
};
}

Expand Down
62 changes: 29 additions & 33 deletions generators/docker/support/check-docker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,42 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import shelljs from 'shelljs';
import chalk from 'chalk';
import runAsync from 'run-async';

/**
* Check that Docker exists.
* @this {import('../../base-core/index.mjs').default}
*/
export const checkDocker = runAsync(function () {
export const checkDocker = async function () {
if (this.abort || this.skipChecks) return;
const done = this.async();
const ret = await this.spawnCommand('docker -v', { reject: false, stdio: 'pipe' });
if (ret.exitCode !== 0) {
this.log.error(
chalk.red(
`Docker version 1.10.0 or later is not installed on your computer.
Read http://docs.docker.com/engine/installation/#installation
`,
),
);
throw new Error();
}

shelljs.exec('docker -v', { silent: true }, (code, stdout, stderr) => {
if (stderr) {
this.log.error(
chalk.red(
'Docker version 1.10.0 or later is not installed on your computer.\n' +
' Read http://docs.docker.com/engine/installation/#installation\n',
),
);
this.abort = true;
} else {
const dockerVersion = stdout.split(' ')[2].replace(/,/g, '');
const dockerVersionMajor = dockerVersion.split('.')[0];
const dockerVersionMinor = dockerVersion.split('.')[1];
if (dockerVersionMajor < 1 || (dockerVersionMajor === 1 && dockerVersionMinor < 10)) {
this.log.error(
chalk.red(
`Docker version 1.10.0 or later is not installed on your computer.
Docker version found: ${dockerVersion}
Read http://docs.docker.com/engine/installation/#installation`,
),
);
this.abort = true;
} else {
this.log.verboseInfo('Docker is installed');
}
}
done();
});
});
const dockerVersion = ret.stdout.split(' ')[2].replace(/,/g, '');
const dockerVersionMajor = dockerVersion.split('.')[0];
const dockerVersionMinor = dockerVersion.split('.')[1];
if (dockerVersionMajor < 1 || (dockerVersionMajor === 1 && dockerVersionMinor < 10)) {
this.log.error(
chalk.red(
`Docker version 1.10.0 or later is not installed on your computer.
Docker version found: ${dockerVersion}
Read http://docs.docker.com/engine/installation/#installation`,
),
);
throw new Error();
} else {
this.log.verboseInfo('Docker is installed');
}
};

/**
* This is the Generator base class.
Expand Down
10 changes: 3 additions & 7 deletions generators/info/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ export default class InfoGenerator extends BaseApplicationGenerator {
},

async checkJHipster() {
try {
const { stdout } = await this.spawnCommand('npm list generator-jhipster', { stdio: 'pipe' });
console.log(`\n\`\`\`\n${stdout}\`\`\`\n`);
} catch (error) {
console.log(`\n\`\`\`\n${(error as any).stdout}\`\`\`\n`);
}
const { stdout } = await this.spawnCommand('npm list generator-jhipster', { stdio: 'pipe', reject: false });
console.log(`\n\`\`\`\n${stdout}\`\`\`\n`);
},

displayConfiguration() {
Expand Down Expand Up @@ -121,7 +117,7 @@ export default class InfoGenerator extends BaseApplicationGenerator {

async checkCommand(command: string, args: string[], printInfo = ({ stdout }: ExecaReturnValue<string>) => console.log(stdout)) {
try {
printInfo(await this.spawnCommand(command, args, { stdio: 'pipe' }));
printInfo(await this.spawn(command, args, { stdio: 'pipe' }));
} catch (_error) {
console.log(chalk.red(`'${command}' command could not be found`));
}
Expand Down
34 changes: 14 additions & 20 deletions generators/kubernetes-knative/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
/* eslint-disable consistent-return */
import fs from 'fs';
import chalk from 'chalk';
import shelljs from 'shelljs';
import runAsync from 'run-async';

import BaseWorkspacesGenerator from '../base-workspaces/index.mjs';

Expand Down Expand Up @@ -62,7 +60,7 @@ export default class KubernetesKnativeGenerator extends BaseWorkspacesGenerator
}

get initializing() {
return {
return this.asInitializingTaskGroup({
sayHello() {
this.log.log(chalk.white(`${chalk.bold('☸')} Welcome to the JHipster Kubernetes Knative Generator ${chalk.bold('☸')}`));
this.log.log(chalk.white(`Files will be generated in the folder: ${chalk.yellow(this.destinationRoot())}`));
Expand All @@ -74,31 +72,27 @@ export default class KubernetesKnativeGenerator extends BaseWorkspacesGenerator
checkDocker,
checkKubernetes,
checkHelm,
checkKnative: runAsync(function () {
async checkKnative() {
if (this.skipChecks) return;
const done = this.async();
shelljs.exec(
'kubectl get deploy -n knative-serving --label-columns=serving.knative.dev/release | grep -E "v0\\.[8-9]{1,3}\\.[0-9]*',
{ silent: true },
(code, stdout, stderr) => {
if (stderr || code !== 0) {
this.log.warn(
'Knative 0.8.* or later is not installed on your computer.\n' +
'Make sure you have Knative and Istio installed. Read https://knative.dev/docs/install/\n',
);
}
done();
},
);
}),
try {
await this.spawnCommand(
'kubectl get deploy -n knative-serving --label-columns=serving.knative.dev/release | grep -E "v0\\.[8-9]{1,3}\\.[0-9]*',
);
} catch (error) {
this.log.warn(
'Knative 0.8.* or later is not installed on your computer.\n' +
'Make sure you have Knative and Istio installed. Read https://knative.dev/docs/install/\n',
);
}
},
loadConfig,
localInit() {
this.deploymentApplicationType = 'microservice';
this.istio = true;
},
setupKubernetesConstants,
setupHelmConstants,
};
});
}

get [BaseWorkspacesGenerator.INITIALIZING]() {
Expand Down
50 changes: 20 additions & 30 deletions generators/kubernetes/kubernetes-base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
*/
import crypto from 'crypto';
import _ from 'lodash';
import runAsync from 'run-async';

import shelljs from 'shelljs';
import { defaultKubernetesConfig } from './kubernetes-constants.mjs';
import { loadFromYoRc } from '../base-workspaces/internal/docker-base.mjs';
import {
Expand Down Expand Up @@ -50,39 +48,31 @@ const { INGRESS } = ServiceTypes;
const { GKE, NGINX } = IngressTypes;
const { K8S, HELM } = GeneratorTypes;

export const checkKubernetes = runAsync(function () {
export const checkKubernetes = async function () {
if (this.skipChecks) return;
const done = this.async();

shelljs.exec('kubectl version', { silent: true }, (code, stdout, stderr) => {
if (stderr) {
this.log.warn(
'kubectl 1.2 or later is not installed on your computer.\n' +
'Make sure you have Kubernetes installed. Read https://kubernetes.io/docs/setup/\n',
);
}
done();
});
});
try {
await this.spawnCommand('kubectl version');
} catch {
this.log.warn(
'kubectl 1.2 or later is not installed on your computer.\n' +
'Make sure you have Kubernetes installed. Read https://kubernetes.io/docs/setup/\n',
);
}
};

export const checkHelm = runAsync(function () {
export const checkHelm = async function () {
if (this.skipChecks) return;
const done = this.async();

shelljs.exec(
'helm version --client | grep -E "(v2\\.1[2-9]{1,2}\\.[0-9]{1,3})|(v3\\.[0-9]{1,2}\\.[0-9]{1,3})"',
{ silent: true },
(code, stdout, stderr) => {
if (stderr || code !== 0) {
this.log.warn(
'helm 2.12.x or later is not installed on your computer.\n' +
'Make sure you have helm installed. Read https://github.com/helm/helm/\n',
);
}
done();
},
);
});
try {
await this.spawnCommand('helm version --client | grep -E "(v2\\.1[2-9]{1,2}\\.[0-9]{1,3})|(v3\\.[0-9]{1,2}\\.[0-9]{1,3})"');
} catch {
this.log.warn(
'helm 2.12.x or later is not installed on your computer.\n' +
'Make sure you have helm installed. Read https://github.com/helm/helm/\n',
);
}
};

export function loadConfig() {
loadFromYoRc.call(this);
Expand Down
Loading
Loading