Skip to content
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

Use mri instead of yargs #23

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d3f54c0
chore: add mri as a dependency
aminya Jul 23, 2021
827d921
fix: replace yargs in papm clean with mri
aminya Jun 25, 2022
6f89aa0
fix: replcae yargs in dedupe with mri
aminya Jun 25, 2022
1142c72
fix: replace yargs in develop with mri
aminya Jun 25, 2022
bd64d33
fix: replace yargs in disable with mri
aminya Jun 25, 2022
84f1cec
fix: replace yargs in docs with mri
aminya Jun 25, 2022
483c7d5
fix: replace yargs in enable with mri
aminya Jun 25, 2022
d210de6
fix: fix the packages parsers for disable, dedupe, develop
aminya Jun 25, 2022
cd4df54
fix: replace yargs with mri in featured
aminya Jun 25, 2022
26ee11c
fix: add missing --json option for develop
aminya Jun 25, 2022
d5584ac
fix: replace yargs with mri in init
aminya Jun 25, 2022
6345dfc
fix: update the help based on the old apm
aminya Jun 25, 2022
34a0321
fix: replace yargs in install with mri
aminya Jun 25, 2022
cdb3875
fix: fix the dedupe parsing
aminya Jun 25, 2022
ea82a4b
fix: replace yargs in link with mri
aminya Jun 25, 2022
d2b4bca
fix: replace yargs in links with mri
aminya Jun 25, 2022
8b2daca
fix: replace yargs with mri in list
aminya Jun 25, 2022
2c1dc61
fix: replace yargs with mri in login
aminya Jun 25, 2022
0580017
fix: add missing defaults for the list options
aminya Jun 25, 2022
e30b8f5
fix: replace yargs in publish with mri
aminya Jun 25, 2022
af64a0c
fix: replace yargs in rebuild-module-cache with mri
aminya Jun 25, 2022
2946021
fix: fix apm disable
aminya Jun 25, 2022
7f93c56
fix: replace yargs in rebuild with mri
aminya Jun 25, 2022
8cb8599
fix: replace yargs in search with mri
aminya Jun 25, 2022
8a9c29c
fix: detect mri with options.help not being a function
aminya Jun 25, 2022
82a0d34
fix: fix help command
aminya Jun 25, 2022
bd418f7
fix: replace yargs in start with mri
aminya Jun 30, 2022
186cfb6
fix: replace yargs in stars with mri
aminya Jun 30, 2022
61727a4
fix: replace yargs with mri in uninstall
aminya Jun 30, 2022
13715c8
fix: replace yargs in unlink with mri
aminya Jun 30, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"dependencies": {
"@aminya/underscore-plus": "^1.9.0",
"@atom/plist": "0.4.4",
"@types/mri": "^1.1.1",
"asar-require": "0.3.0",
"async": "^3.2.0",
"colors": "~1.4.0",
Expand All @@ -53,6 +54,7 @@
"glob": "^7.1.7",
"hosted-git-info": "^3.0.8",
"keytar": "^7.7.0",
"mri": "^1.1.6",
"mv": "2.1.1",
"ncp": "~2.0.0",
"node-gyp": "^9.0.0",
Expand Down
9 changes: 8 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 23 additions & 9 deletions src/apm-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import "asar-require"
import * as config from "./apm"
import fs from "./fs"
import * as git from "./git"
import Command from "./command"

function setupTempDirectory() {
const temp = require("temp")
Expand Down Expand Up @@ -143,12 +144,12 @@ Run \`apm help <command>\` to see the more details about a specific command.\
return options
}

function showHelp(options: CliOptions) {
function showHelp(options: CliOptions, cmd?: Command & { help: () => string }) {
if (options == null) {
return
}

let help = options.help()
let help = typeof options.help !== "function" ? cmd?.help() /* mri */ : options.help() /* yargs */
if (help.indexOf("Options:") >= 0) {
help += "\n Prefix an option with `no-` to set it to false such as --no-color to disable"
help += "\n colored output."
Expand Down Expand Up @@ -280,7 +281,7 @@ function getPythonVersion(callback) {

export type RunCallback = (error?: string | Error | null) => any

export function run(args, callback: RunCallback) {
export function run(args: string[], callback: RunCallback) {
let Command
config.setupApmRcFile()
const options = parseOptions(args)
Expand Down Expand Up @@ -313,21 +314,34 @@ export function run(args, callback: RunCallback) {
return callback?.(error)
}

args = options.argv
const { command } = options
if (args.version) {
return printVersions(args, handleErrorCallback)
} else if (args.help) {
if (options.argv.version) {
return printVersions(options.argv, handleErrorCallback)
} else if (options.argv.help) {
if ((Command = commands[options.command]?.())) {
showHelp(new Command().parseOptions?.(options.command))
const cmd = new Command()
if (typeof cmd.help === "function") {
// converted to mri
showHelp(cmd.parseOptions?.(args), cmd)
} else {
// yargs
showHelp(cmd.parseOptions?.(options.command), cmd)
}
} else {
showHelp(options)
}
return handleErrorCallback()
} else if (command) {
if (command === "help") {
if ((Command = commands[options.commandArgs]?.())) {
showHelp(new Command().parseOptions?.(options.commandArgs))
const cmd = new Command()
if (typeof cmd.help === "function") {
// converted to mri
showHelp(cmd.parseOptions?.(args), cmd)
} else {
// yargs
showHelp(cmd.parseOptions?.(options.commandArgs), cmd)
}
} else {
showHelp(options)
}
Expand Down
28 changes: 14 additions & 14 deletions src/clean.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import yargs from "yargs"
import Command, { LogCommandResultsArgs } from "./command"
import type { CliOptions, RunCallback } from "./apm-cli"
import mri from "mri"

export default class Clean extends Command {
parseOptions(argv: string[]) {
const options = yargs(argv).wrap(Math.min(100, yargs.terminalWidth()))
return mri<{ help: boolean }>(argv, {
alias: { h: "help" },
boolean: "help",
})
}

options.usage(`\
Usage: apm clean
help() {
return `Usage: apm clean

Deletes all packages in the node_modules folder that are not referenced
as a dependency in the package.json file.\
`)
return options.alias("h", "help").describe("help", "Print this usage message")
as a dependency in the package.json file.

Options:
-h, --help Print this usage message
`
}

run(options: CliOptions, callback: RunCallback) {
run(_options: CliOptions, callback: RunCallback) {
process.stdout.write("Removing extraneous modules ")
return this.fork(this.atomNpmPath, ["prune"], (...args: LogCommandResultsArgs) => {
return this.logCommandResults(callback, ...args)
Expand Down
4 changes: 2 additions & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ export default class Command {
return this.spawn(process.execPath, args, ...Array.from(remaining))
}

packageNamesFromArgv(argv) {
packageNamesFromArgv(argv: { _: string[] }): string[] {
return this.sanitizePackageNames(argv._)
}

sanitizePackageNames(packageNames: string[] = []) {
sanitizePackageNames(packageNames: string[] = []): string[] {
packageNames = packageNames.map((packageName) => packageName.trim())
return _.compact(_.uniq(packageNames))
}
Expand Down
59 changes: 39 additions & 20 deletions src/dedupe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,55 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import async from "async"
import yargs from "yargs"
import * as config from "./apm"
import Command, { LogCommandResultsArgs } from "./command"
import Command, { LogCommandResultsArgs, SpawnArgs } from "./command"
import fs from "./fs"
import type { CliOptions, RunCallback } from "./apm-cli"
import mri from "mri"

export default class Dedupe extends Command {
parseOptions(argv: string[]) {
const options = yargs(argv).wrap(Math.min(100, yargs.terminalWidth()))
options.usage(`\
return mri<{
help: boolean
silent: boolean
quiet: boolean
_: string[]

// added later
cwd: string
}>(argv, {
alias: { h: "help" },
boolean: ["help", "silent", "quiet"],
})
}

Usage: apm dedupe [<package_name>...]
help() {
return `Usage: apm dedupe [<package_name>...]

Reduce duplication in the node_modules folder in the current directory.

This command is experimental.\
`)
return options.alias("h", "help").describe("help", "Print this usage message")
This command is experimental.

Options:
-h, --help Print this usage message
--silent
--quiet
`
}

dedupeModules(options, callback) {
dedupeModules(
options: ReturnType<Dedupe["parseOptions"]>,
packageNames: string[],
callback: (error?: string) => void
) {
process.stdout.write("Deduping modules ")

return this.forkDedupeCommand(options, (...args: LogCommandResultsArgs) => {
return this.forkDedupeCommand(options, packageNames, (...args: LogCommandResultsArgs) => {
return this.logCommandResults(callback, ...args)
})
}

forkDedupeCommand(options, callback) {
forkDedupeCommand(options: ReturnType<Dedupe["parseOptions"]>, packageNames: string[], callback: SpawnArgs) {
const dedupeArgs = [
"--globalconfig",
config.getGlobalConfigPath(),
Expand All @@ -42,14 +62,14 @@ This command is experimental.\
"dedupe",
]
dedupeArgs.push(...Array.from(this.getNpmBuildFlags() || []))
if (options.argv.silent) {
if (options.silent) {
dedupeArgs.push("--silent")
}
if (options.argv.quiet) {
if (options.quiet) {
dedupeArgs.push("--quiet")
}

for (const packageName of options.argv._) {
for (const packageName of packageNames) {
dedupeArgs.push(packageName)
}

Expand All @@ -58,7 +78,7 @@ This command is experimental.\
const env = { ...process.env, HOME: this.atomNodeDirectory, RUSTUP_HOME: config.getRustupHomeDirPath() }
this.addBuildEnvVars(env)

const dedupeOptions = { env }
const dedupeOptions: Record<string, string | undefined> = { env }
if (options.cwd) {
dedupeOptions.cwd = options.cwd
}
Expand All @@ -71,16 +91,15 @@ This command is experimental.\
return fs.makeTreeSync(this.atomNodeDirectory)
}

run(options: CliOptions, callback: RunCallback) {
const { cwd } = options
options = this.parseOptions(options.commandArgs)
options.cwd = cwd
run(givenOptions: CliOptions, callback: RunCallback) {
const options = this.parseOptions(givenOptions.commandArgs)
const packageNames = this.packageNamesFromArgv(options)

this.createAtomDirectories()

const commands = []
commands.push((callback) => this.loadInstalledAtomMetadata(callback))
commands.push((callback) => this.dedupeModules(options, callback))
commands.push((callback) => this.dedupeModules(options, packageNames, callback))
return async.waterfall(commands, callback)
}
}
Loading