refactor: convert CLI entry points to ESM; migrate create-docusaurus to ESM (#6661)

* refactor: convert CLI entry points to ESM

* fix

* fix

* fix

* fix!

* create-docusaurus ESM

* fix lock

* final touchups

* fix lodash

* fix

* use lodash

* fix hasYarn
This commit is contained in:
Joshua Chen 2022-02-16 23:00:35 +08:00 committed by GitHub
parent eacc695542
commit 67918e35e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 85 additions and 54 deletions

View file

@ -7,20 +7,19 @@
import logger from '@docusaurus/logger';
import fs from 'fs-extra';
import {execSync} from 'child_process';
import prompts, {type Choice} from 'prompts';
import path from 'path';
import shell from 'shelljs';
import {kebabCase, sortBy} from 'lodash';
import _ from 'lodash';
import supportsColor from 'supports-color';
import {fileURLToPath} from 'url';
const RecommendedTemplate = 'classic';
const TypeScriptTemplateSuffix = '-typescript';
function hasYarn() {
try {
execSync('yarnpkg --version', {stdio: 'ignore'});
return true;
return shell.exec('yarnpkg --version', {silent: true}).code === 0;
} catch (e) {
return false;
}
@ -50,7 +49,7 @@ function readTemplates(templatesDir: string) {
);
// Classic should be first in list!
return sortBy(templates, (t) => t !== RecommendedTemplate);
return _.sortBy(templates, (t) => t !== RecommendedTemplate);
}
function createTemplateChoices(templates: string[]) {
@ -135,7 +134,7 @@ export default async function init(
}> = {},
): Promise<void> {
const useYarn = cliOptions.useNpm ? false : hasYarn();
const templatesDir = path.resolve(__dirname, '../templates');
const templatesDir = fileURLToPath(new URL('../templates', import.meta.url));
const templates = readTemplates(templatesDir);
const hasTS = (templateName: string) =>
fs.pathExistsSync(
@ -296,7 +295,7 @@ export default async function init(
// Update package.json info.
try {
await updatePkg(path.join(dest, 'package.json'), {
name: kebabCase(name),
name: _.kebabCase(name),
version: '0.0.0',
private: true,
});