-
-
Notifications
You must be signed in to change notification settings - Fork 203
Refactoring of the CLI interface #291
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a10873
Refactoring of the CLI interface
arcanis 341f237
Updates the Nock snapshots
arcanis ad79b9b
Regenerates the Nock files on Node 16
arcanis ace2757
Update README.md
arcanis aa34ecf
Adds --cache-only to corepack install -g
arcanis 6bd163d
Fixes hash generation
arcanis 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,64 @@ | ||
import {Command, UsageError} from 'clipanion'; | ||
import fs from 'fs'; | ||
|
||
import {PreparedPackageManagerInfo} from '../Engine'; | ||
import * as corepackUtils from '../corepackUtils'; | ||
import {Context} from '../main'; | ||
import * as nodeUtils from '../nodeUtils'; | ||
import * as specUtils from '../specUtils'; | ||
|
||
export abstract class BaseCommand extends Command<Context> { | ||
async resolvePatternsToDescriptors({all, patterns}: {all: boolean, patterns: Array<string>}) { | ||
if (all && patterns.length > 0) | ||
throw new UsageError(`The --all option cannot be used along with an explicit package manager specification`); | ||
|
||
const resolvedSpecs = all | ||
? await this.context.engine.getDefaultDescriptors() | ||
: patterns.map(pattern => specUtils.parseSpec(pattern, `CLI arguments`, {enforceExactVersion: false})); | ||
|
||
if (resolvedSpecs.length === 0) { | ||
const lookup = await specUtils.loadSpec(this.context.cwd); | ||
switch (lookup.type) { | ||
case `NoProject`: | ||
throw new UsageError(`Couldn't find a project in the local directory - please explicit the package manager to pack, or run this command from a valid project`); | ||
|
||
case `NoSpec`: | ||
throw new UsageError(`The local project doesn't feature a 'packageManager' field - please explicit the package manager to pack, or update the manifest to reference it`); | ||
|
||
default: { | ||
return [lookup.spec]; | ||
} | ||
} | ||
} | ||
|
||
return resolvedSpecs; | ||
} | ||
|
||
async setLocalPackageManager(info: PreparedPackageManagerInfo) { | ||
const lookup = await specUtils.loadSpec(this.context.cwd); | ||
|
||
const content = lookup.target !== `NoProject` | ||
? await fs.promises.readFile(lookup.target, `utf8`) | ||
: ``; | ||
|
||
const {data, indent} = nodeUtils.readPackageJson(content); | ||
|
||
const previousPackageManager = data.packageManager ?? `unknown`; | ||
data.packageManager = `${info.locator.name}@${info.locator.reference}+${info.hash}`; | ||
|
||
const newContent = nodeUtils.normalizeLineEndings(content, `${JSON.stringify(data, null, indent)}\n`); | ||
await fs.promises.writeFile(lookup.target, newContent, `utf8`); | ||
|
||
const command = this.context.engine.getPackageManagerSpecFor(info.locator).commands?.use ?? null; | ||
if (command === null) | ||
return 0; | ||
|
||
// Adding it into the environment avoids breaking package managers that | ||
// don't expect those options. | ||
process.env.COREPACK_MIGRATE_FROM = previousPackageManager; | ||
this.context.stdout.write(`\n`); | ||
|
||
const [binaryName, ...args] = command; | ||
return await corepackUtils.runVersion(info.locator, info, binaryName, args); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.