Skip to content

Commit

Permalink
refactor: convert CLI entry points to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Feb 12, 2022
1 parent 0c7e592 commit ebb0db4
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

// @ts-check

const logger = require('@docusaurus/logger').default;
const semver = require('semver');
const path = require('path');
const {program} = require('commander');
const {default: init} = require('../lib');
const requiredVersion = require('../package.json').engines.node;
import logger from '@docusaurus/logger';
import semver from 'semver';
import path from 'path';
import {program} from 'commander';
import {createRequire} from 'module';
import init from '../lib/index.js';

const requiredVersion = createRequire(import.meta.url)('../package.json')
.engines.node;

if (!semver.satisfies(process.version, requiredVersion)) {
logger.error('Minimum Node.js version not met :(');
Expand Down
2 changes: 1 addition & 1 deletion packages/create-docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build": "tsc",
"watch": "tsc --watch"
},
"bin": "bin/index.js",
"bin": "bin/index.mjs",
"publishConfig": {
"access": "public"
},
Expand Down
16 changes: 9 additions & 7 deletions packages/docusaurus-migrate/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

// @ts-check

const logger = require('@docusaurus/logger').default;
const semver = require('semver');
const cli = require('commander');
const path = require('path');
import logger from '@docusaurus/logger';
import semver from 'semver';
import cli from 'commander';
import path from 'path';
import {createRequire} from 'module';
import {migrateDocusaurusProject, migrateMDToMDX} from '../lib/index.js';

const requiredVersion = require('../package.json').engines.node;

const {migrateDocusaurusProject, migrateMDToMDX} = require('../lib');
const requiredVersion = createRequire(import.meta.url)('../package.json')
.engines.node;

function wrapCommand(fn) {
return (...args) =>
Expand Down Expand Up @@ -50,6 +51,7 @@ cli
const newSitePath = path.resolve(newDir);
wrapCommand(migrateMDToMDX)(sitePath, newSitePath);
});

cli.parse(process.argv);

if (!process.argv.slice(2).length) {
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"access": "public"
},
"bin": {
"docusaurus-migrate": "bin/index.js"
"docusaurus-migrate": "bin/index.mjs"
},
"dependencies": {
"@babel/preset-env": "^7.16.11",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@

// @ts-check

const logger = require('@docusaurus/logger').default;
const fs = require('fs-extra');
const semver = require('semver');
const path = require('path');
const updateNotifier = require('update-notifier');
const boxen = require('boxen');
import logger from '@docusaurus/logger';
import fs from 'fs-extra';
import semver from 'semver';
import path from 'path';
import updateNotifier from 'update-notifier';
import boxen from 'boxen';
import {createRequire} from 'module';

const packageJson = createRequire(import.meta.url)('../package.json');
const sitePkg = createRequire(process.cwd())('./package.json');

const {
name,
version,
engines: {node: requiredVersion},
} = require('../package.json');

// eslint-disable-next-line import/no-dynamic-require
const sitePkg = require(path.resolve(process.cwd(), 'package.json'));
} = packageJson;

/**
* Notify user if `@docusaurus` packages are outdated
Expand Down Expand Up @@ -130,4 +131,4 @@ function beforeCli() {
}
}

module.exports = beforeCli;
export default beforeCli;
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

// @ts-check

const logger = require('@docusaurus/logger').default;
const fs = require('fs');
const cli = require('commander');
const {
import logger from '@docusaurus/logger';
import fs from 'fs';
import cli from 'commander';
import {createRequire} from 'module';
import {
build,
swizzle,
deploy,
Expand All @@ -21,15 +22,16 @@ const {
clear,
writeTranslations,
writeHeadingIds,
} = require('../lib');

const beforeCli = require('./beforeCli');
} from '../lib/index.js';
import beforeCli from './beforeCli.mjs';

beforeCli();

const resolveDir = (dir = '.') => fs.realpathSync(dir);

cli.version(require('../package.json').version).usage('<command> [options]');
cli
.version(createRequire(import.meta.url)('../package.json').version)
.usage('<command> [options]');

cli
.command('build [siteDir]')
Expand Down Expand Up @@ -226,6 +228,9 @@ cli.arguments('<command>').action((cmd) => {
logger.error` Unknown command name=${cmd}.`;
});

/**
* @param {string} command
*/
function isInternalCommand(command) {
return [
'start',
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"directory": "packages/docusaurus"
},
"bin": {
"docusaurus": "bin/docusaurus.js"
"docusaurus": "bin/docusaurus.mjs"
},
"scripts": {
"build": "tsc && tsc -p tsconfig.client.json && node copyUntypedFiles.mjs",
Expand Down
30 changes: 21 additions & 9 deletions packages/docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
* LICENSE file in the root directory of this source tree.
*/

export {default as build} from './commands/build';
export {default as start} from './commands/start';
export {default as swizzle} from './commands/swizzle';
export {default as deploy} from './commands/deploy';
export {default as externalCommand} from './commands/external';
export {default as serve} from './commands/serve';
export {default as clear} from './commands/clear';
export {default as writeTranslations} from './commands/writeTranslations';
export {default as writeHeadingIds} from './commands/writeHeadingIds';
import build from './commands/build';
import clear from './commands/clear';
import deploy from './commands/deploy';
import externalCommand from './commands/external';
import serve from './commands/serve';
import start from './commands/start';
import swizzle from './commands/swizzle';
import writeHeadingIds from './commands/writeHeadingIds';
import writeTranslations from './commands/writeTranslations';

export {
build,
clear,
deploy,
externalCommand,
serve,
start,
swizzle,
writeHeadingIds,
writeTranslations,
};

0 comments on commit ebb0db4

Please sign in to comment.