Skip to content

Commit

Permalink
Make CLI ESM-only (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
kachkaev authored Oct 15, 2022
1 parent fa34f7c commit 314eb08
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 32 deletions.
9 changes: 5 additions & 4 deletions cli/cli.cjs → cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

/* eslint-disable no-console */

const chalk = require("chalk");
const program = require("commander");
const { getPackageVersion, generateUrl, openUrl } = require("./helpers.cjs");
import chalk from "chalk";
import { program } from "commander";

import { generateUrl, getPackageVersion, openUrl } from "./main.js";

const green = chalk.green;
const code = chalk.dim;
Expand Down Expand Up @@ -73,7 +74,7 @@ if (program.rawArgs.length < 3) {

const args = [...program.args];
if (args[0] === ".") {
const finder = require("find-package-json");
const { default: finder } = await import("find-package-json");
const finderInstance = finder();
const packageJsonSearchResult = finderInstance.next();
if (!packageJsonSearchResult.value) {
Expand Down
17 changes: 0 additions & 17 deletions cli/helpers.cjs

This file was deleted.

3 changes: 0 additions & 3 deletions cli/index.cjs

This file was deleted.

21 changes: 21 additions & 0 deletions cli/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import open from "open";

export const getPackageVersion = () => {
const filePath = new URL(import.meta.url).pathname;
const packageJsonPath = resolve(dirname(filePath), "package.json");
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));

return packageJson.version;
};

export const generateUrl = (query) => {
return `https://njt.vercel.app/jump?from=cli%40${getPackageVersion()}&to=${encodeURIComponent(
query,
)}`;
};

export const openUrl = async (url, browser) => {
await open(url, { app: browser });
};
11 changes: 8 additions & 3 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@
"name": "Alexander Kachkaev",
"email": "alexander@kachkaev.ru"
},
"type": "module",
"main": "main.js",
"bin": "cli.js",
"scripts": {
"prepublishOnly": "yarn ncp ../README.md README.md"
},
"dependencies": {
"chalk": "^4.1.2",
"commander": "^8.3.0",
"chalk": "^5.1.2",
"commander": "^9.4.1",
"find-package-json": "^1.2.0",
"open": "^8.4.0"
},
"devDependencies": {
"ncp": "^2.0.0"
},
"engines": {
"node": ">=12.17"
"node": "^14.18.0 || >=16.0.0"
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"husky": "^8.0.1",
"lint-staged": "^12.4.0",
"markdownlint-cli": "^0.31.1",
"ncp": "^2.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.6.2",
"typescript": "^4.6.3"
Expand Down
22 changes: 18 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ __metadata:
languageName: node
linkType: hard

"chalk@npm:^4.0.0, chalk@npm:^4.1.2":
"chalk@npm:^4.0.0":
version: 4.1.2
resolution: "chalk@npm:4.1.2"
dependencies:
Expand All @@ -884,6 +884,13 @@ __metadata:
languageName: node
linkType: hard

"chalk@npm:^5.1.2":
version: 5.1.2
resolution: "chalk@npm:5.1.2"
checksum: 0bfd090fccb1c5918b19bea32a8f34c5e19db774758fa0ff28883aba790d614c0e4cea5061d6a48967c2b4cc91c51b77a3f9f9cee2f4fb5d258b5bf248193739
languageName: node
linkType: hard

"ci-info@npm:^3.3.0":
version: 3.3.0
resolution: "ci-info@npm:3.3.0"
Expand Down Expand Up @@ -982,6 +989,13 @@ __metadata:
languageName: node
linkType: hard

"commander@npm:^9.4.1":
version: 9.4.1
resolution: "commander@npm:9.4.1"
checksum: 7d4c1f3bd4ad451f43fce51b9c73dc2d61fca00a9249196aa2d3d66fc46f2460c917c73ea9221c278fb8d35f496384f5125806e69108581a8e8bc673752c94e6
languageName: node
linkType: hard

"commander@npm:~9.0.0":
version: 9.0.0
resolution: "commander@npm:9.0.0"
Expand Down Expand Up @@ -2695,9 +2709,10 @@ __metadata:
version: 0.0.0-use.local
resolution: "njt@workspace:cli"
dependencies:
chalk: "npm:^4.1.2"
commander: "npm:^8.3.0"
chalk: "npm:^5.1.2"
commander: "npm:^9.4.1"
find-package-json: "npm:^1.2.0"
ncp: "npm:^2.0.0"
open: "npm:^8.4.0"
bin:
njt: cli.js
Expand Down Expand Up @@ -3365,7 +3380,6 @@ __metadata:
lint-staged: "npm:^12.4.0"
lru-cache: "npm:^7.8.1"
markdownlint-cli: "npm:^0.31.1"
ncp: "npm:^2.0.0"
next: "npm:^12.1.5"
npm-run-all: "npm:^4.1.5"
prettier: "npm:^2.6.2"
Expand Down

0 comments on commit 314eb08

Please sign in to comment.