"use strict";

const log = require("npmlog");

const ChildProcessUtilities = require("../ChildProcessUtilities");
const getExecOpts = require("./get-npm-exec-opts");

exports.add = add;
exports.check = check;
exports.remove = remove;

function add(pkg, version, tag, registry) {
  log.silly("npmDistTag.add", tag, version, pkg.name);

  return ChildProcessUtilities.exec(
    "npm",
    ["dist-tag", "add", `${pkg.name}@${version}`, tag],
    getExecOpts(pkg.location, registry)
  );
}

function check(pkg, tag, registry) {
  log.silly("npmDistTag.check", tag, pkg.name);

  const result = ChildProcessUtilities.execSync(
    "npm",
    ["dist-tag", "ls", pkg.name],
    getExecOpts(pkg.location, registry)
  );

  return result.indexOf(tag) >= 0;
}

function remove(pkg, tag, registry) {
  log.silly("npmDistTag.remove", tag, pkg.name);

  return ChildProcessUtilities.exec(
    "npm",
    ["dist-tag", "rm", pkg.name, tag],
    getExecOpts(pkg.location, registry)
  );
}


expect(err.message).toBe("Can't diff. There are no commits in this repository, yet.");

keep keep keep keep keep replace keep keep keep

expect(err.message).toBe("Cannot diff, there are no commits in this repository yet.");

  A tool for managing JavaScript projects with multiple packages.

* [About](#about)
* [Getting Started](#getting-started)
* [How It Works](#how-it-works)
* [Troubleshooting](#troubleshooting)
* [Commands](#commands)
* [Misc](#misc)
* [Lerna.json](#lernajson)
* [Flags](#flags)

## About

Splitting up large codebases into separate independently versioned packages
is extremely useful for code sharing. However, making changes across many
repositories is _messy_ and difficult to track, and testing across repositories
gets complicated really fast.

To solve these (and many other) problems, some projects will organize their
codebases into multi-package repositories (sometimes called [monorepos](https://github.com/babel/babel/blob/master/doc/design/monorepo.md)). Projects like [Babel](https://github.com/babel/babel/tree/master/packages), [React](https://github.com/facebook/react/tree/master/packages), [Angular](https://github.com/angular/angular/tree/master/modules),
[Ember](https://github.com/emberjs/ember.js/tree/master/packages), [Meteor](https://github.com/meteor/meteor/tree/devel/packages), [Jest](https://github.com/facebook/jest/tree/master/packages), and many others develop all of their packages within a
single repository.

**Lerna is a tool that optimizes the workflow around managing multi-package
repositories with git and npm.**

Lerna can also reduce the time and space requirements for numerous
copies of packages in development and build environments - normally a
downside of dividing a project into many separate NPM package. See the
[hoist documentation](doc/hoist.md) for details.

### What does a Lerna repo look like?

There's actually very little to it. You have a file system that looks like this:

```
my-lerna-repo/
  package.json
  packages/
    package-1/
      package.json
    package-2/
      package.json
```

### What can Lerna do?

The two primary commands in Lerna are `lerna bootstrap` and `lerna publish`.

`bootstrap` will link dependencies in the repo together.
`publish` will help publish any updated packages.

## Getting Started

> The instructions below are for Lerna 2.x.
> We recommend using it instead of 1.x for a new Lerna project. Check the [wiki](https://github.com/lerna/lerna/wiki/1.x-Docs) if you need to see the 1.x README.

Let's start by installing Lerna globally with [npm](https://www.npmjs.com/).

```sh
$ npm install --global lerna
```

Next we'll create a new folder:

```sh
$ mkdir lerna-repo
$ cd lerna-repo
```

And now let's turn it into a Lerna repo:

```sh
$ lerna init
```

This will create a `lerna.json` configuration file as well as a `packages` folder, so your folder should now look like this:

```
lerna-repo/
  packages/
  package.json
  lerna.json
```

## How It Works

Lerna allows you to manage your project using one of two modes: Fixed or Independent.

### Fixed/Locked mode (default)

Fixed mode Lerna projects operate on a single version line. The version is kept in the `lerna.json` file at the root of your project under the `version` key. When you run `lerna publish`, if a module has been updated since the last time a release was made, it will be updated to the new version you're releasing. This means that you only publish a new version of a package when you need to.

This is the mode that [Babel](https://github.com/babel/babel) is currently using. Use this if you want to automatically tie all package versions together. One issue with this approach is that a major change in any package will result in all packages having a new major version.

### Independent mode (`--independent`)

Independent mode Lerna projects allows maintainers to increment package versions independently of each other. Each time you publish, you will get a prompt for each package that has changed to specify if it's a patch, minor, major or custom change.

Independent mode allows you to more specifically update versions for each package and makes sense for a group of components. Combining this mode with something like [semantic-release](https://github.com/semantic-release/semantic-release) would make it less painful. (There is work on this already at [atlassian/lerna-semantic-release](https://github.com/atlassian/lerna-semantic-release)).

> The `version` key in `lerna.json` is ignored in independent mode.

## Troubleshooting

If you encounter any issues while using Lerna please check out our [Troubleshooting](doc/troubleshooting.md)
document where you might find the answer to your problem.

## Frequently asked questions

See [FAQ.md](FAQ.md).

## Commands

### init

```sh
$ lerna init
```

Create a new Lerna repo or upgrade an existing repo to the current version of Lerna.

> Lerna assumes the repo has already been initialized with `git init`.

When run, this command will:

1. Add `lerna` as a [`devDependency`](https://docs.npmjs.com/files/package.json#devdependencies) in `package.json` if it doesn't already exist.
2. Create a `lerna.json` config file to store the `version` number.

Example output on a new git repo:

```sh
$ lerna init
lerna info version v2.0.0
lerna info Updating package.json
lerna info Creating lerna.json
lerna success Initialized Lerna files
```

#### --independent, -i

```sh
$ lerna init --independent
```

This flag tells Lerna to use independent versioning mode.

#### --exact

```sh
$ lerna init --exact
```

By default, `lerna init` will use a caret range when adding or updating
the local version of `lerna`, just like `npm install --save-dev lerna`.

To retain the `lerna` 1.x behavior of "exact" comparison, pass this flag.
It will configure `lerna.json` to enforce exact match for all subsequent executions.

```json
{
  "commands": {
    "init": {
      "exact": true
    }
  },
  "version": "0.0.0"
}
```

### bootstrap

```sh
$ lerna bootstrap
```

Bootstrap the packages in the current Lerna repo.
Installs all of their dependencies and links any cross-dependencies.

When run, this command will:

1. `npm install` all external dependencies of each package.
2. Symlink together all Lerna `packages` that are dependencies of each other.
3. `npm run prepublish` in all bootstrapped packages.
4. `npm run prepare` in all bootstrapped packages.

`lerna bootstrap` respects the `--ignore`, `--ignore-scripts`, `--scope` and `--include-filtered-dependencies` flags (see [Flags](#flags)).

Pass extra arguments to npm client by placing them after `--`:

```sh
$ lerna bootstrap -- --production --no-optional
```

May also be configured in `lerna.json`:

```js
{
  ...
  "npmClient": "yarn",
  "npmClientArgs": ["--production", "--no-optional"]
}
```

#### How `bootstrap` works

Let's use `babel` as an example.

* `babel-generator` and `source-map` (among others) are dependencies of `babel-core`.
* `babel-core`'s [`package.json`](https://github.com/babel/babel/blob/13c961d29d76ccd38b1fc61333a874072e9a8d6a/packages/babel-core/package.json#L28-L47) lists both these packages as keys in `dependencies`, as shown below.

### add

```sh
$ lerna add <package>[@version] [--dev]
```

Add local or remote `package` as dependency to packages in the current Lerna repo.

When run, this command will:

1. Add `package` to each applicable package. Applicable are packages that are not `package` and are in scope
2. Bootstrap packages with changes to their manifest file (`package.json`)

`lerna add` respects the `--ignore`, `--scope` and `--include-filtered-dependencies` flags (see [Flags](#flags)).

#### Examples

```
lerna add module-1 --scope=module-2 # Install module-1 to module-2
lerna add module-1 --scope=module-2 --dev # Install module-1 to module-2 in devDependencies
lerna add module-1 # Install module-1 in all modules except module-1
lerna add babel-core # Install babel-core in all modules
```

```js
// babel-core package.json
{
  "name": "babel-core",
  ...
  "dependencies": {
    ...
    "babel-generator": "^6.9.0",
    ...
    "source-map": "^0.5.0"
  }
}
```

* Lerna checks if each dependency is also part of the Lerna repo.
  * In this example, `babel-generator` can be an internal dependency, while `source-map` is always an external dependency.
  * The version of `babel-generator` in the `package.json` of `babel-core` is satisfied by `packages/babel-generator`, passing for an internal dependency.
  * `source-map` is `npm install`ed (or `yarn`ed) like normal.
* `packages/babel-core/node_modules/babel-generator` symlinks to `packages/babel-generator`
* This allows nested directory imports

**Notes:**

* When a dependency version in a package is not satisfied by a package of the same name in the repo, it will be `npm install`ed (or `yarn`ed) like normal.
* Dist-tags, like `latest`, do not satisfy [semver](https://semver.npmjs.com/) ranges.
* Circular dependencies result in circular symlinks which _may_ impact your editor/IDE.

[Webstorm](https://www.jetbrains.com/webstorm/) locks up when circular symlinks are present. To prevent this, add `node_modules` to the list of ignored files and folders in `Preferences | Editor | File Types | Ignored files and folders`.

### publish

```sh
$ lerna publish
```

Publish packages in the current Lerna project. When run, this command does the following:

Creates a new release of the packages that have been updated.
Prompts for a new version.
Creates a new git commit/tag in the process of publishing to npm.

More specifically, this command will:

1. Run the equivalent of `lerna updated` to determine which packages need to be published.
2. If necessary, increment the `version` key in `lerna.json`.
3. Update the `package.json` of all updated packages to their new versions.
4. Update all dependencies of the updated packages with the new versions, specified with a [caret (^)](https://docs.npmjs.com/files/package.json#dependencies).
5. Create a new git commit and tag for the new version.
6. Publish updated packages to npm.

> Lerna won't publish packages which are marked as private (`"private": true` in the `package.json`).

**Note:** to publish scoped packages, you need to add the following to each `package.json`:

```js
"publishConfig": {
  "access": "public"
}
```

#### --exact

```sh
$ lerna publish --exact
```

When run with this flag, `publish` will specify updated dependencies in updated packages exactly (with no punctuation), instead of as semver compatible (with a `^`).

For more information, see the package.json [dependencies](https://docs.npmjs.com/files/package.json#dependencies) documentation.

#### --npm-tag [tagname]

```sh
$ lerna publish --npm-tag=next
```

When run with this flag, `publish` will publish to npm with the given npm [dist-tag](https://docs.npmjs.com/cli/dist-tag) (defaults to `latest`).

This option can be used to publish a [`prerelease`](http://carrot.is/coding/npm_prerelease) or `beta` version.

> Note: the `latest` tag is the one that is used when a user runs `npm install my-package`.
> To install a different tag, a user can run `npm install my-package@prerelease`.

#### --canary, -c

```sh
$ lerna publish --canary
$ lerna publish --canary=beta
```

When run with this flag, `publish` publishes packages in a more granular way (per commit). Before publishing to npm, it creates the new `version` tag by taking the current `version`, bumping it to the next _minor_ version, adding the provided meta suffix (defaults to `alpha`) and appending the current git sha (ex: `1.0.0` becomes `1.1.0-alpha.81e3b443`).

> The intended use case for this flag is a per commit level release or nightly release.

#### --conventional-commits

```sh
$ lerna publish --conventional-commits
```

When run with this flag, `publish` will use the [Conventional Commits Specification](https://conventionalcommits.org/) to [determine the version bump](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-recommended-bump) and [generate CHANGELOG](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-cli)

#### --changelog-preset

```sh
$ lerna publish --conventional-commits --changelog-preset angular-bitbucket
```

By default, the changelog preset is set to [`angular`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-angular#angular-convention).
In some cases you might want to change either use a another preset or a custom one.

Presets are names of built-in or installable configuration for conventional changelog.
Presets may be passed as the full name of the package, or the auto-expanded suffix
(e.g., `angular` is expanded to `conventional-changelog-angular`).

#### --git-remote [remote]

```sh
$ lerna publish --git-remote upstream
```

When run with this flag, `publish` will push the git changes to the specified remote instead of `origin`.

#### --skip-git

```sh
$ lerna publish --skip-git
```

When run with this flag, `publish` will publish to npm without running any of the git commands.

> Only publish to npm; skip committing, tagging, and pushing git changes (this only affects publish).

#### --skip-npm

```sh
$ lerna publish --skip-npm
```

When run with this flag, `publish` will update all `package.json` package
versions and dependency versions, but it will not actually publish the
packages to npm.

This flag can be combined with `--skip-git` to _just_ update versions and
dependencies, without committing, tagging, pushing or publishing.

> Only update versions and dependencies; don't actually publish (this only affects publish).

#### --force-publish [packages]

```sh
$ lerna publish --force-publish=package-2,package-4
# force publish all packages
$ lerna publish --force-publish=*
```

When run with this flag, `publish` will force publish the specified packages (comma-separated) or all packages using `*`.

> This will skip the `lerna updated` check for changed packages and forces a package that didn't have a `git diff` change to be updated.

#### --yes

```sh
$ lerna publish --canary --yes
# skips `Are you sure you want to publish the above changes?`
```

When run with this flag, `publish` will skip all confirmation prompts.
Useful in [Continuous integration (CI)](https://en.wikipedia.org/wiki/Continuous_integration) to automatically answer the publish confirmation prompt.

#### --cd-version

```sh
$ lerna publish --cd-version (major | minor | patch | premajor | preminor | prepatch | prerelease)
# uses the next semantic version(s) value and this skips `Select a new version for...` prompt
```

When run with this flag, `publish` will skip the version selection prompt (in independent mode) and use the next specified semantic version.
You must still use the `--yes` flag to avoid all prompts. This is useful when build systems need to publish without command prompts. Works in both normal and independent modes.

If you have any packages with a prerelease version number (e.g. `2.0.0-beta.3`) and you run `lerna publish` with `--cd-version` and a non-prerelease version increment (major / minor / patch), it will publish those packages in addition to the packages that have changed since the last release.

#### --preid

```sh
$ lerna publish --cd-version=prerelease
# uses the next semantic prerelease version, e.g.
# 1.0.0 => 1.0.0-0

$ lerna publish --cd-version=prepatch --preid=next
# uses the next semantic prerelease version with a specific prerelease identifier, e.g.
# 1.0.0 => 1.0.1-next.0
```

When run with this flag, `lerna publish --cd-version` will
increment `premajor`, `preminor`, `prepatch`, or `prerelease`
versions using the specified [prerelease identifier](http://semver.org/#spec-item-9).

#### --repo-version

```sh
$ lerna publish --repo-version 1.0.1
# applies version and skips `Select a new version for...` prompt
```

When run with this flag, `publish` will skip the version selection prompt and use the specified version.
Useful for bypassing the user input prompt if you already know which version to publish.

#### --message, -m [msg]

```sh
$ lerna publish -m "chore(release): publish %s"
# commit message = "chore(release): publish v1.0.0"

$ lerna publish -m "chore(release): publish %v"
# commit message = "chore(release): publish 1.0.0"

$ lerna publish -m "chore(release): publish" --independent
# commit message = "chore(release): publish
#
# - package-1@3.0.1
# - package-2@1.5.4"
```

When run with this flag, `publish` will use the provided message when committing the version updates
for publication. Useful for integrating lerna into projects that expect commit messages to adhere
to certain guidelines, such as projects which use [commitizen](https://github.com/commitizen/cz-cli) and/or [semantic-release](https://github.com/semantic-release/semantic-release).

If the message contains `%s`, it will be replaced with the new global version version number prefixed with a "v".
If the message contains `%v`, it will be replaced with the new global version version number without the leading "v".
Note that this only applies when using the default "fixed" versioning mode, as there is no "global" version when using `--independent`.

This can be configured in lerna.json, as well:

```json
{
  "commands": {
    "publish": {
      "message": "chore(release): publish %s"
    }
  }
}
```

#### --allow-branch [glob]

Lerna allows you to specify a glob or an array of globs in your `lerna.json` that your current branch needs to match to be publishable.
You can use this flag to override this setting.
If your `lerna.json` contains something like this:

```json
{
  "commands": {
    "publish": {
      "allowBranch": "master"
    }
  }
}
```

```json
{
  "command": {
    "publish": {
      "allowBranch": ["master", "feature/*"]
    }
  }
}
```

and you are not on the branch `master` lerna will prevent you from publishing. To force a publish despite this config, pass the `--allow-branch` flag:

```sh
$ lerna publish --allow-branch my-new-feature
```

### updated

```sh
$ lerna updated
```

Check which `packages` have changed since the last release (the last git tag).

Lerna determines the last git tag created and runs `git diff --name-only v6.8.1` to get all files changed since that tag. It then returns an array of packages that have an updated file.

**Note that configuration for the `publish` command _also_ affects the
`updated` command. For example `config.publish.ignore`**

#### --json

```sh
$ lerna updated --json
```

When run with this flag, `updated` will return an array of objects in the following format:

```json
[
  {
    "name": "package",
    "version": "1.0.0",
    "private": false
  }
]
```

### clean

```sh
$ lerna clean
```

Remove the `node_modules` directory from all packages.

`lerna clean` respects the `--ignore`, `--scope`, and `--yes` flags (see [Flags](#flags)).

### diff

```sh
$ lerna diff [package?]

$ lerna diff
# diff a specific package
$ lerna diff package-name
```

Diff all packages or a single package since the last release.

> Similar to `lerna updated`. This command runs `git diff`.

### ls

```sh
$ lerna ls
```

List all of the public packages in the current Lerna repo.

`lerna ls` respects the `--ignore` and `--scope` flags (see [Flags](#flags)).

#### --json

```sh
$ lerna ls --json
```

When run with this flag, `ls` will return an array of objects in the following format:

```json
[
  {
    "name": "package",
    "version": "1.0.0",
    "private": false
  }
]
```

### run

```sh
$ lerna run <script> -- [..args] # runs npm run my-script in all packages that have it
$ lerna run test
$ lerna run build

# watch all packages and transpile on change, streaming prefixed output
$ lerna run --parallel watch
```

Run an [npm script](https://docs.npmjs.com/misc/scripts) in each package that contains that script. A double-dash (`--`) is necessary to pass dashed arguments to the script execution.

`lerna run` respects the `--concurrency`, `--scope`, `--ignore`, `--stream`, and `--parallel` flags (see [Flags](#flags)).

```sh
$ lerna run --scope my-component test
```

> Note: It is advised to constrain the scope of this command (and `lerna exec`,
> below) when using the `--parallel` flag, as spawning dozens of subprocesses
> may be harmful to your shell's equanimity (or maximum file descriptor limit,
> for example). YMMV

### exec

```sh
$ lerna exec -- <command> [..args] # runs the command in all packages
$ lerna exec -- rm -rf ./node_modules
$ lerna exec -- protractor conf.js
```

Run an arbitrary command in each package.
A double-dash (`--`) is necessary to pass dashed flags to the spawned command, but is not necessary when all the arguments are positional.

`lerna exec` respects the `--concurrency`, `--scope`, `--ignore`, `--stream` and `--parallel` flags (see [Flags](#flags)).

```sh
$ lerna exec --scope my-component -- ls -la
```

To spawn long-running processes, pass the `--parallel` flag:

```sh
# transpile all modules as they change in every package
$ lerna exec --parallel -- babel src -d lib -w
```

You may also get the name of the current package through the environment variable `LERNA_PACKAGE_NAME`:

```sh
$ lerna exec -- npm view \$LERNA_PACKAGE_NAME
```

You may also run a script located in the root dir, in a complicated dir structure through the environment variable `LERNA_ROOT_PATH`:

```sh
$ lerna exec -- node \$LERNA_ROOT_PATH/scripts/some-script.js
```

> Hint: The commands are spawned in parallel, using the concurrency given (except with `--parallel`).
> The output is piped through, so not deterministic.
> If you want to run the command in one package after another, use it like this:

```sh
$ lerna exec --concurrency 1 -- ls -la
```

#### --bail

```sh
$ lerna exec --bail=<boolean> <command>
```

This flag signifies whether or not the `exec` command should halt execution upon encountering an error thrown by one of the spawned subprocesses. Its default value is `true`.

### import

```sh
$ lerna import <path-to-external-repository>
```

Import the package at `<path-to-external-repository>`, with commit history,
into `packages/<directory-name>`. Original commit authors, dates and messages
are preserved. Commits are applied to the current branch.

This is useful for gathering pre-existing standalone packages into a Lerna
repo. Each commit is modified to make changes relative to the package
directory. So, for example, the commit that added `package.json` will
instead add `packages/<directory-name>/package.json`.

### link

```sh
$ lerna link
```

Symlink together all Lerna `packages` that are dependencies of each other in the current Lerna repo.

#### --force-local

```sh
$ lerna link --force-local
```

When passed, this flag causes the `link` command to always symlink local dependencies regardless of matching version range.

## Misc

Lerna will log to a `lerna-debug.log` file (same as `npm-debug.log`) when it encounters an error running a command.

Lerna also has support for [scoped packages](https://docs.npmjs.com/misc/scope).

Running `lerna` without arguments will show all commands/options.

### lerna.json

```js
{
  "version": "1.1.3",
  "commands": {
    "publish": {
      "ignore": [
        "ignored-file",
        "*.md"
      ]
    },
    "bootstrap": {
      "ignore": "component-*"
    }
  },
  "packages": ["packages/*"]
}
```

* `lerna`: the current version of Lerna being used.
* `version`: the current version of the repository.
* `commands.publish.ignore`: an array of globs that won't be included in `lerna updated/publish`. Use this to prevent publishing a new version unnecessarily for changes, such as fixing a `README.md` typo.
* `commands.bootstrap.ignore`: an array of globs that won't be bootstrapped when running the `lerna bootstrap` command.
* `commands.bootstrap.scope`: an array of globs that restricts which packages will be bootstrapped when running the `lerna bootstrap` command.
* `packages`: Array of globs to use as package locations.

### Common `devDependencies`

Most `devDependencies` can be pulled up to the root of a Lerna repo.

This has a few benefits:

* All packages use the same version of a given dependency
* Can keep dependencies at the root up-to-date with an automated tool such as [GreenKeeper](https://greenkeeper.io/)
* Dependency installation time is reduced
* Less storage is needed

Note that `devDependencies` providing "binary" executables that are used by
npm scripts still need to be installed directly in each package where they're
used.

For example the `nsp` dependency is necessary in this case for `lerna run nsp`
(and `npm run nsp` within the package's directory) to work correctly:

```json
{
  "scripts": {
    "nsp": "nsp"
  },
  "devDependencies": {
    "nsp": "^2.3.3"
  }
}
```

### Flags

Options to Lerna can come from configuration (`lerna.json`) or on the command
line. Additionally options in config can live at the top level or may be
applied to specific commands.

Example:

```json
{
  "version": "1.2.0",
  "exampleOption": "foo",
  "commands": {
    "init": {
      "exampleOption": "bar"
    }
  }
}
```

In this case `exampleOption` will be "foo" for all commands except `init`,
where it will be "bar". In all cases it may be overridden to "baz" on the
command-line with `--example-option=baz`.

#### --concurrency

How many threads to use when Lerna parallelizes the tasks (defaults to `4`)

```sh
$ lerna publish --concurrency 1
```

#### --scope [glob]

Scopes a command to a subset of packages.

```sh
$ lerna exec --scope my-component -- ls -la
```

```sh
$ lerna run --scope toolbar-* test
```

#### --since [ref]

When executing a script or command, scope the operation to packages that have been updated since the specified `ref`. If `ref` is not specified, it defaults to the latest tag.

List the contents of packages that have changed since the latest tag:

```sh
$ lerna exec --since -- ls -la
```

Run the tests for all packages that have changed since `master`:

```
$ lerna run test --since master
```

List all packages that have changed since `some-branch`:

```
$ lerna ls --since some-branch
```

_This can be particularly useful when used in CI, if you can obtain the target branch a PR will be going into, because you can use that as the `ref` to the `--since` option. This works well for PRs going into master as well as feature branches._

#### --flatten

When importing repositories with merge commits with conflicts, the import command will fail trying to apply all commits. The user can use this flag to ask for import of "flat" history, i.e. with each merge commit as a single change the merge introduced.

```
$ lerna import ~/Product --flatten
```

#### --ignore [glob]

Excludes a subset of packages when running a command.

```sh
$ lerna bootstrap --ignore component-*
```

The `ignore` flag, when used with the `bootstrap` command, can also be set in `lerna.json` under the `commands.bootstrap` key. The command-line flag will take precedence over this option.

**Example**

```javascript
{
  "version": "0.0.0",
  "commands": {
    "bootstrap": {
      "ignore": "component-*"
    }
  }
}
```

> Hint: The glob is matched against the package name defined in `package.json`,
> not the directory name the package lives in.

#### --ignore-scripts

When used with the `bootstrap` command it won't run any lifecycle scripts in bootstrapped packages.

```sh
$ lerna bootstrap --ignore-scripts
```

#### --include-filtered-dependencies

Used in combination with any command that accepts `--scope` (`bootstrap`, `clean`, `ls`, `run`, `exec`). Ensures that all dependencies (and dev dependencies) of any scoped packages (either through `--scope` or `--ignore`) are operated on as well.

> Note: This will override the `--scope` and `--ignore` flags.
>
> > i.e. A package matched by the `--ignore` flag will still be bootstrapped if it is depended on by another package that is being bootstrapped.

This is useful for situations where you want to "set up" a single package that relies on other packages being set up.

```sh
$ lerna bootstrap --scope my-component --include-filtered-dependencies
# my-component and all of its dependencies will be bootstrapped
```

```sh
$ lerna bootstrap --scope "package-*" --ignore "package-util-*" --include-filtered-dependencies
# all package-util's will be ignored unless they are depended upon by a
# package matched by "package-*"
```

#### --loglevel [silent|error|warn|success|info|verbose|silly]

What level of logs to report. On failure, all logs are written to lerna-debug.log in the current working directory.

Any logs of a higher level than the setting are shown. The default is "info".

#### --max-buffer [in-bytes]

Set a max buffer length for each underlying process call. Useful for example
when someone wants to import a repo with a larger amount of commits while
running `lerna import`. In that case the built-in buffer length might not
be sufficient.

#### --no-sort

By default, all tasks execute on packages in topologically sorted order as to respect the dependency relationships of the packages in question. Cycles are broken on a best-effort basis in a way not guaranteed to be consistent across Lerna invocations.

Topological sorting can cause concurrency bottlenecks if there are a small number of packages with many dependents or if some packages take a disproportionately long time to execute. The `--no-sort` option disables sorting, instead executing tasks in an arbitrary order with maximum concurrency.

This option can also help if you run multiple "watch" commands. Since `lerna run` will execute commands in topologically sorted order, it can end up waiting for a command before moving on. This will block execution when you run "watch" commands, since they typically never end. An example of a "watch" command is [running `babel` with the `--watch` CLI flag](https://babeljs.io/docs/usage/cli/#babel-compile-files).

#### --hoist [glob]

Install external dependencies matching `glob` at the repo root so they're
available to all packages. Any binaries from these dependencies will be
linked into dependent package `node_modules/.bin/` directories so they're
available for npm scripts. If the option is present but no `glob` is given
the default is `**` (hoist everything). This option only affects the
`bootstrap` command.

```sh
$ lerna bootstrap --hoist
```

For background on `--hoist`, see the [hoist documentation](doc/hoist.md).

Note: If packages depend on different _versions_ of an external dependency,
the most commonly used version will be hoisted, and a warning will be emitted.

#### --nohoist [glob]

Do _not_ install external dependencies matching `glob` at the repo root. This
can be used to opt out of hoisting for certain dependencies.

```sh
$ lerna bootstrap --hoist --nohoist=babel-*
```

#### --npm-client [client]

Install external dependencies using `[client] install`. Must be an executable
that knows how to install npm dependencies.

```sh
$ lerna bootstrap --npm-client=yarn
```

May also be configured in `lerna.json`:

```js
{
  ...
  "npmClient": "yarn"
}
```

#### --reject-cycles

Fail immediately if a cycle is found (in `bootstrap`, `exec`, `publish` or `run`).

```sh
$ lerna bootstrap --reject-cycles
```

#### --use-workspaces

Enables integration with [Yarn Workspaces](https://github.com/yarnpkg/rfcs/blob/master/implemented/0000-workspaces-install-phase-1.md) (available since yarn@0.27+).
The values in the array are the commands in which Lerna will delegate operation to Yarn (currently only bootstrapping).
If `--use-workspaces` is true then `packages` will be overridden by the value from `package.json/workspaces.`
May also be configured in `lerna.json`:

```js
{
  ...
  "npmClient": "yarn",
  "useWorkspaces": true
}
```

The root-level package.json must also include a `workspaces` array:

```json
{
  "private": true,
  "devDependencies": {
    "lerna": "^2.2.0"
  },
  "workspaces": ["packages/*"]
}
```

This list is broadly similar to lerna's `packages` config (a list of globs matching directories with a package.json),
except it does not support recursive globs (`"**"`, a.k.a. "globstars").

#### --use-git-version

Allow target versions of dependent packages to be written as [git hosted urls](https://github.com/npm/hosted-git-info) instead of a plain version number.
If enabled, Lerna will attempt to extract and save the interpackage dependency versions from `package.json` files using git url-aware parser.

Eg. assuming monorepo with 2 packages where `my-package-1` depends on `my-package-2`, `package.json` of `my-package-1` could be:

```
// packages/my-package-1/package.json
{
  name: "my-package-1",
  version: "1.0.0",
  bin: "bin.js",
  dependencies: {
    "my-package-2": "github:example-user/my-package-2#v1.0.0"
  },
  devDependencies: {
    "my-dev-dependency": "^1.0.0"
  },
  peerDependencies: {
    "my-peer-dependency": "^1.0.0"
  }
}
```

For the case above Lerna will read the version of `my-package-2` dependency as `1.0.0`.

This allows packages to be distributed via git repos if eg. packages are private and [private npm repo is not an option](https://www.dotconferences.com/2016/05/fabien-potencier-monolithic-repositories-vs-many-repositories).

Please note that using `--use-git-version`

* is limited to urls with [`committish`](https://docs.npmjs.com/files/package.json#git-urls-as-dependencies) part present (ie. `github:example-user/my-package-2` is invalid)
* requires `publish` command to be used with `--exact`

May also be configured in `lerna.json`:

```js
{
  ...
  "useGitVersion": true
}
```

#### --git-version-prefix

Defines version prefix string (defaults to 'v') ignored when extracting version number from a commitish part of git url.
Everything after the prefix will be considered a version.

Eg. given `github:example-user/my-package-2#v1.0.0` and `gitVersionPrefix: 'v'` version will be read as `1.0.0`.

Only used if `--use-git-version` is set to `true`.

May also be configured in `lerna.json`:

```js
{
  ...
  "gitVersionPrefix": "v"
}
```

#### --stream

Stream output from child processes immediately, prefixed with the originating
package name. This allows output from different packages to be interleaved.

```sh
$ lerna run watch --stream
```

#### --parallel

Similar to `--stream`, but completely disregards concurrency and topological sorting, running a given command or script immediately in all matching packages with prefixed streaming output. This is the preferred flag for long-running processes such as `babel src -d lib -w` run over many packages.

```sh
$ lerna exec --parallel -- babel src -d lib -w
```

#### --registry [registry]

When run with this flag, forwarded npm commands will use the specified registry for your package(s).

This is useful if you do not want to explicitly set up your registry
configuration in all of your package.json files individually when e.g. using
private registries.

#### --temp-tag

When passed, this flag will alter the default publish process by first publishing
all changed packages to a temporary dist-tag (`lerna-temp`) and then moving the
new version(s) to the default [dist-tag](https://docs.npmjs.com/cli/dist-tag) (`latest`).

This is not generally necessary, as Lerna will publish packages in topological
order (all dependencies before dependents) by default.

### README Badge

Using Lerna? Add a README badge to show it off: [![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lernajs.io/)

```
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lernajs.io/)
```

### Wizard

If you prefer some guidance for cli (in case you're about to start using lerna or introducing it to a new team), you might like [lerna-wizard](https://github.com/szarouski/lerna-wizard). It will lead you through a series of well-defined steps:

![lerna-wizard demo image](https://raw.githubusercontent.com/szarouski/lerna-wizard/2e269fb5a3af7100397a1f874cea3fa78089486e/demo.png)


class ScribeDocument
  @INDENT_PREFIX: 'indent-'

  @normalizeHtml: (root, options = {}) ->
    if root.childNodes.length == 0
      div = root.ownerDocument.createElement('div')
      return root.appendChild(div)
    Scribe.Utils.groupBlocks(root)
    _.each(_.clone(root.childNodes), (child) =>
      if child.nodeType != child.ELEMENT_NODE
        Scribe.Utils.removeNode(child)
      else if options.ignoreDirty || child.classList.contains(Scribe.Line.DIRTY_CLASS) || true
        Scribe.Line.wrapText(child)
        Scribe.Line.normalizeHtml(child)
    )

  constructor: (@root) ->
    this.buildLines()

  appendLine: (lineNode) ->
    return this.insertLineBefore(lineNode, null)

  applyToLines: (index, length, fn) ->
    [startLine, startOffset] = this.findLineAtOffset(index)
    [endLine, endOffset] = this.findLineAtOffset(index + length)
    if startLine == endLine
      fn(startLine, startOffset, endOffset - startOffset)
    else
      curLine = startLine.next
      fn(startLine, startOffset, startLine.length + 1 - startOffset)
      while curLine? && curLine != endLine
        next = curLine.next
        fn(curLine, 0, curLine.length + 1)
        curLine = next
      fn(endLine, 0, endOffset)

  buildLines: ->
    this.reset()
    ScribeDocument.normalizeHtml(@root, {ignoreDirty: true})
    this.rebuild()

  cleanNode: (lineNode) ->
    return if lineNode.classList.contains(Scribe.Constants.SPECIAL_CLASSES.EXTERNAL)
    line = this.findLine(lineNode)
    if line? && this.updateLine(line)
      lineNode.classList.remove(Scribe.Line.DIRTY_CLASS)
      return true
    return false

  deleteText: (index, length) ->
    if index + length == @length and @trailingNewline
      @trailingNewline = false
      @length -= 1
      length -= 1
    return if length <= 0
    firstLine = lastLine = null
    this.applyToLines(index, length, (line, offset, length) =>
      firstLine = line unless firstLine?
      lastLine = line
      if offset == 0 && length == line.length + 1
        Scribe.Utils.removeNode(line.node)
      else
        line.deleteText(offset, length)
      this.updateLine(line)
    )
    if firstLine != lastLine and this.findLine(firstLine.node) and this.findLine(lastLine.node)
      Scribe.Utils.mergeNodes(firstLine.node, lastLine.node)
      this.updateLine(firstLine)
      this.removeLine(lastLine)

  findLeaf: (node) ->
    lineNode = node.parentNode
    while lineNode? && !Scribe.Line.isLineNode(lineNode)
      lineNode = lineNode.parentNode
    return null if !lineNode?
    line = this.findLine(lineNode)
    return line.findLeaf(node)

  findLine: (node) ->
    node = this.findLineNode(node)
    if node?
      return @lineMap[node.id]
    else
      return null

  findLineAtOffset: (offset) ->
    retLine = @lines.first
    _.all(@lines.toArray(), (line, index) =>
      retLine = line
      if offset < line.length + 1
        return false
      else
        offset -= (line.length + 1) if index < @lines.length - 1
        return true
    )
    return [retLine, offset]

  findLineNode: (node) ->
    while node? && !Scribe.Line.isLineNode(node)
      node = node.parentNode
    return node

  forceTrailingNewline: ->
    unless @trailingNewline
      if @lines.length > 1 && @lines.last.length == 0
        Scribe.Utils.removeNode(@lines.last.node)
        this.removeLine(@lines.last)
      @trailingNewline = true
      @length += 1

  formatText: (index, length, name, value) ->
    this.applyToLines(index, length, (line, offset, length) =>
      line.formatText(offset, length, name, value)
      this.updateLine(line)
    )

  insertLineBefore: (newLineNode, refLine) ->
    line = new Scribe.Line(this, newLineNode)
    if refLine != null
      @lines.insertAfter(refLine.prev, line)
    else
      @lines.append(line)
    @lineMap[line.id] = line
    @length += line.length
    @length += 1 if @lines.length > 1
    return line

  insertText: (index, text) ->
    [line, lineOffset] = this.findLineAtOffset(index)
    textLines = text.split("\n")
    if index == @length && @trailingNewline
      @trailingNewline = false
      @length -= 1    # Doc did not get shorter but _.each loop compensates
      _.each(textLines, (textLine) =>
        contents = this.makeLineContents(textLine)
        div = @root.ownerDocument.createElement('div')
        _.each(contents, (content) ->
          div.appendChild(content)
        )
        @root.appendChild(div)
        this.appendLine(div)
      )
    else if textLines.length == 1
      contents = this.makeLineContents(text)
      this.insertContentsAt(line, lineOffset, contents)
    else
      [line1, line2] = this.insertNewlineAt(line, lineOffset)
      contents = this.makeLineContents(textLines[0])
      this.insertContentsAt(line1, lineOffset, contents)
      contents = this.makeLineContents(textLines[textLines.length - 1])
      this.insertContentsAt(line2, 0, contents)
      if textLines.length > 2
        _.each(textLines.slice(1, -1), (lineText) =>
          lineNode = this.makeLine(lineText)
          @root.insertBefore(lineNode, line2.node)
          this.insertLineBefore(lineNode, line2)
        )

  makeLine: (text) ->
    lineNode = @root.ownerDocument.createElement('div')
    lineNode.classList.add(Scribe.Line.CLASS_NAME)
    contents = this.makeLineContents(text)
    _.each(contents, (content) ->
      lineNode.appendChild(content)
    )
    return lineNode

  makeLineContents: (text) ->
    strings = text.split("\t")
    contents = []
    _.each(strings, (str, strIndex) =>
      contents.push(this.makeText(str)) if str.length > 0
      if strIndex < strings.length - 1
        contents.push(this.makeTab())
    )
    return contents

  makeTab: ->
    tab = @root.ownerDocument.createElement('span')
    tab.classList.add(Scribe.Leaf.TAB_NODE_CLASS)
    tab.classList.add(Scribe.Constants.SPECIAL_CLASSES.ATOMIC)
    tab.textContent = "\t"
    return tab

  makeText: (text) ->
    node = @root.ownerDocument.createElement('span')
    node.textContent = text
    return node

  # insertContentsAt: (Number startIndex, String text) ->
  # insertContentsAt: (ScribeRange startIndex, String text) ->
  insertContentsAt: (line, offset, contents) ->
    return if contents.length == 0
    [leaf, leafOffset] = line.findLeafAtOffset(offset)
    if leaf.node.nodeName != 'BR'
      [beforeNode, afterNode] = line.splitContents(offset)
      parentNode = beforeNode?.parentNode || afterNode?.parentNode
      _.each(contents, (content) ->
        parentNode.insertBefore(content, afterNode)
      )
    else
      parentNode = leaf.node.parentNode
      Scribe.Utils.removeNode(leaf.node)
      _.each(contents, (content) ->
        parentNode.appendChild(content)
      )
    this.updateLine(line)

  insertNewlineAt: (line, offset) ->
    if offset == 0 or offset == line.length
      div = @root.ownerDocument.createElement('div')
      if offset == 0
        @root.insertBefore(div, line.node)
        newLine = this.insertLineBefore(div, line)
        return [newLine, line]
      else
        refLine = line.next
        refLineNode = if refLine? then refLine.node else null
        @root.insertBefore(div, refLineNode)
        newLine = this.insertLineBefore(div, refLine)
        return [line, newLine]
    else
      newLine = this.splitLine(line, offset)
      return [line, newLine]

  rebuild: ->
    this.reset()
    _.each(@root.childNodes, (node) =>
      this.appendLine(node)
    )

  rebuildDirty: ->
    # First and last nodes are always dirty to handle edge cases
    @root.firstChild.classList.add(Scribe.Line.DIRTY_CLASS)
    @root.lastChild.classList.add(Scribe.Line.DIRTY_CLASS)
    dirtyNodes = _.clone(@root.getElementsByClassName(Scribe.Line.DIRTY_CLASS))
    _.each((dirtyNodes), (lineNode, index) =>
      this.cleanNode(lineNode)
      prevNode = lineNode.previousSibling
      nextNode = lineNode.nextSibling
      while prevNode? && prevNode != dirtyNodes[index - 1]
        this.cleanNode(prevNode)
        prevNode = prevNode.previousSibling
      while nextNode? && nextNode != dirtyNodes[index + 1]
        this.cleanNode(nextNode)
        nextNode = nextNode.nextSibling
    )

  removeLine: (line) ->
    delete @lineMap[line.id]
    @lines.remove(line)
    @length -= line.length
    @length -= 1 if @lines.length >= 1

  reset: ->
    @lines = new LinkedList()
    @lineMap = {}
    @length = 1
    @trailingNewline = true

  splitLine: (line, offset) ->
    [lineNode1, lineNode2] = Scribe.Utils.splitNode(line.node, offset, true)
    line.node = lineNode1
    this.updateLine(line)
    return this.insertLineBefore(lineNode2, line.next)

  toDelta: ->
    lines = @lines.toArray()
    ops = _.flatten(_.map(lines, (line, index) ->
      ops = Tandem.Delta.copy(line.delta).ops
      ops.push(new Tandem.InsertOp("\n", line.formats)) if index < lines.length - 1
      return ops
    ), true)
    formats = if @lines.last? then @lines.last.formats else {}
    ops.push(new Tandem.InsertOp("\n", formats)) if @trailingNewline
    delta = new Tandem.Delta(0, @length, ops)
    return delta

  updateDirty: ->
    dirtyNodes = @root.ownerDocument.getElementsByClassName(Scribe.Line.DIRTY_CLASS)
    fixLines = false
    _.each(dirtyNodes, (dirtyNode) =>
      line = this.findLine(dirtyNode)
      if line?
        this.updateLine(line)
        fixLines = true if line.formats['list']?
    )

  updateLine: (line) ->
    @length -= line.length
    didRebuild = line.rebuild()
    @length += line.length
    return didRebuild

window.Scribe ||= {}
window.Scribe.Document = ScribeDocument


#= require socket.io
#= require coffee/ot/jetstate
#= require coffee/network
#= require coffee/editor/presence

# TODO fix sendUpdate code diversion from update

class JetClient
  @DEFAULTS:
    latency: 0
    delegate: null
    resendInterval: 5000
    userId: Stypi.configs.userId

  # Socketio events
  @CONNECT        : "connect"
  @MESSAGE        : "message"
  @DISCONNECT     : "disconnect"
  @RECONNECT      : "reconnect"
  @RECONNECTING   : "reconnecting"

  # jetclient events
  @CONNECTED      : "connected"
  @DISCONNECTED   : "disconnected"
  @LEAVING        : "leaving"
  @LEFT           : "left"
  @WARNING        : "warning"

  # Node packet types
  @ACK:       "ack"
  @HANDSHAKE: "handshake"                       # We have joined
  @HANDSHAKE_RECONNECT: "handshake-reconnect"   # We lost connection, trying to reconnect
  @JOIN:      "user/join"                            # Another user has joined
  @LEAVE:     "user/leave"                           # Another user has left
  @SYNC:      "editor/sync"
  @UPDATE:    "editor/update"                          # Update to some jetstate

  constructor: (options = {}) ->
    @settings = $.extend({}, JetClient.DEFAULTS, options)
    @states = {}
    @status = JetClient.DISCONNECTED
    @inFlight = true
    if Stypi? && Stypi.utils? && Stypi.utils.inDevelopment()
      @packetHistory = {
        sent: []
        recieved: []
      }
    @network = Network
    @lastChangePacket = null
    if @settings.delegate?
      $(@settings.delegate).bind(JetClient.UPDATE, =>
        this.checkRunwayReady.call(this)
      )
    window.onbeforeunload = =>
      status = @status
      @status = JetClient.LEAVING
      setTimeout( =>
        @status = status if @status == JetClient.LEAVING
      , 5000)
      return if @inFlight then return "This document has unsaved changes." else undefined
    window.onunload = =>
      @status = JetClient.LEFT

  connect: (docId, @version = 0) ->
    this.initSocket(docId)
    @status = JetClient.CONNECTED
    this.sync()
    @docId = docId

  initSocket: (docId) ->
    if @docId?
      @network.off(@docId, JetClient.UPDATE)
      @network.off(@docId, JetClient.JOIN)
      @network.off(@docId, JetClient.LEAVE)

    @network.on(docId, JetClient.UPDATE, (packet, callback) =>
      this.remoteUpdate(packet)
    )
    @network.on(docId, JetClient.JOIN, (packet) =>
      this.remoteJoin(packet)
    )
    @network.on(docId, JetClient.LEAVE, (packet) =>
      this.remoteLeave(packet)
    )
    @network.on(docId, JetClient.RECONNECTING, (timeout, attempts) =>
      $(this).trigger(JetClient.DISCONNECT) if attempts == 1 && @status != JetClient.LEAVING && @status != JetClient.LEFT
    )
    @network.on(docId, JetClient.RECONNECT, (transport, attempts) =>
      $(this).trigger(JetClient.RECONNECT)
      @inFlight = false
      syncPacket = {type: JetClient.SYNC, version: @version}
      syncPacket['update'] = @lastChangePacket if @lastChangePacket?
      this.send(syncPacket, true, (response) =>
        this.syncCallback(response)
      )
    )

  sync: ->
    syncPacket = { type: "editor/sync", version: @version }
    this.send(syncPacket, true, (response) =>
      this.syncCallback(response)
    )

  addState: (state) ->
    console.assert(@states[state.type] == undefined, "Adding already existing state of type:", state.type)
    @states[state.type] = state

  whenConnected: (callback, params) ->
    if @status == JetClient.CONNECTED
      callback.call(this, params)
    else
      $(this).one(JetClient.CONNECTED, =>
        callback.call(this, params)
      )

  remoteJoin: (message) ->
    $(Stypi).trigger(Stypi.events.CLIENT_JOIN, message)

  remoteLeave: (message) ->
    $(Stypi).trigger(Stypi.events.CLIENT_LEAVE, message)

  # Handle update from server
  remoteUpdate: (message) ->
    if message.docId != @docId
      console.warn  "Got update with invalid docId", message.docId
      return
    console.assert(@version < message.version, "Version from server update #{message.version} is not newer than ours #{@version}!")
    @version = message.version
    # Assumes server sends changes in order they should be applied
    for change in message.changes
      console.assert(@states[change.type] != undefined, "remote updating state for something we are not tracking: " + change.type)
      @states[change.type].remoteUpdate(change.change, message.authorId)

  # If we can send a packet to server (and have a packet to send), send the packet
  checkRunwayReady: ->
    if @inFlight == false
      changes = []
      for type,state of @states
        ready = state.takeoff()
        changes.push({type: type, change: ready.change}) if ready
      if changes.length > 0
        this.sendUpdate(changes, (message) =>
          this.updateCallback(message)
        )
        @inFlight = true

  prepareChangePacket: (changes) ->
    return {
      type: JetClient.UPDATE
      version: @version
      changes: changes
      changeId: Math.floor(Math.random() * 4294967295)
    }

  syncCallback: (response) ->
    if !response.error? || response.error.length == 0
      if @states[JetState.TEXT]?
        @states[JetState.TEXT].reset("\n") if @version == 0
        for delta in response.deltas
          @states[JetState.TEXT].remoteUpdate(delta)
      Stypi.Presence.setUsers(response.users)
      state.land() for type,state of @states
      @version = response.version
      $(this).trigger(JetClient.CONNECTED)
      @inFlight = false
      this.checkRunwayReady()
    else
      console.warn(error) for error in response.error

  updateCallback: (message) ->
    if message.docId != @docId
      console.warn  "Got ack with invalid docId", message.docId
      return
    console.assert(@version < message.version || message.duplicate == true, "Version from server ack:#{message.version} is not newer than ours:#{@version}")
    @version = message.version
    if !message.duplicate
      state.land() for type,state of @states
    @inFlight = false
    @checkRunwayReady()

  # Helper for sending a generic packet
  send: (packet, priority, callback) ->
    if typeof priority != 'boolean'
      callback = priority
      priority = false
    console.assert(packet.type?, "Sending undefined packet type")
    @packetHistory.sent.push(packet) if Stypi? && Stypi.utils? && Stypi.utils.inDevelopment()
    setTimeout( =>
      retry = true
      while true
        try
          @network.send(@docId, packet.type, packet, priority, callback)
          break
        catch e
          break if retry == false
          retry = false
    , @settings.latency)

  # Creates update packet for changeset and sends, will resend until acknowledged
  sendUpdate: (changes, clientCallback) ->
    packet = @lastChangePacket = this.prepareChangePacket(changes)
    this.send(packet, (args...) =>
      @lastChangePacket = null
      clientCallback.apply(this, args)
    )

# So other files can actually use this library
window.JetClient = JetClient if window?


#= require async

# Manages network connections
#
# Creates socketio connection, reusable by many modules
# Handles configurations, authentication, and connectivity notifications

class Connection
  constructor: (@socket) ->
    @sendQueue = []
    @ready = false
    @stats =
      sent      : {}
      recieved  : {}
      acked     : {}

  addStat: (type, route) ->
    @stats[type] = {} if !@stats[type]?
    @stats[type][route] = 0 if !@stats[type][route]?
    @stats[type][route] += 1

  setReady: ->
    async.until( =>
      return @sendQueue.length == 0
    , (callback) =>
      elem = @sendQueue.splice(0, 1)
      [route, packet, sendCallback] = elem[0]
      console.info "Sending from queue:", route, packet
      @socket.emit(route, packet, (args...) =>
        sendCallback.apply(this, args)
        callback()
      )
    , (err) =>
      @ready = true
    )

  send: (route, packet, priority, callback) ->
    if @ready
      console.info "Sending:", route, packet
      @socket.emit(route, packet, callback)
    else
      console.info "Queued:", route, packet, priority
      if priority
        @sendQueue.unshift([route, packet, callback])
      else
        @sendQueue.push([route, packet, callback])

class NetworkClass
  @ACKED    : 'acked'
  @RECIEVED : 'recieved'
  @SENT     : 'sent'

  constructor: ->
    this.reset()

  authenticate: (docId) ->
    authPacket =
      auth        : @auth
      docId       : docId
      referrer    : window.location.href      # In case access method is obscure url
      sessionId   : @sessionId
      userId      : @userId
    console.info "Connecting to", docId, authPacket
    if @connections[docId]?
      @connections[docId].socket.emit('auth', authPacket, (response) =>
        if !response.error || response.error.length == 0
          console.info "Connected!", response
          @connections[docId].setReady() if @connections[docId].ready == false
        else
          $(document).trigger("notify", ["error", "Could not access this document.", Infinity])
      )
    else
      console.warn "No connection to authenticate for docId", docId

  reset: ->
    @connections  = {}
    @unacked      = 0
    @auth         = Stypi.configs.auth
    @host         = Stypi.configs.nodeHost
    @sessionId    = Stypi.configs.sessionId
    @userId       = Stypi.configs.userId
    @options      = {
      'max reconnection attempts' : Infinity
      'reconnection limit'        : 30000
      'sync disconnect on unload' : false
      'secure'                    : true
      'port'                      : 443
      'force new connection'      : true
    }
    window.onunload = =>
      conn.socket.socket.disconnectSync() for conn in @connections

  getOrCreateConnection: (docId) ->
    if !@connections[docId]?
      socket = io.connect("https://#{@host}", @options)
      @connections[docId] = new Connection(socket)
      socket.on('reconnecting', =>
        @connections[docId].ready = false if  @connections[docId]?
      ).on('reconnect', =>
        this.authenticate(docId) if @connections[docId].ready == false
      )
      this.authenticate(docId)
    return @connections[docId]

  send: (docId, route, packet, priority, sendCallback) ->
    if typeof priority != 'boolean'
      sendCallback = priority
      priority = false
    connection = this.getOrCreateConnection(docId)
    connection.addStat(NetworkClass.SENT, route)
    @unacked += 1
    connection.send(route, packet, priority, (packet, callback) =>
      console.info "Callback", route, packet
      connection.addStat(NetworkClass.ACKED, route)
      @unacked -= 1
      sendCallback(packet, callback) if sendCallback?
    )

  on: (docId, route, onCallback) ->
    connection = this.getOrCreateConnection(docId)
    connection.socket.addListener(route, (args...) =>
      console.info "Got", route, args
      connection.addStat(NetworkClass.RECIEVED, route)
      onCallback.apply(this, args) if onCallback?
    )

  off: (docId, route) ->
    @connections[docId].socket.removeAllListeners(route) if @connections[docId]?

$(document).ready( ->
  window.NetworkClass = NetworkClass     # Necessary for tests to work
  window.Network = new NetworkClass()
)


body
  font-family: 'Helvetica', 'Arial', san-serif
  font-size: 13px
  padding: 25px

#content-container
  margin: auto
  width: 960px

.formatting-container
  background-color: #f5f5f5
  border-bottom: 1px solid #ccc
  height: 32px
  line-height: 32px
  padding: 0px 5px
  .active
    color: green
    font-weight: bold

.editor-container
  height: 600px

.editor-wrapper
  border: 1px solid #aaa
  box-shadow: 0 0 2px 2px #ddd


describe('Selection', ->
  findIndexes = (html) ->
    $('#editor-container').html(html)
    editor = new Scribe.Editor('editor-container')
    lines = editor.doc.lines.toArray()
    lineIndex = 0
    ret = _.reduce(lines, (indexes, line) ->
      offset = 0
      while line.node.textContent.indexOf("|", offset) > -1
        index = line.node.textContent.indexOf("|", offset)
        indexes.push(lineIndex + index - indexes.length)
        offset = index + 1
      lineIndex += line.length + 1
      return indexes
    , [])
    return ret

  describe('findIndexes', (html) ->
    it('should find simple indexes', ->
      indexes = findIndexes('
01|23|4
')
      expect(indexes).to.deep.equal([2,4])
    )
    it('should find multiline indexes', ->
      indexes = findIndexes('
01|234

67|89
')
      expect(indexes).to.deep.equal([2,8])
    )
    it('should find collapsed indexes', ->
      indexes = findIndexes('
012||34
')
      expect(indexes).to.deep.equal([3,3])
    )
  )

  describe('preserve', ->
    tests =
      'basic preservation':
        'inside a node':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(0, '')
          expected: [0]
        'entire node':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(0, '')
          expected: [0]
        'multiple nodes':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(0, '')
          expected: [0]
        'multiple lines':
          lines: ['
0123|4
', '
5|6789
']
          fn: (editor) -> editor.insertAt(0, '')
          expected: [0, 1]
        'entire line':
          lines: ['
0123
', '
|45|
', '
6789
']
          fn: (editor) -> editor.insertAt(0, '')
          expected: [0, 1, 2]
        'entire document':
          lines: ['
|01|
']
          fn: (editor) -> editor.insertAt(0, '')
          expected: [0]
        'empty document before':
          lines: ['
||

']
          fn: (editor) -> editor.insertAt(0, '')
          expected: [0]
        'empty document after':
          lines: ['

||
']
          fn: (editor) -> editor.insertAt(0, '')
          expected: [0]

      'insert tests':
        'insert before':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(1, "A")
          expected: ['
0A123|45|6789
']
        'insert after':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(7, "A")
          expected: ['
0123|45|6A789
']
        'insert at beginning of selection':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(4, "A")
          expected: ['
0123A|45|6789
']
        'insert at end of selection':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(6, "A")
          expected: ['
0123|45|A6789
']
        'insert in middle of selection':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(5, "A")
          expected: ['
0123|4A5|6789
']
        'insert newline at beginning of selection':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(4, "\n")
          expected: ['
0123
', '
|45|6789
']
        'insert newline at end of selection':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(6, "\n")
          expected: ['
0123|45|
', '
6789
']
        'insert newline at middle of selection':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.insertAt(5, "\n")
          expected: ['
0123|4
', '
5|6789
']
        'insert newline to previous line format':
          lines: ['

0123456789

', '0123|45|6789
']
          fn: (editor) -> editor.insertAt(10, "\n")
          expected: [0, '

', 1]

      'delete tests':
        'delete before':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.deleteAt(1, 2)
          expected: ['
03|45|6789
']
        'delete after':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.deleteAt(7, 2)
          expected: ['
0123|45|69
']
        'delete at beginning of selection':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.deleteAt(2, 2)
          expected: ['
01|45|6789
']
        'delete at end of selection':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.deleteAt(6, 2)
          expected: ['
0123|45|89
']
        'delete middle of selection':
          lines: ['
012|3456|789
']
          fn: (editor) -> editor.deleteAt(4, 2)
          expected: ['
012|36|789
']
        'delete selection':
          lines: ['
012|3456|789
']
          fn: (editor) -> editor.deleteAt(3, 4)
          expected: ['
012||789
']
        'delete containing selection':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.deleteAt(3, 4)
          expected: ['
012||789
']
        'delete beginning of leaf':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.deleteAt(1, 2)
          expected: ['
03|45|6789
']
        'delete end of leaf':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.deleteAt(7, 2)
          expected: ['
0123|45|69
']
        'delete entire leaf':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.deleteAt(2, 6)
          expected: ['
01||89
']

      'format tests':
        'apply leaf format before':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(1, 2, 'bold', true)
          expected: ['
0123|45|6789
']
        'apply leaf format after':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(7, 2, 'bold', true)
          expected: ['
0123|45|6789
']
        'apply leaf format in middle':
          lines: ['
012|3456|789
']
          fn: (editor) -> editor.formatAt(4, 2, 'bold', true)
          expected: ['
012|3456|789
']
        'apply leaf format overlapping beginning':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(3, 2, 'bold', true)
          expected: ['
0123|45|6789
']
        'apply leaf format overlapping end':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(5, 2, 'bold', true)
          expected: ['
0123|45|6789
']
        'apply leaf format overlapping beginning and end':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(3, 4, 'bold', true)
          expected: ['
0123|45|6789
']
        'apply leaf format to entire node':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(0, 10, 'bold', true)
          expected: ['
0123|45|6789
']
        'apply leaf format to entire node 2':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(0, 10, 'italic', true)
          expected: ['
0123|45|6789
']
        'apply line format':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(1, 2, 'list', 1)
          expected: ['

0123|45|6789

']

      'remove format tests':
        'remove leaf format before':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(1, 2, 'bold', false)
          expected: ['
0123|45|6789
']
        'remove leaf format after':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(7, 2, 'bold', false)
          expected: ['
0123|45|6789
']
        'remove leaf format in middle':
          lines: ['
012|3456|789
']
          fn: (editor) -> editor.formatAt(4, 2, 'bold', false)
          expected: ['
012|3456|789
']
        'remove leaf format overlapping beginning':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(3, 2, 'bold', false)
          expected: ['
0123|45|6789
']
        'remove leaf format overlapping end':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(5, 2, 'bold', false)
          expected: ['
0123|45|6789
']
        'remove leaf format overlapping beginning and end':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(3, 4, 'bold', false)
          expected: ['
0123|45|6789
']
        'remove leaf format from entire node':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(0, 10, 'bold', false)
          expected: ['
0123|45|6789
']
        'remove leaf format from entire node 2':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(0, 10, 'bold', false)
          expected: ['
0123|45|6789
']
        'remove leaf format from entire node 3':
          lines: ['
0123|45|6789
']
          fn: (editor) -> editor.formatAt(0, 10, 'italic', false)
          expected: ['
0123|45|6789
']
        'remove line format':
          lines: ['

0123|45|6789

']
          fn: (editor) -> editor.formatAt(1, 2, 'list', false)
          expected: ['
0123|45|6789
']

    _.each(tests, (testGroup, groupName) ->
      describe(groupName, ->
        _.each(testGroup, (test, name) ->
          it(name, ->
            html = Scribe.Utils.cleanHtml(test.lines.join(''))
            expectedHtml = _.map(test.expected, (line) ->
              return if _.isNumber(line) then test.lines[line] else line
            ).join('')
            [start, end] = findIndexes(html)
            expectedIndexes = findIndexes(expectedHtml)
            html = html.replace(/\|/g, '')
            expectedHtml = expectedHtml.replace(/\|/g, '')
            $('#editor-container').html(html)
            editor = new Scribe.Editor('editor-container')
            editor.setSelection(new Scribe.Range(editor, start, end))
            sel = editor.getSelection()
            expect([sel.start.index, sel.end.index]).to.deep.equal([start, end])
            test.fn(editor)
            sel = editor.getSelection()
            expect(sel).to.exist
            expect([sel.start.index, sel.end.index]).to.deep.equal(expectedIndexes)
          )
        )
      )
    )
  )
)


!!! 5
%html
  %head
    %title Stypi OT Demo
    %link{:rel => 'stylesheet', :type => 'text/css', :href => '../lib/reset.css'}
    %link{:rel => 'stylesheet', :type => 'text/css', :href => 'styles/style.css'}
    %meta{:charset => 'utf-8'}

  %body
    #content-container
      .editor-wrapper.first
        #formatting-container.formatting-container
          .format-group
            .format-button.first<>
              .format.bold
            .format-button<>
              .format.italic
            .format-button<>
              .format.underline
            .format-button.last<>
              .format.strike
        #editor-container.editor-container

    :javascript
      var Stypi = {
        configs: {"nodeHost":"devnode.stypi.com","registered":true,"auth":["DCGRG9eoSrkK+HxLQSxnHg==","f/+Ke9QJ2nKacZ9xeNCBsao8BCKhVAzpmFX4HZvUH3HjBH/5BHJ8KGvNjovm\nirjD"],"docId":"509b450602701dc20b000013","head":{"startLength":0,"endLength":106,"deltas":[{"text":"Global Marketing Plan","attributes":{"size":"large"}},{"text":"\n\n","attributes":{}},{"text":"Target Geographic Markets","attributes":{"bold":true}},{"text":"\n\n\n\n\n","attributes":{}},{"text":"Primary Marketing Channels","attributes":{"bold":true}},{"text":"\n\n\n\n\n","attributes":{}},{"text":"Creative Concepts","attributes":{"bold":true}},{"text":"\n\n\n\n\n","attributes":{}}]},"sessionId":"ip3ITPRWTeXt7QxF","userId":"4e8109d2ef696a000100004b","version":2,"username":"jason","environment":"development"},
        events: {}
      };
    %script{:type => 'text/javascript', :src => '../lib/jquery.js'}
    %script{:type => 'text/javascript', :src => '../lib/underscore.js'}
    %script{:type => 'text/javascript', :src => '../lib/eventemitter2.js'}
    %script{:type => 'text/javascript', :src => '../lib/rangy/rangy-core.js'}
    %script{:type => 'text/javascript', :src => '../lib/rangy/rangy-selectionsaverestore.js'}
    %script{:type => 'text/javascript', :src => '../lib/diff_match_patch.js'}
    %script{:type => 'text/javascript', :src => '../lib/linked_list.js'}
    %script{:type => 'text/javascript', :src => '../lib/tandem.js'}
    %script{:type => 'text/javascript', :src => '../src/constants.js'}
    %script{:type => 'text/javascript', :src => '../src/utils.js'}
    %script{:type => 'text/javascript', :src => '../src/leaf.js'}
    %script{:type => 'text/javascript', :src => '../src/line.js'}
    %script{:type => 'text/javascript', :src => '../src/document.js'}
    %script{:type => 'text/javascript', :src => '../src/position.js'}
    %script{:type => 'text/javascript', :src => '../src/range.js'}
    %script{:type => 'text/javascript', :src => '../src/selection.js'}
    %script{:type => 'text/javascript', :src => '../src/keyboard.js'}
    %script{:type => 'text/javascript', :src => '../src/renderer.js'}
    %script{:type => 'text/javascript', :src => '../src/undo-manager.js'}
    %script{:type => 'text/javascript', :src => '../src/paste-manager.js'}
    %script{:type => 'text/javascript', :src => '../src/editor.js'}
    %script{:type => 'text/javascript', :src => '../src/debug.js'}

    %script{:type => 'text/javascript', :src => '../lib/diff_match_patch.js'}
    %script{:type => 'text/javascript', :src => '../lib/async.js'}
    %script{:type => 'text/javascript', :src => '../lib/socket.io.js'}
    %script{:type => 'text/javascript', :src => 'scripts/stypi/network.js'}
    %script{:type => 'text/javascript', :src => 'scripts/stypi/ot/jetclient.js'}
    %script{:type => 'text/javascript', :src => 'scripts/stypi/ot/jetstate.js'}
    %script{:type => 'text/javascript', :src => 'scripts/stypi/presence.js'}

    %script{:type => 'text/javascript', :src => 'scripts/ot.js'}


class ScribeUndoManager
  @DEFAULTS:
    delay: 1000

  @computeUndo: (changeDelta, originalDelta) ->
    index = offset = 0
    ops = []
    _.each(changeDelta.ops, (op) ->
      if Tandem.Delta.isInsert(op)
        offset += op.getLength()
      else if Tandem.Delta.isRetain(op)
        start = index + offset
        if op.start > index
          length = op.start - index
          deletedOps = originalDelta.getOpsAt(index, length)
          ops = ops.concat(deletedOps)
          offset -= length
        ops.push(new Tandem.RetainOp(start, start + op.getLength(), op.attributes))
        index = op.end
      else
        console.error("Unrecognized type in op", op)
    )
    if changeDelta.endLength < changeDelta.startLength + offset
      deletedDeltas = originalDelta.getOpsAt(changeDelta.endLength - offset, changeDelta.startLength - changeDelta.endLength + offset)
      ops = ops.concat(deletedDeltas)
    return new Tandem.Delta(changeDelta.endLength, changeDelta.startLength, ops)

  @getLastChangeIndex: (delta) ->
    lastChangeIndex = index = offset = 0
    _.each(delta.ops, (op) ->
      if Tandem.Delta.isInsert(op)
        offset += op.getLength()
        lastChangeIndex = index + offset
      else if Tandem.Delta.isRetain(op)
        if op.start > index
          lastChangeIndex = index + offset
          offset -= (op.start - index)
        index = op.end
    )
    if delta.endLength < delta.startLength + offset
      lastChangeIndex = delta.endLength
    return lastChangeIndex

  constructor: (@editor, options = {}) ->
    @undoStack = []
    @redoStack = []
    @options = _.extend(ScribeUndoManager.DEFAULTS, options)
    @lastRecorded = 0
    this.initListeners()

  initListeners: ->
    @editor.keyboard.addHotkey(Scribe.Keyboard.HOTKEYS.UNDO, =>
      this.undo()
    )
    @editor.keyboard.addHotkey(Scribe.Keyboard.HOTKEYS.REDO, =>
      this.redo()
    )

  record: (changeDelta, oldDelta) ->
    return if changeDelta.isIdentity(changeDelta)
    @redoStack = []
    undoDelta = ScribeUndoManager.computeUndo(changeDelta, oldDelta)
    timestamp = new Date().getTime()
    if @lastRecorded + @options.delay > timestamp and @undoStack.length > 0
      change = @undoStack.pop()
      undoDelta = undoDelta.compose(change.undo.delta)
      changeDelta = change.redo.delta.compose(changeDelta)
    else
      @lastRecorded = timestamp
    @undoStack.push({
      undo:
        cursor: ScribeUndoManager.getLastChangeIndex(undoDelta)
        delta: undoDelta
      redo:
        cursor: ScribeUndoManager.getLastChangeIndex(changeDelta)
        delta: changeDelta
    })

  redo: ->
    if @redoStack.length > 0
      change = @redoStack.pop()
      @editor.applyDelta(change.redo.delta)
      @editor.setSelection(new Scribe.Range(@editor, change.redo.cursor, change.redo.cursor))
      @undoStack.push(change)

  undo: ->
    if @undoStack.length > 0
      change = @undoStack.pop()
      @editor.applyDelta(change.undo.delta)
      @editor.setSelection(new Scribe.Range(@editor, change.undo.cursor, change.undo.cursor))
      @redoStack.push(change)

window.Scribe ||= {}
window.Scribe.UndoManager = ScribeUndoManager


formatText: (index, length, name, value) ->

this.applyToLines(index, length, (line, offset, length) =>

line.formatText(offset, length, name, value)

class ScribeEditor extends EventEmitter2
  @editors: []

  @CONTAINER_ID: 'Scribe-container'
  @ID_PREFIX: 'editor-'
  @CURSOR_PREFIX: 'cursor-'
  @DEFAULTS:
    cursor: 0
    enabled: true
    styles: {}
  @events:
    TEXT_CHANGE      : 'text-change'
    SELECTION_CHANGE : 'selection-change'

  constructor: (@iframeContainer, options) ->
    @options = _.extend(Scribe.Editor.DEFAULTS, options)
    @id = _.uniqueId(ScribeEditor.ID_PREFIX)
    @iframeContainer = document.getElementById(@iframeContainer) if _.isString(@iframeContainer)
    this.reset(true)
    this.enable() if @options.enabled

  reset: (keepHTML = false) ->
    @ignoreDomChanges = true
    options = _.clone(@options)
    options.keepHTML = keepHTML
    @renderer = new Scribe.Renderer(@iframeContainer, options)
    @contentWindow = @renderer.iframe.contentWindow
    @root = @contentWindow.document.getElementById(ScribeEditor.CONTAINER_ID)
    @doc = new Scribe.Document(@root)
    @selection = new Scribe.Selection(this)
    @keyboard = new Scribe.Keyboard(this)
    @undoManager = new Scribe.UndoManager(this)
    @pasteManager = new Scribe.PasteManager(this)
    this.initListeners()
    @ignoreDomChanges = false
    ScribeEditor.editors.push(this)

  disable: ->
    this.doSilently( =>
      @root.setAttribute('contenteditable', false)
    )

  enable: ->
    if !@root.getAttribute('contenteditable')
      this.doSilently( =>
        @root.setAttribute('contenteditable', true)
      )

  initListeners: ->
    onEditOnce = =>
    onEdit = =>
      onEditOnce = _.once(onEdit)
      return if @ignoreDomChanges
      this.update()
    onSubtreeModified = =>
      return if @ignoreDomChanges
      toCall = onEditOnce
      _.defer( =>
        toCall.call(null)
      )
    onEditOnce = _.once(onEdit)
    @root.addEventListener('DOMSubtreeModified', onSubtreeModified)

  keepNormalized: (fn) ->
    fn.call(this)
    @doc.rebuildDirty()
    @doc.forceTrailingNewline()

  applyDelta: (delta, external = true) ->
    return if delta.isIdentity()
    this.doSilently( =>
      @selection.preserve( =>
        console.assert(delta.startLength == @doc.length, "Trying to apply delta to incorrect doc length", delta, @doc, @root)
        index = 0       # Stores where the last retain end was, so if we see another one, we know to delete
        offset = 0      # Tracks how many characters inserted to correctly offset new text
        oldDelta = @doc.toDelta()
        retains = []
        _.each(delta.ops, (op) =>
          if Tandem.Delta.isInsert(op)
            @doc.insertText(index + offset, op.value)
            retains.push(new Tandem.RetainOp(index + offset, index + offset + op.getLength(), op.attributes))
            offset += op.getLength()
          else if Tandem.Delta.isRetain(op)
            if op.start > index
              @doc.deleteText(index + offset, op.start - index)
              offset -= (op.start - index)
            retains.push(new Tandem.RetainOp(op.start + offset, op.end + offset, op.attributes))
            index = op.end
          else
            console.warn('Unrecognized type in delta', op)
        )
        # If end of text was deleted
        if delta.endLength < delta.startLength + offset
          @doc.deleteText(delta.endLength, delta.startLength + offset - delta.endLength)
        retainDelta = new Tandem.Delta(delta.endLength, delta.endLength, retains)
        _.each(retainDelta.ops, (op) =>
          _.each(op.attributes, (value, format) =>
            @doc.formatText(op.start, op.end - op.start, format, value) if value == null
          )
          _.each(op.attributes, (value, format) =>
            @doc.formatText(op.start, op.end - op.start, format, value) if value?
          )
        )
        @doc.forceTrailingNewline()
        @undoManager.record(delta, oldDelta)
        unless external
          this.emit(ScribeEditor.events.TEXT_CHANGE, delta)
      )
    )

  deleteAt: (index, length) ->
    this.doSilently( =>
      @selection.preserve( =>
        return this.trackDelta( =>
          this.keepNormalized( =>
            @doc.deleteText(index, length)
          )
        )
      )
    )

  doSilently: (fn) ->
    oldIgnoreDomChange = @ignoreDomChanges
    @ignoreDomChanges = true
    fn()
    @ignoreDomChanges = oldIgnoreDomChange

  # formatAt: (Number index, Number length, String name, Mixed value) ->
  formatAt: (index, length, name, value) ->
    this.doSilently( =>
      @selection.preserve( =>
        return this.trackDelta( =>
          this.keepNormalized( =>
            @doc.formatText(index, length, name, value)
          )
        )
      )
    )

  getDelta: ->
    return @doc.toDelta()

  getLength: ->
    return @doc.length

  getSelection: ->
    return @selection.getRange()

  insertAt: (index, text) ->
    this.doSilently( =>
      @selection.preserve( =>
        return this.trackDelta( =>
          this.keepNormalized( =>
            @doc.insertText(index, text)
          )
        )
      )
    )

  setSelection: (range) ->
    @selection.setRange(range)

  trackDelta: (fn) ->
    oldDelta = @doc.toDelta()
    fn()
    newDelta = @doc.toDelta()
    decompose = newDelta.decompose(oldDelta)
    compose = oldDelta.compose(decompose)
    newDelta.clearOpsCache()
    compose.clearOpsCache()
    console.assert(_.isEqual(compose, newDelta), oldDelta, newDelta, decompose, compose)
    @undoManager.record(decompose, oldDelta)
    this.emit(ScribeEditor.events.TEXT_CHANGE, decompose) unless  decompose.isIdentity()
    return decompose

  update: ->
    this.doSilently( =>
      @selection.preserve( =>
        return this.trackDelta( =>
          Scribe.Document.normalizeHtml(@root)
          lines = @doc.lines.toArray()
          lineNode = @root.firstChild
          _.each(lines, (line, index) =>
            while line.node != lineNode
              if line.node.parentNode == @root
                newLine = @doc.insertLineBefore(lineNode, line)
                lineNode = lineNode.nextSibling
              else
                @doc.removeLine(line)
                return
            @doc.updateLine(line)
            lineNode = lineNode.nextSibling
          )
          while lineNode != null
            newLine = @doc.appendLine(lineNode)
            lineNode = lineNode.nextSibling
        )
      )
    )

window.Scribe ||= {}
window.Scribe.Editor = ScribeEditor


class ScribeLeaf extends LinkedList.Node
  @ID_PREFIX: 'leaf-'
  @TAB_NODE_CLASS: 'tab'

  @isLeafNode: (node) ->
    return false if !node? || node.nodeType != node.ELEMENT_NODE ||  !node.classList?
    return false if node.classList.contains(Scribe.Constants.SPECIAL_CLASSES.EXTERNAL)
    return true if node.tagName == 'BR'
    return true if node.classList?.contains(Scribe.Leaf.TAB_NODE_CLASS)
    return true if node.childNodes.length == 1 && node.firstChild.nodeType == node.TEXT_NODE
    return false

  @isLeafParent: (node) ->
    return false if !node? || node.nodeType != node.ELEMENT_NODE ||  !node.classList?
    return false if Scribe.Leaf.isLeafNode(node)
    return false if node.classList.contains(Scribe.Constants.SPECIAL_CLASSES.EXTERNAL)
    return false if node.childNodes.length == 0
    return node.childNodes.length > 1 or node.firstChild.nodeType != node.TEXT_NODE

  constructor: (@line, @node, formats) ->
    @formats = _.clone(formats)
    @id = _.uniqueId(Scribe.Leaf.ID_PREFIX)
    @node.textContent = "" if @node.tagName == 'BR'
    @text = @node.textContent
    @length = @text.length

  getFormats: (excludeDefault = false) ->
    formats = if excludeDefault then {} else _.clone(Scribe.Constants.DEFAULT_LEAF_FORMATS)
    return _.extend(formats, @formats, @line.formats)

  getLineOffset: ->
    return Scribe.Position.getIndex(@node, 0, @line.node)

  insertText: (offset, text) ->
    @text = @text.substring(0, offset) + text + @text.substring(offset)
    [node, offset] = Scribe.Position.findDeepestNode(@line.doc.editor, @node, offset)
    node.textContent = node.textContent.substring(0, offset) + text + node.textContent.substring(offset)
    @length = @text.length

class ScribeLeafIterator
  # Start and end are both inclusive
  # Otherwise if end is inclusive, we cannot express end of line unambiguously
  constructor: (@start, @end) ->
    @cur = @start

  next: ->
    ret = @cur
    if @cur == @end || @cur == null
      @cur = null
    else if @cur.next?
      @cur = @cur.next
    else
      line = @cur.line.next
      while line? && line.leaves.length == 0
        line = line.next
      @cur = if line? then line.leaves.first else null
    return ret

  toArray: ->
    arr = []
    itr = new ScribeLeafIterator(@start, @end)
    while next = itr.next()
      arr.push(next)
    return arr

window.Scribe ||= {}
window.Scribe.Leaf = ScribeLeaf
window.Scribe.LeafIterator = ScribeLeafIterator


!!! 5
%html
  %head
    %title Stypi iPhone Editor Demo
    %link{:rel => 'stylesheet', :type => 'text/css', :href => '../lib/reset.css'}
    %link{:rel => 'stylesheet', :type => 'text/css', :href => 'styles/iphone-style.css'}
    %meta{:name=> 'viewport', :content => 'initial-scale=1.0, user-scalable=no'}
    %meta{:charset => 'utf-8'}

  %body
    #content-container
      .editor-wrapper
        #editor-container.editor-container

    %script{:type => 'text/javascript', :src => '../lib/jquery.js'}
    %script{:type => 'text/javascript', :src => '../lib/underscore.js'}
    %script{:type => 'text/javascript', :src => '../lib/eventemitter2.js'}
    %script{:type => 'text/javascript', :src => '../lib/rangy/rangy-core.js'}
    %script{:type => 'text/javascript', :src => '../lib/rangy/rangy-selectionsaverestore.js'}
    %script{:type => 'text/javascript', :src => '../lib/diff_match_patch.js'}
    %script{:type => 'text/javascript', :src => '../lib/linked_list.js'}
    %script{:type => 'text/javascript', :src => '../lib/tandem.js'}
    %script{:type => 'text/javascript', :src => '../src/constants.js'}
    %script{:type => 'text/javascript', :src => '../src/utils.js'}
    %script{:type => 'text/javascript', :src => '../src/leaf.js'}
    %script{:type => 'text/javascript', :src => '../src/line.js'}
    %script{:type => 'text/javascript', :src => '../src/document.js'}
    %script{:type => 'text/javascript', :src => '../src/position.js'}
    %script{:type => 'text/javascript', :src => '../src/range.js'}
    %script{:type => 'text/javascript', :src => '../src/selection.js'}
    %script{:type => 'text/javascript', :src => '../src/keyboard.js'}
    %script{:type => 'text/javascript', :src => '../src/renderer.js'}
    %script{:type => 'text/javascript', :src => '../src/undo-manager.js'}
    %script{:type => 'text/javascript', :src => '../src/paste-manager.js'}
    %script{:type => 'text/javascript', :src => '../src/editor.js'}
    %script{:type => 'text/javascript', :src => '../src/debug.js'}

    %script{:type => 'text/javascript', :src => '../lib/diff_match_patch.js'}
    %script{:type => 'text/javascript', :src => '../lib/async.js'}
    %script{:type => 'text/javascript', :src => '../lib/socket.io.js'}
    %script{:type => 'text/javascript', :src => 'scripts/stypi/network.js'}
    %script{:type => 'text/javascript', :src => 'scripts/stypi/ot/jetclient.js'}
    %script{:type => 'text/javascript', :src => 'scripts/stypi/ot/jetstate.js'}
    %script{:type => 'text/javascript', :src => 'scripts/stypi/presence.js'}

    %script{:type => 'text/javascript', :src => '../lib/ga-js-runtime.js'}

    %script{:type => 'text/javascript', :src => 'scripts/iphone-ot.js'}


describe('Undo manager', ->
  describe('computeUndo', ->
    tests =
      'insert':
        original: new Tandem.Delta(0, 12, [new Tandem.InsertOp("The lazy fox")])
        change: new Tandem.Delta(12, 18, [new Tandem.RetainOp(0, 9), new Tandem.InsertOp('hairy '), new Tandem.RetainOp(9, 12)])
        expected: new Tandem.Delta(18, 12, [new Tandem.RetainOp(0, 9), new Tandem.RetainOp(15, 18)])
      'delete':
        original: new Tandem.Delta(0, 12, [new Tandem.InsertOp("The lazy fox")])
        change: new Tandem.Delta(12, 7, [new Tandem.RetainOp(0, 4), new Tandem.RetainOp(9, 12)])
        expected: new Tandem.Delta(7, 12, [new Tandem.RetainOp(0, 4), new Tandem.InsertOp('lazy '), new Tandem.RetainOp(4, 7)])
      'insert and delete':
        original: new Tandem.Delta(0, 12, [new Tandem.InsertOp("The lazy fox")])
        change: new Tandem.Delta(12, 13, [new Tandem.RetainOp(0, 4), new Tandem.InsertOp('hairy '), new Tandem.RetainOp(9, 12)])
        expected: new Tandem.Delta(13, 12, [new Tandem.RetainOp(0, 4), new Tandem.InsertOp('lazy '), new Tandem.RetainOp(10, 13)])
      'delete at end':
        original: new Tandem.Delta(0, 12, [new Tandem.InsertOp("The lazy fox")])
        change: new Tandem.Delta(12, 9, [new Tandem.RetainOp(0, 9)])
        expected: new Tandem.Delta(9, 12, [new Tandem.RetainOp(0, 9), new Tandem.InsertOp('fox')])
      'append':
        original: new Tandem.Delta(0, 8, [new Tandem.InsertOp("The lazy")])
        change: new Tandem.Delta(8, 12, [new Tandem.RetainOp(0, 8), new Tandem.InsertOp(' fox')])
        expected: new Tandem.Delta(12, 8, [new Tandem.RetainOp(0, 8)])
      'replace':
        original: new Tandem.Delta(0, 3, [new Tandem.InsertOp("fox")])
        change: new Tandem.Delta(3, 4, [new Tandem.InsertOp("lazy")])
        expected: new Tandem.Delta(4, 3, [new Tandem.InsertOp("fox")])

    _.each(tests, (test, name) ->
      it(name, ->
        undoDelta = Scribe.UndoManager.computeUndo(test.change, test.original)
        expect(undoDelta).to.deep.equal(test.expected)
      )
    )
  )
)


_.each(op.attributes, (value, format) =>

@doc.formatText(op.start, op.end - op.start, format, value) if value?

keep keep keep keep replace keep keep keep

# If we had to force newline, pretend user added it

addNewlineDelta = new Tandem.Delta(delta.endLength, [


// Inspired by http://blog.jcoglan.com/2007/07/23/writing-a-linked-list-in-javascript/

function LinkedList() {}
LinkedList.prototype = {
  length: 0,
  first: null,
  last: null
};

LinkedList.prototype.append = function(node) {
  if (typeof this.first === 'undefined' || this.first === null) {
    this.first = node;
  } else {
    node.next = null;
    this.last.next = node;
  }
  node.prev = this.last;
  this.last = node;
  this.length++;
};

LinkedList.prototype.insertAfter = function(node, newNode) {
  newNode.prev = node;
  if (node) {
    newNode.next = node.next;
    if (typeof node.next != 'undefined' && node.next !== null) {
      node.next.prev = newNode;
    }
    node.next = newNode;
    if (node === this.last) {
      this.last = newNode;
    }
  }
  else {
    // Insert after null implies inserting at position 0
    newNode.next = this.first;
    this.first.prev = newNode;
    this.first = newNode;
  }
  this.length++;
};

LinkedList.prototype.remove = function(node) {
  if (this.length > 1) {
    if (typeof node.prev !== 'undefined' && node.prev !== null) {
      node.prev.next = node.next;
    }
    if (typeof node.next !== 'undefined' && node.next !== null) {
      node.next.prev = node.prev;
    }
    if (node == this.first) { this.first = node.next; }
    if (node == this.last) { this.last = node.prev; }
  } else {
    this.first = null;
    this.last = null;
  }
  node.prev = null;
  node.next = null;
  this.length--;
};

LinkedList.prototype.toArray = function() {
  var arr = [];
  var cur = this.first;
  while (cur) {
    arr.push(cur);
    cur = cur.next;
  }
  return arr;
}

LinkedList.Node = function(data) {
  this.prev = null; this.next = null;
  this.data = data;
};