1
mirror of https://github.com/jakejarvis/jakejarvis.git synced 2025-04-26 13:28:28 -04:00

convert to ESM (doesn't affect output)

This commit is contained in:
Jake Jarvis 2022-05-09 10:56:50 -04:00
parent 0489344e53
commit 789302a6bc
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
7 changed files with 132 additions and 51 deletions

1
cli/.gitignore vendored Executable file → Normal file
View File

@ -1,3 +1,2 @@
node_modules/ node_modules/
tmp/
dist/ dist/

0
cli/LICENSE Executable file → Normal file
View File

0
cli/README.md Executable file → Normal file
View File

View File

@ -1,20 +1,16 @@
const path = require("path"); import path from "path";
const fs = require("fs").promises; import fs from "fs-extra";
const babel = require("@babel/core"); import { temporaryDirectoryTask } from "tempy";
const ncc = require("@vercel/ncc"); import { transformFileAsync } from "@babel/core";
const prettyBytes = require("pretty-bytes"); import ncc from "@vercel/ncc";
import prettyBytes from "pretty-bytes";
(async () => {
// prepare some paths // prepare some paths
const tempDir = path.join(__dirname, "tmp"); const inputFile = path.join(process.cwd(), "index.cjs");
const distDir = path.join(__dirname, "dist"); const outputDir = path.join(process.cwd(), "dist");
// remove anything leftover from previous builds
await fs.rm(tempDir, { recursive: true, force: true });
await fs.rm(distDir, { recursive: true, force: true });
// run code through babel // run code through babel
const { code: babelCode } = await babel.transformFileAsync(path.join(__dirname, "index.js"), { const { code: babelCode } = await transformFileAsync(inputFile, {
presets: [ presets: [
[ [
"@babel/preset-react", "@babel/preset-react",
@ -23,32 +19,43 @@ const prettyBytes = require("pretty-bytes");
}, },
], ],
], ],
sourceType: "script", // we're outputting CJS, even though this package is technically ESM
}); });
// save babelified code to a file -- unfortunately, we can't pipe this code directly to ncc below await temporaryDirectoryTask(async (tempPath) => {
await fs.mkdir(tempDir); // save babel-ified code to a temporary file -- unfortunately, we can't pipe this code directly to ncc below
await fs.writeFile(path.join(tempDir, "babel.js"), babelCode); await fs.writeFile(path.join(tempPath, "babel.cjs"), babelCode);
// hacky warning: ncc needs the node_modules we want to bundle next to the input file, so copy them from here
await fs.copy(path.join(process.cwd(), "node_modules"), path.join(tempPath, "node_modules"));
// compile for distribution with ncc // compile for distribution with ncc
const { code, assets } = await ncc(path.join(tempDir, "babel.js"), { const { code, assets } = await ncc(path.join(tempPath, "babel.cjs"), {
cache: false, cache: false,
sourceMap: false, sourceMap: false,
minify: true, minify: true,
debugLog: true, debugLog: true,
assetBuilds: true,
}); });
// write final build to ./dist and make executable // remove anything leftover from previous builds
await fs.mkdir(distDir); await fs.emptyDir(outputDir);
await fs.writeFile(path.join(distDir, "index.js"), code);
await fs.writeFile(path.join(distDir, "xdg-open"), assets["xdg-open"].source); // TODO: external script from 'open' module
await fs.chmod(path.join(distDir, "index.js"), 0o775);
await fs.chmod(path.join(distDir, "xdg-open"), 0o775);
// quick logging of resulting filesize // write final build to ./dist
console.log("✅ Success!"); await Promise.all([
console.log(`dist/index.js\t${prettyBytes((await fs.stat(path.join(distDir, "index.js"))).size)}`); fs.outputFile(path.join(outputDir, "index.cjs"), code),
console.log(`dist/xdg-open\t${prettyBytes((await fs.stat(path.join(distDir, "xdg-open"))).size)}`); // TODO: figure out if script from 'open' module is really necessary
fs.outputFile(path.join(outputDir, "xdg-open"), assets["xdg-open"].source),
]);
// clean up temp files // make everything executable
await fs.rm(tempDir, { recursive: true, force: true }); await Promise.all([
})(); fs.chmod(path.join(outputDir, "index.cjs"), 0o775),
fs.chmod(path.join(outputDir, "xdg-open"), 0o775),
]);
});
// quick logging of resulting filesizes
console.log("🎉 Success!");
console.log(`dist/index.cjs\t${prettyBytes((await fs.stat(path.join(outputDir, "index.cjs"))).size)}`);
console.log(`dist/xdg-open\t${prettyBytes((await fs.stat(path.join(outputDir, "xdg-open"))).size)}`);

0
cli/index.js → cli/index.cjs Executable file → Normal file
View File

View File

@ -18,12 +18,13 @@
"dist" "dist"
], ],
"bin": { "bin": {
"jakejarvis": "./dist/index.js" "jakejarvis": "./dist/index.cjs"
}, },
"type": "module",
"scripts": { "scripts": {
"build": "node build.js", "build": "node build.js",
"run": "yarn build && node dist/index.js", "run": "yarn build && node dist/index.cjs",
"lint": "eslint .", "lint": "eslint . --ext js,cjs",
"prepublishOnly": "yarn build" "prepublishOnly": "yarn build"
}, },
"dependencies": {}, "dependencies": {},
@ -34,13 +35,15 @@
"@vercel/ncc": "^0.33.4", "@vercel/ncc": "^0.33.4",
"eslint": "^8.15.0", "eslint": "^8.15.0",
"eslint-plugin-react": "^7.29.4", "eslint-plugin-react": "^7.29.4",
"fs-extra": "^10.1.0",
"ink": "^3.2.0", "ink": "^3.2.0",
"ink-big-text": "^1.2.0", "ink-big-text": "^1.2.0",
"ink-gradient": "^2.0.0", "ink-gradient": "^2.0.0",
"ink-select-input": "^4.2.1", "ink-select-input": "^4.2.1",
"open": "^8.4.0", "open": "^8.4.0",
"pretty-bytes": "^5.6.0", "pretty-bytes": "^6.0.0",
"react": "^18.1.0" "react": "^18.1.0",
"tempy": "^3.0.0"
}, },
"engines": { "engines": {
"node": ">=10" "node": ">=10"
@ -52,7 +55,7 @@
"plugin:react/jsx-runtime" "plugin:react/jsx-runtime"
], ],
"parserOptions": { "parserOptions": {
"ecmaVersion": 2018, "ecmaVersion": "latest",
"sourceType": "module" "sourceType": "module"
}, },
"env": { "env": {

View File

@ -565,6 +565,13 @@ cross-spawn@^7.0.2:
shebang-command "^2.0.0" shebang-command "^2.0.0"
which "^2.0.1" which "^2.0.1"
crypto-random-string@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-4.0.0.tgz#5a3cc53d7dd86183df5da0312816ceeeb5bb1fc2"
integrity sha512-x8dy3RnvYdlUcPOjkEHqozhiwzKNSq7GcPuXFbnyMOCHxX8V3OgIg/pYuabl2sbUPfIJaeAQB7PMOK8DFIdoRA==
dependencies:
type-fest "^1.0.1"
debug@^4.1.0, debug@^4.1.1, debug@^4.3.2: debug@^4.1.0, debug@^4.1.1, debug@^4.3.2:
version "4.3.4" version "4.3.4"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865" resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
@ -847,6 +854,15 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.5.tgz#76c8584f4fc843db64702a6bd04ab7a8bd666da3" resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.5.tgz#76c8584f4fc843db64702a6bd04ab7a8bd666da3"
integrity sha512-WIWGi2L3DyTUvUrwRKgGi9TwxQMUEqPOPQBVi71R96jZXJdFskXEmf54BoZaS1kknGODoIGASGEzBUYdyMCBJg== integrity sha512-WIWGi2L3DyTUvUrwRKgGi9TwxQMUEqPOPQBVi71R96jZXJdFskXEmf54BoZaS1kknGODoIGASGEzBUYdyMCBJg==
fs-extra@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.1.0.tgz#02873cfbc4084dde127eaa5f9905eef2325d1abf"
integrity sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"
fs.realpath@^1.0.0: fs.realpath@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
@ -930,6 +946,11 @@ globals@^13.6.0, globals@^13.9.0:
dependencies: dependencies:
type-fest "^0.20.2" type-fest "^0.20.2"
graceful-fs@^4.1.6, graceful-fs@^4.2.0:
version "4.2.10"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.10.tgz#147d3a006da4ca3ce14728c7aefc287c367d7a6c"
integrity sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==
gradient-string@^1.2.0: gradient-string@^1.2.0:
version "1.2.0" version "1.2.0"
resolved "https://registry.yarnpkg.com/gradient-string/-/gradient-string-1.2.0.tgz#93f39f2c7c8dcb095608c2ccf0aac24aa315fbac" resolved "https://registry.yarnpkg.com/gradient-string/-/gradient-string-1.2.0.tgz#93f39f2c7c8dcb095608c2ccf0aac24aa315fbac"
@ -1204,6 +1225,11 @@ is-shared-array-buffer@^1.0.2:
dependencies: dependencies:
call-bind "^1.0.2" call-bind "^1.0.2"
is-stream@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-3.0.0.tgz#e6bfd7aa6bef69f4f472ce9bb681e3e57b4319ac"
integrity sha512-LnQR4bZ9IADDRSkvpqMGvt/tEJWclzklNgSw48V5EAaAeDd6qGvN8ei6k5p0tvxSR171VmGyHuTiAOfxAbr8kA==
is-string@^1.0.5, is-string@^1.0.7: is-string@^1.0.5, is-string@^1.0.7:
version "1.0.7" version "1.0.7"
resolved "https://registry.yarnpkg.com/is-string/-/is-string-1.0.7.tgz#0dd12bf2006f255bb58f695110eff7491eebc0fd" resolved "https://registry.yarnpkg.com/is-string/-/is-string-1.0.7.tgz#0dd12bf2006f255bb58f695110eff7491eebc0fd"
@ -1269,6 +1295,15 @@ json5@^2.2.1:
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c" resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c"
integrity sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA== integrity sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==
jsonfile@^6.0.1:
version "6.1.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae"
integrity sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==
dependencies:
universalify "^2.0.0"
optionalDependencies:
graceful-fs "^4.1.6"
"jsx-ast-utils@^2.4.1 || ^3.0.0": "jsx-ast-utils@^2.4.1 || ^3.0.0":
version "3.3.0" version "3.3.0"
resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-3.3.0.tgz#e624f259143b9062c92b6413ff92a164c80d3ccb" resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-3.3.0.tgz#e624f259143b9062c92b6413ff92a164c80d3ccb"
@ -1478,10 +1513,10 @@ prelude-ls@^1.2.1:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396" resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g== integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==
pretty-bytes@^5.6.0: pretty-bytes@^6.0.0:
version "5.6.0" version "6.0.0"
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-5.6.0.tgz#356256f643804773c82f64723fe78c92c62beaeb" resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-6.0.0.tgz#928be2ad1f51a2e336add8ba764739f9776a8140"
integrity sha512-FFw039TmrBqFK8ma/7OL3sDz/VytdtJr044/QUJtH0wK9lb9jLq9tJyIxUwtQJHwar2BqtiA4iCWSwo9JLkzFg== integrity sha512-6UqkYefdogmzqAZWzJ7laYeJnaXDy2/J+ZqiiMtS7t7OfpXWTlaeGMwX8U6EFvPV/YWWEKRkS8hKS4k60WHTOg==
prop-types@^15.7.2, prop-types@^15.8.1: prop-types@^15.7.2, prop-types@^15.8.1:
version "15.8.1" version "15.8.1"
@ -1700,6 +1735,21 @@ supports-color@^7.1.0:
dependencies: dependencies:
has-flag "^4.0.0" has-flag "^4.0.0"
temp-dir@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/temp-dir/-/temp-dir-2.0.0.tgz#bde92b05bdfeb1516e804c9c00ad45177f31321e"
integrity sha512-aoBAniQmmwtcKp/7BzsH8Cxzv8OL736p7v1ihGb5e9DJ9kTwGWHrQrVB5+lfVDzfGrdRzXch+ig7LHaY1JTOrg==
tempy@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/tempy/-/tempy-3.0.0.tgz#a6c0a15f5534a820e92c3e1369f1c1e87ebd6b68"
integrity sha512-B2I9X7+o2wOaW4r/CWMkpOO9mdiTRCxXNgob6iGvPmfPWgH/KyUD6Uy5crtWBxIBe3YrNZKR2lSzv1JJKWD4vA==
dependencies:
is-stream "^3.0.0"
temp-dir "^2.0.0"
type-fest "^2.12.2"
unique-string "^3.0.0"
text-table@^0.2.0: text-table@^0.2.0:
version "0.2.0" version "0.2.0"
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
@ -1745,6 +1795,16 @@ type-fest@^0.21.3:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37" resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37"
integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w== integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==
type-fest@^1.0.1:
version "1.4.0"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-1.4.0.tgz#e9fb813fe3bf1744ec359d55d1affefa76f14be1"
integrity sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA==
type-fest@^2.12.2:
version "2.12.2"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.12.2.tgz#80a53614e6b9b475eb9077472fb7498dc7aa51d0"
integrity sha512-qt6ylCGpLjZ7AaODxbpyBZSs9fCI9SkL3Z9q2oxMBQhs/uyY+VD8jHA8ULCGmWQJlBgqvO3EJeAngOHD8zQCrQ==
unbox-primitive@^1.0.2: unbox-primitive@^1.0.2:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/unbox-primitive/-/unbox-primitive-1.0.2.tgz#29032021057d5e6cdbd08c5129c226dff8ed6f9e" resolved "https://registry.yarnpkg.com/unbox-primitive/-/unbox-primitive-1.0.2.tgz#29032021057d5e6cdbd08c5129c226dff8ed6f9e"
@ -1755,6 +1815,18 @@ unbox-primitive@^1.0.2:
has-symbols "^1.0.3" has-symbols "^1.0.3"
which-boxed-primitive "^1.0.2" which-boxed-primitive "^1.0.2"
unique-string@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-3.0.0.tgz#84a1c377aff5fd7a8bc6b55d8244b2bd90d75b9a"
integrity sha512-VGXBUVwxKMBUznyffQweQABPRRW1vHZAbadFZud4pLFAqRGvv/96vafgjWFqzourzr8YonlQiPgH0YCJfawoGQ==
dependencies:
crypto-random-string "^4.0.0"
universalify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"
integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==
uri-js@^4.2.2: uri-js@^4.2.2:
version "4.4.1" version "4.4.1"
resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e" resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"