Skip to content

Commit 6cf9378

Browse files
mshimatjlahr
andauthored
Convert to ESM (#862)
* Switch from CJS to ESM (#859) * Fix 787 - Switch from CJS to ESM * switch project to type=module * adopt import/export syntax throughout codebase * update meow (ESM support since v10) * remove global-tunnel-ng branch since we're > Node 10 * replace proxyquire (no ESM) with testdouble * replace nyc (no ESM) with c8 * shim __dirname * remove unused --no-update-notifier in CLI * [WIP] disable failing tabtab test * Make sure we're resetting td mocks * Delete .nyc_output directory * revert tab completion to cjs * adjust autocomplete binary * re-add --no-update-notifier * disable update-notifier using env * remove console.log leftover --------- Co-authored-by: Thomas Lahr <[email protected]>
1 parent c66f7a7 commit 6cf9378

32 files changed

+547
-1377
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
node_modules
22
yarn.lock
3-
.nyc_output
3+
coverage
44
.project

lib/cli.js

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
#!/usr/bin/env node
22
'use strict';
3-
const fs = require('fs');
4-
const path = require('path');
5-
const chalk = require('chalk');
6-
const updateNotifier = require('update-notifier');
7-
const yosay = require('yosay');
8-
const stringLength = require('string-length');
9-
const rootCheck = require('root-check');
10-
const meow = require('meow');
11-
const list = require('cli-list');
12-
const Tabtab = require('tabtab');
13-
const pkg = require('../package.json');
14-
const Router = require('./router');
15-
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import chalk from 'chalk';
6+
import updateNotifier from 'update-notifier';
7+
import yosay from 'yosay';
8+
import stringLength from 'string-length';
9+
import rootCheck from 'root-check';
10+
import meow from 'meow';
11+
import list from 'cli-list';
12+
import tabtab from './completion/tabtab.cjs';
13+
import pkg from './utils/project-package.js';
14+
import Router from './router.js';
15+
import {bootstrap} from 'global-agent';
16+
import * as routes from './routes/index.js';
17+
import {getDirname} from './utils/node-shims.js';
18+
19+
const __dirname = getDirname(import.meta.url);
1620
const gens = list(process.argv.slice(2));
1721

18-
// Override http networking to go through a proxy ifone is configured
19-
const MAJOR_NODEJS_VERSION = Number.parseInt(process.version.slice(1).split('.')[0], 10);
20-
21-
if (MAJOR_NODEJS_VERSION >= 10) {
22-
// `global-agent` works with Node.js v10 and above.
23-
require('global-agent').bootstrap();
24-
} else {
25-
// `global-tunnel-ng` works only with Node.js v10 and below.
26-
require('global-tunnel-ng').initialize();
27-
}
28-
29-
/* eslint new-cap: 0, no-extra-parens: 0 */
30-
const tabtab = new Tabtab.Commands.default({
31-
name: 'yo',
32-
completer: 'yo-complete'
33-
});
22+
bootstrap();
3423

3524
const cli = gens.map(gen => {
36-
const minicli = meow({autoHelp: false, autoVersion: true, pkg, argv: gen});
25+
const minicli = meow({autoHelp: false, autoVersion: true, pkg, argv: gen, importMeta: import.meta});
3726
const options = minicli.flags;
3827
const args = minicli.input;
3928

@@ -66,7 +55,9 @@ function updateCheck() {
6655
async function pre() {
6756
// Debugging helper
6857
if (cmd === 'doctor') {
69-
require('yeoman-doctor')();
58+
// eslint-disable-next-line node/no-unsupported-features/es-syntax
59+
const yeomanDoctor = (await import('yeoman-doctor')).default;
60+
yeomanDoctor();
7061
return;
7162
}
7263

@@ -76,7 +67,9 @@ async function pre() {
7667

7768
// Easteregg
7869
if (cmd === 'yeoman' || cmd === 'yo') {
79-
console.log(require('yeoman-character'));
70+
// eslint-disable-next-line node/no-unsupported-features/es-syntax
71+
const yeomanCharacter = (await import('yeoman-character')).default;
72+
console.log(yeomanCharacter);
8073
return;
8174
}
8275

@@ -171,13 +164,13 @@ async function init() {
171164

172165
function runYo(env) {
173166
const router = new Router(env);
174-
router.registerRoute('help', require('./routes/help'));
175-
router.registerRoute('update', require('./routes/update'));
176-
router.registerRoute('run', require('./routes/run'));
177-
router.registerRoute('install', require('./routes/install'));
178-
router.registerRoute('exit', require('./routes/exit'));
179-
router.registerRoute('clearConfig', require('./routes/clear-config'));
180-
router.registerRoute('home', require('./routes/home'));
167+
router.registerRoute('help', routes.help);
168+
router.registerRoute('update', routes.update);
169+
router.registerRoute('run', routes.run);
170+
router.registerRoute('install', routes.install);
171+
router.registerRoute('exit', routes.exit);
172+
router.registerRoute('clearConfig', routes.clearConfig);
173+
router.registerRoute('home', routes.home);
181174

182175
process.once('exit', router.navigate.bind(router, 'exit'));
183176

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const tabtab = require('tabtab')({
44
name: 'yo',
55
cache: !process.env.YO_TEST
66
});
7-
const Completer = require('./completer');
7+
const Completer = require('./completer.cjs');
88

99
(async () => {
1010
// eslint-disable-next-line node/no-unsupported-features/es-syntax

lib/completion/tabtab.cjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const Tabtab = require('tabtab');
2+
3+
// eslint-disable-next-line new-cap
4+
const tabtab = new Tabtab.Commands.default({
5+
name: 'yo',
6+
completer: 'yo-complete',
7+
cache: false
8+
});
9+
10+
module.exports = tabtab;

lib/router.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
'use strict';
2-
const path = require('path');
3-
const titleize = require('titleize');
4-
const humanizeString = require('humanize-string');
5-
const readPkgUp = require('read-pkg-up');
6-
const updateNotifier = require('update-notifier');
7-
const Configstore = require('configstore');
8-
const {namespaceToName} = require('./utils/namespace');
2+
import path from 'path';
3+
import titleize from 'titleize';
4+
import humanizeString from 'humanize-string';
5+
import readPkgUp from 'read-pkg-up';
6+
import updateNotifier from 'update-notifier';
7+
import Configstore from 'configstore';
8+
import {namespaceToName} from './utils/namespace.js';
9+
import pkg from './utils/project-package.js';
910

1011
/**
1112
* The router is in charge of handling `yo` different screens.
1213
* @constructor
1314
* @param {Environment} env A yeoman environment instance
1415
* @param {Configstore} [conf] An optional config store instance
1516
*/
16-
class Router {
17+
export default class Router {
1718
constructor(env, conf) {
18-
const pkg = require('../package.json');
1919
this.routes = {};
2020
this.env = env;
2121
this.conf = conf || new Configstore(pkg.name, {
@@ -83,5 +83,3 @@ class Router {
8383
}
8484
}
8585
}
86-
87-
module.exports = Router;

lib/routes/clear-config.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
2-
const _ = require('lodash');
3-
const chalk = require('chalk');
4-
const inquirer = require('inquirer');
5-
const globalConfig = require('../utils/global-config');
6-
const {namespaceToName} = require('../utils/namespace');
2+
import _ from 'lodash';
3+
import chalk from 'chalk';
4+
import inquirer from 'inquirer';
5+
import globalConfig from '../utils/global-config.js';
6+
import {namespaceToName} from '../utils/namespace.js';
77

8-
module.exports = async app => {
8+
export const clearConfig = async app => {
99
const defaultChoices = [
1010
{
1111
name: 'Take me back home, Yo!',

lib/routes/exit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
2-
const yosay = require('yosay');
2+
import yosay from 'yosay';
33

4-
module.exports = async () => {
4+
export const exit = async () => {
55
const PADDING = 5;
66
const url = 'http://yeoman.io';
77
const maxLength = url.length + PADDING;

lib/routes/help.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
2-
const inquirer = require('inquirer');
3-
const open = require('open');
2+
import inquirer from 'inquirer';
3+
import open from 'open';
44

5-
module.exports = async app => {
5+
export const help = async app => {
66
return inquirer.prompt([{
77
name: 'whereTo',
88
type: 'list',

lib/routes/home.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
'use strict';
2-
const _ = require('lodash');
3-
const chalk = require('chalk');
4-
const fullname = require('fullname');
5-
const inquirer = require('inquirer');
6-
const {isString} = require('lodash');
7-
const {namespaceToName} = require('../utils/namespace');
8-
const globalConfigHasContent = require('../utils/global-config').hasContent;
2+
import _ from 'lodash';
3+
import chalk from 'chalk';
4+
import fullname from 'fullname';
5+
import inquirer from 'inquirer';
6+
import {namespaceToName} from '../utils/namespace.js';
7+
import globalConfig from '../utils/global-config.js';
98

10-
module.exports = async app => {
9+
export const home = async app => {
1110
const defaultChoices = [{
1211
name: 'Install a generator',
1312
value: 'install'
@@ -19,7 +18,7 @@ module.exports = async app => {
1918
value: 'exit'
2019
}];
2120

22-
if (globalConfigHasContent()) {
21+
if (globalConfig.hasContent()) {
2322
defaultChoices.splice(-1, 0, {
2423
name: 'Clear global config',
2524
value: 'clearConfig'
@@ -53,7 +52,7 @@ module.exports = async app => {
5352
}
5453

5554
return fullname().then(name => {
56-
const allo = (name && isString(name)) ? `'Allo ${name.split(' ')[0]}! ` : '\'Allo! ';
55+
const allo = (name && _.isString(name)) ? `'Allo ${name.split(' ')[0]}! ` : '\'Allo! ';
5756

5857
return inquirer.prompt([{
5958
name: 'whatNext',

0 commit comments

Comments
 (0)