Skip to content

Commit

Permalink
Merge pull request #29 from atom-community/fix-types
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Aug 8, 2021
2 parents 48987d6 + 2e3e54a commit 91c7a90
Show file tree
Hide file tree
Showing 40 changed files with 350 additions and 220 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
},
"devDependencies": {
"@types/async": "^3.2.7",
"@types/atom": "^1.40.11",
"@types/express": "^4.17.13",
"@types/first-mate": "^7.0.6",
"@types/fs-plus": "3.0.2",
Expand Down
14 changes: 9 additions & 5 deletions src/apm-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ const commands = {
show: viewClass,
}

function parseOptions(args = []) {
const options = yargs(args).wrap(Math.min(100, yargs.terminalWidth()))
export type CliOptions = yargs.Argv<{}> & { commandArgs: string[] } // TODO pass commandArgs directly

function parseOptions(args: string[] = []): CliOptions {
const options = yargs(args).wrap(Math.min(100, yargs.terminalWidth())) as CliOptions
options.usage(`\
apm - Atom Package Manager powered by https://atom.io
Expand All @@ -141,7 +143,7 @@ Run \`apm help <command>\` to see the more details about a specific command.\
return options
}

function showHelp(options) {
function showHelp(options: CliOptions) {
if (options == null) {
return
}
Expand Down Expand Up @@ -266,7 +268,9 @@ function getPythonVersion(callback) {
})
}

export function run(args, callback) {
export type RunCallback = (error?: string | Error | null) => any

export function run(args, callback: RunCallback) {
let Command
config.setupApmRcFile()
const options = parseOptions(args)
Expand All @@ -276,7 +280,7 @@ export function run(args, callback) {
}

let callbackCalled = false
const handleErrorCallback = (error) => {
const handleErrorCallback = (error?: string | Error) => {
if (callbackCalled) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/apm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function visualStudioIsInstalled(version) {
}
}

export function loadNpm(callback) {
export function loadNpm(callback: (config: null, npmVar: typeof npm) => void) {
const npmOptions = {
userconfig: getUserConfigPath(),
globalconfig: getGlobalConfigPath(),
Expand Down
16 changes: 9 additions & 7 deletions src/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import fs from "./fs"
import yargs from "yargs"
import async from "async"
import * as config from "./apm"
import Command from "./command"
import Command, { LogCommandResultsArgs } from "./command"
import type { CliOptions, RunCallback } from "./apm-cli"

export default class Ci extends Command {
private atomDirectory = config.getAtomDirectory()
private atomNpmPath = require.resolve("npm/bin/npm-cli")
private atomNodeDirectory: string
constructor() {
super()
this.atomDirectory = config.getAtomDirectory()
this.atomNodeDirectory = path.join(this.atomDirectory, ".node-gyp")
this.atomNpmPath = require.resolve("npm/bin/npm-cli")
}

parseOptions(argv) {
parseOptions(argv: string[]) {
const options = yargs(argv).wrap(Math.min(100, yargs.terminalWidth()))
options.usage(`\
Usage: apm ci
Expand Down Expand Up @@ -63,12 +65,12 @@ but cannot be used to install new packages or dependencies.\

const installOptions = { env, streaming: options.argv.verbose }

return this.fork(this.atomNpmPath, installArgs, installOptions, (...args) => {
return this.logCommandResults(callback, ...Array.from(args))
return this.fork(this.atomNpmPath, installArgs, installOptions, (...args: LogCommandResultsArgs) => {
return this.logCommandResults(callback, ...args)
})
}

run(options, callback) {
run(options: CliOptions, callback: RunCallback) {
const opts = this.parseOptions(options.commandArgs)

const commands = []
Expand Down
16 changes: 7 additions & 9 deletions src/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import yargs from "yargs"
import Command from "./command"
import Command, { LogCommandResultsArgs } from "./command"
import type { CliOptions, RunCallback } from "./apm-cli"

export default class Clean extends Command {
constructor() {
super()
this.atomNpmPath = require.resolve("npm/bin/npm-cli")
}
private atomNpmPath = require.resolve("npm/bin/npm-cli")

parseOptions(argv) {
parseOptions(argv: string[]) {
const options = yargs(argv).wrap(Math.min(100, yargs.terminalWidth()))

options.usage(`\
Expand All @@ -25,10 +23,10 @@ as a dependency in the package.json file.\
return options.alias("h", "help").describe("help", "Print this usage message")
}

run(options, callback) {
run(options: CliOptions, callback: RunCallback) {
process.stdout.write("Removing extraneous modules ")
return this.fork(this.atomNpmPath, ["prune"], (...args) => {
return this.logCommandResults(callback, ...Array.from(args))
return this.fork(this.atomNpmPath, ["prune"], (...args: LogCommandResultsArgs) => {
return this.logCommandResults(callback, ...args)
})
}
}
24 changes: 17 additions & 7 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import semver from "semver"
import * as config from "./apm"
import * as git from "./git"

export type LogCommandResultsArgs = [code: number, stderr?: string, stdout?: string]

export default class Command {
protected electronVersion: string
installedAtomVersion: string
protected resourcePath: string
npm: typeof import("npm")
constructor() {
this.logCommandResults = this.logCommandResults.bind(this)
this.logCommandResultsIfFail = this.logCommandResultsIfFail.bind(this)
Expand Down Expand Up @@ -89,7 +95,7 @@ export default class Command {
}
}

logCommandResults(callback, code, stderr = "", stdout = "") {
logCommandResults(callback: (error?: string) => void, code: number, stderr = "", stdout = "") {
if (code === 0) {
this.logSuccess()
return callback()
Expand All @@ -99,7 +105,7 @@ export default class Command {
}
}

logCommandResultsIfFail(callback, code, stderr = "", stdout = "") {
logCommandResultsIfFail(callback: (error?: string) => void, code: number, stderr = "", stdout = "") {
if (code === 0) {
return callback()
} else {
Expand All @@ -108,7 +114,7 @@ export default class Command {
}
}

normalizeVersion(version) {
normalizeVersion(version: string) {
if (typeof version === "string") {
// Remove commit SHA suffix
return version.replace(/-.*$/, "")
Expand All @@ -119,11 +125,15 @@ export default class Command {

loadInstalledAtomMetadata(callback) {
return this.getResourcePath((resourcePath) => {
let electronVersion
let electronVersion: string | undefined
try {
let left, version
;({ version, electronVersion } = (left = require(path.join(resourcePath, "package.json"))) != null ? left : {})
version = this.normalizeVersion(version)
const resourcePathJson: { version: string; electronVersion: string } & Record<string, any> =
require(path.join(resourcePath, "package.json")) ?? {}

electronVersion = resourcePath.electronVersion

const version = this.normalizeVersion(resourcePathJson.version)

if (semver.valid(version)) {
this.installedAtomVersion = version
}
Expand Down
12 changes: 7 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import path from "path"
import yargs from "yargs"
import * as apm from "./apm"
import Command from "./command"
import type { CliOptions, RunCallback } from "./apm-cli"

export default class Config extends Command {
private atomDirectory = apm.getAtomDirectory()
private atomNpmPath = require.resolve("npm/bin/npm-cli")
private atomNodeDirectory: string
constructor() {
super()
const atomDirectory = apm.getAtomDirectory()
this.atomNodeDirectory = path.join(atomDirectory, ".node-gyp")
this.atomNpmPath = require.resolve("npm/bin/npm-cli")
this.atomNodeDirectory = path.join(this.atomDirectory, ".node-gyp")
}

parseOptions(argv) {
parseOptions(argv: string[]) {
const options = yargs(argv).wrap(Math.min(100, yargs.terminalWidth()))
options.usage(`\
Expand All @@ -30,7 +32,7 @@ Usage: apm config set <key> <value>
return options.alias("h", "help").describe("help", "Print this usage message")
}

run(options, callback) {
run(options: CliOptions, callback: RunCallback) {
options = this.parseOptions(options.commandArgs)

let configArgs = ["--globalconfig", apm.getGlobalConfigPath(), "--userconfig", apm.getUserConfigPath(), "config"]
Expand Down
17 changes: 10 additions & 7 deletions src/dedupe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import path from "path"
import async from "async"
import yargs from "yargs"
import * as config from "./apm"
import Command from "./command"
import Command, { LogCommandResultsArgs } from "./command"
import fs from "./fs"
import type { CliOptions, RunCallback } from "./apm-cli"

export default class Dedupe extends Command {
private atomDirectory = config.getAtomDirectory()
private atomNpmPath = require.resolve("npm/bin/npm-cli")
atomPackagesDirectory: string
private atomNodeDirectory: string
constructor() {
super()
this.atomDirectory = config.getAtomDirectory()
this.atomPackagesDirectory = path.join(this.atomDirectory, "packages")
this.atomNodeDirectory = path.join(this.atomDirectory, ".node-gyp")
this.atomNpmPath = require.resolve("npm/bin/npm-cli")
}

parseOptions(argv) {
parseOptions(argv: string[]) {
const options = yargs(argv).wrap(Math.min(100, yargs.terminalWidth()))
options.usage(`\
Expand All @@ -36,8 +39,8 @@ This command is experimental.\
dedupeModules(options, callback) {
process.stdout.write("Deduping modules ")

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

Expand Down Expand Up @@ -79,7 +82,7 @@ This command is experimental.\
return fs.makeTreeSync(this.atomNodeDirectory)
}

run(options, callback) {
run(options: CliOptions, callback: RunCallback) {
const { cwd } = options
options = this.parseOptions(options.commandArgs)
options.cwd = cwd
Expand Down
29 changes: 16 additions & 13 deletions src/deprecated-packages.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS104: Avoid inline assignments
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import semver from "semver"
let deprecatedPackages = null
let deprecatedPackages: DeprecatedPackages | undefined

export function isDeprecatedPackage(name, version) {
if (deprecatedPackages == null) {
let left
deprecatedPackages = (left = require("../deprecated-packages")) != null ? left : {}
type DeprecatedPackage = {
version?: string
hasDeprecations?: boolean
latestHasDeprecations?: boolean
message?: string
hasAlternative?: boolean
alternative?: string
}

type DeprecatedPackages = Record<string, DeprecatedPackage>

export function isDeprecatedPackage(name: string, version: string) {
if (deprecatedPackages === undefined) {
deprecatedPackages = require("../deprecated-packages") as DeprecatedPackages
}
if (!deprecatedPackages.hasOwnProperty(name)) {
if (!(name in deprecatedPackages)) {
return false
}

Expand Down
Loading

0 comments on commit 91c7a90

Please sign in to comment.