forked from jupyterlab/jupyterlab
/
build.ts
202 lines (172 loc) · 6.02 KB
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as path from 'path';
import * as utils from './utils';
/**
* 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 output directory where the build assets should reside.
*/
output: string;
/**
* The names of the packages to ensure.
*/
packageNames: ReadonlyArray<string>;
}
/**
* The JupyterLab extension attributes in a module.
*/
export interface ILabExtension {
/**
* Indicates whether the extension is a standalone extension.
*
* #### Notes
* If `true`, the `main` export of the package is used. If set to a string
* path, the export from that path is loaded as a JupyterLab extension. It
* is possible for one package to have both an `extension` and a
* `mimeExtension` but they cannot be identical (i.e., the same export
* cannot be declared both an `extension` and a `mimeExtension`).
*/
readonly extension?: boolean | string;
/**
* Indicates whether the extension is a MIME renderer extension.
*
* #### Notes
* If `true`, the `main` export of the package is used. If set to a string
* path, the export from that path is loaded as a JupyterLab extension. It
* is possible for one package to have both an `extension` and a
* `mimeExtension` but they cannot be identical (i.e., the same export
* cannot be declared both an `extension` and a `mimeExtension`).
*/
readonly mimeExtension?: boolean | string;
/**
* The local schema file path in the extension package.
*/
readonly schemaDir?: string;
/**
* The local theme file path in the extension package.
*/
readonly themeDir?: string;
}
/**
* A minimal definition of a module's package definition (i.e., package.json).
*/
export interface IModule {
/**
* The JupyterLab metadata/
*/
jupyterlab?: ILabExtension;
/**
* The main entry point in a module.
*/
main?: string;
/**
* The name of a module.
*/
name: string;
}
/**
* Ensures that the assets of plugin packages are populated for a build.
*/
export function ensureAssets(options: IEnsureOptions): void {
let { output, packageNames } = options;
packageNames.forEach(function(name) {
const packageDataPath = require.resolve(path.join(name, 'package.json'));
const packageDir = path.dirname(packageDataPath);
const packageData = utils.readJSONFile(packageDataPath);
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 oldPackagePath = path.join(destination, 'package.json');
const oldPackageData = utils.readJSONFile(oldPackagePath);
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')
);
}
// Handle themes.
if (themeDir) {
const from = path.join(packageDir, themeDir);
const destination = path.join(output, 'themes', 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 theme directory exists.
fs.mkdirpSync(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')
);
}
});
}
/**
* Returns JupyterLab extension metadata from a module.
*/
export function normalizeExtension(module: IModule): ILabExtension {
let { jupyterlab, main, name } = module;
main = main || 'index.js';
if (!jupyterlab) {
throw new Error(`Module ${name} does not contain JupyterLab metadata.`);
}
let { extension, mimeExtension, schemaDir, themeDir } = jupyterlab;
extension = extension === true ? main : extension;
mimeExtension = mimeExtension === true ? main : mimeExtension;
if (extension && mimeExtension && extension === mimeExtension) {
const message = 'extension and mimeExtension cannot be the same export.';
throw new Error(message);
}
return { extension, mimeExtension, schemaDir, themeDir };
}
}