Comparing version 1.1.1 to 2.0.0
190
cli.js
@@ -15,6 +15,18 @@ #!/usr/bin/env node | ||
function getPackageVersion() { | ||
const packageJsonPath = path.join(__dirname, "package.json"); | ||
try { | ||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")); | ||
if (packageJson.version) { | ||
return packageJson.version; | ||
} | ||
} catch (error) { | ||
console.error("Error reading package.json to determine version:", error); | ||
} | ||
return "Unknown"; // Default version if unsuccessful | ||
} | ||
async function main() { | ||
const program = new Command(); | ||
program.version("0.0.1"); | ||
program.version(getPackageVersion()); | ||
@@ -25,8 +37,6 @@ const defaultPort = 3000; | ||
const defaultRegistry = "https://registry.npmjs.org/"; | ||
const esmgenReadmePath = path.join(__dirname, "README.md"); | ||
program | ||
.command("download [pkg] [version]") | ||
.alias("dl") | ||
.alias("d") | ||
.command("add [pkg] [version]") | ||
.alias("a") | ||
.description("Download and convert a package to ESM.") | ||
@@ -50,19 +60,16 @@ .option( | ||
if (!pkg) { | ||
console.error("Package name is required."); | ||
process.exit(1); | ||
} | ||
const esmPackages = getEsmPackages(); | ||
const CONVERTED_DIR = path.resolve(options.dir || defaultDir); | ||
console.log(`Processing ${pkg}@${version}`); | ||
console.log(`Converted output directory: ${CONVERTED_DIR}`); | ||
if (!esmPackages || Object.keys(esmPackages).length === 0) { | ||
console.error( | ||
"No packages specified in the esm section of package.json and no package name provided." | ||
); | ||
return; | ||
} | ||
const esmDirectory = await processPackage( | ||
pkg, | ||
version, | ||
CONVERTED_DIR, | ||
options | ||
); | ||
if (options.serve) { | ||
serve(esmDirectory, options.port, options.host, options.entry); | ||
for (const [name, version] of Object.entries(esmPackages)) { | ||
await processAndOptionallyServe(name, version, options); | ||
} | ||
} else { | ||
await processAndOptionallyServe(pkg, version, options); | ||
} | ||
@@ -80,11 +87,37 @@ }); | ||
.action((options) => { | ||
const CONVERTED_DIR = path.resolve(options.dir || defaultDir); | ||
console.log(`Serving from directory: ${CONVERTED_DIR}`); | ||
serve(CONVERTED_DIR, options.port, options.host, options.entry); | ||
const esmDir = path.resolve(options.dir || defaultDir); | ||
console.log(`Serving from directory: ${esmDir}`); | ||
serve(esmDir, options.port, options.host, options.entry); | ||
}); | ||
program | ||
.command("remove <pkg>") | ||
.alias("rm") | ||
.alias("r") | ||
.description("Remove an ESM package and update package.json.") | ||
.action((pkg) => { | ||
const esmDirectory = path.resolve(defaultDir); | ||
const packageDirs = fs.readdirSync(esmDirectory); | ||
console.log(`Removing ${pkg}...`); | ||
const removed = updatePackageJson(pkg, "remove"); | ||
for (const dir of packageDirs) { | ||
if (dir.startsWith(pkg + "@")) { | ||
const packageDir = path.join(esmDirectory, dir); | ||
if (fs.existsSync(packageDir)) { | ||
fs.rmSync(packageDir, { recursive: true, force: true }); | ||
console.log(`Removed ${pkg} from ESM directory.`); | ||
return; | ||
} | ||
} | ||
} | ||
console.error(`Package ${pkg} is not installed.`); | ||
}); | ||
program.parse(process.argv); | ||
const tempDir = os.tmpdir(); | ||
const DOWNLOAD_DIR = path.join(tempDir, "esmgen-downloads"); | ||
const tmpDir = path.join(tempDir, "esmgen-downloads"); | ||
@@ -226,2 +259,9 @@ function serve(dir, port, host, customEntryFile) { | ||
const inputFilePath = findEntryFile(inputDir); | ||
if (!inputFilePath) { | ||
console.warn( | ||
`No JavaScript entry file found in ${inputDir}. Skipping ESM conversion.` | ||
); | ||
return; | ||
} | ||
const entryDir = path.dirname(inputFilePath); | ||
@@ -255,3 +295,3 @@ | ||
if (!fs.existsSync(packageJsonPath)) { | ||
throw new Error("package.json not found in the root directory"); | ||
return null; | ||
} | ||
@@ -264,3 +304,2 @@ | ||
if (exportFields) { | ||
// Check for direct paths in exports, scrambled accordingly. | ||
const possiblePaths = [exportFields.default, exportFields.require].flat(); | ||
@@ -278,3 +317,2 @@ | ||
// If no exports, look for built files in the dist directory. | ||
const distFiles = ["dist/index.js", "dist/index.cjs"]; | ||
@@ -288,3 +326,2 @@ for (const file of distFiles) { | ||
// Last resort, check source field or typical main alternatives. | ||
const sourceFile = packageJson.source || null; | ||
@@ -303,9 +340,17 @@ const mainFile = packageJson.main || null; | ||
// If none are found, report error. | ||
throw new Error(` | ||
None of the potential entry files specified under exports, or built files (${distFiles.join(", ")}), | ||
source or main were found in ${inputDir} | ||
`); | ||
return null; | ||
} | ||
async function processAndOptionallyServe(pkg, version, options) { | ||
const esmDir = path.resolve(options.dir || defaultDir); | ||
console.log(`Processing ${pkg}@${version}`); | ||
console.log(`Converted output directory: ${esmDir}`); | ||
const esmDirectory = await processPackage(pkg, version, esmDir, options); | ||
if (options.serve) { | ||
serve(esmDirectory, options.port, options.host, options.entry); | ||
} | ||
} | ||
async function processPackage(pkg, version, conversionDir, options) { | ||
@@ -315,9 +360,6 @@ try { | ||
ensureDirectoryExists(DOWNLOAD_DIR); | ||
ensureDirectoryExists(tmpDir); | ||
ensureDirectoryExists(conversionDir); | ||
const downloadOutputDir = path.join( | ||
DOWNLOAD_DIR, | ||
`${pkg}@${data.version}` | ||
); | ||
const downloadOutputDir = path.join(tmpDir, `${pkg}@${data.version}`); | ||
const conversionOutputDir = path.join( | ||
@@ -336,14 +378,33 @@ conversionDir, | ||
console.log(`Converting to ESM modules...`); | ||
console.log(`Processing package assets...`); | ||
const entryFile = findEntryFile(srcDir); | ||
// Always copy assets if any | ||
fs.mkdirSync(conversionOutputDir, { recursive: true }); | ||
if (entryFile) { | ||
console.log( | ||
`Converting to ESM modules from entry file ${entryFile}...` | ||
); | ||
await convertToESM( | ||
srcDir, | ||
conversionOutputDir, | ||
options.includeAllAssets | ||
); | ||
} else { | ||
console.log(`No entry file. Only assets will be copied if present.`); | ||
copyAssets( | ||
srcDir, | ||
conversionOutputDir, | ||
srcDir, | ||
options.includeAllAssets | ||
); | ||
} | ||
const esmOutputDirectory = await convertToESM( | ||
srcDir, | ||
conversionOutputDir, | ||
options.includeAllAssets | ||
); | ||
// Update package.json with the actual resolved version | ||
updatePackageJson(pkg, "add", data.version); | ||
return esmOutputDirectory; | ||
return conversionOutputDir; | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
return null; | ||
} | ||
@@ -381,3 +442,2 @@ } | ||
function copyAssets(srcDir, destDir, entryDir, includeAllAssets = false) { | ||
// Define acceptable asset file extensions hard-coded | ||
const includedAssetsExtensions = [ | ||
@@ -400,3 +460,2 @@ ".css", | ||
if (includeAllAssets) { | ||
// Copy all asset files accepting only specific file types | ||
if ( | ||
@@ -432,3 +491,2 @@ file.isDirectory() || | ||
// Print excluded assets if not included by flag | ||
if (!includeAllAssets && excludedAssets.length > 0) { | ||
@@ -442,2 +500,38 @@ console.log("The following files/assets are not included:"); | ||
} | ||
function getEsmPackages() { | ||
const packageJsonPath = path.join(process.cwd(), "package.json"); | ||
if (!fs.existsSync(packageJsonPath)) return {}; | ||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")); | ||
return packageJson.esm?.packages || {}; | ||
} | ||
function updatePackageJson(pkg, action, version) { | ||
const packageJsonPath = path.join(process.cwd(), "package.json"); | ||
let packageJson; | ||
if (fs.existsSync(packageJsonPath)) { | ||
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")); | ||
} else { | ||
return; | ||
} | ||
if (!packageJson.esm) { | ||
packageJson.esm = { packages: {} }; | ||
} | ||
if (action === "add") { | ||
packageJson.esm.packages[pkg] = version; | ||
} else if (action === "remove") { | ||
delete packageJson.esm.packages[pkg]; | ||
} | ||
fs.writeFileSync( | ||
packageJsonPath, | ||
JSON.stringify(packageJson, null, 2), | ||
"utf-8" | ||
); | ||
console.log(`package.json successfully updated.`); | ||
} | ||
} | ||
@@ -444,0 +538,0 @@ |
{ | ||
"name": "esmgen", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "A CLI tool that generates and packages ESM modules from npm into a structured directory, serving with a static file server.", | ||
@@ -40,3 +40,6 @@ "main": "cli.js", | ||
"tar": "^7.4.3" | ||
}, | ||
"esm": { | ||
"packages": {} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20386
452