-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(endo): Add command line #398
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b299916
feat(endo): Add command line
kriskowal 84260ed
Add endo bin
kriskowal ae1fd50
Use stdout for compartmap
kriskowal 5e4145e
Revise argument forms for endo bin
kriskowal 49aeefe
endo/main Use subcommands, remove dubious ones
kriskowal ed0a6b5
endo/main Fix lint
kriskowal 3e5bcb0
endo/cli Nits
kriskowal 0ed3dbc
Direct bin:endo at bin/endo.js
kriskowal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does.. this work? Is there some weird pseudo-symlink data format going on here that Node.js recognizes in a
bin:
property? I would have expected package.json to point atendo.js
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ll point this to
endo.js
since you’re right that it’s more likely to work depending on what tool you use to install CLI’s. But, Node.js does pierce symlinks such that relative imports are always relative to the canonical location.