Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Remove save-specific CLI flags for ambient deps
Browse files Browse the repository at this point in the history
Use `--ambient` in combination with save flags to switch the `typings.json` write location.
  • Loading branch information
blakeembrey committed Dec 5, 2015
1 parent 9f6f0c5 commit 1a2c87a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 80 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ Install a dependency into the `typings` directory, and optionally write it into
#### Flags
* **--save, -S** Save as a dependency in `typings.json`
* **--save-ambient, -A** Save as an ambient dependency in `typings.json`
* **--save-dev, -D** Save as a dev dependency in `typings.json`
* **--save-ambient-dev** Save as an ambient dependency in `typings.json`
* **--ambient** Write as an ambient dependency (enabled when using `--save-ambient`)
* **--name** The name of the dependency (required for non-registry dependencies)
* **--ambient, -A** Write as an ambient dependency (use with `--save` and `--save-dev`)
* **--name, -n** The name of the dependency (required for non-registry dependencies)
#### Possible Locations
Expand All @@ -84,26 +82,24 @@ Typings installations without a location will be looked up in the [registry](htt
Install the `node` typings from DefinitelyTyped:

```
typings install github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#4ad9bef6cc075c904e034e73e1c993b9ad1ba81b --save-ambient --name node
typings install github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#4ad9bef6cc075c904e034e73e1c993b9ad1ba81b --save --ambient --name node
```

The name is used in `typings.json` and for external module definitions, and it is always required (but inferred when using the registry). We used `--save-ambient` to write it into `typings.json`, which you can then use to re-install your typings later. We use the commit hash for code immutability, there's nothing worse than someone coming into the project and the compiler explodes because the type definition on `master` has changed.
The name is used in `typings.json` and for external module definitions, and it is always required (but inferred when using the registry). The `--save` flag is used to write to `typings.json`, which you can use to re-install your typings later. The `--ambient` flag is used to specify that we know that this dependency is ambient. We use the commit hash in the location for code immutability, since there's nothing worse than someone coming into the project and the compiler explodes because the type definition on `master` has changed.
### Uninstall
```sh
typings uninstall <pkg> [--ambient] [--save|--save-dev|--save-ambient]
typings uninstall <pkg> [--ambient] [--save|--save-dev]
```
Remove a dependency from the `typings/` directory, and optionally remove from `typings.json`.
#### Flags
* **--save, -S** Remove from dependencies in `typings.json`
* **--save-ambient, -A** Remove from ambient dependencies in `typings.json`
* **--save-dev, -D** Remove from dev dependencies in `typings.json`
* **--save-ambient-dev** Remove from ambient deb dependencies in `typings.json`
* **--ambient** Remove as an ambient dependency (enabled when using `--save-ambient`)
* **--ambient, -A** Remove as an ambient dependency (use with `--save` and `--save-dev`)
### List
Expand Down
10 changes: 3 additions & 7 deletions src/bin/typings-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import { archifyDependencyTree } from '../utils/cli'

interface Args {
save: boolean
saveAmbient: boolean
saveDev: boolean
saveAmbientDev: boolean
ambient: boolean
production: boolean
name?: string
Expand All @@ -22,15 +20,13 @@ interface Args {
}

const args = minimist<Args>(process.argv.slice(2), {
boolean: ['save', 'saveAmbient', 'saveDev', 'saveAmbientDev', 'ambient', 'verbose', 'help', 'production'],
boolean: ['save', 'saveDev', 'ambient', 'verbose', 'help', 'production'],
string: ['name', 'source'],
alias: {
save: ['S'],
saveAmbient: ['A', 'save-ambient'],
saveDev: ['save-dev', 'D'],
saveAmbientDev: ['save-ambient-dev'],
name: ['n'],
ambient: ['a'],
ambient: ['A'],
verbose: ['v'],
help: ['h'],
source: ['s']
Expand All @@ -47,7 +43,7 @@ ${PROJECT_NAME} install bitbucket:<bitbucket username>/<bitbucket project>[/<pat
${PROJECT_NAME} install <http:// url>
Aliases: i, in
Options: [--save|--save-dev|--save-ambient|--save-ambient-dev] [--ambient] [--production]
Options: [--save|--save-dev] [--ambient] [--production]
`)

process.exit(0)
Expand Down
10 changes: 3 additions & 7 deletions src/bin/typings-uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,27 @@ import { PROJECT_NAME } from '../utils/config'

interface Args {
save: boolean
saveAmbient: boolean
saveDev: boolean
saveAmbientDev: boolean
ambient: boolean
verbose: boolean
help: boolean
}

const args = minimist<Args>(process.argv.slice(2), {
boolean: ['save', 'saveAmbient', 'saveDev', 'saveAmbientDev', 'ambient', 'verbose', 'help'],
boolean: ['save', 'saveDev', 'ambient', 'verbose', 'help'],
alias: {
save: ['S'],
saveAmbient: ['A', 'save-ambient'],
saveDev: ['D', 'save-dev'],
saveAmbientDev: ['save-ambient-dev'],
name: ['n'],
ambient: ['a'],
ambient: ['A'],
verbose: ['v'],
help: ['h']
}
})

if (args.help) {
console.log(`
${PROJECT_NAME} uninstall <pkg> [--ambient] [--save|--save-dev|--save-ambient|--save-ambient-dev]
${PROJECT_NAME} uninstall <pkg> [--save|--save-dev] [--ambient]
Aliases: r, rm, remove, un
`)
Expand Down
3 changes: 1 addition & 2 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export interface Options {
*/
const DEFAULT_CONFIG: ConfigJson = {
dependencies: {},
devDependencies: {},
ambientDependencies: {}
devDependencies: {}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/install.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ test('install', t => {
}),
installDependency(AMBIENT_DEPENDENCY, {
cwd: FIXTURE_DIR,
saveAmbientDev: true,
saveDev: true,
ambient: true,
name: 'ambient-test'
})
])
Expand Down
26 changes: 12 additions & 14 deletions src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import compile, { Options as CompileOptions } from './lib/compile'
import { findProject } from './utils/find'
import { writeDependency, transformConfig } from './utils/fs'
import { parseDependency } from './utils/parse'
import { isAmbientInstall } from './utils/options'
import { DependencyTree, Dependency, DependencyBranch } from './interfaces/main'

/**
* Options for installing a new dependency.
*/
export interface InstallDependencyOptions {
save?: boolean
saveAmbient?: boolean
saveDev?: boolean
saveAmbientDev?: boolean
ambient?: boolean
name?: string
cwd: string
Expand Down Expand Up @@ -87,13 +84,10 @@ function installTo (location: string, options: InstallDependencyOptions): Promis
return Promise.reject(new TypeError(`Unable to resolve "${location}"`))
}

// Use the ambient option, but override when saving as ambient.
const ambient = isAmbientInstall(options)

return installDependencyTree(tree, {
cwd: options.cwd,
name: options.name,
ambient,
ambient: options.ambient,
meta: true
})
.then(() => writeToConfig(dependency, options))
Expand All @@ -113,19 +107,23 @@ function installDependencyTree (tree: DependencyTree, options: CompileOptions) {
* Write a dependency to the configuration file.
*/
function writeToConfig (dependency: Dependency, options: InstallDependencyOptions) {
if (options.save || options.saveDev || options.saveAmbient || options.saveAmbientDev) {
if (options.save || options.saveDev) {
const { raw } = dependency

return transformConfig(options.cwd, config => {
// Extend different fields depending on the option passed in.
if (options.save) {
config.dependencies = extend(config.dependencies, { [options.name]: raw })
if (options.ambient) {
config.ambientDependencies = extend(config.ambientDependencies, { [options.name]: raw })
} else {
config.dependencies = extend(config.dependencies, { [options.name]: raw })
}
} else if (options.saveDev) {
config.devDependencies = extend(config.devDependencies, { [options.name]: raw })
} else if (options.saveAmbient) {
config.ambientDependencies = extend(config.ambientDependencies, { [options.name]: raw })
} else if (options.saveAmbientDev) {
config.ambientDevDependencies = extend(config.ambientDevDependencies, { [options.name]: raw })
if (options.ambient) {
config.ambientDevDependencies = extend(config.ambientDevDependencies, { [options.name]: raw })
} else {
config.devDependencies = extend(config.devDependencies, { [options.name]: raw })
}
}

return config
Expand Down
45 changes: 22 additions & 23 deletions src/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import extend = require('xtend')
import invariant = require('invariant')
import { removeDependency, transformConfig, DefinitionOptions } from './utils/fs'
import { findProject } from './utils/find'
import { isAmbientInstall } from './utils/options'

/**
* Uninstall options.
*/
export interface UninstallDependencyOptions {
save?: boolean
saveAmbient?: boolean
saveDev?: boolean
saveAmbientDev?: boolean
ambient?: boolean
cwd: string
}
Expand All @@ -20,7 +17,7 @@ export interface UninstallDependencyOptions {
* Uninstall a dependency, given a name.
*/
export function uninstallDependency (name: string, options: UninstallDependencyOptions) {
const ambient = isAmbientInstall(options)
const { ambient } = options

// Remove the dependency from fs and config.
function uninstall (options: DefinitionOptions) {
Expand All @@ -38,28 +35,30 @@ export function uninstallDependency (name: string, options: UninstallDependencyO
* Delete the dependency from the configuration file.
*/
function writeToConfig (name: string, options: UninstallDependencyOptions) {
if (options.save || options.saveDev || options.saveAmbient || options.saveAmbientDev) {
invariant(
(options.save || options.saveDev) &&
(options.ambient || options.saveAmbient || options.saveAmbientDev),
'--save and --save-dev are incompatible with ambient dependencies'
)

if (options.save || options.saveDev) {
return transformConfig(options.cwd, config => {
if (options.save && config.dependencies) {
delete config.dependencies[name]
}

if (options.saveDev && config.devDependencies) {
delete config.devDependencies[name]
}

if (options.saveAmbient && config.ambientDependencies) {
delete config.ambientDependencies[name]
if (options.save) {
if (options.ambient) {
if (config.ambientDependencies) {
delete config.ambientDependencies[name]
}
} else {
if (config.dependencies) {
delete config.dependencies[name]
}
}
}

if (options.saveAmbientDev && config.ambientDevDependencies) {
delete config.ambientDevDependencies[name]
if (options.saveDev) {
if (options.ambient) {
if (config.ambientDevDependencies) {
delete config.ambientDevDependencies[name]
}
} else {
if (config.devDependencies) {
delete config.devDependencies[name]
}
}
}

return config
Expand Down
4 changes: 4 additions & 0 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ export function transformConfig (cwd: string, transform: (config: ConfigJson) =>
config.ambientDependencies = sortKeys(config.ambientDependencies)
}

if (config.ambientDevDependencies) {
config.ambientDevDependencies = sortKeys(config.ambientDevDependencies)
}

return config
})
})
Expand Down
16 changes: 0 additions & 16 deletions src/utils/options.ts

This file was deleted.

0 comments on commit 1a2c87a

Please sign in to comment.