#!/usr/bin/env node
var fs = require('fs-extra');
var path = require('path');
var childProcess = require('child_process');

/**
 * Add an extension to the source tree of JupyterLab.
 * It takes as an argument either a path to a directory
 * on the local filesystem or a URL to a git repository.
 * In the former case, it copies the directory into the
 * source tree, in the latter it adds the repository as
 * a git submodule.
 *
 * It also adds the relevant metadata to the build files.
 */

// Make sure we have required command line arguments.
if (process.argv.length < 3) {
    var msg = '** Must supply a target extension';
    process.stderr.write(msg);
    process.exit(1);
}

// Extract the desired git repository and repository name.
var target = process.argv[2];
var basePath = path.resolve('.');

var packagePath = '';
if (target[0] === '.' || target[0] === '/') {
  // If the target starts with a '.' or a '/', treat it as a local path.
  packagePath = path.resolve(target);
  packageDirName = target.split('/').pop();
  // Copy the package directory contents to the sibling package.
  var newPackagePath = path.join(basePath, 'packages', packageDirName);
  fs.copySync(packagePath, newPackagePath);
} else { 
  // Otherwise treat it as a git reposotory and try to add it.
  var packageDirName = target.split('/').pop().split('.')[0];
  var packagePath = path.join(basePath, 'packages', packageDirName);
  // Add the repository as a submodule.
  childProcess.execSync('git submodule add --force '+ target + ' ' + packagePath);
}

// Get the package.json of the extension.
var package = require(path.join(packagePath, 'package.json'));

// Add the extension to packages/all-packages/package.json
var allPackagesPath = path.join(basePath, 'packages', 'all-packages', 'package.json');
var allPackages = require(allPackagesPath);
allPackages.dependencies[package.name] = '~'+String(package.version);
fs.writeFileSync(allPackagesPath, JSON.stringify(allPackages, null, 2) + '\n');

// Add the extension to packages/all-packages/src/index.ts
var indexPath = path.join(basePath, 'packages', 'all-packages', 'src', 'index.ts');
var index = fs.readFileSync(indexPath, 'utf8');
index = index + 'import "' + package.name + '";\n';
fs.writeFileSync(indexPath, index);

// Add the extension to jupyterlab/package.json
var jupyterlabPackagePath = path.join(basePath, 'jupyterlab', 'package.json');
var jupyterlabPackage = require(jupyterlabPackagePath);
jupyterlabPackage.dependencies[package.name] = '~'+String(package.version);
jupyterlabPackage.jupyterlab.extensions.push(package.name);
fs.writeFileSync(jupyterlabPackagePath, JSON.stringify(jupyterlabPackage, null, 2) + '\n');

@@ -980,16 +980,154 @@
 arget);%0A
+  // Possibly remove a trailing slash.%0A  if (packagePath%5BpackagePath.length-1%5D === '/') %7B%0A    packagePath = packagePath.slice(0, -1);%0A  %7D%0A
   packag
@@ -1129,38 +1129,43 @@
 ackageDirName = 
-target
+packagePath
 .split('/').pop(

Remove a trailing slash from addsibling path.