Browse Source

Clean up ensureAssets and use in example app

Steven Silvester 7 years ago
parent
commit
d44a2131ed

+ 10 - 56
examples/app/extract-data.js

@@ -1,57 +1,11 @@
-var rpt = require('read-package-tree');
 var data = require('./package.json');
-var path = require('path');
-var glob = require('glob');
-var fs = require('fs-extra');
-
-var seen = {};
-
-var schemaDir = path.resolve('./schemas');
-fs.removeSync(schemaDir);
-fs.ensureDirSync(schemaDir);
-
-var themesDir = path.resolve('./themes');
-fs.removeSync(themesDir);
-fs.ensureDirSync(themesDir);
-
-
-function extractNode(data) {
-  data.children.forEach(function(child) {
-    extractNode(child);
-  });
-
-  if (seen[data.package.name]) {
-    return;
-  }
-  seen[data.package.name] = true;
-  var jlab = data.package.jupyterlab
-  if (!jlab) {
-    return;
-  }
-
-  // Handle schemas.
-  var schemaDir = jlab['schemaDir'];
-  if (schemaDir) {
-    schemaDir = path.join(data.realpath, schemaDir);
-    var schemas = glob.sync(path.join(schemaDir, '*'));
-    schemas.forEach(function(schemaPath) {
-      var file = path.basename(schemaPath);
-      var to = path.join('schemas', file);
-      fs.copySync(schemaPath, to);
-    });
-  }
-
-  // Handle themes.
-  var themeDir = jlab['themeDir'];
-  if (themeDir) {
-    var name = data.package.name.replace(/\@/g, '').replace(/\//g, '-');
-    var from = path.join(data.realpath, themeDir);
-    var to = path.join('themes', name);
-    fs.copySync(from, to);
-  }
-}
-
-
-rpt('.', function (er, data) {
-  extractNode(data);
-})
+var Build = require('@jupyterlab/buildutils').Build;
+
+let names = Object.keys(data.dependencies).filter(function(name) {
+  packageData = require(name + '/package.json');
+  return packageData.jupyterlab !== undefined;
+});
+Build.ensureAssets({
+  packageNames: names,
+  output: './build'
+});

+ 1 - 0
examples/app/package.json

@@ -9,6 +9,7 @@
     "@jupyterlab/application": "^0.10.0",
     "@jupyterlab/application-extension": "^0.10.0",
     "@jupyterlab/apputils-extension": "^0.10.2",
+    "@jupyterlab/buildutils": "^0.1.0",
     "@jupyterlab/codemirror-extension": "^0.10.0",
     "@jupyterlab/completer-extension": "^0.10.0",
     "@jupyterlab/console-extension": "^0.10.0",

+ 1 - 0
jupyterlab/package.json

@@ -12,6 +12,7 @@
     "@jupyterlab/application-extension": "^0.10.0",
     "@jupyterlab/apputils": "^0.10.0",
     "@jupyterlab/apputils-extension": "^0.10.2",
+    "@jupyterlab/buildutils": "^0.1.0",
     "@jupyterlab/cells": "^0.10.0",
     "@jupyterlab/codeeditor": "^0.10.0",
     "@jupyterlab/codemirror": "^0.10.0",

+ 1 - 0
packages/all-packages/package.json

@@ -32,6 +32,7 @@
     "@jupyterlab/application-extension": "^0.10.0",
     "@jupyterlab/apputils": "^0.10.0",
     "@jupyterlab/apputils-extension": "^0.10.2",
+    "@jupyterlab/buildutils": "^0.1.0",
     "@jupyterlab/cells": "^0.10.0",
     "@jupyterlab/codeeditor": "^0.10.0",
     "@jupyterlab/codemirror": "^0.10.0",

+ 1 - 0
packages/all-packages/src/index.ts

@@ -5,6 +5,7 @@ import '@jupyterlab/application';
 import '@jupyterlab/application-extension';
 import '@jupyterlab/apputils';
 import '@jupyterlab/apputils-extension';
+import '@jupyterlab/buildutils';
 import '@jupyterlab/cells';
 import '@jupyterlab/codeeditor';
 import '@jupyterlab/codemirror';

+ 75 - 45
packages/buildutils/src/index.ts

@@ -7,21 +7,22 @@ import * as fs from 'fs-extra';
 import * as glob from 'glob';
 import * as path from 'path';
 
+/**
+ *  A namespace for JupyterLab build utilities.
+ */
 export
 namespace Build {
   /**
+   * The options used to ensure a root package has the appropriate
+   * assets for its JupyterLab extension packages.
    */
   export
   interface IEnsureOptions {
     /**
-     * The package data for the extensions being processed.
+     * The input directory that contains the root package's assets.
+     * Defaults to the current directory.
      */
-    module: IModule;
-
-    /**
-     * The input directory that contains a package's assets.
-     */
-    input: string;
+    input?: string;
 
     /**
      * The output directory where the build assets should reside.
@@ -29,12 +30,9 @@ namespace Build {
     output: string;
 
     /**
-     * Whether the current assets should be overwritten if they already exist.
-     *
-     * #### Notes
-     * The default value is `false`.
+     * The names of the packages to ensure.
      */
-    overwrite?: boolean;
+    packageNames: ReadonlyArray<string>;
   }
 
   /**
@@ -99,45 +97,77 @@ namespace Build {
   }
 
   /**
-   * Ensures that the assets of a plugin are populated for a build.
+   * Ensures that the assets of plugin packages are populated for a build.
    */
   export
   function ensureAssets(options: IEnsureOptions): void {
-    const { module, input, output, overwrite } = options;
-    const { name } = module;
-    const extension = normalizeExtension(module);
-    const { schemaDir, themeDir } = extension;
-
-    // Handle schemas.
-    if (schemaDir) {
-      const schemas = glob.sync(path.join(path.join(input, schemaDir), '*'));
-      const destination = path.join(output, 'schemas', name);
-
-      // Make sure the schema directory exists.
-      if (overwrite) {
-        fs.rmdirSync(destination);
+    let { input, output, packageNames } = options;
+    input = input || '.';
+
+    packageNames.forEach(function(name) {
+      const packageDir = fs.realpathSync(path.join(input, 'node_modules', name));
+      const packageData = require(path.join(packageDir, 'package.json'));
+      const extension = normalizeExtension(packageData);
+      const { schemaDir, themeDir } = extension;
+
+      // Handle schemas.
+      if (schemaDir) {
+        const schemas = glob.sync(path.join(path.join(packageDir, schemaDir), '*'));
+        const destination = path.join(output, 'schemas', name);
+
+        // Remove the existing directory if necessary.
+        if (fs.existsSync(destination)) {
+          try {
+            const oldPackageData = require(path.join(destination, 'package.json'));
+            if (oldPackageData.version === packageData.version) {
+              fs.removeSync(destination);
+            }
+          } catch (e) {
+            fs.removeSync(destination);
+          }
+        }
+
+        // Make sure the schema directory exists.
+        fs.mkdirpSync(destination);
+
+        // Copy schemas.
+        schemas.forEach(schema => {
+          const file = path.basename(schema);
+          if (file === 'package.json') {
+            throw new Error('Cannot use name "package.json" for schema file');
+          }
+          fs.copySync(schema, path.join(destination, file));
+        });
+
+        // Write the package.json file for future comparison.
+        fs.copySync(path.join(packageDir, 'package.json'), path.join(destination, 'package.json'));
       }
-      fs.mkdirpSync(destination);
 
-      // Copy schemas.
-      schemas.forEach(schema => {
-        const file = path.basename(schema);
-
-        fs.copySync(schema, path.join(destination, file));
-      });
-    }
-
-    // Handle themes.
-    if (themeDir) {
-      const theme = name.replace(/@/g, '').replace(/\//g, '-');
-      const from = path.join(input, themeDir);
-      const destination = path.join(output, 'themes', theme);
-
-      if (overwrite) {
-        fs.rmdirSync(destination);
+      // Handle themes.
+      if (themeDir) {
+        const theme = name.replace(/@/g, '').replace(/\//g, '-');
+        const from = path.join(packageDir, themeDir);
+        const destination = path.join(output, 'themes', theme);
+
+        // Remove the existing directory if necessary.
+        if (fs.existsSync(destination)) {
+          try {
+            const oldPackageData = require(path.join(destination, 'package.json'));
+            if (oldPackageData.version === packageData.version) {
+              fs.removeSync(destination);
+            }
+          } catch (e) {
+            fs.removeSync(destination);
+          }
+        }
+
+        // Copy the theme folder.
+        fs.copySync(from, destination);
+
+        // Write the package.json file for future comparison.
+        fs.copySync(path.join(packageDir, 'package.json'), path.join(destination, 'package.json'));
       }
-      fs.copySync(from, destination);
-    }
+    });
   }
 
   /**

+ 1 - 1
packages/cells/src/collapser.tsx

@@ -8,7 +8,7 @@ import {
 } from '@jupyterlab/apputils';
 
 import {
-  VirtualNode
+  VirtualNode, h
 } from '@phosphor/virtualdom';
 
 import {