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

WIP - start migrating cli to mjs #20500

Closed
wants to merge 10 commits into from
Closed
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
9 changes: 2 additions & 7 deletions .mocharc.cjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const { pathToFileURL } = require('url');

const loaderPath = require.resolve('@node-loaders/auto/strict');
const loaderUrl = pathToFileURL(loaderPath).href;

module.exports = {
recursive: true,
reporter: 'spec',
slow: 0,
timeout: 50000,
ui: 'bdd',
extension: ['js', 'spec.cjs', 'spec.mjs', 'spec.ts', 'spec.mts'],
extension: ['spec.cjs', 'spec.mjs', 'spec.ts', 'spec.mts'],
require: ['mocha-expect-snapshot/old-format', 'test/mocha.config.cjs'],
// Resolve absolute path for test with fork and different cwd.
// `loader` options is passed to forks, but `require` is not.
// Use node-option instead (it overrides loader option)
'node-option': [`loader=${loaderUrl}`],
'node-option': [`require=${require.resolve('@esbuild-kit/cjs-loader')}`, `loader=${require.resolve('@esbuild-kit/esm-loader')}`],
parallel: true,
};
14 changes: 14 additions & 0 deletions .mocharc.mocked.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
recursive: true,
reporter: 'spec',
slow: 0,
timeout: 30000,
ui: 'bdd',
extension: ['spec.mocked.mts'],
require: ['mocha-expect-snapshot/old-format', 'test/mocha.config.cjs'],
// Resolve absolute path for test with fork and different cwd.
// `loader` options is passed to forks, but `require` is not.
// Use node-option instead (it overrides loader option)
'node-option': [`require=${require.resolve('@esbuild-kit/cjs-loader')}`, `loader=${require.resolve('testdouble')}`],
parallel: true,
};
6 changes: 3 additions & 3 deletions cli/cli.cjs → cli/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
* limitations under the License.
*/

const { runJHipster } = require('./program.cjs');
const { done, logger } = require('./utils.cjs');
import { runJHipster } from './program.mjs';
import { done, logger } from './utils.mjs';

module.exports = runJHipster().catch(done);
export default runJHipster().catch(done);

process.on('unhandledRejection', up => {
logger.error('Unhandled promise rejection at:');
Expand Down
18 changes: 9 additions & 9 deletions cli/download.cjs → cli/download.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs');
const https = require('https');
const path = require('path');
const cliUtils = require('./utils.cjs');
const { packageJson } = require('../lib/index.cjs');

const { logger, toString } = cliUtils;
import fs from 'fs';
import https from 'https';
import path from 'path';
import { logger, toStringJoinArgs } from './utils.mjs';
import { packageJson } from '../lib/index.mjs';

const downloadFile = (url, filename) => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -52,9 +50,9 @@ const downloadFile = (url, filename) => {
* @param {string[]} args[0] jdl files
* @param {any} options options passed from CLI
*/
module.exports = ([jdlFiles = []], options = {}) => {
const downloadJdl = ([jdlFiles = []], options = {}) => {
logger.debug('cmd: download');
logger.debug(`jdlFiles: ${toString(jdlFiles)}`);
logger.debug(`jdlFiles: ${toStringJoinArgs(jdlFiles)}`);
if (!jdlFiles || jdlFiles.length === 0) {
return Promise.reject(new Error('\nAt least one jdl file is required.\n'));
}
Expand All @@ -80,3 +78,5 @@ module.exports = ([jdlFiles = []], options = {}) => {
})
);
};

export default downloadJdl;
27 changes: 16 additions & 11 deletions cli/environment-builder.cjs → cli/environment-builder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const assert = require('assert');
const chalk = require('chalk');
const _ = require('lodash');
const path = require('path');
const { existsSync, readFileSync } = require('fs');
const Environment = require('yeoman-environment');
const { CLI_NAME, logger } = require('./utils.cjs');
const { packageNameToNamespace } = require('../generators/utils.cjs');
const { parseBlueprintInfo, loadBlueprintsFromConfiguration, mergeBlueprints } = require('../utils/blueprint.cjs');
import assert from 'assert';
import chalk from 'chalk';
import _ from 'lodash';
import { existsSync, readFileSync } from 'fs';
import Environment from 'yeoman-environment';
import path from 'path';
import { fileURLToPath } from 'url';

import { CLI_NAME, logger } from './utils.mjs';
import { packageNameToNamespace } from '../generators/utils.cjs';
import { parseBlueprintInfo, loadBlueprintsFromConfiguration, mergeBlueprints } from '../utils/blueprint.cjs';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

function loadYoRc(filePath = '.yo-rc.json') {
if (!existsSync(filePath)) {
Expand All @@ -45,7 +50,7 @@ const createEnvironment = (args, options = {}, adapter) => {
return Environment.createEnv(args, { newErrorHandler: true, ...options, sharedOptions }, adapter);
};

module.exports = class EnvironmentBuilder {
export default class EnvironmentBuilder {
/**
* Creates a new EnvironmentBuilder with a new Environment.
*
Expand Down Expand Up @@ -418,4 +423,4 @@ module.exports = class EnvironmentBuilder {
});
return result;
}
};
}
42 changes: 21 additions & 21 deletions cli/import-jdl.cjs → cli/import-jdl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const chalk = require('chalk');
const fs = require('fs');
const _ = require('lodash');
const path = require('path');
const pluralize = require('pluralize');
const { fork: forkProcess } = require('child_process');

const EnvironmentBuilder = require('./environment-builder.cjs');
const { CLI_NAME, GENERATOR_NAME, logger, toString, printSuccess, getOptionAsArgs } = require('./utils.cjs');

const { packageJson: packagejs } = require('../lib/index.cjs');
const statistics = require('../generators/statistics.cjs');
const { JHIPSTER_CONFIG_DIR } = require('../generators/generator-constants.cjs');

const jhipsterCli = require.resolve('./cli.cjs');
const { writeConfigFile } = require('./export-utils.cjs');

import chalk from 'chalk';
import fs from 'fs';
import _ from 'lodash';
import path from 'path';
import pluralize from 'pluralize';
import { createRequire } from 'module';
import { fork as forkProcess } from 'child_process';
import EnvironmentBuilder from './environment-builder.mjs';
import { CLI_NAME, GENERATOR_NAME, logger, toStringJoinArgs, printSuccess, getOptionAsArgs } from './utils.mjs';
import { packageJson as packagejs } from '../lib/index.cjs';
import statistics from '../generators/statistics.cjs';
import { JHIPSTER_CONFIG_DIR } from '../generators/generator-constants.mjs';
import { writeConfigFile } from './export-utils.cjs';

const require = createRequire(import.meta.url);
const jhipsterCli = require.resolve('./cli.mjs');
const getDeploymentType = deployment => deployment && deployment[GENERATOR_NAME] && deployment[GENERATOR_NAME].deploymentType;

/**
Expand Down Expand Up @@ -295,7 +294,6 @@ const generateApplicationFiles = ({ processor, applicationWithEntities }) => {
if (!fork) {
generatorOptions.applicationWithEntities = applicationWithEntities;
}

return runGenerator('app', { cwd, fork, createEnvBuilder }, generatorOptions);
};

Expand Down Expand Up @@ -351,7 +349,7 @@ const generateEntityFiles = (processor, exportedEntities, env) => {
class JDLProcessor {
constructor(jdlFiles, jdlContent, options, createEnvBuilder) {
logger.debug(
`JDLProcessor started with ${jdlContent ? `content: ${jdlContent}` : `files: ${jdlFiles}`} and options: ${toString(options)}`
`JDLProcessor started with ${jdlContent ? `content: ${jdlContent}` : `files: ${jdlFiles}`} and options: ${toStringJoinArgs(options)}`
);
this.jdlFiles = jdlFiles;
this.jdlContent = jdlContent;
Expand Down Expand Up @@ -525,9 +523,9 @@ class JDLProcessor {
* @param {any} [options] options passed from CLI
* @param {any} [env] the yeoman environment
*/
module.exports = async (jdlFiles, options = {}, env, _envBuilder, createEnvBuilder = EnvironmentBuilder.createDefaultBuilder) => {
const jdl = async (jdlFiles, options = {}, env, _envBuilder, createEnvBuilder = EnvironmentBuilder.createDefaultBuilder) => {
logger.info(chalk.yellow(`Executing import-jdl ${options.inline ? 'with inline content' : jdlFiles.join(' ')}`));
logger.debug(chalk.yellow(`Options: ${toString({ ...options, inline: options.inline ? 'inline content' : '' })}`));
logger.debug(chalk.yellow(`Options: ${toStringJoinArgs({ ...options, inline: options.inline ? 'inline content' : '' })}`));
try {
const jdlImporter = new JDLProcessor(jdlFiles, options.inline, options, createEnvBuilder);
await jdlImporter.importJDL();
Expand Down Expand Up @@ -558,3 +556,5 @@ module.exports = async (jdlFiles, options = {}, env, _envBuilder, createEnvBuild
return Promise.reject(new Error(`Error during import-jdl: ${e.message}`));
}
};

export default jdl;
4 changes: 2 additions & 2 deletions cli/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { createProgram, buildCommands, buildJHipster, runJHipster, printJHipsterLogo, done, logger } from './program.cjs';
export { default as EnvironmentBuilder } from './environment-builder.cjs';
export { createProgram, buildCommands, buildJHipster, runJHipster, printJHipsterLogo, done, logger } from './program.mjs';
export { default as EnvironmentBuilder } from './environment-builder.mjs';
22 changes: 12 additions & 10 deletions cli/jdl.cjs → cli/jdl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const cliUtils = require('./utils.cjs');
const importJdl = require('./import-jdl.cjs');
const download = require('./download.cjs');

const { logger } = cliUtils;
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import { logger } from './utils.mjs';
import importJdl from './import-jdl.mjs';
import download from './download.mjs';

/**
* Add jdl extension to the file
Expand All @@ -44,7 +42,7 @@ const toJdlFile = file => {
* @param {EnvironmentBuilder} envBuilder
* @param {(string[], object) => EnvironmentBuilder} createEnvBuilder
*/
module.exports = ([jdlFiles = []], options = {}, env, envBuilder, createEnvBuilder) => {
const jdl = ([jdlFiles = []], options = {}, env, envBuilder, createEnvBuilder) => {
logger.debug('cmd: import-jdl from ./import-jdl');
logger.debug(`jdlFiles: ${toString(jdlFiles)}`);
if (options.inline) {
Expand All @@ -60,5 +58,9 @@ module.exports = ([jdlFiles = []], options = {}, env, envBuilder, createEnvBuild
}
return Promise.resolve(filename);
});
return Promise.all(promises).then(jdlFiles => importJdl(jdlFiles.flat(), options, env, envBuilder, createEnvBuilder));
return Promise.all(promises).then(jdlFiles => {
return importJdl(jdlFiles.flat(), options, env, envBuilder, createEnvBuilder);
});
};

export default jdl;
10 changes: 4 additions & 6 deletions cli/jhipster.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
import semver from 'semver';
import path from 'path';
import { pathToFileURL } from 'url';

import cliUtils from './utils.cjs';
import { logger } from './utils.mjs';
import { packageJson } from '../lib/index.mjs';

const { logger } = cliUtils;

const currentNodeVersion = process.versions.node;
const minimumNodeVersion = packageJson.engines.node;

Expand Down Expand Up @@ -66,7 +63,8 @@ async function requireCLI(preferLocal) {
// load local version
/* eslint-disable import/no-dynamic-require */
logger.info(LOCAL_VERSION_MESSAGE);
await import(pathToFileURL(localCLI).href);
await import(localCLI);
// await import(pathToFileURL(localCLI).href);
return;
}
} catch (e) {
Expand All @@ -75,6 +73,6 @@ async function requireCLI(preferLocal) {
}
// load current jhipster
logger.info(message);
await import('./cli.cjs');
await import('./cli.mjs');
/* eslint-enable */
}
42 changes: 19 additions & 23 deletions cli/program.cjs → cli/program.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
* limitations under the License.
*/

const chalk = require('chalk');
const { Option } = require('commander');
const didYouMean = require('didyoumean');
const fs = require('fs');
const path = require('path');
import chalk from 'chalk';
import { Option } from 'commander';
import didYouMean from 'didyoumean';
import fs from 'fs';
import path from 'path';

const EnvironmentBuilder = require('./environment-builder.cjs');
const SUB_GENERATORS = require('./commands.cjs');
const JHipsterCommand = require('./jhipster-command.cjs');
const { CLI_NAME, logger, getCommand, done } = require('./utils.cjs');
const { packageJson } = require('../lib/index.cjs');
const { packageNameToNamespace } = require('../generators/utils.cjs');
const { logo } = require('./logo.cjs');
import { logo } from './logo.cjs';
import EnvironmentBuilder from './environment-builder.mjs';
import SUB_GENERATORS from './commands.cjs';
import JHipsterCommand from './jhipster-command.cjs';
import { CLI_NAME, logger, getCommand, done } from './utils.mjs';
import { packageJson } from '../lib/index.cjs';
import { packageNameToNamespace } from '../generators/utils.cjs';

const { version: JHIPSTER_VERSION } = packageJson;
const JHIPSTER_NS = CLI_NAME;
Expand Down Expand Up @@ -228,7 +228,11 @@ const buildCommands = ({

if (cliOnly) {
logger.debug('Executing CLI only script');
return loadCommand(cmdName)(args, options, env, envBuilder, createEnvBuilder);
return Promise.resolve(loadCommand(cmdName)).then(module => {
if (module instanceof Function) {
module(args, options, env, envBuilder, createEnvBuilder);
}
});
}
await env.composeWith('jhipster:bootstrap', options);

Expand Down Expand Up @@ -259,7 +263,7 @@ const buildJHipster = ({
env = envBuilder.getEnvironment(),
/* eslint-disable-next-line global-require, import/no-dynamic-require */
loadCommand = key => {
return require(`./${key}.cjs`);
return import(`./${key}.mjs`);
},
defaultCommand,
} = {}) => {
Expand All @@ -276,12 +280,4 @@ const runJHipster = (args = {}) => {
return buildJHipster(args).parseAsync(argv);
};

module.exports = {
createProgram,
buildCommands,
buildJHipster,
runJHipster,
printJHipsterLogo,
done,
logger,
};
export { createProgram, buildCommands, buildJHipster, runJHipster, printJHipsterLogo, done, logger };
Loading