Skip to content

Commit a039e71

Browse files
committed
feat: migrate to ESM
1 parent 3b542f4 commit a039e71

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

lib/cli.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
const yargsParser = require("yargs-parser");
2-
const path = require("path");
3-
const chalk = require("chalk");
4-
const inquirer = require("inquirer");
5-
const fuzzy = require("fuzzy");
6-
const ora = require("ora");
7-
const { extract } = require("pacote");
8-
const glob = require("fast-glob");
9-
const fs = require("fs-extra");
10-
const os = require("os");
1+
import yargsParser from "yargs-parser";
2+
import path from "path";
3+
import chalk from "chalk";
4+
import inquirer from "inquirer";
5+
import fuzzy from "fuzzy";
6+
import ora from "ora";
7+
import * as pactoe from "pacote";
8+
import glob from "fast-glob";
9+
import fs from "fs-extra";
10+
import os from "os";
11+
import elapsed from "elapsed-time-logger";
12+
import compareVersions from "compare-versions";
1113

14+
const { extract } = pactoe;
1215
const packageName = "html5-boilerplate";
1316
const tempDir = `${os.tmpdir()}/${packageName}-staging`;
14-
const elapsed = require("elapsed-time-logger");
15-
const compareVersions = require("compare-versions");
16-
const langsList = require("./countries.json");
1717

1818
let spinner;
1919
inquirer.registerPrompt(
@@ -58,6 +58,10 @@ const onLoad = async (targetDir, version, argv) => {
5858
if (skipPrompts) {
5959
return;
6060
}
61+
const langsList = JSON.parse(
62+
await fs.readFile(new URL("./countries.json", import.meta.url))
63+
);
64+
6165
const langListMap = {};
6266
const langListOut = [];
6367
/* istanbul ignore if */
@@ -118,7 +122,7 @@ const onLoad = async (targetDir, version, argv) => {
118122
}
119123
};
120124

121-
module.exports = async (argvs) => {
125+
export default async function CreateHtml5BoilerplateCLI(argvs) {
122126
const argv = yargsParser(argvs, {
123127
alias: { release: ["r"], yes: ["y"] },
124128
});
@@ -160,4 +164,4 @@ module.exports = async (argvs) => {
160164
} finally {
161165
await fs.remove(tempDir);
162166
}
163-
};
167+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"license": "MIT",
1515
"author": "@h5bp",
1616
"main": "index.js",
17+
"type": "module",
1718
"bin": {
1819
"create-html5-boilerplate": "./index.js"
1920
},

tests/test.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
* @jest-environment node
33
*/
44

5-
const fs = require("fs-extra");
6-
const cli = require("../lib/cli");
7-
const os = require("os");
5+
import fs from "fs-extra";
6+
import os from "os";
7+
import cli from "../lib/cli";
8+
89
const packageName = "html5-boilerplate";
9-
const tempDir = os.tmpdir() + `/${packageName}-staging`;
10+
const tempDir = `${os.tmpdir()}/${packageName}-staging`;
1011
const defaultDir = "./out/default_dir";
1112

1213
// TODO: fetch all versions:
1314
// const { packument } = require('pacote');
1415
// const versions = await packument(packageName);
1516
// cases = ['', 'latest', '-r=7.3.0', ...Object.keys(versions)];
16-
const all_versions = [
17+
const allVersions = [
1718
"0.0.1",
1819
"5.3.0",
1920
"6.0.0",
@@ -33,10 +34,10 @@ const cases = [
3334
"-r=v7.2.0",
3435
"-r=v7.2",
3536
"--release=7.3.0",
36-
...all_versions.map((v) => "-r=" + v),
37+
...allVersions.map((v) => `-r=${v}`),
3738
];
3839

39-
const outputFolder = (version = null) => "./out/" + (version || "default_dir");
40+
const outputFolder = (version = null) => `./out/${version || "default_dir"}`;
4041

4142
const runCli = async ({
4243
version = null,
@@ -47,7 +48,7 @@ const runCli = async ({
4748
let argvs = [];
4849
let prevCwd;
4950
if (dir) {
50-
argvs.push("./out/" + dir);
51+
argvs.push(`./out/${dir}`);
5152
} else {
5253
await fs.ensureDir(defaultDir);
5354
prevCwd = process.cwd();
@@ -66,9 +67,10 @@ const runCli = async ({
6667

6768
await cli(argvs);
6869
if (prevCwd) {
69-
process.chdir(prevCwd); //revert process current dir
70+
process.chdir(prevCwd); // revert process current dir
7071
}
7172
};
73+
7274
describe.each(cases)("Downloading %s", (version) => {
7375
beforeAll(async () => {
7476
await runCli({ version: version, dir: version, skip: true });
@@ -77,7 +79,7 @@ describe.each(cases)("Downloading %s", (version) => {
7779
await fs.remove(outputFolder(version));
7880
});
7981

80-
if (version && version != "-r=latest") {
82+
if (version && version !== "-r=latest") {
8183
// if we will fetch all versions from npm registry we will be able to check latest
8284
// for now we will skip this test for 'latest' version
8385
test(`Version is correct: ${version}`, async () => {

0 commit comments

Comments
 (0)