-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change provides a preliminary command line for `endo`, mostly because it is convenient for ad-hoc testing, though it will likely grow into something else.
- Loading branch information
Showing
7 changed files
with
142 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
endo.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env node | ||
import fs from "fs"; | ||
import { main } from "../src/cli.js"; | ||
|
||
main(process, { fs: fs.promises }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* eslint no-shadow: [0] */ | ||
import "./lockdown.js"; | ||
import { writeArchive } from "./main.js"; | ||
import { search } from "./search.js"; | ||
import { compartmentMapForNodeModules } from "./compartmap.js"; | ||
|
||
function usage(message) { | ||
console.error(message); | ||
return 1; | ||
} | ||
|
||
async function noEntryUsage() { | ||
return usage(`expected path to program`); | ||
} | ||
|
||
async function noArchiveUsage() { | ||
return usage(`expected path for archive`); | ||
} | ||
|
||
async function subcommand([arg, ...rest], handlers) { | ||
const keys = Object.keys(handlers); | ||
if (arg === undefined || !keys.includes(arg)) { | ||
return usage(`expected one of ${keys.join(", ")}`); | ||
} | ||
return handlers[arg](rest); | ||
} | ||
|
||
async function parameter(args, handle, usage) { | ||
const [arg, ...rest] = args; | ||
if (arg === undefined) { | ||
return usage(`expected an argument`); | ||
} | ||
if (arg.startsWith("-")) { | ||
return usage(`unexpected flag: ${arg}`); | ||
} | ||
return handle(arg, rest); | ||
} | ||
|
||
async function run(args, { cwd, read, write, stdout }) { | ||
async function compartmap(args) { | ||
async function handleEntry(applicationPath, args) { | ||
if (args.length) { | ||
return usage(`unexpected arguments: ${JSON.stringify(args)}`); | ||
} | ||
const currentLocation = new URL(`${cwd()}/`, "file:///"); | ||
const applicationLocation = new URL(applicationPath, currentLocation); | ||
const { packageLocation } = await search(read, applicationLocation); | ||
const compartmentMap = await compartmentMapForNodeModules( | ||
read, | ||
packageLocation | ||
); | ||
stdout.write(`${JSON.stringify(compartmentMap, null, 2)}\n`); | ||
return 0; | ||
} | ||
return parameter(args, handleEntry, noEntryUsage); | ||
} | ||
|
||
async function archive(args) { | ||
async function handleArchive(archivePath, args) { | ||
async function handleEntry(applicationPath, args) { | ||
if (args.length) { | ||
return usage(`unexpected arguments: ${JSON.stringify(args)}`); | ||
} | ||
const currentLocation = new URL(`${cwd()}/`, "file:///"); | ||
const archiveLocation = new URL(archivePath, currentLocation); | ||
const applicationLocation = new URL(applicationPath, currentLocation); | ||
await writeArchive(write, read, archiveLocation, applicationLocation); | ||
return 0; | ||
} | ||
return parameter(args, handleEntry, noEntryUsage); | ||
} | ||
return parameter(args, handleArchive, noArchiveUsage); | ||
} | ||
|
||
return subcommand(args, { compartmap, archive }); | ||
} | ||
|
||
export async function main(process, modules) { | ||
const { fs } = modules; | ||
const { cwd, stdout } = process; | ||
|
||
// Filesystem errors often don't have stacks: | ||
|
||
async function read(location) { | ||
try { | ||
return await fs.readFile(new URL(location).pathname); | ||
} catch (error) { | ||
throw new Error(error.message); | ||
} | ||
} | ||
|
||
async function write(location, content) { | ||
try { | ||
return await fs.writeFile(new URL(location).pathname, content); | ||
} catch (error) { | ||
throw new Error(error.message); | ||
} | ||
} | ||
|
||
try { | ||
process.exitCode = await run(process.argv.slice(2), { | ||
read, | ||
write, | ||
cwd, | ||
stdout | ||
}); | ||
} catch (error) { | ||
process.exitCode = usage(error.stack || error.message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters