1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path = require("path");
|
4 | const glob = require("glob");
|
5 | const fs = require("fs-extra");
|
6 | const childProcess = require("child_process");
|
7 | const dependency_graph_1 = require("dependency-graph");
|
8 | const sortPackageJson = require("sort-package-json");
|
9 | const coreutils = require("@phosphor/coreutils");
|
10 | const backSlash = /\\/g;
|
11 |
|
12 |
|
13 |
|
14 | function getLernaPaths(basePath = '.') {
|
15 | basePath = path.resolve(basePath);
|
16 | let packages;
|
17 | try {
|
18 | let baseConfig = require(path.join(basePath, 'package.json'));
|
19 | if (baseConfig.workspaces) {
|
20 | packages = baseConfig.workspaces.packages || baseConfig.workspaces;
|
21 | }
|
22 | else {
|
23 | baseConfig = require(path.join(basePath, 'lerna.json'));
|
24 | packages = baseConfig.packages;
|
25 | }
|
26 | }
|
27 | catch (e) {
|
28 | if (e.code === 'MODULE_NOT_FOUND') {
|
29 | throw new Error(`No yarn workspace / lerna package list found in ${basePath}`);
|
30 | }
|
31 | throw e;
|
32 | }
|
33 | let paths = [];
|
34 | for (let config of packages) {
|
35 | paths = paths.concat(glob.sync(path.join(basePath, config)));
|
36 | }
|
37 | return paths.filter(pkgPath => {
|
38 | return fs.existsSync(path.join(pkgPath, 'package.json'));
|
39 | });
|
40 | }
|
41 | exports.getLernaPaths = getLernaPaths;
|
42 |
|
43 |
|
44 |
|
45 | function getCorePaths() {
|
46 | let spec = path.resolve(path.join('.', 'packages', '*'));
|
47 | return glob.sync(spec);
|
48 | }
|
49 | exports.getCorePaths = getCorePaths;
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function writePackageData(pkgJsonPath, data) {
|
60 | let text = JSON.stringify(sortPackageJson(data), null, 2) + '\n';
|
61 | let orig = fs
|
62 | .readFileSync(pkgJsonPath, 'utf8')
|
63 | .split('\r\n')
|
64 | .join('\n');
|
65 | if (text !== orig) {
|
66 | fs.writeFileSync(pkgJsonPath, text, 'utf8');
|
67 | return true;
|
68 | }
|
69 | return false;
|
70 | }
|
71 | exports.writePackageData = writePackageData;
|
72 |
|
73 |
|
74 |
|
75 | function readJSONFile(filePath) {
|
76 | try {
|
77 | return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
78 | }
|
79 | catch (e) {
|
80 | throw `Cannot read JSON for path ${filePath}: ${e}`;
|
81 | }
|
82 | }
|
83 | exports.readJSONFile = readJSONFile;
|
84 |
|
85 |
|
86 |
|
87 | function writeJSONFile(filePath, data) {
|
88 | function sortObjByKey(value) {
|
89 |
|
90 | return typeof value === 'object'
|
91 | ? Array.isArray(value)
|
92 | ? value.map(sortObjByKey)
|
93 | : Object.keys(value)
|
94 | .sort()
|
95 | .reduce((o, key) => {
|
96 | const v = value[key];
|
97 | o[key] = sortObjByKey(v);
|
98 | return o;
|
99 | }, {})
|
100 | : value;
|
101 | }
|
102 | let text = JSON.stringify(data, sortObjByKey(data), 2) + '\n';
|
103 | let orig = {};
|
104 | try {
|
105 | orig = readJSONFile(filePath);
|
106 | }
|
107 | catch (e) {
|
108 |
|
109 | }
|
110 | if (!coreutils.JSONExt.deepEqual(data, orig)) {
|
111 | fs.writeFileSync(filePath, text, 'utf8');
|
112 | return true;
|
113 | }
|
114 | return false;
|
115 | }
|
116 | exports.writeJSONFile = writeJSONFile;
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | function fromTemplate(templ, subs, options = {}) {
|
137 |
|
138 | const autoindent = options.autoindent === undefined ? true : options.autoindent;
|
139 | const end = options.end === undefined ? '\n' : options.end;
|
140 | Object.keys(subs).forEach(key => {
|
141 | const val = subs[key];
|
142 | if (autoindent) {
|
143 |
|
144 | templ = templ.split(`{{${key}}}`).reduce((acc, cur) => {
|
145 |
|
146 | let indentRe = acc.match(/([^\S\r\n]*).*$/);
|
147 | let indent = indentRe ? indentRe[1] : '';
|
148 | return acc + val.split('\n').join('\n' + indent) + cur;
|
149 | });
|
150 | }
|
151 | else {
|
152 | templ = templ.split(`{{${key}}}`).join(val);
|
153 | }
|
154 | });
|
155 | return templ.trim() + end;
|
156 | }
|
157 | exports.fromTemplate = fromTemplate;
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | function checkStatus(cmd) {
|
163 | const data = childProcess.spawnSync(cmd, { shell: true });
|
164 | return data.status;
|
165 | }
|
166 | exports.checkStatus = checkStatus;
|
167 |
|
168 |
|
169 |
|
170 | function getPythonVersion() {
|
171 | const cmd = 'python setup.py --version';
|
172 | return run(cmd, { stdio: 'pipe' }, true);
|
173 | }
|
174 | exports.getPythonVersion = getPythonVersion;
|
175 |
|
176 |
|
177 |
|
178 | function getJSVersion(pkg) {
|
179 | const filePath = path.resolve(path.join('.', 'packages', pkg, 'package.json'));
|
180 | const data = readJSONFile(filePath);
|
181 | return data.version;
|
182 | }
|
183 | exports.getJSVersion = getJSVersion;
|
184 |
|
185 |
|
186 |
|
187 | function prebump() {
|
188 |
|
189 | run('python -m pip install bump2version');
|
190 |
|
191 | let status = run('git status --porcelain', {
|
192 | stdio: 'pipe',
|
193 | encoding: 'utf8'
|
194 | });
|
195 | if (status.length > 0) {
|
196 | throw new Error(`Must be in a clean git state with no untracked files.
|
197 | Run "git status" to see the issues.
|
198 |
|
199 | ${status}`);
|
200 | }
|
201 | }
|
202 | exports.prebump = prebump;
|
203 |
|
204 |
|
205 |
|
206 | function postbump() {
|
207 |
|
208 | const curr = getPythonVersion();
|
209 |
|
210 | let filePath = path.resolve(path.join('.', 'dev_mode', 'package.json'));
|
211 | let data = readJSONFile(filePath);
|
212 | data.jupyterlab.version = curr;
|
213 | writeJSONFile(filePath, data);
|
214 | }
|
215 | exports.postbump = postbump;
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | function run(cmd, options = {}, quiet) {
|
222 | options = options || {};
|
223 | options['stdio'] = options.stdio || 'inherit';
|
224 | if (!quiet) {
|
225 | console.log('>', cmd);
|
226 | }
|
227 | const value = childProcess.execSync(cmd, options);
|
228 | if (value === null) {
|
229 | return '';
|
230 | }
|
231 | return value
|
232 | .toString()
|
233 | .replace(/(\r\n|\n)$/, '')
|
234 | .trim();
|
235 | }
|
236 | exports.run = run;
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | function getPackageGraph() {
|
242 |
|
243 | const paths = getLernaPaths();
|
244 | const locals = {};
|
245 |
|
246 |
|
247 | paths.push('./jupyterlab/tests/mock_packages/extension');
|
248 | paths.push('./jupyterlab/tests/mock_packages/mimeextension');
|
249 |
|
250 | paths.forEach(pkgPath => {
|
251 |
|
252 | let data;
|
253 | try {
|
254 | data = readJSONFile(path.join(pkgPath, 'package.json'));
|
255 | }
|
256 | catch (e) {
|
257 | console.error(e);
|
258 | return;
|
259 | }
|
260 | locals[data.name] = data;
|
261 | });
|
262 |
|
263 |
|
264 | const graph = new dependency_graph_1.DepGraph();
|
265 | Object.keys(locals).forEach(name => {
|
266 | const data = locals[name];
|
267 | graph.addNode(name, data);
|
268 | const deps = data.dependencies || {};
|
269 | Object.keys(deps).forEach(depName => {
|
270 | if (!graph.hasNode(depName)) {
|
271 | let depData;
|
272 |
|
273 |
|
274 | if (depName in locals) {
|
275 | depData = locals[depName];
|
276 | }
|
277 | else {
|
278 | depData = requirePackage(name, depName);
|
279 | }
|
280 | graph.addNode(depName, depData);
|
281 | }
|
282 | graph.addDependency(data.name, depName);
|
283 | });
|
284 | });
|
285 | return graph;
|
286 | }
|
287 | exports.getPackageGraph = getPackageGraph;
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | function requirePackage(parentModule, module) {
|
295 | const packagePath = `${module}/package.json`;
|
296 | let parentModulePath;
|
297 |
|
298 | try {
|
299 | parentModulePath = require.resolve(parentModule);
|
300 | }
|
301 | catch (_a) {
|
302 | return require(packagePath);
|
303 | }
|
304 | const requirePath = require.resolve(packagePath, {
|
305 | paths: [parentModulePath]
|
306 | });
|
307 | return require(requirePath);
|
308 | }
|
309 |
|
310 |
|
311 |
|
312 | function ensureUnixPathSep(source) {
|
313 | if (path.sep === '/') {
|
314 | return source;
|
315 | }
|
316 | return source.replace(backSlash, '/');
|
317 | }
|
318 | exports.ensureUnixPathSep = ensureUnixPathSep;
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | function stem(path) {
|
327 | return path
|
328 | .split('\\')
|
329 | .pop()
|
330 | .split('/')
|
331 | .pop()
|
332 | .split('.')
|
333 | .shift();
|
334 | }
|
335 | exports.stem = stem;
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | function camelCase(str, upper = false) {
|
348 | return str.replace(/(?:^\w|[A-Z]|\b\w|\s+|-+|_+)/g, function (match, index) {
|
349 | if (+match === 0 || match[0] === '-') {
|
350 | return '';
|
351 | }
|
352 | else if (index === 0 && !upper) {
|
353 | return match.toLowerCase();
|
354 | }
|
355 | else {
|
356 | return match.toUpperCase();
|
357 | }
|
358 | });
|
359 | }
|
360 | exports.camelCase = camelCase;
|
361 |
|
\ | No newline at end of file |